csvparser.class.inc.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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) && (strlen($this->m_aCurrRow[0]) > 0))
  84. {
  85. $this->m_aDataSet[] = $this->m_aCurrRow;
  86. }
  87. else
  88. {
  89. // blank line, skip silently
  90. }
  91. $this->m_aCurrRow = array();
  92. }
  93. function ToArray($iToSkip = 1, $aFieldMap = null, $iMax = 0)
  94. {
  95. $aTransitions = array();
  96. $aTransitions[stSTARTING][evSEPARATOR] = array('__AddCell', stSTARTING);
  97. $aTransitions[stSTARTING][evNEWLINE] = array('__AddRow', stSTARTING);
  98. $aTransitions[stSTARTING][evTEXTQUAL] = array('', stQUALIFIED);
  99. $aTransitions[stSTARTING][evOTHERCHAR] = array('__AddChar', stRAW);
  100. $aTransitions[stRAW][evSEPARATOR] = array('__AddCell', stSTARTING);
  101. $aTransitions[stRAW][evNEWLINE] = array('__AddRow', stSTARTING);
  102. $aTransitions[stRAW][evTEXTQUAL] = array('__AddChar', stRAW);
  103. $aTransitions[stRAW][evOTHERCHAR] = array('__AddChar', stRAW);
  104. $aTransitions[stQUALIFIED][evSEPARATOR] = array('__AddChar', stQUALIFIED);
  105. $aTransitions[stQUALIFIED][evNEWLINE] = array('__AddChar', stQUALIFIED);
  106. $aTransitions[stQUALIFIED][evTEXTQUAL] = array('', stESCAPED);
  107. $aTransitions[stQUALIFIED][evOTHERCHAR] = array('__AddChar', stQUALIFIED);
  108. $aTransitions[stESCAPED][evSEPARATOR] = array('__AddCell', stSTARTING);
  109. $aTransitions[stESCAPED][evNEWLINE] = array('__AddRow', stSTARTING);
  110. $aTransitions[stESCAPED][evTEXTQUAL] = array('__AddChar', stQUALIFIED);
  111. $aTransitions[stESCAPED][evOTHERCHAR] = array('__AddChar', stSTARTING);
  112. // Reset parser variables
  113. $this->m_sCurrCell = '';
  114. $this->m_aCurrRow = array();
  115. $this->m_iToSkip = $iToSkip;
  116. $this->m_aDataSet = array();
  117. $iState = stSTARTING;
  118. for($i = 0; $i < strlen($this->m_sCSVData) ; $i++)
  119. {
  120. $c = $this->m_sCSVData[$i];
  121. // // Note: I did that because the unit test was not working fine (file edited with notepad: \n chars padded :-(
  122. // if (ord($c) == 0) continue;
  123. if ($c == $this->m_sSep)
  124. {
  125. $iEvent = evSEPARATOR;
  126. }
  127. elseif ($c == "\n")
  128. {
  129. $iEvent = evNEWLINE;
  130. }
  131. elseif ($c == $this->m_sTextQualifier)
  132. {
  133. $iEvent = evTEXTQUAL;
  134. }
  135. else
  136. {
  137. $iEvent = evOTHERCHAR;
  138. }
  139. $sAction = $aTransitions[$iState][$iEvent][0];
  140. $iState = $aTransitions[$iState][$iEvent][1];
  141. if (!empty($sAction))
  142. {
  143. $aCallSpec = array($this, $sAction);
  144. if (is_callable($aCallSpec))
  145. {
  146. call_user_func($aCallSpec, $c, $aFieldMap);
  147. }
  148. else
  149. {
  150. throw new CSVParserException("CSVParser: unknown verb '$sAction'");
  151. }
  152. }
  153. $iLineCount = count($this->m_aDataSet);
  154. if (($iMax > 0) && ($iLineCount >= $iMax)) break;
  155. }
  156. // Close the final line
  157. $this->__AddRow(null, $aFieldMap);
  158. return $this->m_aDataSet;
  159. }
  160. public function ListFields()
  161. {
  162. $aHeader = $this->ToArray(0, null, 1);
  163. return $aHeader[0];
  164. }
  165. }
  166. ?>