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