Beispiel #1
0
        internal getStrlineTypeResult getStrlineType(string str)
        {
            str = str.Trim();

            getStrlineTypeResult tmp = new getStrlineTypeResult();

            if (str.StartsWith(">>>>>>>>[") && str.IndexOf("]<<<<<<<<") > 9)
            {
                tmp.type     = strlineType.fileHead;
                tmp.value    = new string[1];
                tmp.value[0] = str.Substring(9, str.IndexOf("]<<<<<<<<") - 9);
            }
            else if (str.StartsWith("----------------[") && str.IndexOf("]----------------") > 17)
            {
                tmp.type = strlineType.labelHead;
                List <string> tmpList = new List <string>();
                tmpList.Add(str.Substring(17, str.IndexOf("]----------------") - 17));
                string rightText = str.Substring(str.IndexOf("]----------------") + 17);
                if (rightText.StartsWith("[") && rightText.EndsWith("]"))
                {
                    rightText = rightText.Substring(1, rightText.Length - 2);
                    string[] splitText = rightText.Split(',');
                    foreach (string s in splitText)
                    {
                        tmpList.Add(s);
                    }
                }
                else
                {
                    tmp.value = tmpList.ToArray();
                    goto TheEnd;
                }
                tmp.value = tmpList.ToArray();
            }
            else
            {
                tmp.type     = strlineType.normal;
                tmp.value    = new string[1];
                tmp.value[0] = str;
            }

TheEnd:
            return(tmp);
        }
        internal getStrlineTypeResult getStrlineType(string str)
        {
            str = str.Trim();

            getStrlineTypeResult tmp = new getStrlineTypeResult();
            if (str.StartsWith(">>>>>>>>[") && str.IndexOf("]<<<<<<<<") > 9)
            {
                tmp.type = strlineType.fileHead;
                tmp.value = new string[1];
                tmp.value[0] = str.Substring(9, str.IndexOf("]<<<<<<<<") - 9);
            }
            else if (str.StartsWith("----------------[") && str.IndexOf("]----------------") > 17)
            {
                tmp.type = strlineType.labelHead;
                List<string> tmpList = new List<string>();
                tmpList.Add(str.Substring(17, str.IndexOf("]----------------") - 17));
                string rightText = str.Substring(str.IndexOf("]----------------") + 17);
                if (rightText.StartsWith("[") && rightText.EndsWith("]"))
                {
                    rightText = rightText.Substring(1, rightText.Length - 2);
                    string[] splitText = rightText.Split(',');
                    foreach (string s in splitText) { tmpList.Add(s); }
                }
                else
                {
                    tmp.value = tmpList.ToArray();
                    goto TheEnd;
                }
                tmp.value = tmpList.ToArray();
            }
            else
            {
                tmp.type = strlineType.normal;
                tmp.value = new string[1];
                tmp.value[0] = str;
            }

            TheEnd:
            return tmp;
        }
        public void FromFile(string path)
        {
            //错误信息
            int error_lineNum = 0;
            string error_state = "";

            try
            {
                store = new Dictionary<string, List<LabelItem>>();
                groupStringList = new List<string>();

                stateEnum state = stateEnum.start;
                string nowFilename = "";
                string nowText = "";
                string[] nowLabelResultValues = { };
                getStrlineTypeResult result = new getStrlineTypeResult();

                StreamReader sr = new StreamReader(path, Encoding.UTF8, true);
                while (!sr.EndOfStream)
                {
                    string str = sr.ReadLine();
                    error_lineNum++;
                    error_state = "imageFile=" + nowFilename + ", nowState=" + state.ToString();
                    result = getStrlineType(str);

                    switch (state)
                    {
                        case stateEnum.start:
                            if (result.type == strlineType.fileHead)
                            {
                                //处理Label文件的文件头
                                readLabelFileStartBlocks(nowText);

                                state = stateEnum.file;
                                nowFilename = result.value[0];

                                //创建新文件项
                                addFilenameToStore(nowFilename);
                            }
                            else if (result.type == strlineType.normal)
                            {
                                nowText += "\r\n" + result.value[0];
                            }
                            break;
                        case stateEnum.file:
                            if (result.type == strlineType.labelHead)
                            {
                                state = stateEnum.label;
                                nowText = "";
                                nowLabelResultValues = result.value;

                            }
                            else if (result.type == strlineType.fileHead)
                            {
                                state = stateEnum.file;
                                nowFilename = result.value[0];
                                //创建新文件项
                                if (!addFilenameToStore(nowFilename)) state = stateEnum.start;
                            }
                            break;
                        case stateEnum.label:
                            switch (result.type)
                            {
                                case strlineType.normal:
                                    if (nowText == "") nowText = result.value[0];
                                    else nowText += "\r\n" + result.value[0];
                                    break;
                                case strlineType.labelHead:
                                    //保存之前的内容
                                    addLabelToStore(nowText, nowLabelResultValues, nowFilename);
                                    nowText = "";
                                    nowLabelResultValues = result.value;
                                    break;
                                case strlineType.fileHead:
                                    //保存之前的内容
                                    addLabelToStore(nowText, nowLabelResultValues, nowFilename);

                                    state = stateEnum.file;
                                    nowFilename = result.value[0];
                                    if (!addFilenameToStore(nowFilename)) state = stateEnum.start;
                                    break;
                            }
                            break;

                    }   //switch (state)
                }   //while (!sr.EndOfStream)

                if (state == stateEnum.label)
                {
                    addLabelToStore(nowText, nowLabelResultValues, nowFilename);
                }

                if (state == stateEnum.start)
                {
                    //处理Label文件的文件头
                    readLabelFileStartBlocks(nowText);

                    state = stateEnum.file;
                    nowFilename = result.value[0];
                }

                OnFileListChanged();
                OnLabelItemListChanged();
                OnGroupListChanged();
            }
            catch(Exception e)
            {
                throw new Exception("ReadFromFileError in line" + error_lineNum.ToString()
                    + "\r\n" + error_state
                    + "\r\n\r\n" + e.ToString());
            }
        }
