static int PreprocessFile(string sname, string dname, Options options) { TextReader inputReader; TextWriter outputWriter = null; FileStream sourceStream = null; FileStream destStream = null; string sourceDirectory = null; try { if (sname == String.Empty) { inputReader = Console.In; } else { sourceStream = new FileStream(sname, FileMode.Open, FileAccess.Read); inputReader = new StreamReader(sourceStream); try { sourceDirectory = Path.GetDirectoryName(Path.GetFullPath(sname)); } catch (Exception ex) { // This shouldn't happen unless permissions are really screwy, or I've made a terrible mistake. Console.Error.WriteLine("Non-fatal exception attempting to read source path: " + ex); } } if (options.CountReferences) { // When counting references in the input file we don't need an // output file - the count result is returned as the errorLevel. } else { if (dname == String.Empty) { outputWriter = Console.Out; } else { // Unicode BOM causes syntax errors in the gettext utils Encoding utf8WithoutBom = new UTF8Encoding(false); outputWriter = new StreamWriter( new FileStream(dname, FileMode.Create, FileAccess.Write), utf8WithoutBom ); } // determine which newline character to use. string newline = cNewline_Default; switch (options.NewlinePreference) { case NewLineOptions.SameAsSource: // lookahead in inputReader to see whether the line is broken with LF or CRLF if (sourceStream != null && sourceStream.CanSeek) { long startPosition = sourceStream.Position; int peekedChar; while ((peekedChar = sourceStream.ReadByte()) != -1) { if (peekedChar == (int)('\n')) { // We encountered a LF newline = cNewline_LF; break; } else if (peekedChar == (int)('\r')) { // We encountered a CR newline = cNewline_CRLF; break; } } sourceStream.Seek(startPosition, SeekOrigin.Begin); } break; case NewLineOptions.LF: newline = cNewline_LF; break; case NewLineOptions.CRLF: newline = cNewline_CRLF; break; } outputWriter.NewLine = newline; } } catch (Exception ex) { Console.Error.WriteLine("Error: {0}", ex.Message); if (sourceStream != null) sourceStream.Close(); if (destStream != null) destStream.Close(); return (int)ErrorLevel.FatalError_InvalidArgs; } Preprocessor pp = new Preprocessor(options); if (options.CountReferences) { return pp.CountReferences(inputReader, sourceDirectory); } else { int result = pp.Process(inputReader, sourceDirectory, outputWriter); try { outputWriter.Close(); CleanupBadOutput(result, dname); } catch { } return result; } }
static int PreprocessFile(string sname, string dname, Options options) { TextReader inputReader; TextWriter outputWriter = null; FileStream sourceStream = null; FileStream destStream = null; string sourceDirectory = null; try { if (sname == String.Empty) { inputReader = Console.In; } else { sourceStream = new FileStream(sname, FileMode.Open, FileAccess.Read); inputReader = new StreamReader(sourceStream); try { sourceDirectory = Path.GetDirectoryName(Path.GetFullPath(sname)); } catch (Exception ex) { // This shouldn't happen unless permissions are really screwy, or I've made a terrible mistake. Console.Error.WriteLine("Non-fatal exception attempting to read source path: " + ex); } } if (options.CountReferences) { // When counting references in the input file we don't need an // output file - the count result is returned as the errorLevel. } else { if (dname == String.Empty) { outputWriter = Console.Out; } else { // Unicode BOM causes syntax errors in the gettext utils Encoding utf8WithoutBom = new UTF8Encoding(false); outputWriter = new StreamWriter( new FileStream(dname, FileMode.Create, FileAccess.Write), utf8WithoutBom ); } // determine which newline character to use. string newline = cNewline_Default; switch (options.NewlinePreference) { case NewLineOptions.SameAsSource: // lookahead in inputReader to see whether the line is broken with LF or CRLF if (sourceStream != null && sourceStream.CanSeek) { long startPosition = sourceStream.Position; int peekedChar; while ((peekedChar = sourceStream.ReadByte()) != -1) { if (peekedChar == (int)('\n')) { // We encountered a LF newline = cNewline_LF; break; } else if (peekedChar == (int)('\r')) { // We encountered a CR newline = cNewline_CRLF; break; } } sourceStream.Seek(startPosition, SeekOrigin.Begin); } break; case NewLineOptions.LF: newline = cNewline_LF; break; case NewLineOptions.CRLF: newline = cNewline_CRLF; break; } outputWriter.NewLine = newline; } } catch (Exception ex) { Console.Error.WriteLine("Error: {0}", ex.Message); if (sourceStream != null) { sourceStream.Close(); } if (destStream != null) { destStream.Close(); } return((int)ErrorLevel.FatalError_InvalidArgs); } Preprocessor pp = new Preprocessor(options); if (options.CountReferences) { return(pp.CountReferences(inputReader, sourceDirectory)); } else { int result = pp.Process(inputReader, sourceDirectory, outputWriter); try { outputWriter.Close(); CleanupBadOutput(result, dname); } catch { } return(result); } }