diff --git a/core/csvparser.class.inc.php b/core/csvparser.class.inc.php index 9c64cd3f4..64cb15e71 100644 --- a/core/csvparser.class.inc.php +++ b/core/csvparser.class.inc.php @@ -33,6 +33,7 @@ define('stRAW', 2); //building a non-qualified string define('stQUALIFIED', 3); //building qualified string define('stESCAPED', 4); //just encountered an escape char +define('evBLANK', 0); define('evSEPARATOR', 1); define('evNEWLINE', 2); define('evTEXTQUAL', 3); // used for escaping as well @@ -70,23 +71,32 @@ class CSVParser { $this->m_sCurrCell = ''; } - protected function __AddCell($c = null, $aFieldMap = null) + protected function __AddCell($c = null, $aFieldMap = null, $bTrimSpaces = false) { + if ($bTrimSpaces) + { + $sCell = trim($this->m_sCurrCell); + } + else + { + $sCell = $this->m_sCurrCell; + } + if (!is_null($aFieldMap)) { $iNextCol = count($this->m_aCurrRow); $iNextName = $aFieldMap[$iNextCol]; - $this->m_aCurrRow[$iNextName] = $this->m_sCurrCell; + $this->m_aCurrRow[$iNextName] = $sCell; } else { - $this->m_aCurrRow[] = $this->m_sCurrCell; + $this->m_aCurrRow[] = $sCell; } $this->m_sCurrCell = ''; } - protected function __AddRow($c = null, $aFieldMap = null) + protected function __AddRow($c = null, $aFieldMap = null, $bTrimSpaces = false) { - $this->__AddCell($c, $aFieldMap); + $this->__AddCell($c, $aFieldMap, $bTrimSpaces); if ($this->m_iToSkip > 0) { @@ -112,26 +122,39 @@ class CSVParser } $this->m_aCurrRow = array(); } + protected function __AddCellTrimmed($c = null, $aFieldMap = null) + { + $this->__AddCell($c, $aFieldMap, true); + } + + protected function __AddRowTrimmed($c = null, $aFieldMap = null) + { + $this->__AddRow($c, $aFieldMap, true); + } function ToArray($iToSkip = 1, $aFieldMap = null, $iMax = 0) { $aTransitions = array(); + $aTransitions[stSTARTING][evBLANK] = array('', stSTARTING); $aTransitions[stSTARTING][evSEPARATOR] = array('__AddCell', stSTARTING); $aTransitions[stSTARTING][evNEWLINE] = array('__AddRow', stSTARTING); $aTransitions[stSTARTING][evTEXTQUAL] = array('', stQUALIFIED); $aTransitions[stSTARTING][evOTHERCHAR] = array('__AddChar', stRAW); - $aTransitions[stRAW][evSEPARATOR] = array('__AddCell', stSTARTING); - $aTransitions[stRAW][evNEWLINE] = array('__AddRow', stSTARTING); + $aTransitions[stRAW][evBLANK] = array('__AddChar', stRAW); + $aTransitions[stRAW][evSEPARATOR] = array('__AddCellTrimmed', stSTARTING); + $aTransitions[stRAW][evNEWLINE] = array('__AddRowTrimmed', stSTARTING); $aTransitions[stRAW][evTEXTQUAL] = array('__AddChar', stRAW); $aTransitions[stRAW][evOTHERCHAR] = array('__AddChar', stRAW); + $aTransitions[stQUALIFIED][evBLANK] = array('__AddChar', stQUALIFIED); $aTransitions[stQUALIFIED][evSEPARATOR] = array('__AddChar', stQUALIFIED); $aTransitions[stQUALIFIED][evNEWLINE] = array('__AddChar', stQUALIFIED); $aTransitions[stQUALIFIED][evTEXTQUAL] = array('', stESCAPED); $aTransitions[stQUALIFIED][evOTHERCHAR] = array('__AddChar', stQUALIFIED); + $aTransitions[stESCAPED][evBLANK] = array('', stESCAPED); $aTransitions[stESCAPED][evSEPARATOR] = array('__AddCell', stSTARTING); $aTransitions[stESCAPED][evNEWLINE] = array('__AddRow', stSTARTING); $aTransitions[stESCAPED][evTEXTQUAL] = array('__AddChar', stQUALIFIED); @@ -155,6 +178,14 @@ class CSVParser { $iEvent = evSEPARATOR; } + elseif ($c == ' ') + { + $iEvent = evBLANK; + } + elseif ($c == "\t") + { + $iEvent = evBLANK; + } elseif ($c == "\n") { $iEvent = evNEWLINE; diff --git a/core/test.class.inc.php b/core/test.class.inc.php index 015eb7877..eaebc80af 100644 --- a/core/test.class.inc.php +++ b/core/test.class.inc.php @@ -41,6 +41,8 @@ require_once('dbobject.class.php'); require_once('dbobjectsearch.class.php'); require_once('dbobjectset.class.php'); +require_once('../application/cmdbabstract.class.inc.php'); + require_once('userrights.class.inc.php'); require_once('../webservices/webservices.class.inc.php'); diff --git a/pages/testlist.inc.php b/pages/testlist.inc.php index 5d53f74aa..c16b92da7 100644 --- a/pages/testlist.inc.php +++ b/pages/testlist.inc.php @@ -237,6 +237,8 @@ a2","b","c" $sDataFile = '?field1?;?field2?;?field3? ?a?;?b?;?c? a;b;c + ? a ? ; ? b ? ; ? c ? + a ; b ; c ??;??;?? ;; ?a"?;?b?;?c? @@ -256,6 +258,8 @@ a2?;?b?;?c? //array('field1', 'field2', 'field3'), array('a', 'b', 'c'), array('a', 'b', 'c'), + array(' a ', ' b ', ' c '), + array('a', 'b', 'c'), array('', '', ''), array('', '', ''), array('a"', 'b', 'c'), @@ -263,7 +267,6 @@ a2?;?b?;?c? array('a1,a2', 'b', 'c'), array('a', 'b', "c1,\",c2\n,c3"), array('a', 'b', 'ouf !'), - array('a', 'b', 'a'), ); $oCSVParser = new CSVParser($sDataFile, ';', '?');