예제 #1
0
        /// <summary>
        /// Formats data by seperating by the seperation characters and placing the individual values
        /// into their own strings.
        /// </summary>
        /// <param name="seperationChar">The character to be used to format the string</param>
        /// <param name="lines">The lines to be formatted</param>
        /// <returns></returns>
        protected virtual CSVData FormatData(char seperationChar, string[] lines)
        {
            CSVData data = new CSVData();                                      // the data that will be returned

            foreach (string line in lines)                                     // loop through each line in the provided lines
            {
                string[] formattedLine = new string[CountColumnsInLine(line)]; // the line in the proper format, initalized.

                if (formattedLine.Length > 0)                                  // if there were no columns in the line, ignore it.
                {
                    int    column       = 0;                                   // a counter for indexing columns
                    string currentValue = "";                                  // intializing the current value to null.

                    // loop through each character in the line
                    foreach (char c in line)
                    {
                        if (c == this._seperationChar) // if we have reached the seperation character, send the characters in the buffer to the string ad clear the buffer.
                        {
                            formattedLine[column] = currentValue;
                            currentValue          = "";
                            column++; // advancing to the next column
                        }
                        else
                        {
                            if (!_ignoreList.Contains(c)) // only add the character if it is not to be ignored.
                            {
                                currentValue += c;        // add this character (not the seperation character) to the buffer.
                            }
                        }
                    }

                    formattedLine[column] = currentValue;     // add the final character to the buffer, since the loop will exit before it is added.

                    data.AddLine(new CSVLine(formattedLine)); // add the formatted line to the data structure
                }
            }

            return(data); // return the new data structure filled with formatted lines.
        }
예제 #2
0
 /// <summary>
 /// Default constructor.
 /// </summary>
 public CSVUtil()                     // default constructor
 {
     _data           = new CSVData(); // initialize data
     _currentPath    = "";            // initialize path
     _seperationChar = ',';           // set seperation character to default (comma)
 }
예제 #3
0
 /// <summary>
 /// Resets the stored data in the object.
 /// </summary>
 public virtual void ClearData()
 {
     _data = new CSVData(); // reinitializes data structure.
 }
예제 #4
0
 /// <summary>
 /// Sets the CSVData to be written.
 /// </summary>
 /// <param name="data">The data to write.</param>
 public virtual void SetData(CSVData data)
 {
     this._data = data;
 }
예제 #5
0
 /// <summary>
 /// Sets path and data and calls default constructor.
 /// </summary>
 /// <param name="path">Path of data file</param>
 /// <param name="data">CSVData to write to file</param>
 public CSVWriter(string path, CSVData data) : this()
 {
     this._currentPath = path;
     this.SetData(data);
 }
예제 #6
0
 /// <summary>
 /// Sets data and calls default constructor.
 /// </summary>
 /// <param name="data">The data to write.</param>
 public CSVWriter(CSVData data) : this()
 {
     this.SetData(data);
 }
예제 #7
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="data">Data to initialize with.</param>
        public CSVData(CSVData data)
        {
            this._lines = new List <CSVLine>();

            AddData(data);
        }