Home » U++ Library support » U++ Core » Read CSV Line (Solved)
Read CSV Line (Solved) [message #36171] |
Tue, 08 May 2012 16:21 |
|
I was looking for how to read CSV line, so to correctly handle the case with the double quotes, the lack of quotation marks (two close delimiter) or double quotes within quotes.
Perhaps what I found to be useful to someone.
Vector<String> CsvlinePopulate(String line, String delimiter)
{
Vector<String> record;
int linepos=0;
bool inquotes=false;
String c;
//int i;
int linemax=line.GetLength();
String curstring;
record.Clear();
while(linepos < linemax){
c = line.Mid(linepos,1);
if (!inquotes && curstring.GetLength()==0 && c=="\"") {
//beginquotechar
inquotes=true;
} else if (inquotes && c=="\"") {
//quotechar
if ( (linepos+1 <linemax) && (line.Mid(linepos+1,1)=="\"") ) {
//encountered 2 double quotes in a row (resolves to 1 double quote)
curstring.Cat() << c;
linepos++;
} else {
//endquotechar
inquotes=false;
}
} else if (!inquotes && c==delimiter) {
//end of field
record.Add( curstring );
curstring="";
} else if (!inquotes && (c=="\r" || c=="\n") ) {
record.Add( curstring );
return record;
} else {
curstring.Cat() << c;
}
linepos++;
}
record.Add( curstring );
return record;
}
Just found a variant of Mirek:
http://www.ultimatepp.org/forum/index.php?t=msg&goto=336 79&&srch=csv#msg_33679
SergeyNikitin<U++>( linux, wine )
{
under( Ubuntu || Debian || Raspbian );
}
[Updated on: Tue, 08 May 2012 16:23] Report message to a moderator
|
|
|
|
|
|
Goto Forum:
Current Time: Sat May 04 15:44:17 CEST 2024
Total time taken to generate the page: 0.02585 seconds
|