Example #1
0
        private void InitForArrayValue(CsvValueType type, string[] values)
        {
            if (values.Length > 0)
            {
                ArrayValue = new object[values.Length];
            }
            else
            {
                throw new CsvException("empty string[] value for ArrayValue");
            }

            switch (type)
            {
            case CsvValueType.ArrayString:
                ArrayValue = values;
                break;

            case CsvValueType.ArrayInt:
                for (int i = 0; i < values.Length; i++)
                {
                    int tempIntValue = default(int);
                    if (string.IsNullOrEmpty(values[i]) || int.TryParse(values[i], out tempIntValue))
                    {
                        ArrayValue[i] = tempIntValue;
                    }
                    else
                    {
                        throw new CsvException(string.Format("parse int error,string is {0}", values[i]));
                    }
                }
                break;

            case CsvValueType.ArrayLong:
                for (int i = 0; i < values.Length; i++)
                {
                    long tempLongValue = default(long);
                    if (string.IsNullOrEmpty(values[i]) || long.TryParse(values[i], out tempLongValue))
                    {
                        ArrayValue[i] = tempLongValue;
                    }
                    else
                    {
                        throw new CsvException(string.Format("parse long error,string is {0}", values[i]));
                    }
                }
                break;

            case CsvValueType.ArrayFloat:
                for (int i = 0; i < values.Length; i++)
                {
                    float tempFloatValue = default(float);
                    if (string.IsNullOrEmpty(values[i]) || float.TryParse(values[i], out tempFloatValue))
                    {
                        ArrayValue[i] = tempFloatValue;
                    }
                    else
                    {
                        throw new CsvException(string.Format("parse float error,string is {0}", values[i]));
                    }
                }
                break;

            default:
                throw new ArgumentOutOfRangeException("type");
            }
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="type"></param>
        /// <param name="strValue"></param>
        /// <exception cref="CsvException">
        /// strValue can not change to CsvValue
        ///
        /// </exception>
        public CsvValue(CsvValueType type, string strValue)
        {
            Type = type;
            switch (type)
            {
            case CsvValueType.Int:
                int intValue = default(int);
                if (string.IsNullOrEmpty(strValue) || int.TryParse(strValue, out intValue))
                {
                    Value = intValue;
                }
                else
                {
                    throw new CsvException(string.Format("parse int error,string is {0}", strValue));
                }
                break;

            case CsvValueType.ArrayInt:
                string[] intStrings = strValue.Split(new char[] { ArraySeparateChar }, StringSplitOptions.RemoveEmptyEntries);
                int[]    intArray   = new int[intStrings.Length];
                for (int i = 0; i < intStrings.Length; i++)
                {
                    int intValue2 = default(int);
                    if (string.IsNullOrEmpty(intStrings[i]) || int.TryParse(intStrings[i], out intValue2))
                    {
                        intArray[i] = intValue2;
                    }
                    else
                    {
                        throw new CsvException(string.Format("parse int array error,string is {0}", strValue));
                    }
                }
                Value = intArray;
                break;

            case CsvValueType.Long:
                long longValue = default(long);
                if (string.IsNullOrEmpty(strValue) || long.TryParse(strValue, out longValue))
                {
                    Value = longValue;
                }
                else
                {
                    throw new CsvException(string.Format("parse long error,string is {0}", strValue));
                }
                break;

            case CsvValueType.ArrayLong:
                string[] longStrings = strValue.Split(new char[] { ArraySeparateChar }, StringSplitOptions.RemoveEmptyEntries);
                long[]   longArray   = new long[longStrings.Length];
                for (int i = 0; i < longStrings.Length; i++)
                {
                    long longValue2 = default(long);
                    if (string.IsNullOrEmpty(longStrings[i]) || long.TryParse(longStrings[i], out longValue2))
                    {
                        longArray[i] = longValue2;
                    }
                    else
                    {
                        throw new CsvException(string.Format("parse long array error,string is {0}", strValue));
                    }
                }
                Value = longArray;
                break;

            case CsvValueType.Bool:
                string low = null == strValue ? null : strValue.ToLower();
                if (low == "true" || low == "yes")
                {
                    Value = true;
                }
                else if (string.IsNullOrEmpty(low) || low == "false" || low == "no")
                {
                    Value = false;
                }
                else
                {
                    throw new CsvException(string.Format("parse bool error,string is {0}", strValue));
                }
                break;

            case CsvValueType.ArrayBool:
                string[] boolStrings = strValue.Split(new char[] { ArraySeparateChar }, StringSplitOptions.RemoveEmptyEntries);
                bool[]   boolArray   = new bool[boolStrings.Length];
                for (int i = 0; i < boolStrings.Length; i++)
                {
                    if (boolStrings[i].ToLower() == "true" || boolStrings[i].ToLower() == "yes")
                    {
                        boolArray[i] = true;
                    }
                    else if (boolStrings[i].ToLower() == "false" || boolStrings[i].ToLower() == "no")
                    {
                        boolArray[i] = false;
                    }
                    else
                    {
                        throw new CsvException(string.Format("parse bool array error,string is {0}", strValue));
                    }
                }
                Value = boolArray;
                break;

            case CsvValueType.Float:
                float fValue = default(float);
                if (string.IsNullOrEmpty(strValue) || float.TryParse(strValue, out fValue))
                {
                    Value = fValue;
                }
                else
                {
                    throw new CsvException(string.Format("parse float error,string is {0}", strValue));
                }
                break;


            case CsvValueType.ArrayFloat:
                string[] floatStrings = strValue.Split(new char[] { ArraySeparateChar }, StringSplitOptions.RemoveEmptyEntries);
                float[]  floatArray   = new float[floatStrings.Length];
                for (int i = 0; i < floatStrings.Length; i++)
                {
                    float fValue2 = default(float);
                    if (string.IsNullOrEmpty(floatStrings[i]) || float.TryParse(floatStrings[i], out fValue2))
                    {
                        floatArray[i] = fValue2;
                    }
                    else
                    {
                        throw new CsvException(string.Format("parse float array error,string is {0}", strValue));
                    }
                }
                Value = floatArray;
                break;

            case CsvValueType.Double:
                double dValue = default(double);
                if (string.IsNullOrEmpty(strValue) || double.TryParse(strValue, out dValue))
                {
                    Value = dValue;
                }
                else
                {
                    throw new CsvException(string.Format("parse double error,string is {0}", strValue));
                }
                break;

            case CsvValueType.ArrayDouble:
                string[] doubleStrings = strValue.Split(new char[] { ArraySeparateChar }, StringSplitOptions.RemoveEmptyEntries);
                double[] doubleArray   = new double[doubleStrings.Length];
                for (int i = 0; i < doubleStrings.Length; i++)
                {
                    double dValue2 = default(double);
                    if (string.IsNullOrEmpty(doubleStrings[i]) || double.TryParse(doubleStrings[i], out dValue2))
                    {
                        doubleArray[i] = dValue2;
                    }
                    else
                    {
                        throw new CsvException(string.Format("parse double array error,string is {0}", strValue));
                    }
                }
                Value = doubleArray;
                break;

            case CsvValueType.String:
                Value = strValue ?? string.Empty;
                break;

            case CsvValueType.ArrayString:
                string[] stringArray = strValue.Split(new char[] { ArraySeparateChar }, StringSplitOptions.RemoveEmptyEntries);
                Value = stringArray;
                break;

            case CsvValueType.None:
                break;

            default:
                throw new ArgumentOutOfRangeException("type");
            }
        }
Example #3
0
        private void Init(CsvValueType type, string strValue)
        {
            Type = type;
            switch (type)
            {
            case CsvValueType.Int:
                int intValue = default(int);
                if (string.IsNullOrEmpty(strValue) || int.TryParse(strValue, out intValue))
                {
                    Value = intValue;
                }
                else
                {
                    throw new CsvException(string.Format("parse int error,string is {0}", strValue));
                }
                break;

            case CsvValueType.Long:
                long longValue = default(long);
                if (string.IsNullOrEmpty(strValue) || long.TryParse(strValue, out longValue))
                {
                    Value = longValue;
                }
                else
                {
                    throw new CsvException(string.Format("parse long error,string is {0}", strValue));
                }
                break;

            case CsvValueType.Bool:
                string low = null == strValue ? null : strValue.ToLower();
                if (low == "true" || low == "yes")
                {
                    Value = true;
                }
                else if (string.IsNullOrEmpty(low) || low == "false" || low == "no")
                {
                    Value = false;
                }
                else
                {
                    throw new CsvException(string.Format("parse bool error,string is {0}", strValue));
                }
                break;

            case CsvValueType.Float:
                float fValue = default(float);
                if (string.IsNullOrEmpty(strValue) || float.TryParse(strValue, out fValue))
                {
                    Value = fValue;
                }
                else
                {
                    throw new CsvException(string.Format("parse float error,string is {0}", strValue));
                }
                break;

            case CsvValueType.Double:
                double dValue = default(double);
                if (string.IsNullOrEmpty(strValue) || double.TryParse(strValue, out dValue))
                {
                    Value = dValue;
                }
                else
                {
                    throw new CsvException(string.Format("parse double error,string is {0}", strValue));
                }
                break;

            case CsvValueType.String:
                Value = strValue ?? string.Empty;
                break;

            case CsvValueType.ArrayString:     // add by lxh
            case CsvValueType.ArrayInt:
            case CsvValueType.ArrayLong:
            case CsvValueType.ArrayFloat:
            case CsvValueType.ArrayVFixedPoint:
                string[] arrString = strValue.Split(';');
                InitForArrayValue(type, arrString);
                break;

            case CsvValueType.None:
                break;

            default:
                throw new ArgumentOutOfRangeException("type");
            }
        }
Example #4
0
        public CsvValue(CsvValueType type, object objValue)
        {
            string strValue = objValue.ToString();

            Init(type, strValue);
        }
Example #5
0
 public CsvValue(CsvValueType type, string strValue)
 {
     Init(type, strValue);
 }
Example #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, 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);
            }
        }
Example #7
0
 private void Reset()
 {
     _configDic.Clear();
     _keyType = CsvValueType.None;
     _fieldNames.Clear();
 }