Beispiel #1
0
        private int _rowIndex = 1; // 从第1行开始

        protected bool ParseReader(TextReader oReader)
        {
            // 首行
            var headLine = oReader.ReadLine();

            if (headLine == null)
            {
                OnException(TableFileExceptionType.HeadLineNull);
                return(false);
            }

            var metaLine = oReader.ReadLine(); // 声明行

            if (metaLine == null)
            {
                OnException(TableFileExceptionType.MetaLineNull);
                return(false);
            }


            string[] firstLineSplitString = headLine.Split(_config.Separators, StringSplitOptions.None);  // don't remove RemoveEmptyEntries!
            string[] firstLineDef         = new string[firstLineSplitString.Length];

            var metaLineArr = metaLine.Split(_config.Separators, StringSplitOptions.None);

            Array.Copy(metaLineArr, 0, firstLineDef, 0, metaLineArr.Length);  // 拷贝,确保不会超出表头的

            for (int i = 0; i < firstLineSplitString.Length; i++)
            {
                var headerString = firstLineSplitString[i];

                var headerInfo = new HeaderInfo
                {
                    ColumnIndex = i,
                    HeaderName  = headerString,
                    HeaderMeta  = firstLineDef[i],
                };

                Headers[headerInfo.HeaderName] = headerInfo;
            }
            _colCount = firstLineSplitString.Length;  // 標題

            // 读取行内容

            string sLine = "";

            while (sLine != null)
            {
                sLine = oReader.ReadLine();
                if (sLine != null)
                {
                    string[] splitString1 = sLine.Split(_config.Separators, StringSplitOptions.None);

                    TabInfo[_rowIndex] = splitString1;

                    //新建行对象
                    T newT = new T();// prevent IL2CPP stripping!

                    //储存行数和头信息到行对象中
                    newT.Ctor(_rowIndex, Headers);  // the New Object may not be used this time, so cache it!

                    //储存行内容
                    newT.Values = splitString1;

                    if (!newT.IsAutoParse)
                    {
                        newT.Parse(splitString1);
                    }
                    else
                    {
                        AutoParse(newT, splitString1);
                    }

                    if (newT.GetPrimaryKey() != null)
                    {
                        TableFileRow oldT;
                        if (!PrimaryKey2Row.TryGetValue(newT.GetPrimaryKey(), out oldT))  // 原本不存在,使用new的,释放cacheNew,下次直接new
                        {
                            PrimaryKey2Row[newT.GetPrimaryKey()] = newT;
                        }
                        else  // 原本存在,使用old的, cachedNewObj(newT)因此残留, 留待下回合使用
                        {
                            TableFileRow toT = oldT;
                            // Check Duplicated Primary Key, 使用原来的,不使用新new出来的, 下回合直接用_cachedNewObj
                            OnException(TableFileExceptionType.DuplicatedKey, toT.GetPrimaryKey().ToString());
                            newT = (T)toT;
                        }
                    }

                    Rows[_rowIndex] = newT;
                    _rowIndex++;
                }
            }

            return(true);
        }
Beispiel #2
0
        protected bool ParseReader(TextReader oReader)
        {
            //1. 首行。每一项保存到HeaderInfo.HeaderName
            var headLine = oReader.ReadLine();

            if (headLine == null)
            {
                OnException(TableFileExceptionType.HeadLineNull);
                return(false);
            }

            //2. 声明行。每一项保存到HeaderInfo.HeaderMeta
            var metaLine = oReader.ReadLine();

            if (metaLine == null)
            {
                OnException(TableFileExceptionType.MetaLineNull);
                return(false);
            }

            //首行是每一项的名称,先读取到HeaderInfo
            string[] firstLineSplitString = headLine.Split(_config.Separators, StringSplitOptions.None);  // don't remove RemoveEmptyEntries!
            string[] firstLineDef         = new string[firstLineSplitString.Length];

            //将声明行的数据拷贝firstLineDef,确保不会超出表头的。再赋值给HeaderInfo
            var metaLineArr = metaLine.Split(_config.Separators, StringSplitOptions.None);

            Array.Copy(metaLineArr, 0, firstLineDef, 0, metaLineArr.Length);

            for (int i = 0; i < firstLineSplitString.Length; i++)
            {
                var headerString = firstLineSplitString[i];

                //创建一个HeaderInfo
                var headerInfo = new HeaderInfo
                {
                    ColumnIndex = i,
                    HeaderName  = headerString,
                    HeaderMeta  = firstLineDef[i],
                };

                Headers[headerInfo.HeaderName] = headerInfo;
            }

            //列数
            _colCount = firstLineSplitString.Length;

            //3. 剩余的行内容,创建TableFileRow来保存
            string sLine = "";

            while (sLine != null)
            {
                sLine = oReader.ReadLine();
                if (sLine != null)
                {
                    string[] splitString1 = sLine.Split(_config.Separators, StringSplitOptions.None);

                    TabInfo[_rowIndex] = splitString1;

                    //创建一行的数据TableFileRow
                    T newT = new T();
                    newT.Ctor(_rowIndex, Headers);
                    newT.Values = splitString1;

                    if (!newT.IsAutoParse)
                    {
                        //非自动解析
                        newT.Parse(splitString1);
                    }
                    else
                    {
                        //自动解析,反射,赋值给TableFileRow
                        AutoParse(newT, splitString1);
                    }

                    if (newT.GetPrimaryKey() != null)
                    {
                        TableFileRow oldT;
                        if (!PrimaryKey2Row.TryGetValue(newT.GetPrimaryKey(), out oldT))
                        {
                            // 原本不存在,使用new的,释放cacheNew,下次直接new
                            PrimaryKey2Row[newT.GetPrimaryKey()] = newT;
                        }
                        else
                        {
                            // 原本存在,说明表里有多个PrimaryKey,报个错
                            TableFileRow toT = oldT;

                            OnException(TableFileExceptionType.DuplicatedKey, toT.GetPrimaryKey().ToString());

                            // 使用原来的,不使用新new出来的, 下回合直接用_cachedNewObj
                            newT = (T)toT;
                        }
                    }

                    //TableFileRow保存到Rows
                    Rows[_rowIndex] = newT;
                    _rowIndex++;
                }
            }

            return(true);
        }