Beispiel #4
0
        public void FromFile(string path)
        {
            //错误信息
            int    error_lineNum = 0;
            string error_state   = "";

            try
            {
                store           = new Dictionary <string, List <LabelItem> >();
                groupStringList = new List <string>();

                stateEnum            state                = stateEnum.start;
                string               nowFilename          = "";
                string               nowText              = "";
                string[]             nowLabelResultValues = { };
                getStrlineTypeResult result               = new getStrlineTypeResult();

                StreamReader sr = new StreamReader(path, Encoding.UTF8, true);
                while (!sr.EndOfStream)
                {
                    string str = sr.ReadLine();
                    error_lineNum++;
                    error_state = "imageFile=" + nowFilename + ", nowState=" + state.ToString();
                    result      = getStrlineType(str);

                    switch (state)
                    {
                    case stateEnum.start:
                        if (result.type == strlineType.fileHead)
                        {
                            //处理Label文件的文件头
                            readLabelFileStartBlocks(nowText);

                            state       = stateEnum.file;
                            nowFilename = result.value[0];

                            //创建新文件项
                            addFilenameToStore(nowFilename);
                        }
                        else if (result.type == strlineType.normal)
                        {
                            nowText += "\r\n" + result.value[0];
                        }
                        break;

                    case stateEnum.file:
                        if (result.type == strlineType.labelHead)
                        {
                            state   = stateEnum.label;
                            nowText = "";
                            nowLabelResultValues = result.value;
                        }
                        else if (result.type == strlineType.fileHead)
                        {
                            state       = stateEnum.file;
                            nowFilename = result.value[0];
                            //创建新文件项
                            if (!addFilenameToStore(nowFilename))
                            {
                                state = stateEnum.start;
                            }
                        }
                        break;

                    case stateEnum.label:
                        switch (result.type)
                        {
                        case strlineType.normal:
                            if (nowText == "")
                            {
                                nowText = result.value[0];
                            }
                            else
                            {
                                nowText += "\r\n" + result.value[0];
                            }
                            break;

                        case strlineType.labelHead:
                            //保存之前的内容
                            addLabelToStore(nowText, nowLabelResultValues, nowFilename);
                            nowText = "";
                            nowLabelResultValues = result.value;
                            break;

                        case strlineType.fileHead:
                            //保存之前的内容
                            addLabelToStore(nowText, nowLabelResultValues, nowFilename);

                            state       = stateEnum.file;
                            nowFilename = result.value[0];
                            if (!addFilenameToStore(nowFilename))
                            {
                                state = stateEnum.start;
                            }
                            break;
                        }
                        break;
                    } //switch (state)
                }     //while (!sr.EndOfStream)

                if (state == stateEnum.label)
                {
                    addLabelToStore(nowText, nowLabelResultValues, nowFilename);
                }

                if (state == stateEnum.start)
                {
                    //处理Label文件的文件头
                    readLabelFileStartBlocks(nowText);

                    state       = stateEnum.file;
                    nowFilename = result.value[0];
                }

                OnFileListChanged();
                OnLabelItemListChanged();
                OnGroupListChanged();
            }
            catch (Exception e)
            {
                throw new Exception("ReadFromFileError in line" + error_lineNum.ToString()
                                    + "\r\n" + error_state
                                    + "\r\n\r\n" + e.ToString());
            }
        }