Beispiel #1
0
        // main function to archivate
        private bool Archivate(Session session, int id, bool isPositional, bool isBlock)
        {
            var watch = new Stopwatch();

            watch.Start();

            var dictionary = new List <string>();
            //  var encodedText = new List<string>();
            var encodedBuilder = new StringBuilder();

            //READ file
            var sourceText = FileManipulator.ReadFile(openFD.FileName);

            if (sourceText.Contains(FieldDelimeterChar) || sourceText.Contains(ValueDelimeterChar))
            {
                if (MessageBox.Show(
                        text: "Файл содержит спец.символы, которые приведут к потере данных при дешифровке.\nПродолжить?",
                        caption: "Внимание!",
                        buttons: MessageBoxButtons.YesNo,
                        icon: MessageBoxIcon.Error
                        ) == DialogResult.No)
                {
                    return(false);
                }
            }
            if (string.IsNullOrEmpty(sourceText))
            {
                SetStatusMessage("Empty source");
                return(false);
            }
            int step = isBlock ? session.ElementLength : 1;

            SetStatusMessage("Text spliting");
            var splittedText = StringManipulator.SplitText(sourceText, step, session.ElementLength, dictionary);

            SetStatusMessage("Dictionary building");
            List <string> encodedDictionary = isPositional
                ? CodeSimplifier.BuildCode(dictionary, session)
                : HaffmanCode.BuildCode(splittedText, dictionary, session);

            SetStatusMessage("Arc info building");

            encodedBuilder.Append("TYPE=arc17" + FieldDelimeterChar);
            encodedBuilder.Append(session.IsBlock ? "1" + FieldDelimeterChar : "0" + FieldDelimeterChar);
            encodedBuilder.Append(session.ElementLength.ToString() + FieldDelimeterChar);

            for (int i = 0; i < encodedDictionary.Count; i++)
            {
                encodedBuilder.Append(dictionary[i] + ValueDelimeterChar + encodedDictionary[i] + FieldDelimeterChar);
            }
            session.InfoLength = encodedBuilder.Length;

            SetStatusMessage("Text encoding");
            for (int m = 0; m < splittedText.Count; m++)
            {
                int    index      = dictionary.IndexOf(splittedText[m]);
                string stringCode = encodedDictionary[index];
                encodedBuilder.Append(stringCode);
            }


            SetStatusMessage("Encoded file writing");

            FileManipulator.WriteFile(
                encodedBuilder.ToString(),
                FileManipulator.DoFileName(openFD.FileName, id, ".arc17"),
                rewriteFilesToolStripMenuItem.Checked);
            session.SourceLength      = sourceText.Length;
            session.DestinationLength = encodedBuilder.ToString().Length;

            /*
             * SetStatusMessage("Decoding");
             *
             * // if (elementType is Blocks)
             * if (radioBlocks.Checked)
             * {
             *  for (int k = 0; k < encodedText.Count; k++)
             *  {
             *      int index = encodedDictionary.IndexOf(encodedText[k]);
             *      decodedText += dictionary[index];
             *      SetProgressValue((int)(100 * k / session.SourceLength));
             *  }
             *  // fileout = Path.GetDirectoryName(openFD.FileName) + @"\decodedBlock" + openFD.SafeFileName;
             *
             *  //fileout = id > 0 ? fileout.Replace(Path.GetFileNameWithoutExtension(fileout), Path.GetFileNameWithoutExtension(fileout) + id) : fileout;
             *  FileManipulator.WriteFile(decodedText,
             *                             FileManipulator.DoFileName(openFD.FileName, id),
             *                             rewriteFilesToolStripMenuItem.Checked);
             * }
             * else //elementType is L-grams
             * {
             *  decodedText += dictionary[encodedDictionary.IndexOf(encodedText[0])];
             *  for (int i = 1; i < encodedText.Count; i++)
             *  {
             *      int index = encodedDictionary.IndexOf(encodedText[i]);
             *      string t = dictionary[index];
             *      t = t.Substring(session.ElementLength - 1, 1);
             *      decodedText += t;
             *  }
             *
             *  FileManipulator.WriteFile( decodedText,
             *                             FileManipulator.DoFileName(openFD.FileName, id),
             *                             rewriteFilesToolStripMenuItem.Checked);
             * }
             */
            SetStatusMessage("Finished");
            SetProgressValue(0);
            watch.Stop();
            session.TimeSpent = watch.Elapsed;
            return(true);
        }
