Ejemplo n.º 1
0
        public static double[,] ReadCSV(string fileName)
        {
            try
            {
                using (StreamReader reader = new StreamReader(File.OpenRead(fileName)))
                {
                    double[,] result = null;
                    int r = 0;
                    NumberFormatInfo provider = new NumberFormatInfo();
                    provider.NumberDecimalSeparator = ".";
                    provider.NumberGroupSeparator   = "";
                    while (!reader.EndOfStream)
                    {
                        var      line = reader.ReadLine();
                        double[] row  = line.Split(';').Select(x => Convert.ToDouble(x, provider)).ToArray();

                        if (result == null)
                        {
                            result = new double[row.Length - 1, row.Length];
                        }
                        if (row.Length != result.GetLength(1))
                        {
                            throw new WrongDataFormatException();
                        }
                        for (int c = 0; c < row.Length; ++c)
                        {
                            result[r, c] = row[c];
                        }

                        ++r;
                    }
                    if (r != result.GetLength(0))
                    {
                        throw new WrongDataFormatException();
                    }
                    return(result);
                }
            }
            catch (System.IndexOutOfRangeException)
            {
                WrongDataFormatException argEx = new WrongDataFormatException();
                throw argEx;
            }
            catch (System.FormatException)
            {
                WrongDataFormatException argEx = new WrongDataFormatException();
                throw argEx;
            }
        }
