From 8926e64bc285cca10e231ad91d9267df75f5615e Mon Sep 17 00:00:00 2001 From: Romain Quetiez Date: Sun, 5 Sep 2010 07:02:13 +0000 Subject: [PATCH] #223 Trim spaces on CSV import was not handling well the last line SVN:trunk[760] --- core/csvparser.class.inc.php | 63 ++++++++++++++++++++---------------- core/test.class.inc.php | 24 +++++++------- pages/testlist.inc.php | 17 ++-------- 3 files changed, 50 insertions(+), 54 deletions(-) diff --git a/core/csvparser.class.inc.php b/core/csvparser.class.inc.php index 64cb15e71..75b7b16a9 100644 --- a/core/csvparser.class.inc.php +++ b/core/csvparser.class.inc.php @@ -38,6 +38,7 @@ define('evSEPARATOR', 1); define('evNEWLINE', 2); define('evTEXTQUAL', 3); // used for escaping as well define('evOTHERCHAR', 4); +define('evEND', 5); /** @@ -141,24 +142,28 @@ class CSVParser $aTransitions[stSTARTING][evNEWLINE] = array('__AddRow', stSTARTING); $aTransitions[stSTARTING][evTEXTQUAL] = array('', stQUALIFIED); $aTransitions[stSTARTING][evOTHERCHAR] = array('__AddChar', stRAW); + $aTransitions[stSTARTING][evEND] = 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[stRAW][evEND] = array('__AddRowTrimmed', stSTARTING); $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[stQUALIFIED][evEND] = array('__AddRow', stSTARTING); $aTransitions[stESCAPED][evBLANK] = array('', stESCAPED); $aTransitions[stESCAPED][evSEPARATOR] = array('__AddCell', stSTARTING); $aTransitions[stESCAPED][evNEWLINE] = array('__AddRow', stSTARTING); $aTransitions[stESCAPED][evTEXTQUAL] = array('__AddChar', stQUALIFIED); $aTransitions[stESCAPED][evOTHERCHAR] = array('__AddChar', stSTARTING); + $aTransitions[stESCAPED][evEND] = array('__AddRow', stSTARTING); // Reset parser variables $this->m_sCurrCell = ''; @@ -166,37 +171,43 @@ class CSVParser $this->m_iToSkip = $iToSkip; $this->m_aDataSet = array(); + $iDataLength = strlen($this->m_sCSVData); + $iState = stSTARTING; - for($i = 0; $i < strlen($this->m_sCSVData) ; $i++) + for($i = 0; $i <= $iDataLength ; $i++) { - $c = $this->m_sCSVData[$i]; - -// // Note: I did that because the unit test was not working fine (file edited with notepad: \n chars padded :-( -// if (ord($c) == 0) continue; - - if ($c == $this->m_sSep) + if ($i == $iDataLength) { - $iEvent = evSEPARATOR; - } - elseif ($c == ' ') - { - $iEvent = evBLANK; - } - elseif ($c == "\t") - { - $iEvent = evBLANK; - } - elseif ($c == "\n") - { - $iEvent = evNEWLINE; - } - elseif ($c == $this->m_sTextQualifier) - { - $iEvent = evTEXTQUAL; + $iEvent = evEND; } else { - $iEvent = evOTHERCHAR; + $c = $this->m_sCSVData[$i]; + + if ($c == $this->m_sSep) + { + $iEvent = evSEPARATOR; + } + elseif ($c == ' ') + { + $iEvent = evBLANK; + } + elseif ($c == "\t") + { + $iEvent = evBLANK; + } + elseif ($c == "\n") + { + $iEvent = evNEWLINE; + } + elseif ($c == $this->m_sTextQualifier) + { + $iEvent = evTEXTQUAL; + } + else + { + $iEvent = evOTHERCHAR; + } } $sAction = $aTransitions[$iState][$iEvent][0]; @@ -218,8 +229,6 @@ class CSVParser $iLineCount = count($this->m_aDataSet); if (($iMax > 0) && ($iLineCount >= $iMax)) break; } - // Close the final line - $this->__AddRow(null, $aFieldMap); return $this->m_aDataSet; } diff --git a/core/test.class.inc.php b/core/test.class.inc.php index b716ad086..b8baa4ebf 100644 --- a/core/test.class.inc.php +++ b/core/test.class.inc.php @@ -88,8 +88,8 @@ abstract class TestHandler $this->m_aErrors = array(); } - abstract static public function GetName(); - abstract static public function GetDescription(); + static public function GetName() {return "fooname";} + static public function GetDescription(){return "foodesc";} protected function DoPrepare() {return true;} abstract protected function DoExecute(); @@ -278,8 +278,8 @@ abstract class TestSoapWebService extends TestHandler */ abstract class TestFunctionInOut extends TestFunction { - abstract static public function GetCallSpec(); // parameters to call_user_func - abstract static public function GetInOut(); // array of input => output +// abstract static public function GetCallSpec(); // parameters to call_user_func +// abstract static public function GetInOut(); // array of input => output protected function DoExecute() { @@ -316,9 +316,9 @@ abstract class TestFunctionInOut extends TestFunction */ abstract class TestUrl extends TestHandler { - abstract static public function GetUrl(); - abstract static public function GetErrorKeywords(); - abstract static public function GetWarningKeywords(); +// abstract static public function GetUrl(); +// abstract static public function GetErrorKeywords(); +// abstract static public function GetWarningKeywords(); protected function DoExecute() { @@ -348,10 +348,10 @@ abstract class TestUserRights extends TestHandler */ abstract class TestScenarioOnDB extends TestHandler { - abstract static public function GetDBHost(); - abstract static public function GetDBUser(); - abstract static public function GetDBPwd(); - abstract static public function GetDBName(); +// abstract static public function GetDBHost(); +// abstract static public function GetDBUser(); +// abstract static public function GetDBPwd(); +// abstract static public function GetDBName(); protected function DoPrepare() { @@ -385,7 +385,7 @@ abstract class TestBizModel extends TestHandler { // abstract static public function GetDBSubName(); // abstract static public function GetBusinessModelFile(); - abstract static public function GetConfigFile(); +// abstract static public function GetConfigFile(); protected function DoPrepare() { diff --git a/pages/testlist.inc.php b/pages/testlist.inc.php index c16b92da7..d263dd064 100644 --- a/pages/testlist.inc.php +++ b/pages/testlist.inc.php @@ -220,20 +220,6 @@ class TestCSVParser extends TestFunction public function DoExecute() { - $sDataFile = '"field1","field2","field3" -"a","b","c" -a,b,c -"","","" -,, -"a""","b","c" -"a1 -a2","b","c" -"a1,a2","b","c" -"a","b","c1,"",c2 -,c3" -"a","b","ouf !" -'; - $sDataFile = '?field1?;?field2?;?field3? ?a?;?b?;?c? a;b;c @@ -248,7 +234,7 @@ a2?;?b?;?c? ?a?;?b?;?c1,",c2 ,c3? ?a?;?b?;?ouf !? -'; + Espace sur la fin ; 1234; e@taloc.com '; echo "
\n";
 		print_r($sDataFile);
@@ -267,6 +253,7 @@ a2?;?b?;?c?
 			array('a1,a2', 'b', 'c'),
 			array('a', 'b', "c1,\",c2\n,c3"),
 			array('a', 'b', 'ouf !'),
+			array('Espace sur la fin', '1234', 'e@taloc.com'),
 		);
 	
 		$oCSVParser = new CSVParser($sDataFile, ';', '?');