/// <summary>
        /// Method used for testing that displays encoding to the console.
        /// </summary>
        /// <param name="root"></param>
        private void DisplayEncodingTable(BinaryTreeNode <CharacterFrequency> root)
        {
            if (root != null)
            {
                Encoding.Append("0");
                DisplayEncodingTable(root.Left);
                if (root.IsLeaf())
                {
                    var charEncoding = new CharacterEncoding((byte)root.Data.Character, Encoding.ToString());
                    Console.Write(charEncoding.Character.ToString());
                    Console.Write(" - ");
                    Console.WriteLine(charEncoding.Encoding);
                }

                Encoding.Append("1");
                DisplayEncodingTable(root.Right);

                if (Encoding.Length > 0)
                {
                    Encoding.Remove(Encoding.Length - 1, 1);
                }
            }
            else
            {
                // Remove a character from the encoding string// Check and reove multiple 0's too maybe?
                Encoding.Remove(Encoding.Length - 1, 1);
                //Console.WriteLine("remove!");
            }
        }
 public static CharacterEncoding[] BuildEmptyArray(int size)
 {
     CharacterEncoding[] arr = new CharacterEncoding[size];
     for (int i = 0; i < arr.Length; i++)
     {
         arr[i] = new CharacterEncoding((byte)i, string.Empty);
     }
     return(arr);
 }
        /// <summary>
        /// Writes CharEncodings to file. For Decompression using RegEx.
        /// </summary>
        public void FileWriterEncoding()
        {
            StringBuilder path = new StringBuilder();

            path.Append(Directory.GetCurrentDirectory());
            path.Append(@"\" + FileWrite + ".txt");

            try
            {
                using (StreamWriter writer = new StreamWriter(path.ToString()))
                {
                    CharacterEncoding.WriteEncoding(writer, CharEncodings);
                }
                Console.WriteLine($"File {FileWrite}.txt has been written");
            }
            catch
            {
                Console.WriteLine("Could not write File");
            }
        }
예제 #4
0
        static void Main(string[] args)
        {
            const byte INPUT  = 0;
            const byte OUTPUT = 1;
            const byte MODE   = 2;

            CompressionInputValidation(args);


            var compressionManager = new CompressionManager
            {
                FileRead        = args[INPUT],
                FileWrite       = args[OUTPUT],
                CharFrequencies = CharacterFrequency.BuildEmptyArray(256),
                CharEncodings   = CharacterEncoding.BuildEmptyArray(256)
            };

            if (args[MODE].ToUpper() == "COMPRESS")
            {
                compressionManager.Compress();
            }
            else if (args[MODE].ToUpper() == "COMPRESSTEST")
            {
                compressionManager.CompressTest();
            }
            else if (args[MODE].ToUpper() == "DECOMPRESS")
            {
                compressionManager.Decompress();
            }
            else
            {
                Console.WriteLine("Please enter COMPRESS or DECOMPRESS after both files");
                Console.ReadKey();
                Environment.Exit(0);
            }
            #endregion
        }