#223 Trim spaces in CSV parsing

SVN:trunk[719]
This commit is contained in:
Romain Quetiez
2010-08-30 15:18:14 +00:00
parent 8fc34a9fd5
commit ab808ebf93
3 changed files with 44 additions and 8 deletions

View File

@@ -33,6 +33,7 @@ define('stRAW', 2); //building a non-qualified string
define('stQUALIFIED', 3); //building qualified string define('stQUALIFIED', 3); //building qualified string
define('stESCAPED', 4); //just encountered an escape char define('stESCAPED', 4); //just encountered an escape char
define('evBLANK', 0);
define('evSEPARATOR', 1); define('evSEPARATOR', 1);
define('evNEWLINE', 2); define('evNEWLINE', 2);
define('evTEXTQUAL', 3); // used for escaping as well define('evTEXTQUAL', 3); // used for escaping as well
@@ -70,23 +71,32 @@ class CSVParser
{ {
$this->m_sCurrCell = ''; $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)) if (!is_null($aFieldMap))
{ {
$iNextCol = count($this->m_aCurrRow); $iNextCol = count($this->m_aCurrRow);
$iNextName = $aFieldMap[$iNextCol]; $iNextName = $aFieldMap[$iNextCol];
$this->m_aCurrRow[$iNextName] = $this->m_sCurrCell; $this->m_aCurrRow[$iNextName] = $sCell;
} }
else else
{ {
$this->m_aCurrRow[] = $this->m_sCurrCell; $this->m_aCurrRow[] = $sCell;
} }
$this->m_sCurrCell = ''; $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) if ($this->m_iToSkip > 0)
{ {
@@ -112,26 +122,39 @@ class CSVParser
} }
$this->m_aCurrRow = array(); $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) function ToArray($iToSkip = 1, $aFieldMap = null, $iMax = 0)
{ {
$aTransitions = array(); $aTransitions = array();
$aTransitions[stSTARTING][evBLANK] = array('', stSTARTING);
$aTransitions[stSTARTING][evSEPARATOR] = array('__AddCell', stSTARTING); $aTransitions[stSTARTING][evSEPARATOR] = array('__AddCell', stSTARTING);
$aTransitions[stSTARTING][evNEWLINE] = array('__AddRow', stSTARTING); $aTransitions[stSTARTING][evNEWLINE] = array('__AddRow', stSTARTING);
$aTransitions[stSTARTING][evTEXTQUAL] = array('', stQUALIFIED); $aTransitions[stSTARTING][evTEXTQUAL] = array('', stQUALIFIED);
$aTransitions[stSTARTING][evOTHERCHAR] = array('__AddChar', stRAW); $aTransitions[stSTARTING][evOTHERCHAR] = array('__AddChar', stRAW);
$aTransitions[stRAW][evSEPARATOR] = array('__AddCell', stSTARTING); $aTransitions[stRAW][evBLANK] = array('__AddChar', stRAW);
$aTransitions[stRAW][evNEWLINE] = array('__AddRow', stSTARTING); $aTransitions[stRAW][evSEPARATOR] = array('__AddCellTrimmed', stSTARTING);
$aTransitions[stRAW][evNEWLINE] = array('__AddRowTrimmed', stSTARTING);
$aTransitions[stRAW][evTEXTQUAL] = array('__AddChar', stRAW); $aTransitions[stRAW][evTEXTQUAL] = array('__AddChar', stRAW);
$aTransitions[stRAW][evOTHERCHAR] = array('__AddChar', stRAW); $aTransitions[stRAW][evOTHERCHAR] = array('__AddChar', stRAW);
$aTransitions[stQUALIFIED][evBLANK] = array('__AddChar', stQUALIFIED);
$aTransitions[stQUALIFIED][evSEPARATOR] = array('__AddChar', stQUALIFIED); $aTransitions[stQUALIFIED][evSEPARATOR] = array('__AddChar', stQUALIFIED);
$aTransitions[stQUALIFIED][evNEWLINE] = array('__AddChar', stQUALIFIED); $aTransitions[stQUALIFIED][evNEWLINE] = array('__AddChar', stQUALIFIED);
$aTransitions[stQUALIFIED][evTEXTQUAL] = array('', stESCAPED); $aTransitions[stQUALIFIED][evTEXTQUAL] = array('', stESCAPED);
$aTransitions[stQUALIFIED][evOTHERCHAR] = array('__AddChar', stQUALIFIED); $aTransitions[stQUALIFIED][evOTHERCHAR] = array('__AddChar', stQUALIFIED);
$aTransitions[stESCAPED][evBLANK] = array('', stESCAPED);
$aTransitions[stESCAPED][evSEPARATOR] = array('__AddCell', stSTARTING); $aTransitions[stESCAPED][evSEPARATOR] = array('__AddCell', stSTARTING);
$aTransitions[stESCAPED][evNEWLINE] = array('__AddRow', stSTARTING); $aTransitions[stESCAPED][evNEWLINE] = array('__AddRow', stSTARTING);
$aTransitions[stESCAPED][evTEXTQUAL] = array('__AddChar', stQUALIFIED); $aTransitions[stESCAPED][evTEXTQUAL] = array('__AddChar', stQUALIFIED);
@@ -155,6 +178,14 @@ class CSVParser
{ {
$iEvent = evSEPARATOR; $iEvent = evSEPARATOR;
} }
elseif ($c == ' ')
{
$iEvent = evBLANK;
}
elseif ($c == "\t")
{
$iEvent = evBLANK;
}
elseif ($c == "\n") elseif ($c == "\n")
{ {
$iEvent = evNEWLINE; $iEvent = evNEWLINE;

View File

@@ -41,6 +41,8 @@ require_once('dbobject.class.php');
require_once('dbobjectsearch.class.php'); require_once('dbobjectsearch.class.php');
require_once('dbobjectset.class.php'); require_once('dbobjectset.class.php');
require_once('../application/cmdbabstract.class.inc.php');
require_once('userrights.class.inc.php'); require_once('userrights.class.inc.php');
require_once('../webservices/webservices.class.inc.php'); require_once('../webservices/webservices.class.inc.php');

View File

@@ -237,6 +237,8 @@ a2","b","c"
$sDataFile = '?field1?;?field2?;?field3? $sDataFile = '?field1?;?field2?;?field3?
?a?;?b?;?c? ?a?;?b?;?c?
a;b;c 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('field1', 'field2', 'field3'),
array('a', 'b', 'c'), array('a', 'b', 'c'),
array('a', 'b', 'c'), array('a', 'b', 'c'),
array(' a ', ' b ', ' c '),
array('a', 'b', 'c'),
array('', '', ''), array('', '', ''),
array('', '', ''), array('', '', ''),
array('a"', 'b', 'c'), array('a"', 'b', 'c'),
@@ -263,7 +267,6 @@ a2?;?b?;?c?
array('a1,a2', 'b', 'c'), array('a1,a2', 'b', 'c'),
array('a', 'b', "c1,\",c2\n,c3"), array('a', 'b', "c1,\",c2\n,c3"),
array('a', 'b', 'ouf !'), array('a', 'b', 'ouf !'),
array('a', 'b', 'a'),
); );
$oCSVParser = new CSVParser($sDataFile, ';', '?'); $oCSVParser = new CSVParser($sDataFile, ';', '?');