Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Read CSV Line (Solved)
Read CSV Line (Solved) [message #36171] Tue, 08 May 2012 16:21 Go to previous message
sergeynikitin is currently offline  sergeynikitin
Messages: 748
Registered: January 2008
Location: Moscow, Russia
Contributor

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

 
Read Message
Read Message
Read Message
Read Message
Previous Topic: problem with Format
Next Topic: Jsonize problem with Null
Goto Forum:
  


Current Time: Mon Apr 29 16:59:08 CEST 2024

Total time taken to generate the page: 0.02342 seconds