Ejemplo n.º 1
0
        private void WriteData <T>(
            IEnumerable <T> values,
            string fileName,
            TextWriter stream,
            CsvFileDescription fileDescription)
        {
            FieldMapper <T> fm = new FieldMapper <T>(fileDescription, fileName, true);
            CsvStream       cs = new CsvStream(null, stream, fileDescription.SeparatorChar);

            List <string> row = new List <string>();

            if (fileDescription.FirstLineHasColumnNames)
            {
                fm.WriteNames(ref row);
                cs.WriteRow(row, fileDescription.QuoteAllFields);
            }

            // -----

            foreach (T obj in values)
            {
                // Convert obj to row
                fm.WriteObject(obj, ref row);
                cs.WriteRow(row, fileDescription.QuoteAllFields);
            }
        }
Ejemplo n.º 2
0
 public void Write <T>(
     IEnumerable <T> values,
     TextWriter stream,
     CsvFileDescription fileDescription)
 {
     WriteData <T>(values, null, stream, fileDescription);
 }
Ejemplo n.º 3
0
 public FieldMapper_Reading(
     CsvFileDescription fileDescription,
     string fileName,
     bool writingFile)
     : base(fileDescription, fileName, writingFile)
 {
 }
Ejemplo n.º 4
0
 public void Write <T>(
     IEnumerable <T> values,
     string fileName,
     CsvFileDescription fileDescription)
 {
     using (StreamWriter sw = new StreamWriter(new FileStream(fileName, FileMode.Open, FileAccess.Read), fileDescription.TextEncoding))
     {
         WriteData <T>(values, fileName, sw, fileDescription);
     }
 }
Ejemplo n.º 5
0
        public FieldMapper(CsvFileDescription fileDescription, string fileName, bool writingFile)
        {
            if ((!fileDescription.FirstLineHasColumnNames) &&
                (!fileDescription.EnforceCsvColumnAttribute))
            {
                throw new CsvColumnAttributeRequiredException();
            }
            m_fileDescription = fileDescription;
            m_fileName        = fileName;

            m_NameToInfo = new Dictionary <string, TypeFieldInfo>();

            AnalyzeType(
                typeof(T),
                !fileDescription.FirstLineHasColumnNames,
                writingFile && !fileDescription.FirstLineHasColumnNames);
        }
Ejemplo n.º 6
0
        public IEnumerable <T> Read <T>(string fileName, CsvFileDescription fileDescription) where T : class, ICsvRow, new()
        {
            IEnumerable <T> ie = ReadData <T>(fileName, null, fileDescription);

            return(ie);
        }
Ejemplo n.º 7
0
        private IEnumerable <T> ReadData <T>(
            string fileName,
            StreamReader stream,
            CsvFileDescription fileDescription) where T : class, ICsvRow, new()
        {
            bool readingRawDataRows = typeof(IDataRow).IsAssignableFrom(typeof(T));


            FieldMapper_Reading <T> fm = null;

            if (!readingRawDataRows)
            {
                fm = new FieldMapper_Reading <T>(fileDescription, fileName, false);
            }



            bool readingFile = !string.IsNullOrEmpty(fileName);

            if (readingFile)
            {
                stream = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read));
            }
            else
            {
                if ((stream == null) || (!stream.BaseStream.CanSeek))
                {
                    throw new BadStreamException();
                }

                stream.BaseStream.Seek(0, SeekOrigin.Begin);
            }


            CsvStream cs = new CsvStream(stream, null, fileDescription.SeparatorChar);


            IDataRow row = null;

            if (readingRawDataRows)
            {
                row = new T() as IDataRow;
            }
            else
            {
                row = new DataRow();
            }

            AggregatedException ae =
                new AggregatedException(typeof(T).ToString(), fileName, fileDescription.MaximumNbrExceptions);

            try
            {
                bool firstRow = true;
                while (cs.ReadRow(ref row))
                {
                    if ((row.Count == 1) &&
                        ((row[0].Value == null) ||
                         (string.IsNullOrEmpty(row[0].Value.Trim()))))
                    {
                        continue;
                    }

                    if (firstRow && fileDescription.FirstLineHasColumnNames)
                    {
                        if (!readingRawDataRows)
                        {
                            fm.ReadNames(row);
                        }
                    }
                    else
                    {
                        T obj = default(T);
                        try
                        {
                            if (readingRawDataRows)
                            {
                                obj = row as T;
                            }
                            else
                            {
                                obj = fm.ReadObject(row, ae);
                            }
                        }
                        catch (AggregatedException ae2)
                        {
                            throw ae2;
                        }
                        catch (Exception e)
                        {
                            ae.AddException(e);
                        }

                        yield return(obj);
                    }
                    firstRow = false;
                }
            }
            finally
            {
                if (readingFile)
                {
                    stream.Dispose();
                }


                ae.ThrowIfExceptionsStored();
            }
        }
Ejemplo n.º 8
0
 public IEnumerable <T> Read <T>(StreamReader stream, CsvFileDescription fileDescription) where T :  class, ICsvRow, new()
 {
     return(ReadData <T>(null, stream, fileDescription));
 }