原始碼修改-ReadDocument

來源:互聯網
上載者:User
using System;using System.Collections.Generic;using System.IO;using System.Text;namespace 原始碼修改{    public class ReadDocument : IOperateDocument    {        private FileInfo documentFileInfo;        private StreamReader reader;        //  以原語言為鍵,翻譯後的語言為值        private Dictionary<string, string> stringTable;        private bool hasFoundDocument;        public bool HasFoundDocument        {            get            {                return hasFoundDocument;            }        }        private bool documentFormatIsOK;        public bool DocumentFormatIsOK        {            get            {                return documentFormatIsOK;            }        }        private string GetKey(ref string aLine)        {            //  跳過分隔字元內的說明用的資料            int i = 0;            int countOfHasFoundSeparator = 0 ;            for (; i != aLine.Length; ++i)            {                if (aLine[i] == SeparatorBetweenTwoAreas)                {                    ++countOfHasFoundSeparator;                    if (countOfHasFoundSeparator == AmountOfSepator)                    {                        ++i;                        break;                    }                }            }            if (i >= aLine.Length)            {                //  讀取鍵失敗                return null;            }            string key = null;            for (; i != aLine.Length && aLine[i] != SeparatingString[0]; ++i)                key += aLine[i];            string newALine = null;            for (; i != aLine.Length; ++i)                newALine += aLine[i];            aLine = newALine;            return key;        }        private string GetValue(string aLine)        {            if (aLine == null)                return null;            if (aLine.Length <= SeparatingString.Length)                return null;            for (int i = 0; i != SeparatingString.Length; ++i)            {                if (aLine[i] != SeparatingString[i])                    return null;            }            string value = null;            for (int i = SeparatingString.Length; i != aLine.Length; ++i)                value += aLine[i];            return value;        }        //  將翻譯後的文檔讀入尋找表中        private void ReadStringToTable()        {            string aLine = reader.ReadLine();            if (aLine == null)            {                documentFormatIsOK = false;                return;            }            while (aLine != null)            {                string originalString = GetKey(ref aLine);                if (originalString == null)                {                    documentFormatIsOK = false;                    return;                }                if (!stringTable.ContainsKey(originalString))                {                    string targetString = GetValue(aLine);                    if (targetString == null)                    {                        documentFormatIsOK = false;                        stringTable.Clear();                        return;                    }                    stringTable[originalString] = targetString;                    aLine = reader.ReadLine();                }                else                {                    //  出現了重複待修改的代碼                    documentFormatIsOK = false;                    return;                }            }            documentFormatIsOK = true;        }        public ReadDocument()        {            documentFileInfo = new FileInfo(DocumentName);            try            {                reader = documentFileInfo.OpenText();            }            catch (FileNotFoundException)            {                //  沒有找到DocumentName檔案.                hasFoundDocument = false;                return;            }            hasFoundDocument = true;            stringTable = new Dictionary<string, string>();            ReadStringToTable();        }        public bool ContainsOriginalString(string originalString)        {            return stringTable.ContainsKey(originalString);        }        public string GetTargetString(string originalString)        {            return stringTable[originalString];        }        public void OverReading()        {            if (reader != null)                reader.Close();        }        public void CreateANewDocument()        {            StreamWriter writer = new StreamWriter(documentFileInfo.FullName, false, Encoding.UTF8);            writer.Close();            reader = documentFileInfo.OpenText();        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.