コード例 #1
0
 public CsvConfigField(int sequence, bool isKey, bool isIndex, string name, CsvValue value)
 {
     Sequence = sequence;
     IsKey    = isKey;
     Name     = name;
     IsIndex  = isIndex;
     Value    = value;
 }
コード例 #2
0
ファイル: CsvConfig.cs プロジェクト: Yutianyuan1989/RGame
 public void Set(string name, int obj)
 {
     if (_namedFields.ContainsKey(name))
     {
         _namedFields[name].Value.Value = obj;
     }
     else
     {
         CsvValue       addValue = new CsvValue(CsvValue.CsvValueType.Int, obj);
         CsvConfigField addField = new CsvConfigField(_namedFields.Count, false, false, name, addValue);
         addField.Value.Value = obj;
         AddField(addField);
     }
 }
コード例 #3
0
 /// <summary>
 /// if <see cref="KeyType"/>
 ///
 /// </summary>
 /// <param name="key"></param>
 /// <returns></returns>
 public CsvConfig this[CsvValue key]
 {
     get
     {
         foreach (KeyValuePair <CsvValue, CsvConfig> pair in _configDic)
         {
             if (pair.Key.Equals(key))
             {
                 return(pair.Value);
             }
         }
         return(null);
     }
 }
コード例 #4
0
        public List <CsvConfig> GetConfigsByIndex(string fieldName, CsvValue value)
        {
            if (_indexDic.ContainsKey(fieldName) == false)
            {
                return(null);
            }
            Dictionary <CsvValue, List <CsvConfig> > dic = _indexDic[fieldName];

            foreach (var iter in dic)
            {
                if (iter.Key.Equals(value))
                {
                    return(dic[iter.Key]);
                }
            }
            return(null);
        }
コード例 #5
0
ファイル: CsvValue.cs プロジェクト: Yutianyuan1989/RGame
        public override bool Equals(object other)
        {
            if (other == null)
            {
                return(false);
            }
            CsvValue otherValue = other as CsvValue;

            if (otherValue == null || otherValue.Type == CsvValueType.None)
            {
                return(false);
            }

            switch (Type)
            {
            case CsvValueType.Bool:
                if (otherValue.Type != CsvValueType.Bool)
                {
                    return(false);
                }
                return((bool)Value == (bool)otherValue.Value);

            case CsvValueType.Int:
                if (otherValue.Type == CsvValueType.Bool || otherValue.Type == CsvValueType.String)
                {
                    return(false);
                }
                int intValue = (int)Value;
                switch (otherValue.Type)
                {
                case CsvValueType.Int:
                    return(intValue == (int)otherValue.Value);

                case CsvValueType.Long:
                    return(intValue == (long)otherValue.Value);

                case CsvValueType.Float:
                    return(Approximately(intValue, (float)otherValue.Value));

                case CsvValueType.Double:
                    return(Approximately(intValue, (double)otherValue.Value));
                }
                return(false);

            case CsvValueType.Long:
                if (otherValue.Type == CsvValueType.Bool || otherValue.Type == CsvValueType.String)
                {
                    return(false);
                }
                long longValue = (long)Value;

                switch (otherValue.Type)
                {
                case CsvValueType.Int:
                    return(longValue == (int)(otherValue.Value));

                case CsvValueType.Long:
                    return(longValue == (long)(otherValue.Value));

                case CsvValueType.Float:
                    return(Approximately(longValue, (float)otherValue.Value));

                case CsvValueType.Double:
                    return(Approximately(longValue, (double)otherValue.Value));
                }
                return(false);

            case CsvValueType.Float:
                if (otherValue.Type == CsvValueType.Bool || otherValue.Type == CsvValueType.String)
                {
                    return(false);
                }
                float fValue = (float)Value;
                switch (otherValue.Type)
                {
                case CsvValueType.Int:
                    return(Approximately((int)otherValue.Value, fValue));

                case CsvValueType.Long:
                    return(Approximately((long)otherValue.Value, fValue));

                case CsvValueType.Float:
                    return(Approximately((float)otherValue.Value, fValue));

                case CsvValueType.Double:
                    return(Approximately((double)otherValue.Value, fValue));
                }
                return(false);

            case CsvValueType.Double:
                if (otherValue.Type == CsvValueType.Bool || otherValue.Type == CsvValueType.String)
                {
                    return(false);
                }
                double dValue = (double)Value;
                switch (otherValue.Type)
                {
                case CsvValueType.Int:
                    return(Approximately((int)otherValue.Value, dValue));

                case CsvValueType.Long:
                    return(Approximately((long)otherValue.Value, dValue));

                case CsvValueType.Float:
                    return(Approximately((float)otherValue.Value, dValue));

                case CsvValueType.Double:
                    return(Approximately((double)otherValue.Value, dValue));
                }
                return(false);

            case CsvValueType.VFixedPoint:
                if (otherValue.Type == CsvValueType.VFixedPoint)
                {
                    return(false);
                }
                return(Value == otherValue.Value);

            case CsvValueType.String:
                if (otherValue.Type != CsvValueType.String)
                {
                    return(false);
                }
                return(Value.Equals(otherValue.Value));

            case CsvValueType.ArrayFloat:     // add by lxh
            case CsvValueType.ArrayInt:
            case CsvValueType.ArrayLong:
            case CsvValueType.ArrayString:
            case CsvValueType.ArrayVFixedPoint:
                if (Type != otherValue.Type ||
                    ArrayValue.Length != otherValue.ArrayValue.Length)
                {
                    return(false);
                }

                for (int i = 0; i < ArrayValue.Length; i++)
                {
                    if (ArrayValue[i] != otherValue.ArrayValue[i])
                    {
                        return(false);
                    }
                }

                break;

            case CsvValueType.None:
            default:
                throw new ArgumentOutOfRangeException("Error in CsvValue.Equals");
            }

            return(false);
        }
