源代码修改-ReadDocument

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();
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值