Bläddra i källkod

#223 Trim spaces in CSV parsing

git-svn-id: http://svn.code.sf.net/p/itop/code/trunk@719 a333f486-631f-4898-b8df-5754b55c2be0
romainq 14 år sedan
förälder
incheckning
05af66fbd2
3 ändrade filer med 44 tillägg och 8 borttagningar
  1. 38 7
      core/csvparser.class.inc.php
  2. 2 0
      core/test.class.inc.php
  3. 4 1
      pages/testlist.inc.php

+ 38 - 7
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;

+ 2 - 0
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');

+ 4 - 1
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, ';', '?');