CsvOptions.cs :  » Persistence-Frameworks » FileHelpers-Library » FileHelpers » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Persistence Frameworks » FileHelpers Library 
FileHelpers Library » FileHelpers » CsvOptions.cs
using System;
using System.Text;
using FileHelpers.RunTime;

namespace FileHelpers{
  /// <summary>Class used to pass information to the <see cref="FileHelpers.RunTime.CsvClassBuilder"/> and the <see cref="CsvEngine"/></summary>
  public sealed class CsvOptions
  {

    /// <summary>Create a Csv Wrapper using the specified number of fields.</summary>
    /// <param name="className">The name of the record class</param>
    /// <param name="delimiter">The delimiter for each field</param>
    /// <param name="numberOfFields">The number of fields of each record</param>
    public CsvOptions(string className, char delimiter, int numberOfFields)
    {
      mRecordClassName = className;
      mDelimiter = delimiter;
      mNumberOfFields = numberOfFields;
    }

    /// <summary>Create a Csv Wrapper using the specified sample file with their headers.</summary>
    /// <param name="className">The name of the record class</param>
    /// <param name="delimiter">The delimiter for each field</param>
    /// <param name="sampleFile">A sample file with a header that contains the names of the fields.</param>
    public CsvOptions(string className, char delimiter, string sampleFile)
    {
      mRecordClassName = className;
      mDelimiter = delimiter;
      mSampleFileName = sampleFile;
    }

    private string mSampleFileName = string.Empty;
    private char mDelimiter = ',';
    private char mHeaderDelimiter = char.MinValue;
    private int mHeaderLines = 1;
    private string mRecordClassName = string.Empty;
    private int mNumberOfFields = -1;
    private string mFieldsPrefix = "Field_";
    private string mDateFormat= "dd/MM/yyyy";
    private string mDecimalSeparator = ".";
    private Encoding mEncoding = Encoding.Default;
    

    /// <summary>A sample file from where to read the field names and number.</summary>
    public string SampleFileName
    {
      get { return mSampleFileName; }
      set { mSampleFileName = value; }
    }

    /// <summary>The delimiter for each field.</summary>
    public char Delimiter
    {
      get { return mDelimiter; }
      set { mDelimiter = value; }
    }

    /// <summary>The delimiter for each fiel name in the header.</summary>
    public char HeaderDelimiter
    {
      get { return mHeaderDelimiter; }
      set { mHeaderDelimiter = value; }
    }

    /// <summary>The name used for the record class (a valid .NET class).</summary>
    public string RecordClassName
    {
      get { return mRecordClassName; }
      set { mRecordClassName = value; }
    }

    /// <summary>The prefix used when you only specified the number of fields</summary>
    public string FieldsPrefix
    {
      get { return mFieldsPrefix; }
      set { mFieldsPrefix = ClassBuilder.StringToIdentifier(value); }
    }

    /// <summary>The number of fields that the file contains.</summary>
    public int NumberOfFields
    {
      get { return mNumberOfFields; }
      set { mNumberOfFields = value; }
    }

    /// <summary>The number of header lines</summary>
    public int HeaderLines
    {
      get { return mHeaderLines; }
      set { mHeaderLines = value; }
    }

    /// <summary>The DateFormat used to read and write datetime values</summary>
    public string DateFormat
    {
      get { return mDateFormat; }
      set { mDateFormat = value; }
    }

    /// <summary>The Decimal Separator used to read and write doubles, singles and decimal values</summary>
    public string DecimalSeparator
    {
      get { return mDecimalSeparator; }
      set { mDecimalSeparator = value; }
    }

    
    /// <summary>
    /// Encoding used when handling the CSV files.
    /// </summary>
    public Encoding Encoding
    {
      get { return mEncoding; }
      set { mEncoding = value; }
    }

    ConvertHelpers.DecimalConverter mDecimalConv;
    ConvertHelpers.DoubleConverter mDoubleConv;
    ConvertHelpers.SingleConverter mSingleConv;
    ConvertHelpers.DateTimeConverter mDateConv;

    internal string ValueToString(object o)
    {
      if (mDecimalConv == null)
      {
        mDecimalConv = new ConvertHelpers.DecimalConverter(DecimalSeparator);
        mDoubleConv = new ConvertHelpers.DoubleConverter(DecimalSeparator);
        mSingleConv = new ConvertHelpers.SingleConverter(DecimalSeparator);
        mDateConv= new ConvertHelpers.DateTimeConverter(DateFormat);
      }
      
      if (o == null)
        return string.Empty;
      else if (o is DateTime)
        return mDateConv.FieldToString(o);
      else if (o is Decimal)
        return mDecimalConv.FieldToString(o);
      else if (o is Double)
        return mDoubleConv.FieldToString(o);
      else if (o is Single)
        return mSingleConv.FieldToString(o);
      else 
        return o.ToString();
      
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.