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 next 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

Re: Read CSV Line (Solved) [message #36172 is a reply to message #36171] Tue, 08 May 2012 16:27 Go to previous messageGo to next message
sergeynikitin is currently offline  sergeynikitin
Messages: 748
Registered: January 2008
Location: Moscow, Russia
Contributor

May be include best variant of this function (or class?) in U++?

SergeyNikitin<U++>( linux, wine )
{
    under( Ubuntu || Debian || Raspbian );
}
Re: Read CSV Line (Solved) [message #36175 is a reply to message #36172] Wed, 09 May 2012 08:25 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
sergeynikitin wrote on Tue, 08 May 2012 10:27

May be include best variant of this function (or class?) in U++?


GetCsvLine now in Core.
Re: Read CSV Line (Solved) [message #36176 is a reply to message #36175] Wed, 09 May 2012 09:55 Go to previous message
sergeynikitin is currently offline  sergeynikitin
Messages: 748
Registered: January 2008
Location: Moscow, Russia
Contributor

Smile

Surprised by the speed!


SergeyNikitin<U++>( linux, wine )
{
    under( Ubuntu || Debian || Raspbian );
}

[Updated on: Wed, 09 May 2012 09:57]

Report message to a moderator

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


Current Time: Fri Mar 29 13:38:30 CET 2024

Total time taken to generate the page: 0.01591 seconds