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