PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Web1.2/_code/CsvDataReader.cs

#
C# | 187 lines | 145 code | 13 blank | 29 comment | 47 complexity | dd85901e052ed5701b711583e4d8c09f MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**********************************************************************************************************************
  2. * The contents of this file are subject to the SugarCRM Public License Version 1.1.3 ("License"); You may not use this
  3. * file except in compliance with the License. You may obtain a copy of the License at http://www.sugarcrm.com/SPL
  4. * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
  5. * express or implied. See the License for the specific language governing rights and limitations under the License.
  6. *
  7. * All copies of the Covered Code must include on each user interface screen:
  8. * (i) the "Powered by SugarCRM" logo and
  9. * (ii) the SugarCRM copyright notice
  10. * (iii) the SplendidCRM copyright notice
  11. * in the same form as they appear in the distribution. See full license for requirements.
  12. *
  13. * The Original Code is: SplendidCRM Open Source
  14. * The Initial Developer of the Original Code is SplendidCRM Software, Inc.
  15. * Portions created by SplendidCRM Software are Copyright (C) 2006 SplendidCRM Software, Inc. All Rights Reserved.
  16. * Contributor(s): ______________________________________.
  17. *********************************************************************************************************************/
  18. using System;
  19. using System.IO;
  20. using System.Data;
  21. using System.Data.Common;
  22. using System.Configuration;
  23. using System.Text;
  24. //using MySql.Data.MySqlClient;
  25. namespace SplendidCRM
  26. {
  27. /// <summary>
  28. /// Summary description for CsvDataReader.
  29. /// </summary>
  30. public class CsvDataReader
  31. {
  32. private DataTable m_tbl;
  33. public DataTable Table
  34. {
  35. get { return m_tbl; }
  36. }
  37. public CsvDataReader(Stream stm) : this(stm, ',')
  38. {
  39. }
  40. public CsvDataReader(Stream stm, char chFieldSeparator)
  41. {
  42. m_tbl = new DataTable();
  43. using ( TextReader reader = new StreamReader(stm) )
  44. {
  45. m_tbl.Columns.Add("Column001");
  46. string sLine = null;
  47. while ( (sLine = reader.ReadLine()) != null )
  48. {
  49. if ( sLine.Length == 0 )
  50. continue;
  51. DataRow row = m_tbl.NewRow();
  52. m_tbl.Rows.Add(row);
  53. int i = 0;
  54. int nMode = 0;
  55. int nField = 0;
  56. bool bContinueParsing = true;
  57. while ( bContinueParsing )
  58. {
  59. switch ( nMode )
  60. {
  61. case 0: // Search for next entry.
  62. {
  63. if ( chFieldSeparator == ControlChars.Tab )
  64. {
  65. // Don't skip the tab when it is used as a separator.
  66. while ( Char.IsWhiteSpace(sLine[i]) && sLine[i] != ControlChars.Tab )
  67. i++;
  68. }
  69. else
  70. {
  71. while ( Char.IsWhiteSpace(sLine[i]) )
  72. i++;
  73. }
  74. nMode = 1;
  75. break;
  76. }
  77. case 1: // Determine if field is quoted or unquoted.
  78. {
  79. // first check if field is empty.
  80. char chPunctuation = sLine[i];
  81. if ( chPunctuation == chFieldSeparator )
  82. {
  83. i++;
  84. nField++;
  85. if ( nField >= m_tbl.Columns.Count )
  86. m_tbl.Columns.Add("Column" + nField.ToString("000"));
  87. nMode = 0;
  88. }
  89. if ( chPunctuation == '\"' )
  90. {
  91. i++;
  92. // Field is quoted, so start reading until next quote.
  93. nMode = 3;
  94. }
  95. else
  96. {
  97. // Field is unquoted, so start reading until next separator or end-of-line.
  98. nMode = 2;
  99. }
  100. break;
  101. }
  102. case 2: // Extract unquoted field.
  103. {
  104. nField++;
  105. if ( nField > m_tbl.Columns.Count )
  106. m_tbl.Columns.Add("Column" + nField.ToString("000"));
  107. int nFieldStart = i;
  108. // Field is unquoted, so start reading until next separator or end-of-line.
  109. while ( i < sLine.Length && sLine[i] != chFieldSeparator )
  110. i++;
  111. int nFieldEnd = i;
  112. string sField = sLine.Substring(nFieldStart, nFieldEnd-nFieldStart);
  113. row[nField-1] = sField;
  114. nMode = 0;
  115. i++;
  116. break;
  117. }
  118. case 3: // Extract quoted field.
  119. {
  120. nField++;
  121. if ( nField > m_tbl.Columns.Count )
  122. m_tbl.Columns.Add("Column" + nField.ToString("000"));
  123. bool bMultiline = false;
  124. StringBuilder sbField = new StringBuilder();
  125. do
  126. {
  127. int nFieldStart = i;
  128. // Field is quoted, so start reading until next quote. Watch out for an escaped quote (two double quotes).
  129. while ( ( i < sLine.Length && sLine[i] != '\"' ) || ( i + 1 < sLine.Length && sLine[i] == '\"' && sLine[i + 1] == '\"' ) )
  130. {
  131. if ( i + 1 < sLine.Length && sLine[i] == '\"' && sLine[i + 1] == '\"' )
  132. i++;
  133. i++;
  134. }
  135. int nFieldEnd = i;
  136. if ( sbField.Length > 0 )
  137. sbField.Append(ControlChars.CrLf);
  138. sbField.Append(sLine.Substring(nFieldStart, nFieldEnd - nFieldStart));
  139. // 08/23/2006 Paul. If we are at the end of the line, then it must be a multi-line string.
  140. bMultiline = (i == sLine.Length);
  141. if ( bMultiline )
  142. {
  143. sLine = reader.ReadLine();
  144. i = 0;
  145. if ( sLine == null )
  146. break;
  147. }
  148. }
  149. while ( bMultiline );
  150. if ( sLine != null )
  151. {
  152. // Skip all characters until we reach the separator or end-of-line.
  153. while ( i < sLine.Length && sLine[i] != chFieldSeparator )
  154. i++;
  155. }
  156. string sField = sbField.ToString();
  157. sField = sField.Replace("\"\"", "\"");
  158. row[nField-1] = sField;
  159. nMode = 0;
  160. i++;
  161. break;
  162. }
  163. default:
  164. bContinueParsing = false;
  165. break;
  166. }
  167. if ( i >= sLine.Length )
  168. break;
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }