line endings converted to DOS (CR/LF)

This commit is contained in:
S T Chan 2018-12-05 23:24:56 -05:00
parent 9e24ff6ceb
commit 213db0409d
2 changed files with 268 additions and 270 deletions

View File

@ -59,9 +59,8 @@ Global
{E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Release|x64.Build.0 = Release|x64 {E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Release|x64.Build.0 = Release|x64
{E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Release|x86.ActiveCfg = Release|x86 {E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Release|x86.ActiveCfg = Release|x86
{E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Release|x86.Build.0 = Release|x86 {E8679E1B-8C89-4201-97D5-7E43C5A486C9}.Release|x86.Build.0 = Release|x86
{3C255536-A7F1-4913-9C7F-966DFFEE01BC}.Debug|Any CPU.ActiveCfg = Debug|x86 {3C255536-A7F1-4913-9C7F-966DFFEE01BC}.Debug|Any CPU.ActiveCfg = Debug|x64
{3C255536-A7F1-4913-9C7F-966DFFEE01BC}.Debug|x64.ActiveCfg = Debug|x64 {3C255536-A7F1-4913-9C7F-966DFFEE01BC}.Debug|x64.ActiveCfg = Debug|x64
{3C255536-A7F1-4913-9C7F-966DFFEE01BC}.Debug|x64.Build.0 = Debug|x64
{3C255536-A7F1-4913-9C7F-966DFFEE01BC}.Debug|x86.ActiveCfg = Debug|x86 {3C255536-A7F1-4913-9C7F-966DFFEE01BC}.Debug|x86.ActiveCfg = Debug|x86
{3C255536-A7F1-4913-9C7F-966DFFEE01BC}.Debug|x86.Build.0 = Debug|x86 {3C255536-A7F1-4913-9C7F-966DFFEE01BC}.Debug|x86.Build.0 = Debug|x86
{3C255536-A7F1-4913-9C7F-966DFFEE01BC}.Release|Any CPU.ActiveCfg = Release|x64 {3C255536-A7F1-4913-9C7F-966DFFEE01BC}.Release|Any CPU.ActiveCfg = Release|x64
@ -78,7 +77,6 @@ Global
{8562A5BC-A70E-40DD-A7E7-5611D332A983}.Debug|x86.Build.0 = Debug|Any CPU {8562A5BC-A70E-40DD-A7E7-5611D332A983}.Debug|x86.Build.0 = Debug|Any CPU
{8562A5BC-A70E-40DD-A7E7-5611D332A983}.Release|Any CPU.ActiveCfg = Release|Any CPU {8562A5BC-A70E-40DD-A7E7-5611D332A983}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8562A5BC-A70E-40DD-A7E7-5611D332A983}.Release|x64.ActiveCfg = Release|Any CPU {8562A5BC-A70E-40DD-A7E7-5611D332A983}.Release|x64.ActiveCfg = Release|Any CPU
{8562A5BC-A70E-40DD-A7E7-5611D332A983}.Release|x64.Build.0 = Release|Any CPU
{8562A5BC-A70E-40DD-A7E7-5611D332A983}.Release|x86.ActiveCfg = Release|Any CPU {8562A5BC-A70E-40DD-A7E7-5611D332A983}.Release|x86.ActiveCfg = Release|Any CPU
{8562A5BC-A70E-40DD-A7E7-5611D332A983}.Release|x86.Build.0 = Release|Any CPU {8562A5BC-A70E-40DD-A7E7-5611D332A983}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection

View File

@ -1,253 +1,253 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Windows.Forms; using System.Windows.Forms;
namespace PdfScribe namespace PdfScribe
{ {
public class Program public class Program
{ {
#region Message constants #region Message constants
const string errorDialogCaption = "PDF Scribe"; // Error taskdialog caption text const string errorDialogCaption = "PDF Scribe"; // Error taskdialog caption text
const string errorDialogInstructionPDFGeneration = "There was a PDF generation error."; const string errorDialogInstructionPDFGeneration = "There was a PDF generation error.";
const string errorDialogInstructionCouldNotWrite = "Could not create the output file."; const string errorDialogInstructionCouldNotWrite = "Could not create the output file.";
const string errorDialogInstructionUnexpectedError = "There was an internal error. Enable tracing for details."; const string errorDialogInstructionUnexpectedError = "There was an internal error. Enable tracing for details.";
const string errorDialogTextFileInUse = "{0} is being used by another process."; const string errorDialogTextFileInUse = "{0} is being used by another process.";
const string errorDialogTextGhostScriptConversion = "Ghostscript error code {0}."; const string errorDialogTextGhostScriptConversion = "Ghostscript error code {0}.";
const string warnFileNotDeleted = "{0} could not be deleted."; const string warnFileNotDeleted = "{0} could not be deleted.";
#endregion #endregion
#region Other constants #region Other constants
const string traceSourceName = "PdfScribe"; const string traceSourceName = "PdfScribe";
const string defaultOutputFilename = "PDFSCRIBE.PDF"; const string defaultOutputFilename = "PDFSCRIBE.PDF";
#endregion #endregion
static TraceSource logEventSource = new TraceSource(traceSourceName); static TraceSource logEventSource = new TraceSource(traceSourceName);
[STAThread] [STAThread]
static void Main(string[] args) static void Main(string[] args)
{ {
// Install the global exception handler // Install the global exception handler
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Application_UnhandledException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(Application_UnhandledException);
String standardInputFilename = Path.GetTempFileName(); String standardInputFilename = Path.GetTempFileName();
String outputFilename = String.Empty; String outputFilename = String.Empty;
try try
{ {
using (BinaryReader standardInputReader = new BinaryReader(Console.OpenStandardInput())) using (BinaryReader standardInputReader = new BinaryReader(Console.OpenStandardInput()))
{ {
using (FileStream standardInputFile = new FileStream(standardInputFilename, FileMode.Create, FileAccess.ReadWrite)) using (FileStream standardInputFile = new FileStream(standardInputFilename, FileMode.Create, FileAccess.ReadWrite))
{ {
standardInputReader.BaseStream.CopyTo(standardInputFile); standardInputReader.BaseStream.CopyTo(standardInputFile);
} }
} }
if (GetPdfOutputFilename(ref outputFilename)) if (GetPdfOutputFilename(ref outputFilename))
{ {
// Remove the existing PDF file if present // Remove the existing PDF file if present
File.Delete(outputFilename); File.Delete(outputFilename);
// Only set absolute minimum parameters, let the postscript input // Only set absolute minimum parameters, let the postscript input
// dictate as much as possible // dictate as much as possible
String[] ghostScriptArguments = { "-dBATCH", "-dNOPAUSE", "-dSAFER", "-sDEVICE=pdfwrite", String[] ghostScriptArguments = { "-dBATCH", "-dNOPAUSE", "-dSAFER", "-sDEVICE=pdfwrite",
String.Format("-sOutputFile={0}", outputFilename), standardInputFilename }; String.Format("-sOutputFile={0}", outputFilename), standardInputFilename };
GhostScript64.CallAPI(ghostScriptArguments); GhostScript64.CallAPI(ghostScriptArguments);
DisplayPdf(outputFilename); DisplayPdf(outputFilename);
} }
} }
catch (IOException ioEx) catch (IOException ioEx)
{ {
// We couldn't delete, or create a file // We couldn't delete, or create a file
// because it was in use // because it was in use
logEventSource.TraceEvent(TraceEventType.Error, logEventSource.TraceEvent(TraceEventType.Error,
(int)TraceEventType.Error, (int)TraceEventType.Error,
errorDialogInstructionCouldNotWrite + errorDialogInstructionCouldNotWrite +
Environment.NewLine + Environment.NewLine +
"Exception message: " + ioEx.Message); "Exception message: " + ioEx.Message);
DisplayErrorMessage(errorDialogCaption, DisplayErrorMessage(errorDialogCaption,
errorDialogInstructionCouldNotWrite + Environment.NewLine + errorDialogInstructionCouldNotWrite + Environment.NewLine +
String.Format("{0} is in use.", outputFilename)); String.Format("{0} is in use.", outputFilename));
} }
catch (UnauthorizedAccessException unauthorizedEx) catch (UnauthorizedAccessException unauthorizedEx)
{ {
// Couldn't delete a file // Couldn't delete a file
// because it was set to readonly // because it was set to readonly
// or couldn't create a file // or couldn't create a file
// because of permissions issues // because of permissions issues
logEventSource.TraceEvent(TraceEventType.Error, logEventSource.TraceEvent(TraceEventType.Error,
(int)TraceEventType.Error, (int)TraceEventType.Error,
errorDialogInstructionCouldNotWrite + errorDialogInstructionCouldNotWrite +
Environment.NewLine + Environment.NewLine +
"Exception message: " + unauthorizedEx.Message); "Exception message: " + unauthorizedEx.Message);
DisplayErrorMessage(errorDialogCaption, DisplayErrorMessage(errorDialogCaption,
errorDialogInstructionCouldNotWrite + Environment.NewLine + errorDialogInstructionCouldNotWrite + Environment.NewLine +
String.Format("Insufficient privileges to either create or delete {0}", outputFilename)); String.Format("Insufficient privileges to either create or delete {0}", outputFilename));
} }
catch (ExternalException ghostscriptEx) catch (ExternalException ghostscriptEx)
{ {
// Ghostscript error // Ghostscript error
logEventSource.TraceEvent(TraceEventType.Error, logEventSource.TraceEvent(TraceEventType.Error,
(int)TraceEventType.Error, (int)TraceEventType.Error,
String.Format(errorDialogTextGhostScriptConversion, ghostscriptEx.ErrorCode.ToString()) + String.Format(errorDialogTextGhostScriptConversion, ghostscriptEx.ErrorCode.ToString()) +
Environment.NewLine + Environment.NewLine +
"Exception message: " + ghostscriptEx.Message); "Exception message: " + ghostscriptEx.Message);
DisplayErrorMessage(errorDialogCaption, DisplayErrorMessage(errorDialogCaption,
errorDialogInstructionPDFGeneration + Environment.NewLine + errorDialogInstructionPDFGeneration + Environment.NewLine +
String.Format(errorDialogTextGhostScriptConversion, ghostscriptEx.ErrorCode.ToString())); String.Format(errorDialogTextGhostScriptConversion, ghostscriptEx.ErrorCode.ToString()));
} }
finally finally
{ {
try try
{ {
File.Delete(standardInputFilename); File.Delete(standardInputFilename);
} }
catch catch
{ {
logEventSource.TraceEvent(TraceEventType.Warning, logEventSource.TraceEvent(TraceEventType.Warning,
(int)TraceEventType.Warning, (int)TraceEventType.Warning,
String.Format(warnFileNotDeleted, standardInputFilename)); String.Format(warnFileNotDeleted, standardInputFilename));
} }
logEventSource.Flush(); logEventSource.Flush();
} }
} }
/// <summary> /// <summary>
/// All unhandled exceptions will bubble their way up here - /// All unhandled exceptions will bubble their way up here -
/// a final error dialog will be displayed before the crash and burn /// a final error dialog will be displayed before the crash and burn
/// </summary> /// </summary>
/// <param name="sender"></param> /// <param name="sender"></param>
/// <param name="e"></param> /// <param name="e"></param>
static void Application_UnhandledException(object sender, UnhandledExceptionEventArgs e) static void Application_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{ {
logEventSource.TraceEvent(TraceEventType.Critical, logEventSource.TraceEvent(TraceEventType.Critical,
(int)TraceEventType.Critical, (int)TraceEventType.Critical,
((Exception)e.ExceptionObject).Message + Environment.NewLine + ((Exception)e.ExceptionObject).Message + Environment.NewLine +
((Exception)e.ExceptionObject).StackTrace); ((Exception)e.ExceptionObject).StackTrace);
DisplayErrorMessage(errorDialogCaption, DisplayErrorMessage(errorDialogCaption,
errorDialogInstructionUnexpectedError); errorDialogInstructionUnexpectedError);
} }
static bool GetPdfOutputFilename(ref String outputFile) static bool GetPdfOutputFilename(ref String outputFile)
{ {
bool filenameRetrieved = false; bool filenameRetrieved = false;
switch (Properties.Settings.Default.AskUserForOutputFilename) switch (Properties.Settings.Default.AskUserForOutputFilename)
{ {
case (true) : case (true) :
using (SetOutputFilename dialogOwner = new SetOutputFilename()) using (SetOutputFilename dialogOwner = new SetOutputFilename())
{ {
dialogOwner.TopMost = true; dialogOwner.TopMost = true;
dialogOwner.TopLevel = true; dialogOwner.TopLevel = true;
dialogOwner.Show(); // Form won't actually show - Application.Run() never called dialogOwner.Show(); // Form won't actually show - Application.Run() never called
// but having a topmost/toplevel owner lets us bring the SaveFileDialog to the front // but having a topmost/toplevel owner lets us bring the SaveFileDialog to the front
dialogOwner.BringToFront(); dialogOwner.BringToFront();
using (SaveFileDialog pdfFilenameDialog = new SaveFileDialog()) using (SaveFileDialog pdfFilenameDialog = new SaveFileDialog())
{ {
pdfFilenameDialog.AddExtension = true; pdfFilenameDialog.AddExtension = true;
pdfFilenameDialog.AutoUpgradeEnabled = true; pdfFilenameDialog.AutoUpgradeEnabled = true;
pdfFilenameDialog.CheckPathExists = true; pdfFilenameDialog.CheckPathExists = true;
pdfFilenameDialog.Filter = "pdf files (*.pdf)|*.pdf"; pdfFilenameDialog.Filter = "pdf files (*.pdf)|*.pdf";
pdfFilenameDialog.ShowHelp = false; pdfFilenameDialog.ShowHelp = false;
pdfFilenameDialog.Title = "PDF Scribe - Set output filename"; pdfFilenameDialog.Title = "PDF Scribe - Set output filename";
pdfFilenameDialog.ValidateNames = true; pdfFilenameDialog.ValidateNames = true;
if (pdfFilenameDialog.ShowDialog(dialogOwner) == DialogResult.OK) if (pdfFilenameDialog.ShowDialog(dialogOwner) == DialogResult.OK)
{ {
outputFile = pdfFilenameDialog.FileName; outputFile = pdfFilenameDialog.FileName;
filenameRetrieved = true; filenameRetrieved = true;
} }
} }
dialogOwner.Close(); dialogOwner.Close();
} }
break; break;
default: default:
outputFile = GetOutputFilename(); outputFile = GetOutputFilename();
filenameRetrieved = true; filenameRetrieved = true;
break; break;
} }
return filenameRetrieved; return filenameRetrieved;
} }
private static String GetOutputFilename() private static String GetOutputFilename()
{ {
String outputFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), defaultOutputFilename); String outputFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), defaultOutputFilename);
if (!String.IsNullOrEmpty(Properties.Settings.Default.OutputFile) && if (!String.IsNullOrEmpty(Properties.Settings.Default.OutputFile) &&
!String.IsNullOrWhiteSpace(Properties.Settings.Default.OutputFile)) !String.IsNullOrWhiteSpace(Properties.Settings.Default.OutputFile))
{ {
if (IsFilePathValid(Properties.Settings.Default.OutputFile)) if (IsFilePathValid(Properties.Settings.Default.OutputFile))
{ {
outputFilename = Properties.Settings.Default.OutputFile; outputFilename = Properties.Settings.Default.OutputFile;
} }
else else
{ {
if (IsFilePathValid(Environment.ExpandEnvironmentVariables(Properties.Settings.Default.OutputFile))) if (IsFilePathValid(Environment.ExpandEnvironmentVariables(Properties.Settings.Default.OutputFile)))
{ {
outputFilename = Environment.ExpandEnvironmentVariables(Properties.Settings.Default.OutputFile); outputFilename = Environment.ExpandEnvironmentVariables(Properties.Settings.Default.OutputFile);
} }
} }
} }
else else
{ {
logEventSource.TraceEvent(TraceEventType.Warning, logEventSource.TraceEvent(TraceEventType.Warning,
(int)TraceEventType.Warning, (int)TraceEventType.Warning,
String.Format("Using default output filename {0}", String.Format("Using default output filename {0}",
outputFilename)); outputFilename));
} }
return outputFilename; return outputFilename;
} }
static bool IsFilePathValid(String filePath) static bool IsFilePathValid(String filePath)
{ {
bool pathIsValid = false; bool pathIsValid = false;
if (!String.IsNullOrEmpty(filePath) && filePath.Length <= 260) if (!String.IsNullOrEmpty(filePath) && filePath.Length <= 260)
{ {
String directoryName = Path.GetDirectoryName(filePath); String directoryName = Path.GetDirectoryName(filePath);
String filename = Path.GetFileName(filePath); String filename = Path.GetFileName(filePath);
if (Directory.Exists(directoryName)) if (Directory.Exists(directoryName))
{ {
// Check for invalid filename chars // Check for invalid filename chars
Regex containsABadCharacter = new Regex("[" Regex containsABadCharacter = new Regex("["
+ Regex.Escape(new String(System.IO.Path.GetInvalidPathChars())) + "]"); + Regex.Escape(new String(System.IO.Path.GetInvalidPathChars())) + "]");
pathIsValid = !containsABadCharacter.IsMatch(filename); pathIsValid = !containsABadCharacter.IsMatch(filename);
} }
} }
else else
{ {
logEventSource.TraceEvent(TraceEventType.Warning, logEventSource.TraceEvent(TraceEventType.Warning,
(int)TraceEventType.Warning, (int)TraceEventType.Warning,
"Output filename is longer than 260 characters, or blank."); "Output filename is longer than 260 characters, or blank.");
} }
return pathIsValid; return pathIsValid;
} }
/// <summary> /// <summary>
/// Opens the PDF in the default viewer /// Opens the PDF in the default viewer
/// if the OpenAfterCreating app setting is "True" /// if the OpenAfterCreating app setting is "True"
/// and the file extension is .PDF /// and the file extension is .PDF
/// </summary> /// </summary>
/// <param name="pdfFilename"></param> /// <param name="pdfFilename"></param>
static void DisplayPdf(String pdfFilename) static void DisplayPdf(String pdfFilename)
{ {
if (Properties.Settings.Default.OpenAfterCreating && if (Properties.Settings.Default.OpenAfterCreating &&
@ -255,25 +255,25 @@ namespace PdfScribe
(Path.GetExtension(pdfFilename).ToUpper() == ".PDF")) (Path.GetExtension(pdfFilename).ToUpper() == ".PDF"))
{ {
Process.Start(pdfFilename); Process.Start(pdfFilename);
} }
} }
/// <summary> /// <summary>
/// Displays up a topmost, OK-only message box for the error message /// Displays up a topmost, OK-only message box for the error message
/// </summary> /// </summary>
/// <param name="boxCaption">The box's caption</param> /// <param name="boxCaption">The box's caption</param>
/// <param name="boxMessage">The box's message</param> /// <param name="boxMessage">The box's message</param>
static void DisplayErrorMessage(String boxCaption, static void DisplayErrorMessage(String boxCaption,
String boxMessage) String boxMessage)
{ {
MessageBox.Show(boxMessage, MessageBox.Show(boxMessage,
boxCaption, boxCaption,
MessageBoxButtons.OK, MessageBoxButtons.OK,
MessageBoxIcon.Error, MessageBoxIcon.Error,
MessageBoxDefaultButton.Button1, MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly); MessageBoxOptions.DefaultDesktopOnly);
} }
} }
} }