コード例 #1
0
        public void IncludeTextReader(string fileName)
        {
            fileName = CanonicalizeFileName(fileName);
            if (!CheckFileName(fileName))
            {
                return;
            }
            Encoding     encoding = EncodingDetector.DetectEncoding(fileName);
            StreamReader sr       = new StreamReader(fileName, encoding);

            IncludeTextReader(sr, fileName);
        }
コード例 #2
0
        public void ReplaceText(string textFileName, string outputFileName)
        {
            Encoding encoding = EncodingDetector.DetectEncoding(textFileName);

            using (var fileStream = new FileStream(textFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                using (var streamReader = new StreamReader(fileStream, encoding))
                {
                    ReplaceText(streamReader, textFileName, outputFileName);
                }
            }
        }
コード例 #3
0
 private void openTextFileButton_Click(object sender, EventArgs e)
 {
     using (var openFileDialog = new OpenFileDialog())
     {
         openFileDialog.DefaultExt = "txt";
         openFileDialog.Filter     = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
         if (openFileDialog.ShowDialogWithTopic(DialogTopic.OpenDocument) == DialogResult.OK)
         {
             this.FileName = openFileDialog.FileName;
             var encoding = EncodingDetector.DetectEncoding(openFileDialog.FileName);
             scintilla1.Text = File.ReadAllText(openFileDialog.FileName, encoding);
         }
     }
 }
コード例 #4
0
            private void ReadReplacementFile(TextReaderWrapper tr, string textFileName)
            {
                textFileName = Path.GetFullPath(textFileName);
                if (IncludedFiles.Contains(textFileName.ToUpperInvariant()))
                {
                    return;
                }
                IncludedFiles.Set(textFileName.ToUpperInvariant());

                string line;

                while (true)
                {
                    line = tr.ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    //remove initial whitespace
                    line = line.TrimStart();

                    //check for "#include"
                    if (line.StartsWith("#include "))
                    {
                        string filenameToInclude = line.Substring("#include ".Length);
                        //check for quotes?
                        if (filenameToInclude.StartsWith("\"") && filenameToInclude.EndsWith("\""))
                        {
                            filenameToInclude = filenameToInclude.Substring(1, filenameToInclude.Length - 2);
                        }
                        string basePath = tr.DirectoryName;
                        filenameToInclude = Path.Combine(basePath, filenameToInclude);
                        if (!File.Exists(filenameToInclude))
                        {
                            throw new FileNotFoundException("Cannot find file: " + filenameToInclude, filenameToInclude);
                        }

                        if (File.Exists(filenameToInclude) && !IncludedFiles.Contains(filenameToInclude.ToUpperInvariant()))
                        {
                            IncludedFiles.Add(filenameToInclude.ToUpperInvariant());
                            var encoding = EncodingDetector.DetectEncoding(filenameToInclude);
                            tr.IncludeTextReader(new StreamReader(filenameToInclude, encoding));
                        }
                        continue;
                    }


                    //remove commented text
                    int indexOfComment = line.IndexOf('#');
                    if (indexOfComment >= 0)
                    {
                        line = line.Substring(0, indexOfComment);
                    }

                    //reading one of these lines:
                    //CODE
                    //function x functionName  (or func, f)
                    //string x text (or str, s)
                    //message x text (or msg, m)
                    //id x text (or i)
                    //x text (same as id x text)

                    string lineTrim = line.Trim();

                    if (lineTrim.Equals("CODE", StringComparison.OrdinalIgnoreCase) || lineTrim.Equals("CODE2", StringComparison.OrdinalIgnoreCase))
                    {
                        bool          isCode2  = lineTrim.Equals("CODE2", StringComparison.OrdinalIgnoreCase);
                        StringBuilder codeText = new StringBuilder();
                        while (true)
                        {
                            line     = tr.ReadLine();
                            lineTrim = line.Trim();
                            if (lineTrim.StartsWith("#include"))
                            {
                                string filenameToInclude = lineTrim.Substring("#include ".Length);
                                //check for quotes?
                                if (filenameToInclude.StartsWith("\"") && filenameToInclude.EndsWith("\""))
                                {
                                    filenameToInclude = filenameToInclude.Substring(1, filenameToInclude.Length - 2);
                                }
                                string basePath = tr.DirectoryName;
                                if (!Path.IsPathRooted(filenameToInclude))
                                {
                                    filenameToInclude = Path.Combine(basePath, filenameToInclude);
                                }
                                filenameToInclude = Path.GetFullPath(filenameToInclude);

                                if (!File.Exists(filenameToInclude))
                                {
                                    throw new FileNotFoundException("Cannot find file: " + filenameToInclude, filenameToInclude);
                                }

                                if (File.Exists(filenameToInclude) && !IncludedFiles.Contains(filenameToInclude.ToUpperInvariant()))
                                {
                                    IncludedFiles.Add(filenameToInclude.ToUpperInvariant());

                                    if (isCode2)
                                    {
                                        //replace with #include <fullpath>, let the compiler handle the actual include
                                        codeText.AppendLine("#include " + AssemblerProjectWriter.EscapeAndQuoteString(filenameToInclude));
                                    }
                                    else
                                    {
                                        var encoding = EncodingDetector.DetectEncoding(filenameToInclude);
                                        tr.IncludeTextReader(new StreamReader(filenameToInclude, encoding));
                                    }
                                }
                                continue;
                            }

                            if (lineTrim.ToUpperInvariant() == "ENDCODE")
                            {
                                if (isCode2)
                                {
                                    CodePatches2.AppendLine(codeText.ToString());
                                }
                                else
                                {
                                    CodePatches.Set(currentFunctionName, codeText.ToString());
                                }
                                break;
                            }
                            else
                            {
                                codeText.AppendLine(line);
                            }
                        }
                        continue;
                    }

                    //find first space
                    int spaceIndex = line.IndexOf(' ');
                    if (spaceIndex == -1)
                    {
                        continue;
                    }
                    string tagName = line.Substring(0, spaceIndex);
                    line = line.Substring(spaceIndex + 1);
                    int number;
                    //if it starts with a number, it's a legacy text replacement
                    if (IntUtil.TryParse(tagName, out number) == true)
                    {
                        tagName = "id";
                    }
                    else
                    {
                        bool   isFunction   = false;
                        string tagNameLower = tagName.ToLowerInvariant();
                        if (tagNameLower == "f" || tagNameLower == "func" || tagNameLower == "function")
                        {
                            isFunction = true;
                        }

                        line       = line.TrimStart();
                        spaceIndex = line.IndexOf(' ');
                        if (spaceIndex == -1)
                        {
                            if (isFunction)
                            {
                                number = -1;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            string numberString = line.Substring(0, spaceIndex);
                            line = line.Substring(spaceIndex + 1);

                            if (IntUtil.TryParse(numberString, out number) == false)
                            {
                                if (isFunction)
                                {
                                    line   = numberString + " " + line;
                                    number = -1;
                                }
                                else
                                {
                                    continue;
                                }
                            }
                        }
                    }

                    line = StringExportImport.UnescapeText(line);

                    switch (tagName.ToLowerInvariant())
                    {
                    case "f":
                    case "func":
                    case "function":
                        string nextFunctionName = line.Trim();
                        var    function         = ainFile.GetFunction(number);
                        if (function == null)
                        {
                            function = ainFile.GetFunction(nextFunctionName);
                            if (function == null)
                            {
                                continue;
                            }
                        }
                        currentFunctionName = function.Name;
                        stringDictionary    = stringEntries.GetOrAddNew(currentFunctionName);
                        messageDictionary   = messageEntries.GetOrAddNew(currentFunctionName);
                        break;

                    case "string":
                    case "str":
                    case "s":
                        stringDictionary.Set(number, line);
                        break;

                    case "msg":
                    case "message":
                    case "m":
                        messageDictionary.Set(number, line);
                        break;

                    case "i":
                    case "id":
                        numberedStrings.Set(number, line);
                        break;
                    }
                }
            }
コード例 #5
0
        public bool ImportTextFile(string fileName, string outputFileName)
        {
            var encoding = EncodingDetector.DetectEncoding(fileName);

            string[] lines = File.ReadAllLines(fileName, encoding);

            ParseTextFile(lines);
            int firstMessageIdNumber = GetFirstMessageIdNumber();
            int lastMessageIdNumber  = firstMessageIdNumber + ainFile.Messages.Count - 1;
            int firstStringIdNumber  = GetFirstStringIdNumber();
            int lastStringIdNumber   = firstStringIdNumber + ainFile.Strings.Count - 1;

            //old system also exported filenames and global group names, we never want to change these, but don't throw error messages if these are seen.
            int firstExtraNumber = firstStringIdNumber + ainFile.Strings.Count;
            int lastExtraNumber  = firstExtraNumber + ainFile.Filenames.Count + ainFile.GlobalGroupNames.Count - 1;

            foreach (var key in lineNumbersAndStrings.Keys)
            {
                if (!((key >= firstMessageIdNumber && key <= lastMessageIdNumber) ||
                      (key >= firstStringIdNumber && key <= lastStringIdNumber) ||
                      (key >= firstExtraNumber && key <= lastExtraNumber)))
                {
                    string prompt = "Invalid line number " + key + "." + Environment.NewLine +
                                    "Line numbers must be between " + firstMessageIdNumber + " and " + lastMessageIdNumber + " (messages) " +
                                    "or between " + firstStringIdNumber + " and " + lastStringIdNumber + " (strings)." + Environment.NewLine +
                                    "Import aborted.";
                    if (Program.ConsoleMode)
                    {
                        Console.WriteLine(prompt);
                    }
                    else
                    {
                        MessageBox.Show(prompt, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                    return(false);
                }
            }


            var ainCopy = ainFile.Clone();

            GetExclusionList();

            foreach (var pair in lineNumbersAndStrings)
            {
                string text         = pair.Value;
                int    idNumber     = pair.Key;
                int    messageIndex = idNumber - firstMessageIdNumber;
                int    stringIndex  = idNumber - firstStringIdNumber;
                if (messageIndex >= 0 && messageIndex < ainCopy.Messages.Count)
                {
                    ainCopy.Messages[messageIndex] = text;
                }
                if (stringIndex >= 0 && stringIndex < ainCopy.Strings.Count)
                {
                    if (ApplyFiltersOnImport && stringsToExclude.ContainsKey(stringIndex))
                    {
                        //it's filtered, do nothing
                    }
                    else
                    {
                        ainCopy.Strings[stringIndex] = text;
                    }
                }
            }

            ainCopy.WriteAndEncryptAinFile(outputFileName);
            return(true);
        }