示例#1
0
 public CsvConfigField(int index, bool isKey, string name, CsvValue value)
 {
     Index = index;
     IsKey = isKey;
     Name  = name;
     Value = value;
 }
示例#2
0
        public override string ToString()
        {
            string value = "";

            switch (Type)
            {
            case CsvValueType.Bool:
            case CsvValueType.Int:
            case CsvValueType.Long:
            case CsvValueType.Float:
            case CsvValueType.Double:
            case CsvValueType.String:
                value = Value.ToString();
                break;

            case CsvValueType.ArrayBool:
                bool[] bools = Value as bool[];
                value = Array2String <bool>(bools);
                break;

            case CsvValueType.ArrayInt:
                int[] ints = Value as int[];
                value = Array2String <int>(ints);
                break;

            case CsvValueType.ArrayLong:
                long[] longs = Value as long[];
                value = Array2String <long>(longs);
                break;

            case CsvValueType.ArrayFloat:
                float[] floats = Value as float[];
                value = Array2String <float>(floats);
                break;

            case CsvValueType.ArrayDouble:
                double[] doubles = Value as double[];
                value = Array2String <double>(doubles);
                break;

            case CsvValueType.ArrayString:
                string[] strings = Value as string[];
                value = Array2String <string>(strings);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(string.Format("(type:{0},value:{1})", Type, value));
        }
示例#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 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.String:
                if (otherValue.Type != CsvValueType.String)
                {
                    return(false);
                }
                return(Value.Equals(otherValue.Value));

            case CsvValueType.None:
            default:
                return(false);
            }

            return(false);
        }
示例#5
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, CsvValueType> typeDic = new Dictionary <int, CsvValueType>();

                bool hasName = false;
                Dictionary <int, string> nameDic = new Dictionary <int, string>();
                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))
                            {
                                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}", strings[i]));
                            }
                        }
                        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));
                            }
                            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];
                        CsvConfigField field = new CsvConfigField(i, i == keyIndex, nameDic[i],
                                                                  new CsvValue(typeDic[i], value));
                        config.AddField(field);
                    }

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