Purpose: Splits a line from a .csv file into it's fields. Copes with quoted values automatically, removes double-quote pairs if present. Note this function does not de-escapes sequences.
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | SplitCSV | (char**,char*,uint32_t,char,) |
Declared in file: hzTextproc.h
Defined in file : hzTextproc.cpp
Function Logic:
Function body:
hzEcode SplitCSV (char** ar)char* line, uint32_t arSize, char cDelim,
{
// Category: Text Processing
//
// Purpose: Splits a line from a .csv file into it's fields. Copes with quoted values automatically, removes double-quote pairs
// if present. Note this function does not de-escapes sequences.
//
// Arguments: 1) ar Either an array of char* or an array of hzString
// 2) line The line to be split (char*)
// 3) cDelim Delimitor char
//
// Returns: E_ARGUMENT If either the target array or the line is not supplied or the array size is 0
// E_FORMAT If the number of fields does not match expected number
// E_OK If successful
char* i ; // Input iterator
uint32_t nPos ; // Position within array
if (!ar || !arSize)
return E_ARGUMENT ;
nPos = 0;
if (!line || !line[0])
return E_ARGUMENT ;
if (!cDelim || cDelim == CHAR_DQUOTE)
cDelim = CHAR_COMMA ;
for (i = line ; *i && nPos < arSize ; i++)
{
if (*i == CHAR_DQUOTE)
{
// Handle quoted CSV entry
i++ ;
ar[nPos++] = i ;
for (; *i ; i++)
{
if (*i == CHAR_DQUOTE)
{ *i++ = 0; break ; }
}
// Expect delimiter
if (*i && *i != cDelim)
return E_FORMAT ;
continue ;
}
// Handle unquoted CSV entry
ar[nPos++] = i ;
for (; *i ; i++)
{
if (*i == cDelim)
{ i++ ; break ; }
}
}
return E_OK ;
}