/// <summary> /// Wandelt eine Texteingabedatei in eine interne Repräsentation um. /// </summary> /// <param name="path">Der volle Pfad zur Datei.</param> private static void ProcessFile(string path) { // See if this is a table bool isTable = (0 == string.Compare(Path.GetExtension(path), ".xht", true)); // The huffman pair table HuffmanPairTable pairTable; // Check mode if (isTable) { // Load the table pairTable = HuffmanPairTable.Load(path); } else { // Create the huffman pair table pairTable = new HuffmanPairTable(); // Open the file for line wiese reading using (StreamReader reader = new StreamReader(path, Encoding.GetEncoding(1252))) { // Read the format string format = reader.ReadLine(); // Create the delegate ProcessLineHandler processor = (ProcessLineHandler)Delegate.CreateDelegate(typeof(ProcessLineHandler), typeof(Program), format); // Process all lines for (string line; null != (line = reader.ReadLine());) { // Process the line processor(pairTable, line); } } } // Create the binary representation TableCreator table = pairTable.CreateBinary(); // Core name string targetName = Path.GetFileNameWithoutExtension(path); // Report Console.WriteLine("{0} ({1} Bytes): Lookup={2} Linked={3} [{6} Patterns total] Dead={4} Unsupported={5}", targetName, table.GetTable().Length, table.DirectTables, table.LinkedTables, table.DeadEnds, table.CharacterNotSupported, table.LinkedItems); // Load the table uint[] encoderTable = table.CreateEncoderTable(); // See if we created the table if (!isTable) { // Overall usage double usage = 0.0; // Process all for (int i = 127 + 1; i-- > 0;) { // Attach to the table index int index = i * (1 + 127 + 1) * 2; // Overall weight long weight = 0; // Process all for (int j = 1 + 127 + 1; j-- > 0;) { // Load items uint width = encoderTable[index++]; uint pattern = encoderTable[index++]; // Check it if (width > 0) { weight += ((long)1) << (int)(32 - width); } } // Get the private usage double privUsage = weight * 1.0 / (((long)1) << 32); // Report pairTable[(0 == i) ? HuffmanItem.StartOrEnd : (char)(i - 1 + 1)].FillFactor = privUsage; // Sum up usage += privUsage; } // Correct usage /= (127 + 1); // Report usage pairTable.FillFactor = usage; // Save XML representation pairTable.Save(Path.ChangeExtension(path, ".xht")); } }
public LineHandler(Regex regex, ProcessLineHandler handler) { _regex = regex; _handler = handler; }