コード例 #6
0
        public void Read(TextReader reader)
        {
            Reset();
            try
            {
                CsvReader csvReader  = new CsvReader(reader, false);
                int       fieldCount = csvReader.FieldCount;

                bool hasKey   = false;
                int  keyIndex = -1;
                Dictionary <int, CsvValue.CsvValueType> typeDic = new Dictionary <int, CsvValue.CsvValueType>();

                bool hasName = false;
                Dictionary <int, string> nameDic       = new Dictionary <int, string>();
                Dictionary <int, bool>   buildIndexDic = new Dictionary <int, bool>();
                foreach (string[] strings in csvReader)
                {
                    //check fieldCount
                    if (strings.Length != fieldCount)
                    {
                        StringBuilder sb    = new StringBuilder();
                        bool          first = true;
                        foreach (string s in strings)
                        {
                            if (first)
                            {
                                sb.Append(s);
                                first = false;
                            }
                            else
                            {
                                sb.Append(",").Append(s);
                            }
                        }
                        throw new CsvException(string.Format("fields length error,{0}", sb.ToString()));
                    }

                    //set key
                    if (!hasKey)
                    {
                        for (int i = 0; i < fieldCount; i++)
                        {
                            string low = strings[i].ToLower();
                            if (!_typeDic.ContainsKey(low) && !_keyTypeDic.ContainsKey(low) && low.EndsWith("i"))
                            {
                                buildIndexDic.Add(i, true);
                                low = low.TrimEnd('i');
                            }
                            else
                            {
                                buildIndexDic.Add(i, false);
                            }

                            if (_typeDic.ContainsKey(low))
                            {
                                typeDic.Add(i, _typeDic[low]);
                            }
                            else if (_keyTypeDic.ContainsKey(low))
                            {
                                if (hasKey)
                                {
                                    throw new CsvException(string.Format("already has key{0}", low));
                                }
                                hasKey   = true;
                                keyIndex = i;
                                typeDic.Add(i, _keyTypeDic[low]);
                                _keyType = _keyTypeDic[low];
                            }
                            else
                            {
                                throw new CsvException(string.Format("error header:{0}", low));
                            }
                        }
                        hasKey = true;
                        continue;
                    }

                    //set names
                    if (!hasName)
                    {
                        for (int i = 0; i < fieldCount; i++)
                        {
                            string name = strings[i];
                            if (_fieldNames.Contains(name))
                            {
                                throw new CsvException(string.Format("same field name:{0}", name));
                            }
                            if (buildIndexDic[i])
                            {
                                _indexDic[name] = new Dictionary <CsvValue, List <CsvConfig> >();
                            }
                            nameDic.Add(i, name);
                            _fieldNames.Add(name);
                        }
                        hasName = true;
                        continue;
                    }

                    CsvConfig config = new CsvConfig();
                    for (int i = 0; i < fieldCount; i++)
                    {
                        string         value    = strings[i];
                        CsvValue       csvValue = new CsvValue(typeDic[i], value);
                        CsvConfigField field    = new CsvConfigField(i, i == keyIndex, buildIndexDic[i], nameDic[i],
                                                                     csvValue);
                        config.AddField(field);
                        if (buildIndexDic[i])
                        {
                            bool     alreadyContanValue = false;
                            CsvValue indexValue         = null;
                            foreach (KeyValuePair <CsvValue, List <CsvConfig> > pair in _indexDic[nameDic[i]])
                            {
                                if (pair.Key.Equals(csvValue))
                                {
                                    alreadyContanValue = true;
                                    indexValue         = pair.Key;
                                }
                            }
                            if (!alreadyContanValue)
                            {
                                _indexDic[nameDic[i]][csvValue] = new List <CsvConfig>();
                                _indexDic[nameDic[i]][csvValue].Add(config);
                            }
                            else
                            {
                                _indexDic[nameDic[i]][indexValue].Add(config);
                            }
                        }
                    }

                    CsvValue keyValue = new CsvValue(typeDic[keyIndex], strings[keyIndex]);
                    foreach (KeyValuePair <CsvValue, CsvConfig> pair in _configDic)
                    {
                        if (pair.Key.Equals(keyValue))
                        {
                            throw new CsvException(string.Format("already contains key :{0}", keyValue.Value));
                        }
                    }
                    _configDic.Add(keyValue, config);
                }
                if (!hasKey)
                {
                    throw new CsvException("miss key in header.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                throw new CsvException(e.Message);
            }
        }