源代码修改-WriteDocument

来源:互联网 发布:代理记账业务网络平台 编辑:程序博客网 时间:2024/05/17 21:30
using System;using System.Collections.Generic;using System.IO;using System.Text;namespace 源代码修改{    //  将字符串写入文档    public class WriteDocument : IOperateDocument    {        private StreamWriter writer;        private Dictionary<int, string> buffer;        private int amountOfLines;        private int maxLengthOfFileNameArea;        private int maxLengthOfALineInSourceFileArea;        public WriteDocument()        {            writer = new StreamWriter(DocumentName, false, Encoding.UTF8);            buffer = new Dictionary<int, string>();            amountOfLines = 0;            maxLengthOfFileNameArea = 0;            maxLengthOfALineInSourceFileArea = 0;        }        private void AddPrefixToALine(ref string aLine, string fullSourceFileName, string theWholeLineInSourceFile)        {            aLine = fullSourceFileName + SeparatorBetweenTwoAreas + theWholeLineInSourceFile + SeparatorBetweenTwoAreas + aLine;        }        //  获得一串字符串的屏幕长度        //  每个Unicode字符长度为2        private int GetLengthOfAString(string str)        {            int length = 0;            for (int i = 0; i != str.Length; ++i)            {                if ((int)str[i] > 127)                    length += 2;                else                    ++length;            }            return length;        }        private string GenerateSpaceString(int amountOfSpaces)        {            string spaceString = null;            for (int i = 0; i != amountOfSpaces; ++i)                spaceString += " ";            return spaceString;        }        private void DeleteAllSpacesOnTheLeftOfAString(ref string str)        {            int i = 0;            for (; i != str.Length && str[i] == ' '; ++i)                ;            string newStr = null;            for (; i != str.Length; ++i)                newStr += str[i];            str = newStr;        }        private string GetKey(string aLine)        {            //  跳过分隔符内的说明用的数据            int i = 0;            int countOfHasFoundSeparator = 0;            for (; i != aLine.Length; ++i)            {                if (aLine[i] == SeparatorBetweenTwoAreas)                {                    ++countOfHasFoundSeparator;                    if (countOfHasFoundSeparator == AmountOfSepator)                    {                        ++i;                        break;                    }                }            }            string key = null;            for (; i != aLine.Length && aLine[i] != SeparatingString[0]; ++i)                key += aLine[i];            return key;        }        public void WriteAString(string aLine, string fullSourceFileName, string theWholeLineInSourceFile)        {            foreach (KeyValuePair<int, string> cell in buffer)            {                if (GetKey(cell.Value) == aLine)                {                    //  键相同,不添加                    return;                }            }            if (GetLengthOfAString(fullSourceFileName) > maxLengthOfFileNameArea)                maxLengthOfFileNameArea = GetLengthOfAString(fullSourceFileName);            theWholeLineInSourceFile = theWholeLineInSourceFile.Trim();            if (GetLengthOfAString(theWholeLineInSourceFile) > maxLengthOfALineInSourceFileArea)                maxLengthOfALineInSourceFileArea = GetLengthOfAString(theWholeLineInSourceFile);            AddPrefixToALine(ref aLine, fullSourceFileName, theWholeLineInSourceFile);            ++amountOfLines;            buffer[amountOfLines] = aLine;        }        public void OverWritting()        {            if (amountOfLines == 0)            {                writer.Close();                return;            }            for (int i = 1; i != amountOfLines + 1; ++i)            {                string[] areaBuffer = buffer[i].Split(SeparatorBetweenTwoAreas);                string sourceFileName = areaBuffer[0];                string aLineInSourceFile = areaBuffer[1];                string stringShouldBeReplaced = areaBuffer[2];                //  补齐处理                writer.WriteLine(                    sourceFileName + GenerateSpaceString(maxLengthOfFileNameArea - GetLengthOfAString(sourceFileName)) + SeparatorBetweenTwoAreas +                    aLineInSourceFile + GenerateSpaceString(maxLengthOfALineInSourceFileArea - GetLengthOfAString(aLineInSourceFile)) + SeparatorBetweenTwoAreas +                    stringShouldBeReplaced + SeparatingString);            }            writer.Close();        }    }}
	
				
		
原创粉丝点击