Beispiel #1
0
        public override Trie <byte> ParseReplacementFile(FileStream inStream, byte delimiter, byte[] lineDelimiter)
        {
            Trie <byte> trie = new Trie <byte>();
            Trie <byte> cur  = trie;

            using (inStream)
            {
                List <byte> dest = new List <byte>();
                int         count;
                bool        writingSource      = true;
                int         lineDelimiterIndex = 0;

                // read in a chunk of data and parse it
                while ((count = inStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    // iterate through the loaded data
                    for (int i = 0; i < count; i++)
                    {
                        // pull out byte
                        byte b = buffer[i];

                        // check if the byte is equal to the delimiter
                        if (b == delimiter)
                        {
                            // switch to reading the bytes as the destination replacement
                            lineDelimiterIndex = 0;
                            writingSource      = false;
                        }

                        // check to see if the byte is equal to the current line delimiter
                        else if (b == lineDelimiter[lineDelimiterIndex])
                        {
                            // increment the line delimiter index
                            lineDelimiterIndex++;

                            // check to see if the byte string matches the line delimiter exactly
                            if (lineDelimiterIndex == lineDelimiter.Length)
                            {
                                // add the mapping to the trie and start again
                                cur.Replacement = dest.ToArray();
                                cur             = trie;
                                dest.Clear();
                                lineDelimiterIndex = 0;
                                writingSource      = true;
                            }
                        }

                        // check if the byte should go in the source trie
                        else if (writingSource)
                        {
                            // add it to the trie
                            lineDelimiterIndex = 0;
                            cur.Add(b);

                            // add a child trie to the trie if there is a need
                            if (cur.GetChild(b) == null)
                            {
                                cur.SetChild(b, new Trie <byte>());
                            }

                            // move the cur reference to the child trie
                            cur = cur.GetChild(b);
                        }

                        // add the byte to the destination array
                        else
                        {
                            lineDelimiterIndex = 0;
                            dest.Add(b);
                        }
                    }
                }
            }
            return(trie);
        }
Beispiel #2
0
 public void SetChild(T t, Trie <T> child)
 {
     children[t] = child;
 }
Beispiel #3
0
 public void Add(T t)
 {
     children[t] = new Trie <T>();
 }
Beispiel #4
0
 public void Add(T t, Trie <T> child)
 {
     children[t] = child;
 }
Beispiel #5
0
 public TrieEnumerator(Trie <T> trie)
 {
     this.trie = trie;
 }
Beispiel #6
0
        private void wxBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            // read all replacement definitions from file
            try
            {
                FileInfo replacementFileInfo = new FileInfo(wxReplacementPath.Text);
                long     fileLength          = replacementFileInfo.Length;

                FileStream replaceReader = new FileStream(wxReplacementPath.Text, FileMode.Open);

                Trie <byte> trie = replacer.ParseReplacementFile(replaceReader, 0x2C, new byte[] { 0x0D, 0x0A }); // use comma as delimiter with CRLF as line delimiter

                /*Dictionary<byte, Trie<byte>>.KeyCollection.Enumerator test = trie.Children.Keys.GetEnumerator();
                 * while (test.MoveNext())
                 * {
                 *  Console.WriteLine(test.Current);
                 * }*/

                FileStream sourceReader = new FileStream(wxSourcePath.Text, FileMode.Open);
                FileStream destWriter   = new FileStream(wxDestPath.Text, FileMode.Create);

                replacer.Replace(sourceReader, destWriter, trie, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not open replacement word map file: " + ex.Message);
                Console.WriteLine(ex.StackTrace);
                return;
            }

            // write all data to the destination file

            /*try
             * {
             *  FileStream destWriter = new FileStream(wxDestPath.Text, FileMode.Create);
             *  using (destWriter)
             *  {
             *      try
             *      {
             *          // load source file to read from
             *          StreamReader sourceReader;
             *          if (uxInferFileEncoding.Checked)
             *          {
             *              sourceReader = new StreamReader(wxSourcePath.Text, true);
             *              destFileEncoding = sourceReader.CurrentEncoding;
             *          } else
             *          {
             *              sourceReader = new StreamReader(wxSourcePath.Text, sourceFileEncoding);
             *          }
             *
             *          FileInfo sourceFileInfo = new FileInfo(wxSourcePath.Text);
             *          long fileLength = sourceFileInfo.Length;
             *
             *          using (sourceReader)
             *          {
             *              int progress = 0;
             *              while (!sourceReader.EndOfStream)
             *              {
             *                  if (wxBackgroundWorker.CancellationPending)
             *                  {
             *                      break;
             *                  }
             *                  string line = sourceReader.ReadLine();
             *                  int i = 0;
             *                  foreach (KeyValuePair<string, string> item in map)
             *                  {
             *                      if (wxBackgroundWorker.CancellationPending)
             *                      {
             *                          break;
             *                      }
             *                      line = Regex.Replace(line, item.Key, item.Value, wxCaseSensitive.Checked ? RegexOptions.None : RegexOptions.IgnoreCase);
             *                      progress = (int)(((double)(sourceReader.BaseStream.Position * i) / (double)(fileLength * map.Count)) * 50) + 50;
             *                      if (progressForm.GetProgressBar().Value != progress)
             *                      {
             *                          wxBackgroundWorker.ReportProgress(progress);
             *                      }
             *                      i++;
             *                  }
             *                  byte[] lineArray = destFileEncoding.GetBytes(line + "\n");
             *                  destWriter.Write(lineArray, 0, lineArray.Length);
             *              }
             *          }
             *      }
             *      catch (Exception ex)
             *      {
             *          MessageBox.Show("Could not open source file: " + ex.Message);
             *          return;
             *      }
             *  }
             * }
             * catch (Exception ex)
             * {
             *  MessageBox.Show("Unable to save destination file: " + ex.Message);
             *  return;
             * }*/
        }