csvparser.class.inc.php 6.5 KB

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