Ejemplo n.º 1
0
 /// <summary>
 /// Constructor that will deal with streams.
 /// </summary>
 /// <param name="stream">The stream the csv data exists in.</param>
 /// <param name="options">The stream options we need to use for parsing.</param>
 public CsvStreamWriter(Stream stream, CsvStreamOptions options)
 {
     _options   = options;
     _stream    = stream;
     _converter = new CsvConverter <TModel>(_options, new List <string>());
     _writer    = new StreamWriter(_stream, Encoding.UTF8);
     _buffer    = new StringBuilder(256 * 1024);
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Constructor that will deal with files and convert them into a stream.
 /// </summary>
 /// <param name="path">The file path that we need to create a stream from.</param>
 /// <param name="options">The stream options we need to use for parsing.</param>
 public CsvStreamWriter(string path, CsvStreamOptions options)
 {
     _options   = options;
     _stream    = File.Open(path, FileMode.Truncate, FileAccess.Write);
     _converter = new CsvConverter <TModel>(_options, new List <string>());
     _writer    = new StreamWriter(_stream, Encoding.UTF8);
     _buffer    = new StringBuilder(256 * 1024);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Method is meant to deal with reading csv files from the system.
 /// </summary>
 /// <param name="path">The file path to the csv file that needs to be parsed.</param>
 /// <param name="options">The options that should be sent off to the stream.</param>
 /// <typeparam name="TModel">The model the parser needs to turn into.</typeparam>
 /// <returns>The list of objects that the parser comes back at.</returns>
 public static List <TModel> ParseFile <TModel>(string path, CsvStreamOptions options)
     where TModel : class, new()
 {
     using (var reader = new CsvStreamReader <TModel>(path, options))
     {
         return(reader.AsEnumerable().ToList());
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Method is meant to parse a csv string.
 /// </summary>
 /// <param name="csv">The csv string that needs to be parsed.</param>
 /// <param name="options">The options that should be sent off to the stream.</param>
 /// <typeparam name="TModel">The model the parser needs to turn into.</typeparam>
 /// <returns>The list of objects that the parser comes back at.</returns>
 public static List <TModel> Parse <TModel>(string csv, CsvStreamOptions options)
     where TModel : class, new()
 {
     using (var reader = new CsvStreamReader <TModel>(GenerateStream(csv), options))
     {
         return(reader.AsEnumerable().ToList());
     }
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Method is meant to parse out the models to a string for use somewhere else.
        /// </summary>
        /// <param name="models">The models that need to be parsed into a string.</param>
        /// <param name="options">The options that should be sent off to the stream.</param>
        /// <typeparam name="TModel">The model the parser needs to translate from.</typeparam>
        /// <returns>Will return a parsed string of the objects being passed in.</returns>
        public static string Stringify <TModel>(IEnumerable <TModel> models, CsvStreamOptions options)
            where TModel : class, new()
        {
            var result = string.Empty;
            var stream = GenerateStream(string.Empty);

            using (var writer = new CsvStreamWriter <TModel>(stream, options))
            {
                if (options.WriteHeaders)
                {
                    writer.WriteHeader();
                }
                foreach (var model in models)
                {
                    writer.WriteLine(model);
                }
                writer.Flush();
                result = ReadStream(stream);
            }
            return(result);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Constructor to cache various pieces of the parser to deal with converting to the model given in the generic class.
        /// </summary>
        /// <param name="options">The options this converter should use when building out the objects.</param>
        /// <param name="headers">The headers that should be used to build the objects.</param>
        /// <param name="emptyColumns">The lambda expression to deal with empty columns.</param>
        public CsvConverter(CsvStreamOptions options, List <string> headers = null, Func <int, string> emptyColumns = null)
        {
            if (options.Wrapper != null && options.RowDelimiter.IndexOf(options.Wrapper.Value) > -1)
            {
                throw new ArgumentException("Row delimiter cannot contain a value from Wrapper or Delimiter");
            }
            if (options.RowDelimiter.IndexOf(options.Delimiter) > -1)
            {
                throw new ArgumentException("Row delimiter cannot contain a value from Wrapper or Delimiter");
            }
            if (options.Wrapper.ToString() == options.Delimiter)
            {
                throw new ArgumentException("Wrapper and Delimiter cannot be equal");
            }
            if (headers == null && options.ParseHeaders)
            {
                throw new ArgumentException("No headers were found.");
            }

            _options      = options;
            _headers      = headers;
            _emptyColumns = emptyColumns;
        }
Ejemplo n.º 7
0
 public static List <TModel> ParseFile <TModel>(Stream stream, CsvStreamOptions csvStreamOptions)
     where TModel : class, new()
 {
     using var reader = new CsvStreamReader <TModel>(stream, csvStreamOptions);
     return(reader.AsEnumerable().ToList());
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Method is meant to write a file with a set of models.
 /// </summary>
 /// <param name="path">The path to the file we are saving to.</param>
 /// <param name="models">The models we are saving.</param>
 /// <param name="options">The options that should be sent off to the stream.</param>
 /// <typeparam name="TModel">The model the parser needs to translate from.</typeparam>
 public static void SaveFile <TModel>(string path, IEnumerable <TModel> models, CsvStreamOptions options)
     where TModel : class, new()
 {
     using (var writer = new CsvStreamWriter <TModel>(path, options))
     {
         if (options.WriteHeaders)
         {
             writer.WriteHeader();
         }
         foreach (var model in models)
         {
             writer.WriteLine(model);
         }
     }
 }