Пример #1
0
 //for a better reusability I created this method to avoid redundancy
 private static void DumpBuffer(Row row, char[] buffer)
 {
     String tmp = "";
     foreach (char c in buffer)
         if (c != '\0')
             tmp += c;
         else break; //otherwise keeps looping through the empty slot of the buffer
     Value<object> value = new Value<object>(tmp);
     row.AddValue(value);
 }
Пример #2
0
 /// <summary>
 /// Deserialize the file defined in the constructor
 /// </summary>
 /// <returns>A Document containing the deserialized values</returns>
 public Document Deserialize()
 {
     Document doc = new Document();
     int index = 0;
     while (!File.EndOfStream)
     {
         Row row = new Row();
         char[] line = File.ReadLine().ToCharArray();
         char[] buffer = new char[line.Length]; //temporary buffer for composing values
         int bufferIndex = 0;
         for (int i = 0; i < line.Length; i++)
         {
             if (line[i] == '\"')
             {
                 int closingQuoteIndex = IndexOf(line, '\"', i + 1); //copies all the stuff between the double quotes in the buffer
                 if (closingQuoteIndex == -1)
                     throw new CSVFileExcepion("Double quote mismatched");
                 for (int a = i + 1; a < closingQuoteIndex; a++)
                 {
                     buffer[bufferIndex] = line[a];
                     ++bufferIndex;
                 }
                 i = closingQuoteIndex; //the index is now the closing double quote, next loop it will be the ,
             }           
             else if (line[i] == ',') //when it sees a comma, it dumps the content of the buffer in the row
             {
                 DumpBuffer(row, buffer);
                 buffer = new char[line.Length]; //clears the array after dumping values
                 bufferIndex = 0;
             } //TODO: deal with the space
             else
             {
                 buffer[bufferIndex] = line[i];
                 ++bufferIndex;
             }
         }
         DumpBuffer(row, buffer); //the last column must be added
         if (index == 0) //the first row is the header
             doc.SetHeader(row);
         else
             doc.AddRow(row);
         ++index;
     }
     return doc;
 }
Пример #3
0
 /// <summary>
 /// Allows to add a row to the document
 /// </summary>
 /// <param name="row">The row to be added</param>
 public void AddRow(Row row)
 {
     Rows.Add(row);
 }
Пример #4
0
 /// <summary>
 /// Allows to set the header for the document
 /// </summary>
 /// <param name="header">Header of the document</param>
 public void SetHeader(Row header)
 {
     foreach (Value<object> v in header.Values)
         Headers.Add(v.ToString());
 }