#223 Trim spaces on CSV import was not handling well the last line

SVN:trunk[760]
This commit is contained in:
Romain Quetiez
2010-09-05 07:02:13 +00:00
parent 81cd840604
commit 8926e64bc2
3 changed files with 50 additions and 54 deletions

View File

@@ -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;
}

View File

@@ -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()
{

View File

@@ -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 "<pre style=\"font-style:italic;\">\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, ';', '?');