csvparser.class.inc.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. // Copyright (C) 2010-2012 Combodo SARL
  3. //
  4. // This file is part of iTop.
  5. //
  6. // iTop is free software; you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // iTop is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with iTop. If not, see <http://www.gnu.org/licenses/>
  18. /**
  19. * CSV parser
  20. *
  21. * @copyright Copyright (C) 2010-2012 Combodo SARL
  22. * @license http://opensource.org/licenses/AGPL-3.0
  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. define('NULL_VALUE', '<NULL>');
  38. /**
  39. * CSVParser
  40. *
  41. * @package iTopORM
  42. */
  43. class CSVParser
  44. {
  45. private $m_sCSVData;
  46. private $m_sSep;
  47. private $m_sTextQualifier;
  48. private $m_iTimeLimitPerRow;
  49. public function __construct($sTxt, $sSep = ',', $sTextQualifier = '"', $iTimeLimitPerRow = null)
  50. {
  51. $this->m_sCSVData = str_replace("\r\n", "\n", $sTxt);
  52. $this->m_sSep = $sSep;
  53. $this->m_sTextQualifier = $sTextQualifier;
  54. $this->m_iTimeLimitPerRow = $iTimeLimitPerRow;
  55. }
  56. protected $m_sCurrCell = '';
  57. protected $m_aCurrRow = array();
  58. protected $m_iToSkip = 0;
  59. protected $m_aDataSet = array();
  60. protected function __AddChar($c)
  61. {
  62. $this->m_sCurrCell .= $c;
  63. }
  64. protected function __ClearCell()
  65. {
  66. $this->m_sCurrCell = '';
  67. }
  68. protected function __AddCell($c = null, $aFieldMap = null, $bTrimSpaces = false)
  69. {
  70. if ($bTrimSpaces)
  71. {
  72. $sCell = trim($this->m_sCurrCell);
  73. }
  74. else
  75. {
  76. $sCell = $this->m_sCurrCell;
  77. }
  78. if ($sCell == NULL_VALUE)
  79. {
  80. $sCell = null;
  81. }
  82. if (!is_null($aFieldMap))
  83. {
  84. $iNextCol = count($this->m_aCurrRow);
  85. $iNextName = $aFieldMap[$iNextCol];
  86. $this->m_aCurrRow[$iNextName] = $sCell;
  87. }
  88. else
  89. {
  90. $this->m_aCurrRow[] = $sCell;
  91. }
  92. $this->m_sCurrCell = '';
  93. }
  94. protected function __AddRow($c = null, $aFieldMap = null, $bTrimSpaces = false)
  95. {
  96. $this->__AddCell($c, $aFieldMap, $bTrimSpaces);
  97. if ($this->m_iToSkip > 0)
  98. {
  99. $this->m_iToSkip--;
  100. }
  101. elseif (count($this->m_aCurrRow) > 1)
  102. {
  103. $this->m_aDataSet[] = $this->m_aCurrRow;
  104. }
  105. elseif (count($this->m_aCurrRow) == 1)
  106. {
  107. // Get the unique value
  108. $aValues = array_values($this->m_aCurrRow);
  109. $sValue = $aValues[0];
  110. if (strlen($sValue) > 0)
  111. {
  112. $this->m_aDataSet[] = $this->m_aCurrRow;
  113. }
  114. }
  115. else
  116. {
  117. // blank line, skip silently
  118. }
  119. $this->m_aCurrRow = array();
  120. // More time for the next row
  121. if ($this->m_iTimeLimitPerRow !== null)
  122. {
  123. set_time_limit($this->m_iTimeLimitPerRow);
  124. }
  125. }
  126. protected function __AddCellTrimmed($c = null, $aFieldMap = null)
  127. {
  128. $this->__AddCell($c, $aFieldMap, true);
  129. }
  130. protected function __AddRowTrimmed($c = null, $aFieldMap = null)
  131. {
  132. $this->__AddRow($c, $aFieldMap, true);
  133. }
  134. function ToArray($iToSkip = 1, $aFieldMap = null, $iMax = 0)
  135. {
  136. $aTransitions = array();
  137. $aTransitions[stSTARTING][evBLANK] = array('', stSTARTING);
  138. $aTransitions[stSTARTING][evSEPARATOR] = array('__AddCell', stSTARTING);
  139. $aTransitions[stSTARTING][evNEWLINE] = array('__AddRow', stSTARTING);
  140. $aTransitions[stSTARTING][evTEXTQUAL] = array('', stQUALIFIED);
  141. $aTransitions[stSTARTING][evOTHERCHAR] = array('__AddChar', stRAW);
  142. $aTransitions[stSTARTING][evEND] = array('__AddRow', stSTARTING);
  143. $aTransitions[stRAW][evBLANK] = array('__AddChar', stRAW);
  144. $aTransitions[stRAW][evSEPARATOR] = array('__AddCellTrimmed', stSTARTING);
  145. $aTransitions[stRAW][evNEWLINE] = array('__AddRowTrimmed', stSTARTING);
  146. $aTransitions[stRAW][evTEXTQUAL] = array('__AddChar', stRAW);
  147. $aTransitions[stRAW][evOTHERCHAR] = array('__AddChar', stRAW);
  148. $aTransitions[stRAW][evEND] = array('__AddRowTrimmed', stSTARTING);
  149. $aTransitions[stQUALIFIED][evBLANK] = array('__AddChar', stQUALIFIED);
  150. $aTransitions[stQUALIFIED][evSEPARATOR] = array('__AddChar', stQUALIFIED);
  151. $aTransitions[stQUALIFIED][evNEWLINE] = array('__AddChar', stQUALIFIED);
  152. $aTransitions[stQUALIFIED][evTEXTQUAL] = array('', stESCAPED);
  153. $aTransitions[stQUALIFIED][evOTHERCHAR] = array('__AddChar', stQUALIFIED);
  154. $aTransitions[stQUALIFIED][evEND] = array('__AddRow', stSTARTING);
  155. $aTransitions[stESCAPED][evBLANK] = array('', stESCAPED);
  156. $aTransitions[stESCAPED][evSEPARATOR] = array('__AddCell', stSTARTING);
  157. $aTransitions[stESCAPED][evNEWLINE] = array('__AddRow', stSTARTING);
  158. $aTransitions[stESCAPED][evTEXTQUAL] = array('__AddChar', stQUALIFIED);
  159. $aTransitions[stESCAPED][evOTHERCHAR] = array('__AddChar', stSTARTING);
  160. $aTransitions[stESCAPED][evEND] = array('__AddRow', stSTARTING);
  161. // Reset parser variables
  162. $this->m_sCurrCell = '';
  163. $this->m_aCurrRow = array();
  164. $this->m_iToSkip = $iToSkip;
  165. $this->m_aDataSet = array();
  166. $iDataLength = strlen($this->m_sCSVData);
  167. $iState = stSTARTING;
  168. $iTimeLimit = null;
  169. if ($this->m_iTimeLimitPerRow !== null)
  170. {
  171. // Give some time for the first row
  172. $iTimeLimit = ini_get('max_execution_time');
  173. set_time_limit($this->m_iTimeLimitPerRow);
  174. }
  175. for($i = 0; $i <= $iDataLength ; $i++)
  176. {
  177. if ($i == $iDataLength)
  178. {
  179. $c = null;
  180. $iEvent = evEND;
  181. }
  182. else
  183. {
  184. $c = $this->m_sCSVData[$i];
  185. if ($c == $this->m_sSep)
  186. {
  187. $iEvent = evSEPARATOR;
  188. }
  189. elseif ($c == ' ')
  190. {
  191. $iEvent = evBLANK;
  192. }
  193. elseif ($c == "\t")
  194. {
  195. $iEvent = evBLANK;
  196. }
  197. elseif ($c == "\n")
  198. {
  199. $iEvent = evNEWLINE;
  200. }
  201. elseif ($c == $this->m_sTextQualifier)
  202. {
  203. $iEvent = evTEXTQUAL;
  204. }
  205. else
  206. {
  207. $iEvent = evOTHERCHAR;
  208. }
  209. }
  210. $sAction = $aTransitions[$iState][$iEvent][0];
  211. $iState = $aTransitions[$iState][$iEvent][1];
  212. if (!empty($sAction))
  213. {
  214. $aCallSpec = array($this, $sAction);
  215. if (is_callable($aCallSpec))
  216. {
  217. call_user_func($aCallSpec, $c, $aFieldMap);
  218. }
  219. else
  220. {
  221. throw new CSVParserException("CSVParser: unknown verb '$sAction'");
  222. }
  223. }
  224. $iLineCount = count($this->m_aDataSet);
  225. if (($iMax > 0) && ($iLineCount >= $iMax)) break;
  226. }
  227. if ($iTimeLimit !== null)
  228. {
  229. // Restore the previous time limit
  230. set_time_limit($iTimeLimit);
  231. }
  232. return $this->m_aDataSet;
  233. }
  234. public function ListFields()
  235. {
  236. $aHeader = $this->ToArray(0, null, 1);
  237. return $aHeader[0];
  238. }
  239. }
  240. ?>