原始碼修改-IOperateSource

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.