Ejemplo n.º 2
0
        /// ///////////////////////////////////////////////////////////////////////
        /// ReadObject
        ///
        /// <summary>
        /// Creates an object of type T from the data in row and returns that object.
        ///
        /// </summary>
        /// <param name="row"></param>
        /// <param name="firstRow"></param>
        /// <returns></returns>
        public T ReadObject(IDataRow row, AggregatedException ae)
        {
            //If there are more columns than the required
            if (row.Count > m_IndexToInfo.Length)
            {
                //Are we ignoring unknown columns?
                if (!m_fileDescription.IgnoreUnknownColumns)
                {
                    // Too many fields
                    throw new TooManyDataFieldsException(typeof(T).ToString(), row[0].LineNbr, m_fileName);
                }
            }

            // -----

            T obj = new T();

            //If we will be using the mappings, we just iterate through all the cells in this row
            int maxRowCount = _mappingIndexes.Count > 0 ? row.Count : Math.Min(row.Count, m_IndexToInfo.Length);

            for (int i = 0; i < maxRowCount; i++)
            {
                TypeFieldInfo tfi;
                //If there is some index mapping generated and the IgnoreUnknownColums is `true`
                if (m_fileDescription.IgnoreUnknownColumns && _mappingIndexes.Count > 0)
                {
                    if (!_mappingIndexes.ContainsKey(i))
                    {
                        continue;
                    }
                    tfi = m_IndexToInfo[_mappingIndexes[i]];
                }
                else
                {
                    tfi = m_IndexToInfo[i];
                }

                if (m_fileDescription.EnforceCsvColumnAttribute &&
                    (!tfi.hasColumnAttribute))
                {
                    // enforcing column attr, but this field/prop has no column attr.
                    // So there are too many fields in this record.
                    throw new TooManyNonCsvColumnDataFieldsException(typeof(T).ToString(), row[i].LineNbr, m_fileName);
                }

                // -----

                if ((!m_fileDescription.FirstLineHasColumnNames) &&
                    (tfi.index == Int32.MaxValue))
                {
                    // First line in the file does not have field names, so we're
                    // depending on the FieldIndex of each field in the type
                    // to ensure each value is placed in the correct field.
                    // However, now hit a field where there is no FieldIndex.
                    throw new MissingFieldIndexException(typeof(T).ToString(), row[i].LineNbr, m_fileName);
                }

                // -----

                if (m_fileDescription.UseFieldIndexForReadingData && (!m_fileDescription.FirstLineHasColumnNames) &&
                    (tfi.index > row.Count))
                {
                    // First line in the file does not have field names, so we're
                    // depending on the FieldIndex of each field in the type
                    // to ensure each value is placed in the correct field.
                    // However, now hit a field where the FieldIndex is bigger
                    // than the total number of items in a row generated by the separatorChar
                    throw new WrongFieldIndexException(typeof(T).ToString(), row[i].LineNbr, m_fileName);
                }

                int index = m_fileDescription.UseFieldIndexForReadingData ? tfi.index - 1 : i;

                // value to put in the object
                string value = row[index].Value;

                if (value == null)
                {
                    if (!tfi.canBeNull)
                    {
                        ae.AddException(
                            new MissingRequiredFieldException(
                                typeof(T).ToString(),
                                tfi.name,
                                row[i].LineNbr,
                                m_fileName));
                    }
                }
                else
                {
                    try
                    {
                        Object objValue = null;

                        // Normally, either tfi.typeConverter is not null,
                        // or tfi.parseNumberMethod is not null.
                        //
                        if (tfi.typeConverter != null)
                        {
                            objValue = tfi.typeConverter.ConvertFromString(
                                null,
                                m_fileDescription.FileCultureInfo,
                                value);
                        }
                        else if (tfi.parseExactMethod != null)
                        {
                            objValue =
                                tfi.parseExactMethod.Invoke(
                                    tfi.fieldType,
                                    new Object[] {
                                value,
                                tfi.outputFormat,
                                m_fileDescription.FileCultureInfo
                            });
                        }
                        else if (tfi.parseNumberMethod != null)
                        {
                            objValue =
                                tfi.parseNumberMethod.Invoke(
                                    tfi.fieldType,
                                    new Object[] {
                                value,
                                tfi.inputNumberStyle,
                                m_fileDescription.FileCultureInfo
                            });
                        }
                        else
                        {
                            // No TypeConverter and no Parse method available.
                            // Try direct approach.
                            objValue = value;
                        }

                        if (tfi.memberInfo is PropertyInfo)
                        {
                            ((PropertyInfo)tfi.memberInfo).SetValue(obj, objValue, null);
                        }
                        else
                        {
                            ((FieldInfo)tfi.memberInfo).SetValue(obj, objValue);
                        }
                    }
                    catch (Exception e)
                    {
                        if (e is TargetInvocationException)
                        {
                            e = e.InnerException;
                        }

                        if (e is FormatException)
                        {
                            e = new WrongDataFormatException(
                                typeof(T).ToString(),
                                tfi.name,
                                value,
                                row[i].LineNbr,
                                m_fileName,
                                e);
                        }

                        ae.AddException(e);
                    }
                }
            }

            // Visit any remaining fields in the type for which no value was given
            // in the data row, to see whether any of those was required.
            // If only looking at fields with CsvColumn attribute, do ignore
            // fields that don't have that attribute.

            for (int i = row.Count; i < m_IndexToInfo.Length; i++)
            {
                TypeFieldInfo tfi = m_IndexToInfo[i];

                if (((!m_fileDescription.EnforceCsvColumnAttribute) ||
                     tfi.hasColumnAttribute) &&
                    (!tfi.canBeNull))
                {
                    ae.AddException(
                        new MissingRequiredFieldException(
                            typeof(T).ToString(),
                            tfi.name,
                            row[row.Count - 1].LineNbr,
                            m_fileName));
                }
            }

            return(obj);
        }
