public void Dispose() { if (OutputWriter != null) { OutputWriter.Flush(); OutputWriter.Close(); } }
protected override void EndProcessing() { if (_formatManager != null) { var data = _formatManager.End(); if (data.Count > 0) { var processor = FormatProcessor.Get(OutputWriter, data[0].Shape); foreach (var formatPayload in data) { processor.ProcessPayload(formatPayload); } } } if (OutputWriter != null) { OutputWriter.Close(); } }
/// <summary> /// The main entry point for the application. /// </summary> public static int Main(string[] args) { string OutputFormat = " {0,-14} 0x{1,-14:x}"; //IO variables. string InFilename = null; FileStream InFileStream = null; BinaryReader InputReader = null; string OutFilename = null; BinaryWriter OutputWriter = null; //Used to pass line termination chars to FixOrConvert. byte [] crlfArr = new byte[] { 0x0d, 0x0a }; byte [] lfArr = new byte[] { 0x0a }; //Get file expression from argument. try { DirectoryInfo curdir = new DirectoryInfo(System.Environment.CurrentDirectory); string FileExpression = null; //Usage if (args.Length < 1) { Console.WriteLine(nl + "Usage: crlf <filename | expression>" + nl); Console.WriteLine("Requires filename or wildcard expression." + nl); }//end if else { FileExpression = args[0]; FileInfo[] files = curdir.GetFiles(FileExpression); if (files.Length < 1) { Console.WriteLine("File does not exist or invalid file expression."); return(1); }//end if //Loop to cover all files in expression list foreach (FileInfo fi in files) { int c1 = 0; int c2 = 0; TermListType TermList = new TermListType(); string Reply = null; bool NeedsFix = false; bool AllLF = false; bool AllCRLF = false; bool CRLF = false; bool LFOnly = false; bool CROnlyFix = false; CRFixType CRFix = CRFixType.Delete; FileStream OutputFileStream = null; string Prompt = null; Console.WriteLine("Processing file: {0}", fi.Name); //Check again that file exists. Probably redundant but doesn't hurt. if (!File.Exists(fi.FullName)) { Console.WriteLine("{0} does not exist, skipping file."); continue; }//end if try { InFilename = fi.FullName; InFileStream = new FileStream(InFilename, FileMode.Open); InputReader = new BinaryReader(InFileStream, System.Text.Encoding.ASCII); }//end try catch (Exception e) { bool Exit = OutputException("Problem opening file: " + InFilename, e); if (Exit) { return(1); } else //Go to next file in list. { continue; } }//end catch InputReader.BaseStream.Seek(0, SeekOrigin.Begin); InputReader.BaseStream.Position = 0; if (InputReader.BaseStream.Length > 2) { c1 = InputReader.Read(); c2 = InputReader.Read(); } //end if else { Console.WriteLine("File too small"); //Go to next file in list. continue; } //end else if (c1 == 0x0a) { TermListItem tli = new TermListItem((long)1, TermTypeEnum.LFOnly); TermList.Add(tli); } //end if long Count = 2; //Loop through file while (c2 > -1) { if (c1 == 0x0d && c2 != 0x0a) { TermListItem CRtli = new TermListItem((long)Count - 1, TermTypeEnum.CROnly); TermList.Add(CRtli); }//end if if (c1 != 0x0d && c2 == 0x0a) { TermListItem LFtli = new TermListItem((long)Count, TermTypeEnum.LFOnly); TermList.Add(LFtli); }//end if if (c1 == 0x0d && c2 == 0x0a) { TermListItem CRLFtli = new TermListItem((long)Count, TermTypeEnum.CRLF); TermList.Add(CRLFtli); }//end if c1 = c2; c2 = InputReader.Read(); Count++; }//end while Console.WriteLine(nl + "Number of unmatched CRs: {0, 12}", TermList.CROnlyCount); Console.WriteLine("Number of LF-only chars {0, 12}", TermList.LFOnlyCount); Console.WriteLine("Number of CRLF sets: {0, 12}" + nl, TermList.CRLFCount); Console.WriteLine("Size of input file: {0}", InputReader.BaseStream.Length); if (TermList.LFOnlyCount > 0 && TermList.CRLFCount == 0) { Console.WriteLine("File is LF-only terminated" + nl); Console.WriteLine("Total number of LF terminated lines: {0}", TermList.LFOnlyCount); AllLF = true; }//end if else { if (TermList.CRLFCount > 0 && TermList.LFOnlyCount == 0) { Console.WriteLine("File is CRLF terminated"); Console.WriteLine("Total number of CRLF terminated lines: {0}", TermList.CRLFCount); AllCRLF = true; } else { if (TermList.LFOnlyCount > TermList.CRLFCount && TermList.LFOnlyCount > 0) { Console.WriteLine("File is mostly LF-only terminated"); NeedsFix = true; }//end if else { if (TermList.CRLFCount > TermList.LFOnlyCount && TermList.CRLFCount > 0) { Console.WriteLine("File is mostly CRLF terminated" + nl); NeedsFix = true; } //end if } //end else Console.WriteLine("Number of unexpected CR chars if file is LF-only: {0}" + nl, TermList.CRLFCount); if (TermList.CRLFCount > 0) { OutputLocations(TermList, TermList.FirstCRLF, OutputFormat, false, TermList.CRLFCount); }//end if Console.WriteLine("Number of unexpected LF-only chars if file is CRLF: {0}" + nl, TermList.LFOnlyCount); if (TermList.LFOnlyCount > 0) { OutputLocations(TermList, TermList.FirstLFOnly, OutputFormat, false, TermList.LFOnlyCount); }//end if else { Console.WriteLine("Insufficient line termination chars to " + "determine termination type." + nl); //Jump to next file in list. continue; } //end else } //end else } //end else if (TermList.CROnlyCount > 0) { CROnlyFix = true; Console.WriteLine("Number of unmatched CR-only chars: {0}" + nl, TermList.CROnlyCount); OutputLocations(TermList, TermList.FirstCROnly, OutputFormat, true, TermList.CROnlyCount); }//end if if (NeedsFix || CROnlyFix) { Prompt = nl + "Do you wish to fix this file? "; }//end if else { if (AllLF) { Prompt = "Do you wish to convert this file to all CRLF? "; }//end if else { if (AllCRLF) { Prompt = "Do you wish to convert this file to all LF-only? "; } //end if } //end else } //end else Console.Write(Prompt); Reply = Console.ReadLine(); if (Reply.ToLower() == "y" || Reply.ToLower() == "yes") { try { OutFilename = Path.GetFileNameWithoutExtension(InFilename) + ".fixed" + Path.GetExtension(InFilename); OutputFileStream = new FileStream(OutFilename, FileMode.Create); OutputWriter = new BinaryWriter(OutputFileStream, System.Text.Encoding.ASCII); }//end try catch (Exception e) { bool Exit = OutputException("Problem opening file: " + OutFilename, e); if (Exit) { return(1); } else { continue; } }//end catch //Reposition input file seek for second read from start. InputReader.BaseStream.Seek(0, SeekOrigin.Begin); InputReader.BaseStream.Position = 0; //If we got here with 'yes' then we will convert to opposite if no fixes required. if (AllLF) { CRLF = true; }//end if else { if (AllCRLF) { LFOnly = true; } //end if } //end else //Since we need fixes, ask which way to convert. //Ask to convert towards most common one first. if (NeedsFix) { if (TermList.CRLFCount >= TermList.LFOnlyCount) //Mostly CRLF { Console.Write("Do you wish to make file CRLF? "); Reply = Console.ReadLine(); if (Reply.ToLower() == "y" || Reply.ToLower() == "yes") { CRLF = true; }//end if else { Console.WriteLine("Modified file will be LF-only..."); LFOnly = true; } //end else } //end if else //Mostly LF-only { Console.Write("Do you wish to make file LF-only? "); Reply = Console.ReadLine(); if (Reply.ToLower() == "y" || Reply.ToLower() == "yes") { LFOnly = true; }//end if else { Console.WriteLine("Modified file will be CRLF..."); CRLF = true; } //end else } } //end if if (TermList.CROnlyCount > 0) { Console.Write("Do you wish to delete unmatched CR chars? "); Reply = Console.ReadLine(); if (Reply.ToLower() == "y" || Reply.ToLower() == "yes") { CRFix = CRFixType.Delete; } else { Console.WriteLine("Unmatched CR chars will be replaced with normal line termination."); CRFix = CRFixType.Replace; } }//end if if (NeedsFix || CROnlyFix) { if (CROnlyFix && !NeedsFix) //CROnlyFix case { if (CRLF) { FixOrConvert(TermList, InputReader, OutputWriter, crlfArr, TermList.FirstCROnly, CRFix); }//end if else { if (LFOnly) { FixOrConvert(TermList, InputReader, OutputWriter, lfArr, TermList.FirstCROnly, CRFix); } //end if } //end else } //end if else { if (CRLF) //Change LF-onlys to CRLF during fix { FixOrConvert(TermList, InputReader, OutputWriter, crlfArr, TermList.FirstLFOnly, CRFix); }//end if else { if (LFOnly) //Change CRLFs to LF-only during fix { FixOrConvert(TermList, InputReader, OutputWriter, lfArr, TermList.FirstCRLF, CRFix); } //end if } //end else } //end if } //end else else //File conversion only { if (CRLF) //Convert to CRLF { FixOrConvert(TermList, InputReader, OutputWriter, crlfArr, -1, CRFix); }//end if else { if (LFOnly) //Convert to LF-only { FixOrConvert(TermList, InputReader, OutputWriter, lfArr, -1, CRFix); } //end if } //end else } //end else OutputWriter.BaseStream.Flush(); OutputWriter.Close(); OutputWriter = null; Console.WriteLine(nl + "Modified file saved to: {0}", OutFilename); } //end if else //Reply was 'no' { Console.WriteLine("Exiting without creating fixed or modified file."); }//end else InputReader.Close(); InputReader = null; Console.WriteLine(); } //end foreach } //end else return(0); } //end try catch (Exception e) { OutputException("Unexpected exception", e); return(1); }//end catch finally { if (InputReader != null) { InputReader.Close(); }//end if if (OutputWriter != null) { OutputWriter.Close(); } //end if } //end finally } //end Main
public void Close() { Stop(); OutputWriter.Close(); OutputWriter = null; }
public static void Main(string[] args) { if (args == null || args.Length == 0) { throw new ApplicationException(USAGE); } int Index = 0; // method to call string ActivityName = args[Index++]; // write to console or file? TextWriter OutputWriter = null; string OutputFileName = null; if (Index < args.Length && "-o".Equals(args[Index], StringComparison.OrdinalIgnoreCase)) { Index++; OutputFileName = args[Index++]; if (!OutputFileName.Contains(ITER_CHAR)) { OutputWriter = new StreamWriter(OutputFileName); } } else { OutputWriter = Console.Out; } string[] SerializedParams = new string[args.Length - Index]; Array.Copy(args, Index, SerializedParams, 0, SerializedParams.Length); object Output = null; List <object> Params = ParseParams(SerializedParams); switch (ActivityName) { case "IsCSVReadyFileExists": { var CSVRootPathInput = Params[0] as string; Output = LoadAppLogic.IsCSVReadyFileExists(CSVRootPathInput); break; } case "ReadCSVReadyFile": { var CSVRootPathInput = Params[0] as string; Output = LoadAppLogic.ReadCSVReadyFile(CSVRootPathInput); break; } case "IsMatchCSVFileTables": { var CSVFileEntries = Params[0] as List <LoadAppLogic.CSVFileEntry>; Output = LoadAppLogic.IsMatchCSVFileTables(CSVFileEntries); break; } case "CreateEmptyLoadDB": { var JobIDInput = Params[0] as string; Output = LoadAppLogic.CreateEmptyLoadDB(JobIDInput); break; } case "SplitList": { var CSVFileEntries = Params[0] as List <LoadAppLogic.CSVFileEntry>; int FileCount = 0; foreach (LoadAppLogic.CSVFileEntry Entry in CSVFileEntries) { if (OutputFileName != null) { OutputWriter = new StreamWriter(OutputFileName.Replace(ITER_CHAR, "" + FileCount++)); OutputWriter.Write(SerializeParam(Entry)); OutputWriter.Close(); } else { Console.WriteLine(SerializeParam(Entry)); Console.WriteLine(); } } return; //break; } case "IsExistsCSVFile": { var FileEntry = Params[0] as LoadAppLogic.CSVFileEntry; Output = LoadAppLogic.IsExistsCSVFile(FileEntry); break; } case "ReadCSVFileColumnNames": { var FileEntry = Params[0] as LoadAppLogic.CSVFileEntry; Output = LoadAppLogic.ReadCSVFileColumnNames(FileEntry); break; } case "IsMatchCSVFileColumnNames": { var FileEntry = Params[0] as LoadAppLogic.CSVFileEntry; Output = LoadAppLogic.IsMatchCSVFileColumnNames(FileEntry); break; } case "LoadCSVFileIntoTable": { var DBEntry = Params[0] as LoadAppLogic.DatabaseEntry; var FileEntry = Params[1] as LoadAppLogic.CSVFileEntry; Output = LoadAppLogic.LoadCSVFileIntoTable(DBEntry, FileEntry); break; } case "UpdateComputedColumns": { var DBEntry = Params[0] as LoadAppLogic.DatabaseEntry; var FileEntry = Params[1] as LoadAppLogic.CSVFileEntry; Output = LoadAppLogic.UpdateComputedColumns(DBEntry, FileEntry); break; } case "IsMatchTableRowCount": { var DBEntry = Params[0] as LoadAppLogic.DatabaseEntry; var FileEntry = Params[1] as LoadAppLogic.CSVFileEntry; Output = LoadAppLogic.IsMatchTableRowCount(DBEntry, FileEntry); break; } case "IsMatchTableColumnRanges": { var DBEntry = Params[0] as LoadAppLogic.DatabaseEntry; var FileEntry = Params[1] as LoadAppLogic.CSVFileEntry; Output = LoadAppLogic.IsMatchTableColumnRanges(DBEntry, FileEntry); break; } case "CompactDatabase": { var DBEntry = Params[0] as LoadAppLogic.DatabaseEntry; LoadAppLogic.CompactDatabase(DBEntry); break; } default: { Console.WriteLine("Activity not found! " + ActivityName); break; } } if (Output != null) { OutputWriter.Write(SerializeParam(Output)); OutputWriter.Close(); } }