Beispiel #2
0
        //old sample arc + dearc
        private bool OldArchivate(Session session, int id)
        {
            var watch = new Stopwatch();

            watch.Start();

            var dictionary     = new List <string>();
            var encodedText    = new List <string>();
            var encodedBuilder = new StringBuilder();
            var decodedText    = string.Empty;
            //READ file
            var sourceText = FileManipulator.ReadFile(openFD.FileName);

            if (string.IsNullOrEmpty(sourceText))
            {
                SetStatusMessage("Empty source");
                return(false);
            }
            int step = radioBlocks.Checked ? session.ElementLength : 1;

            SetStatusMessage("Text spliting");
            var splittedText = StringManipulator.SplitText(sourceText, step, session.ElementLength, dictionary);

            SetStatusMessage("Dictionary building");
            List <string> encodedDictionary = radioPositional.Checked
                ? CodeSimplifier.BuildCode(dictionary, session)
                : HaffmanCode.BuildCode(splittedText, dictionary, session);

            SetStatusMessage("Arc info building");

            encodedBuilder.Append("TYPE=arc17" + FieldDelimeterChar);
            encodedBuilder.Append(session.IsPositional ? "1" + FieldDelimeterChar : "0" + FieldDelimeterChar);
            encodedBuilder.Append(session.IsBlock ? '1' : '0');

            for (int i = 0; i < encodedDictionary.Count; i++)
            {
                encodedBuilder.Append('|' + dictionary[i] + '=' + encodedDictionary[i]);
            }
            session.InfoLength = encodedBuilder.Length;
            SetStatusMessage("Text encoding");
            for (int m = 0; m < splittedText.Count; m++)
            {
                int    index      = dictionary.IndexOf(splittedText[m]);
                string stringCode = encodedDictionary[index];
                encodedText.Add(stringCode);
                encodedBuilder.Append(stringCode);
            }

            SetStatusMessage("Encoded file writing");

            FileManipulator.WriteFile(
                encodedBuilder.ToString(),
                FileManipulator.DoFileName(openFD.FileName, id, ".arc17"),
                rewriteFilesToolStripMenuItem.Checked);
            session.SourceLength      = sourceText.Length;
            session.DestinationLength = encodedBuilder.ToString().Length;

            SetStatusMessage("Decoding");

            // if (elementType is Blocks)
            if (radioBlocks.Checked)
            {
                for (int k = 0; k < encodedText.Count; k++)
                {
                    int index = encodedDictionary.IndexOf(encodedText[k]);
                    decodedText += dictionary[index];
                    SetProgressValue((int)(100 * k / session.SourceLength));
                }
                // fileout = Path.GetDirectoryName(openFD.FileName) + @"\decodedBlock" + openFD.SafeFileName;

                //fileout = id > 0 ? fileout.Replace(Path.GetFileNameWithoutExtension(fileout), Path.GetFileNameWithoutExtension(fileout) + id) : fileout;
                FileManipulator.WriteFile(decodedText,
                                          FileManipulator.DoFileName(openFD.FileName, id),
                                          rewriteFilesToolStripMenuItem.Checked);
            }
            else //elementType is L-grams
            {
                decodedText += dictionary[encodedDictionary.IndexOf(encodedText[0])];
                for (int i = 1; i < encodedText.Count; i++)
                {
                    int    index = encodedDictionary.IndexOf(encodedText[i]);
                    string t     = dictionary[index];
                    t            = t.Substring(session.ElementLength - 1, 1);
                    decodedText += t;
                }

                FileManipulator.WriteFile(decodedText,
                                          FileManipulator.DoFileName(openFD.FileName, id),
                                          rewriteFilesToolStripMenuItem.Checked);
            }

            SetStatusMessage("Finished");
            SetProgressValue(0);
            watch.Stop();
            session.TimeSpent = watch.Elapsed;
            return(true);
        }