csvparser.class.inc.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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('evBLANK', 0);
  32. define('evSEPARATOR', 1);
  33. define('evNEWLINE', 2);
  34. define('evTEXTQUAL', 3); // used for escaping as well
  35. define('evOTHERCHAR', 4);
  36. define('evEND', 5);
  37. /**
  38. * CSVParser
  39. *
  40. * @package iTopORM
  41. */
  42. class CSVParser
  43. {
  44. private $m_sCSVData;
  45. private $m_sSep;
  46. private $m_sTextQualifier;
  47. public function __construct($sTxt, $sSep = ',', $sTextQualifier = '"')
  48. {
  49. $this->m_sCSVData = str_replace("\r\n", "\n", $sTxt);
  50. $this->m_sSep = $sSep;
  51. $this->m_sTextQualifier = $sTextQualifier;
  52. }
  53. protected $m_sCurrCell = '';
  54. protected $m_aCurrRow = array();
  55. protected $m_iToSkip = 0;
  56. protected $m_aDataSet = array();
  57. protected function __AddChar($c)
  58. {
  59. $this->m_sCurrCell .= $c;
  60. }
  61. protected function __ClearCell()
  62. {
  63. $this->m_sCurrCell = '';
  64. }
  65. protected function __AddCell($c = null, $aFieldMap = null, $bTrimSpaces = false)
  66. {
  67. if ($bTrimSpaces)
  68. {
  69. $sCell = trim($this->m_sCurrCell);
  70. }
  71. else
  72. {
  73. $sCell = $this->m_sCurrCell;
  74. }
  75. if (!is_null($aFieldMap))
  76. {
  77. $iNextCol = count($this->m_aCurrRow);
  78. $iNextName = $aFieldMap[$iNextCol];
  79. $this->m_aCurrRow[$iNextName] = $sCell;
  80. }
  81. else
  82. {
  83. $this->m_aCurrRow[] = $sCell;
  84. }
  85. $this->m_sCurrCell = '';
  86. }
  87. protected function __AddRow($c = null, $aFieldMap = null, $bTrimSpaces = false)
  88. {
  89. $this->__AddCell($c, $aFieldMap, $bTrimSpaces);
  90. if ($this->m_iToSkip > 0)
  91. {
  92. $this->m_iToSkip--;
  93. }
  94. elseif (count($this->m_aCurrRow) > 1)
  95. {
  96. $this->m_aDataSet[] = $this->m_aCurrRow;
  97. }
  98. elseif (count($this->m_aCurrRow) == 1)
  99. {
  100. // Get the unique value
  101. $aValues = array_values($this->m_aCurrRow);
  102. $sValue = $aValues[0];
  103. if (strlen($sValue) > 0)
  104. {
  105. $this->m_aDataSet[] = $this->m_aCurrRow;
  106. }
  107. }
  108. else
  109. {
  110. // blank line, skip silently
  111. }
  112. $this->m_aCurrRow = array();
  113. }
  114. protected function __AddCellTrimmed($c = null, $aFieldMap = null)
  115. {
  116. $this->__AddCell($c, $aFieldMap, true);
  117. }
  118. protected function __AddRowTrimmed($c = null, $aFieldMap = null)
  119. {
  120. $this->__AddRow($c, $aFieldMap, true);
  121. }
  122. function ToArray($iToSkip = 1, $aFieldMap = null, $iMax = 0)
  123. {
  124. $aTransitions = array();
  125. $aTransitions[stSTARTING][evBLANK] = array('', stSTARTING);
  126. $aTransitions[stSTARTING][evSEPARATOR] = array('__AddCell', stSTARTING);
  127. $aTransitions[stSTARTING][evNEWLINE] = array('__AddRow', stSTARTING);
  128. $aTransitions[stSTARTING][evTEXTQUAL] = array('', stQUALIFIED);
  129. $aTransitions[stSTARTING][evOTHERCHAR] = array('__AddChar', stRAW);
  130. $aTransitions[stSTARTING][evEND] = array('__AddRow', stSTARTING);
  131. $aTransitions[stRAW][evBLANK] = array('__AddChar', stRAW);
  132. $aTransitions[stRAW][evSEPARATOR] = array('__AddCellTrimmed', stSTARTING);
  133. $aTransitions[stRAW][evNEWLINE] = array('__AddRowTrimmed', stSTARTING);
  134. $aTransitions[stRAW][evTEXTQUAL] = array('__AddChar', stRAW);
  135. $aTransitions[stRAW][evOTHERCHAR] = array('__AddChar', stRAW);
  136. $aTransitions[stRAW][evEND] = array('__AddRowTrimmed', stSTARTING);
  137. $aTransitions[stQUALIFIED][evBLANK] = array('__AddChar', stQUALIFIED);
  138. $aTransitions[stQUALIFIED][evSEPARATOR] = array('__AddChar', stQUALIFIED);
  139. $aTransitions[stQUALIFIED][evNEWLINE] = array('__AddChar', stQUALIFIED);
  140. $aTransitions[stQUALIFIED][evTEXTQUAL] = array('', stESCAPED);
  141. $aTransitions[stQUALIFIED][evOTHERCHAR] = array('__AddChar', stQUALIFIED);
  142. $aTransitions[stQUALIFIED][evEND] = array('__AddRow', stSTARTING);
  143. $aTransitions[stESCAPED][evBLANK] = array('', stESCAPED);
  144. $aTransitions[stESCAPED][evSEPARATOR] = array('__AddCell', stSTARTING);
  145. $aTransitions[stESCAPED][evNEWLINE] = array('__AddRow', stSTARTING);
  146. $aTransitions[stESCAPED][evTEXTQUAL] = array('__AddChar', stQUALIFIED);
  147. $aTransitions[stESCAPED][evOTHERCHAR] = array('__AddChar', stSTARTING);
  148. $aTransitions[stESCAPED][evEND] = array('__AddRow', stSTARTING);
  149. // Reset parser variables
  150. $this->m_sCurrCell = '';
  151. $this->m_aCurrRow = array();
  152. $this->m_iToSkip = $iToSkip;
  153. $this->m_aDataSet = array();
  154. $iDataLength = strlen($this->m_sCSVData);
  155. $iState = stSTARTING;
  156. for($i = 0; $i <= $iDataLength ; $i++)
  157. {
  158. if ($i == $iDataLength)
  159. {
  160. $iEvent = evEND;
  161. }
  162. else
  163. {
  164. $c = $this->m_sCSVData[$i];
  165. if ($c == $this->m_sSep)
  166. {
  167. $iEvent = evSEPARATOR;
  168. }
  169. elseif ($c == ' ')
  170. {
  171. $iEvent = evBLANK;
  172. }
  173. elseif ($c == "\t")
  174. {
  175. $iEvent = evBLANK;
  176. }
  177. elseif ($c == "\n")
  178. {
  179. $iEvent = evNEWLINE;
  180. }
  181. elseif ($c == $this->m_sTextQualifier)
  182. {
  183. $iEvent = evTEXTQUAL;
  184. }
  185. else
  186. {
  187. $iEvent = evOTHERCHAR;
  188. }
  189. }
  190. $sAction = $aTransitions[$iState][$iEvent][0];
  191. $iState = $aTransitions[$iState][$iEvent][1];
  192. if (!empty($sAction))
  193. {
  194. $aCallSpec = array($this, $sAction);
  195. if (is_callable($aCallSpec))
  196. {
  197. call_user_func($aCallSpec, $c, $aFieldMap);
  198. }
  199. else
  200. {
  201. throw new CSVParserException("CSVParser: unknown verb '$sAction'");
  202. }
  203. }
  204. $iLineCount = count($this->m_aDataSet);
  205. if (($iMax > 0) && ($iLineCount >= $iMax)) break;
  206. }
  207. return $this->m_aDataSet;
  208. }
  209. public function ListFields()
  210. {
  211. $aHeader = $this->ToArray(0, null, 1);
  212. return $aHeader[0];
  213. }
  214. }
  215. ?>