Ejemplo n.º 3
0
        public T ReadObject(IDataRow row, AggregatedException ae)
        {
            if (row.Count > fieldIndexInfo.IndexToInfo.Length)
            {
                //Are we ignoring unknown columns?
                if (!FileDescription.IgnoreUnknownColumns)
                {
                    // Too many fields
                    throw new TooManyDataFieldsException(typeof(T).ToString(), row[0].LineNbr, FileName);
                }
            }

            T obj = new T();

            //If we will be using the mappings, we just iterate through all the cells in this row
            int maxRowCount = fieldIndexInfo.GetMaxRowCount(row.Count);


            for (int i = 0; i < maxRowCount; i++)
            {
                TypeFieldInfo tfi = fieldIndexInfo.QueryTypeFieldInfo(FileDescription.IgnoreUnknownColumns, i);
                if (tfi == null)
                {
                    continue;
                }

                if (FileDescription.EnforceCsvColumnAttribute &&
                    (!tfi.HasColumnAttribute))
                {
                    // enforcing column attr, but this field/prop has no column attr.
                    // So there are too many fields in this record.
                    throw new TooManyNonCsvColumnDataFieldsException(typeof(T).ToString(), row[i].LineNbr, FileName);
                }

                if ((!FileDescription.FirstLineHasColumnNames) &&
                    (tfi.Index == ColumnAttribute.McDefaultFieldIndex))
                {
                    // First line in the file does not have field names, so we're
                    // depending on the FieldIndex of each field in the type
                    // to ensure each value is placed in the correct field.
                    // However, now hit a field where there is no FieldIndex.
                    throw new MissingFieldIndexException(typeof(T).ToString(), row[i].LineNbr, FileName);
                }

                if (FileDescription.UseFieldIndexForReadingData && (!FileDescription.FirstLineHasColumnNames) &&
                    (tfi.Index > row.Count))
                {
                    // First line in the file does not have field names, so we're
                    // depending on the FieldIndex of each field in the type
                    // to ensure each value is placed in the correct field.
                    // However, now hit a field where the FieldIndex is bigger
                    // than the total number of items in a row generated by the separatorChar
                    throw new WrongFieldIndexException(typeof(T).ToString(), row[i].LineNbr, FileName);
                }


                int index = FileDescription.UseFieldIndexForReadingData ? tfi.Index - 1 : i;

                // value to put in the object
                string value = row[index].Value;

                if (value == null)
                {
                    if (!tfi.CanBeNull)
                    {
                        ae.AddException(new MissingRequiredFieldException(
                                            typeof(T).ToString(), tfi.Name, row[i].LineNbr, FileName));
                    }
                }
                else
                {
                    try
                    {
                        if (tfi.OutputFormat == "HH:mm:ss.fff")
                        {
                            value = value.Substring(0, 8) + "." + value.Substring(9);
                        }
                        Object objValue = tfi.UpdateObjectValue(value, FileDescription.FileCultureInfo);

                        if (tfi.MemberInfo is PropertyInfo)
                        {
                            ((PropertyInfo)tfi.MemberInfo).SetValue(obj, objValue, null);
                        }
                        else
                        {
                            ((FieldInfo)tfi.MemberInfo).SetValue(obj, objValue);
                        }
                    }
                    catch (Exception e)
                    {
                        if (e is TargetInvocationException)
                        {
                            e = e.InnerException;
                        }

                        if (e is FormatException)
                        {
                            e = new WrongDataFormatException(typeof(T).ToString(), tfi.Name,
                                                             value, row[i].LineNbr, FileName, e);
                        }
                        ae.AddException(e);
                    }
                }
            }

            // Visit any remaining fields in the type for which no value was given
            // in the data row, to see whether any of those was required.
            // If only looking at fields with CsvColumn attribute, do ignore
            // fields that don't have that attribute.
            for (int i = row.Count; i < fieldIndexInfo.IndexToInfo.Length; i++)
            {
                TypeFieldInfo tfi = fieldIndexInfo.IndexToInfo[i];

                if (((!FileDescription.EnforceCsvColumnAttribute) || tfi.HasColumnAttribute) &&
                    (!tfi.CanBeNull))
                {
                    ae.AddException(new MissingRequiredFieldException(typeof(T).ToString(), tfi.Name,
                                                                      row[row.Count - 1].LineNbr, FileName));
                }
            }
            return(obj);
        }