csvparser.class.inc.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * CSVParser
  4. * CSV interpreter helper
  5. *
  6. * @package iTopORM
  7. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  8. * @author Denis Flaven <denisflave@free.fr>
  9. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  10. * @link www.itop.com
  11. * @since 1.0
  12. * @version 1.1.1.1 $
  13. */
  14. class CSVParserException extends CoreException
  15. {
  16. }
  17. define('stSTARTING', 1); //grey zone: the type is undetermined
  18. define('stRAW', 2); //building a non-qualified string
  19. define('stQUALIFIED', 3); //building qualified string
  20. define('stESCAPED', 4); //just encountered an escape char
  21. define('evSEPARATOR', 1);
  22. define('evNEWLINE', 2);
  23. define('evTEXTQUAL', 3); // used for escaping as well
  24. define('evOTHERCHAR', 4);
  25. /**
  26. * CSVParser
  27. *
  28. * @package iTopORM
  29. * @author Romain Quetiez <romainquetiez@yahoo.fr>
  30. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  31. * @link www.itop.com
  32. * @since 1.0
  33. * @version $itopversion$
  34. */
  35. class CSVParser
  36. {
  37. private $m_sCSVData;
  38. private $m_sSep;
  39. private $m_sTextQualifier;
  40. public function __construct($sTxt, $sSep = ',', $sTextQualifier = '"')
  41. {
  42. $this->m_sCSVData = str_replace("\r\n", "\n", $sTxt);
  43. $this->m_sSep = $sSep;
  44. $this->m_sTextQualifier = $sTextQualifier;
  45. }
  46. protected $m_sCurrCell = '';
  47. protected $m_aCurrRow = array();
  48. protected $m_iToSkip = 0;
  49. protected $m_aDataSet = array();
  50. protected function __AddChar($c)
  51. {
  52. $this->m_sCurrCell .= $c;
  53. }
  54. protected function __ClearCell()
  55. {
  56. $this->m_sCurrCell = '';
  57. }
  58. protected function __AddCell($c = null, $aFieldMap = null)
  59. {
  60. if (!is_null($aFieldMap))
  61. {
  62. $iNextCol = count($this->m_aCurrRow);
  63. $iNextName = $aFieldMap[$iNextCol];
  64. $this->m_aCurrRow[$iNextName] = $this->m_sCurrCell;
  65. }
  66. else
  67. {
  68. $this->m_aCurrRow[] = $this->m_sCurrCell;
  69. }
  70. $this->m_sCurrCell = '';
  71. }
  72. protected function __AddRow($c = null, $aFieldMap = null)
  73. {
  74. $this->__AddCell($c, $aFieldMap);
  75. if ($this->m_iToSkip > 0)
  76. {
  77. $this->m_iToSkip--;
  78. }
  79. elseif (count($this->m_aCurrRow) > 1)
  80. {
  81. $this->m_aDataSet[] = $this->m_aCurrRow;
  82. }
  83. elseif (count($this->m_aCurrRow) == 1)
  84. {
  85. // Get the unique value
  86. $aValues = array_values($this->m_aCurrRow);
  87. $sValue = $aValues[0];
  88. if (strlen($sValue) > 0)
  89. {
  90. $this->m_aDataSet[] = $this->m_aCurrRow;
  91. }
  92. }
  93. else
  94. {
  95. // blank line, skip silently
  96. }
  97. $this->m_aCurrRow = array();
  98. }
  99. function ToArray($iToSkip = 1, $aFieldMap = null, $iMax = 0)
  100. {
  101. $aTransitions = array();
  102. $aTransitions[stSTARTING][evSEPARATOR] = array('__AddCell', stSTARTING);
  103. $aTransitions[stSTARTING][evNEWLINE] = array('__AddRow', stSTARTING);
  104. $aTransitions[stSTARTING][evTEXTQUAL] = array('', stQUALIFIED);
  105. $aTransitions[stSTARTING][evOTHERCHAR] = array('__AddChar', stRAW);
  106. $aTransitions[stRAW][evSEPARATOR] = array('__AddCell', stSTARTING);
  107. $aTransitions[stRAW][evNEWLINE] = array('__AddRow', stSTARTING);
  108. $aTransitions[stRAW][evTEXTQUAL] = array('__AddChar', stRAW);
  109. $aTransitions[stRAW][evOTHERCHAR] = array('__AddChar', stRAW);
  110. $aTransitions[stQUALIFIED][evSEPARATOR] = array('__AddChar', stQUALIFIED);
  111. $aTransitions[stQUALIFIED][evNEWLINE] = array('__AddChar', stQUALIFIED);
  112. $aTransitions[stQUALIFIED][evTEXTQUAL] = array('', stESCAPED);
  113. $aTransitions[stQUALIFIED][evOTHERCHAR] = array('__AddChar', stQUALIFIED);
  114. $aTransitions[stESCAPED][evSEPARATOR] = array('__AddCell', stSTARTING);
  115. $aTransitions[stESCAPED][evNEWLINE] = array('__AddRow', stSTARTING);
  116. $aTransitions[stESCAPED][evTEXTQUAL] = array('__AddChar', stQUALIFIED);
  117. $aTransitions[stESCAPED][evOTHERCHAR] = array('__AddChar', stSTARTING);
  118. // Reset parser variables
  119. $this->m_sCurrCell = '';
  120. $this->m_aCurrRow = array();
  121. $this->m_iToSkip = $iToSkip;
  122. $this->m_aDataSet = array();
  123. $iState = stSTARTING;
  124. for($i = 0; $i < strlen($this->m_sCSVData) ; $i++)
  125. {
  126. $c = $this->m_sCSVData[$i];
  127. // // Note: I did that because the unit test was not working fine (file edited with notepad: \n chars padded :-(
  128. // if (ord($c) == 0) continue;
  129. if ($c == $this->m_sSep)
  130. {
  131. $iEvent = evSEPARATOR;
  132. }
  133. elseif ($c == "\n")
  134. {
  135. $iEvent = evNEWLINE;
  136. }
  137. elseif ($c == $this->m_sTextQualifier)
  138. {
  139. $iEvent = evTEXTQUAL;
  140. }
  141. else
  142. {
  143. $iEvent = evOTHERCHAR;
  144. }
  145. $sAction = $aTransitions[$iState][$iEvent][0];
  146. $iState = $aTransitions[$iState][$iEvent][1];
  147. if (!empty($sAction))
  148. {
  149. $aCallSpec = array($this, $sAction);
  150. if (is_callable($aCallSpec))
  151. {
  152. call_user_func($aCallSpec, $c, $aFieldMap);
  153. }
  154. else
  155. {
  156. throw new CSVParserException("CSVParser: unknown verb '$sAction'");
  157. }
  158. }
  159. $iLineCount = count($this->m_aDataSet);
  160. if (($iMax > 0) && ($iLineCount >= $iMax)) break;
  161. }
  162. // Close the final line
  163. $this->__AddRow(null, $aFieldMap);
  164. return $this->m_aDataSet;
  165. }
  166. public function ListFields()
  167. {
  168. $aHeader = $this->ToArray(0, null, 1);
  169. return $aHeader[0];
  170. }
  171. }
  172. ?>