源代码修改-WriteSourceFile

来源:互联网 发布:代理记账业务网络平台 编辑:程序博客网 时间:2024/06/07 14:29
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;


namespace 源代码修改
{
    public class WriteSourceFile : IOperateSource
    {
        private ReadDocument readDocument;
        private List<string> sourceFileContent;
        private int amountOfChanges;
        public int AmountOfChanges
        {
            get
            {
                return amountOfChanges;
            }
        }


        public WriteSourceFile(ReadDocument readDocument)
        {
            this.readDocument = readDocument;
            sourceFileContent = new List<string>();
            amountOfChanges = 0;
        }


        //  精简掉代码中;后面的所有内容.
        private void SimplifyALine(ref string aLine)
        {
            bool doubleQuotationHasOpend = false;


            for (int i = 0; i != aLine.Length; ++i)
            {
                if (aLine[i] == '"')
                    doubleQuotationHasOpend = !doubleQuotationHasOpend;
                else if (aLine[i] == ';')
                {
                    if (doubleQuotationHasOpend)
                    {
                        //  在代码中的字符串常量中出现了;
                        continue;
                    }
                    else
                    {
                        //   此;是结束代码的;
                        if (i + 1 != aLine.Length)
                        {
                            aLine = aLine.Remove(i + 1);
                            return;
                        }
                        //  否则,代码不需要精简
                    }
                }
            }
        }
          
        //  添加注释之前去掉代码;后的原所有注释如果有
        private void AddExegesis(ref string aProcessedLine, string originalString)
        {
            SimplifyALine(ref aProcessedLine);
            aProcessedLine += ("//   " + originalString);
        }


        private void ProcessALine(ref string aLine)
        {
            for (int i = 0; i < aLine.Length; ++i)
            {
                if (aLine[i] == KeyChar && i + 1 < aLine.Length && aLine[i + 1] == '"' && i + 2 < aLine.Length)
                {
                    string originalString = null;
                    for (int j = i + 2; j < aLine.Length && aLine[j] != '"'; ++j)
                        originalString += aLine[j];
                    if (readDocument.ContainsOriginalString(originalString))
                    {
                        ++amountOfChanges;
                        aLine = aLine.Replace(originalString, readDocument.GetTargetString(originalString));
                        AddExegesis(ref aLine, originalString);
                    }
                    //  用以应对一行中出现多个待翻译字符串的情况
                    i += (originalString.Length + 2);
                }
            }
        }


        override public void Go(FileInfo sourceFineInfo)
        {
            //  将想要被替换的信息用文档中的相应内容替换
            StreamReader reader = sourceFineInfo.OpenText();
            string aLine = reader.ReadLine();
            while (aLine != null)
            {
                ProcessALine(ref aLine);
                sourceFileContent.Add(aLine);
                aLine = reader.ReadLine();
            }
            reader.Close();
            StreamWriter writer = new StreamWriter(sourceFineInfo.FullName, false, Encoding.UTF8);
            foreach (string aProcessedLine in sourceFileContent)
            {
                writer.WriteLine(aProcessedLine);
            }
            writer.Close();
            sourceFileContent.Clear();
        }
    }
}
	
				
		
原创粉丝点击