csvparser.class.inc.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * CSV parser
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. class CSVParserException extends CoreException
  25. {
  26. }
  27. define('stSTARTING', 1); //grey zone: the type is undetermined
  28. define('stRAW', 2); //building a non-qualified string
  29. define('stQUALIFIED', 3); //building qualified string
  30. define('stESCAPED', 4); //just encountered an escape char
  31. define('evSEPARATOR', 1);
  32. define('evNEWLINE', 2);
  33. define('evTEXTQUAL', 3); // used for escaping as well
  34. define('evOTHERCHAR', 4);
  35. /**
  36. * CSVParser
  37. *
  38. * @package iTopORM
  39. */
  40. class CSVParser
  41. {
  42. private $m_sCSVData;
  43. private $m_sSep;
  44. private $m_sTextQualifier;
  45. public function __construct($sTxt, $sSep = ',', $sTextQualifier = '"')
  46. {
  47. $this->m_sCSVData = str_replace("\r\n", "\n", $sTxt);
  48. $this->m_sSep = $sSep;
  49. $this->m_sTextQualifier = $sTextQualifier;
  50. }
  51. protected $m_sCurrCell = '';
  52. protected $m_aCurrRow = array();
  53. protected $m_iToSkip = 0;
  54. protected $m_aDataSet = array();
  55. protected function __AddChar($c)
  56. {
  57. $this->m_sCurrCell .= $c;
  58. }
  59. protected function __ClearCell()
  60. {
  61. $this->m_sCurrCell = '';
  62. }
  63. protected function __AddCell($c = null, $aFieldMap = null)
  64. {
  65. if (!is_null($aFieldMap))
  66. {
  67. $iNextCol = count($this->m_aCurrRow);
  68. $iNextName = $aFieldMap[$iNextCol];
  69. $this->m_aCurrRow[$iNextName] = $this->m_sCurrCell;
  70. }
  71. else
  72. {
  73. $this->m_aCurrRow[] = $this->m_sCurrCell;
  74. }
  75. $this->m_sCurrCell = '';
  76. }
  77. protected function __AddRow($c = null, $aFieldMap = null)
  78. {
  79. $this->__AddCell($c, $aFieldMap);
  80. if ($this->m_iToSkip > 0)
  81. {
  82. $this->m_iToSkip--;
  83. }
  84. elseif (count($this->m_aCurrRow) > 1)
  85. {
  86. $this->m_aDataSet[] = $this->m_aCurrRow;
  87. }
  88. elseif (count($this->m_aCurrRow) == 1)
  89. {
  90. // Get the unique value
  91. $aValues = array_values($this->m_aCurrRow);
  92. $sValue = $aValues[0];
  93. if (strlen($sValue) > 0)
  94. {
  95. $this->m_aDataSet[] = $this->m_aCurrRow;
  96. }
  97. }
  98. else
  99. {
  100. // blank line, skip silently
  101. }
  102. $this->m_aCurrRow = array();
  103. }
  104. function ToArray($iToSkip = 1, $aFieldMap = null, $iMax = 0)
  105. {
  106. $aTransitions = array();
  107. $aTransitions[stSTARTING][evSEPARATOR] = array('__AddCell', stSTARTING);
  108. $aTransitions[stSTARTING][evNEWLINE] = array('__AddRow', stSTARTING);
  109. $aTransitions[stSTARTING][evTEXTQUAL] = array('', stQUALIFIED);
  110. $aTransitions[stSTARTING][evOTHERCHAR] = array('__AddChar', stRAW);
  111. $aTransitions[stRAW][evSEPARATOR] = array('__AddCell', stSTARTING);
  112. $aTransitions[stRAW][evNEWLINE] = array('__AddRow', stSTARTING);
  113. $aTransitions[stRAW][evTEXTQUAL] = array('__AddChar', stRAW);
  114. $aTransitions[stRAW][evOTHERCHAR] = array('__AddChar', stRAW);
  115. $aTransitions[stQUALIFIED][evSEPARATOR] = array('__AddChar', stQUALIFIED);
  116. $aTransitions[stQUALIFIED][evNEWLINE] = array('__AddChar', stQUALIFIED);
  117. $aTransitions[stQUALIFIED][evTEXTQUAL] = array('', stESCAPED);
  118. $aTransitions[stQUALIFIED][evOTHERCHAR] = array('__AddChar', stQUALIFIED);
  119. $aTransitions[stESCAPED][evSEPARATOR] = array('__AddCell', stSTARTING);
  120. $aTransitions[stESCAPED][evNEWLINE] = array('__AddRow', stSTARTING);
  121. $aTransitions[stESCAPED][evTEXTQUAL] = array('__AddChar', stQUALIFIED);
  122. $aTransitions[stESCAPED][evOTHERCHAR] = array('__AddChar', stSTARTING);
  123. // Reset parser variables
  124. $this->m_sCurrCell = '';
  125. $this->m_aCurrRow = array();
  126. $this->m_iToSkip = $iToSkip;
  127. $this->m_aDataSet = array();
  128. $iState = stSTARTING;
  129. for($i = 0; $i < strlen($this->m_sCSVData) ; $i++)
  130. {
  131. $c = $this->m_sCSVData[$i];
  132. // // Note: I did that because the unit test was not working fine (file edited with notepad: \n chars padded :-(
  133. // if (ord($c) == 0) continue;
  134. if ($c == $this->m_sSep)
  135. {
  136. $iEvent = evSEPARATOR;
  137. }
  138. elseif ($c == "\n")
  139. {
  140. $iEvent = evNEWLINE;
  141. }
  142. elseif ($c == $this->m_sTextQualifier)
  143. {
  144. $iEvent = evTEXTQUAL;
  145. }
  146. else
  147. {
  148. $iEvent = evOTHERCHAR;
  149. }
  150. $sAction = $aTransitions[$iState][$iEvent][0];
  151. $iState = $aTransitions[$iState][$iEvent][1];
  152. if (!empty($sAction))
  153. {
  154. $aCallSpec = array($this, $sAction);
  155. if (is_callable($aCallSpec))
  156. {
  157. call_user_func($aCallSpec, $c, $aFieldMap);
  158. }
  159. else
  160. {
  161. throw new CSVParserException("CSVParser: unknown verb '$sAction'");
  162. }
  163. }
  164. $iLineCount = count($this->m_aDataSet);
  165. if (($iMax > 0) && ($iLineCount >= $iMax)) break;
  166. }
  167. // Close the final line
  168. $this->__AddRow(null, $aFieldMap);
  169. return $this->m_aDataSet;
  170. }
  171. public function ListFields()
  172. {
  173. $aHeader = $this->ToArray(0, null, 1);
  174. return $aHeader[0];
  175. }
  176. }
  177. ?>