/// <summary> /// Main routine to call and run the process. /// </summary> /// <returns></returns> static int DoTheWork() { m_NL = Sfm2Xml.Converter.WideToMulti(System.Environment.NewLine, System.Text.Encoding.ASCII); if (m_OutputFileName == null) // || File.Exists { m_OutputFileName = Path.GetTempFileName(); // get a temporaryfile name and full path Console.WriteLine("Created temp file for output: " + m_OutputFileName); // m_createdTempFile = true; } FileStream outputFile = new FileStream(m_OutputFileName, FileMode.Create); BinaryWriter bWriter = new BinaryWriter(outputFile); try { // collect the statistics from the file Sfm2Xml.SfmFileReaderEx reader = new Sfm2Xml.SfmFileReaderEx(m_FileName); int[] byteCounts = reader.GetByteCounts; // get a sorted list of sfm values System.Collections.ArrayList sfmSorted = new System.Collections.ArrayList(reader.SfmInfo); sfmSorted.Sort(); RemoveSFMsFromByteCount(ref byteCounts, sfmSorted, reader); bWriter.Write(MakeBytes(OutputHeader())); OutputByteCountInfo(byteCounts, bWriter); OutputSfmInfo(sfmSorted, bWriter, reader); OutputFollowedByInfo(sfmSorted, bWriter, reader); } finally { if (bWriter != null) { bWriter.Close(); } if (outputFile != null) { outputFile.Close(); } } return(0); }
/// <summary> /// Main routine to call and run the process. /// </summary> /// <returns></returns> static int DoTheWork() { m_NL = Sfm2Xml.Converter.WideToMulti(System.Environment.NewLine, System.Text.Encoding.ASCII); if (m_OutputFileName == null)// || File.Exists { m_OutputFileName = Path.GetTempFileName(); // get a temporaryfile name and full path Console.WriteLine("Created temp file for output: " + m_OutputFileName); // m_createdTempFile = true; } FileStream outputFile = new FileStream(m_OutputFileName, FileMode.Create); BinaryWriter bWriter = new BinaryWriter(outputFile); try { // collect the statistics from the file Sfm2Xml.SfmFileReaderEx reader = new Sfm2Xml.SfmFileReaderEx(m_FileName); int[] byteCounts = reader.GetByteCounts; // get a sorted list of sfm values System.Collections.ArrayList sfmSorted = new System.Collections.ArrayList(reader.SfmInfo); sfmSorted.Sort(); RemoveSFMsFromByteCount(ref byteCounts, sfmSorted, reader); bWriter.Write(MakeBytes(OutputHeader())); OutputByteCountInfo(byteCounts, bWriter); OutputSfmInfo(sfmSorted, bWriter, reader); OutputFollowedByInfo(sfmSorted, bWriter, reader); } finally { if (bWriter != null) bWriter.Close(); if (outputFile != null) outputFile.Close(); } return 0; }
/// <summary> /// Output each sfm and the sfm that followed it with the number of occurances in the input file. /// Each sfm should be listed, except for the case where the last sfm in the file is only used once. /// </summary> /// <param name="sfmSorted"></param> /// <param name="w"></param> /// <param name="reader"></param> static void OutputFollowedByInfo(System.Collections.ArrayList sfmSorted, BinaryWriter w, Sfm2Xml.SfmFileReaderEx reader) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("{0}{1}{2}{3}{4}", nl, "SFM Followed by Info:", nl, "--------------------------------------", nl); sb.AppendFormat("{0}({1}){2}", reader.FileName, reader.Count, nl); w.Write(MakeBytes(sb.ToString())); byte[] a = new byte[] { (byte)' ', (byte)'-', (byte)' ' }; byte[] b = new byte[] { (byte)'\t', (byte)'=', (byte)' ' }; Dictionary <string, Dictionary <string, int> > fbInfo = reader.GetFollowedByInfo(); foreach (string sfm in sfmSorted) { if (fbInfo.ContainsKey(sfm) == false) // case where sfm is only used once and is last marker { continue; } Dictionary <string, int> kvp = fbInfo[sfm]; System.Collections.ArrayList sfm2 = new System.Collections.ArrayList(kvp.Keys); sfm2.Sort(); foreach (string sfmNext in sfm2) { w.Write(MakeBytes(sfm)); w.Write(a); w.Write(MakeBytes(sfmNext)); w.Write(b); w.Write(MakeBytes((kvp[sfmNext]).ToString("N0"))); w.Write(m_NL); } } }
/// <summary> /// Remove the byte counts of the SFMs from the bytecount array. That way the bytecount info /// will be from the data only. /// </summary> /// <param name="byteCounts">int counts for each byte index</param> /// <param name="sfms">list of sfms</param> /// <param name="reader">actual reader object</param> static void RemoveSFMsFromByteCount(ref int[] byteCounts, System.Collections.ArrayList sfms, Sfm2Xml.SfmFileReaderEx reader) { foreach (string sfm in sfms) { int count = reader.GetSFMCount(sfm); byteCounts[(byte)'\\'] -= count; byte[] bytes = MakeBytes(sfm); foreach (byte b in bytes) { byteCounts[b] -= count; } } }
/// <summary> /// Output a count for each sfm in the input file, sorted by sfm. /// </summary> /// <param name="sfmSorted"></param> /// <param name="w"></param> /// <param name="reader"></param> static void OutputSfmInfo(System.Collections.ArrayList sfmSorted, BinaryWriter w, Sfm2Xml.SfmFileReaderEx reader) { StringBuilder sb = new StringBuilder(); sb.Append(nl); sb.AppendFormat("{0}{1}{2,5}{3,9}{4}{5}{6}", "SfmCount info:", nl, "SFM", "count", nl, "--------------", nl); w.Write(MakeBytes(sb.ToString())); byte[] a = new byte[] { (byte)'\t', (byte)'=', (byte)' ' }; foreach (string sfm in sfmSorted) { w.Write(MakeBytes(sfm)); w.Write(a); w.Write(MakeBytes((reader.GetSFMCount(sfm)).ToString("N0"))); w.Write(m_NL); } }