parser needed

G

Giulio

I've got to take some data from a CSV file, so i need a good string parser
to recognize data to store in variables.
using google I found the code below.
that seems to be a good solution but I had some problems using it, which
libraries should I include?

Can someone suggest me any other libraries to do what I need?

thanx alot
Giulio

--------------------------
//// Get substring, chMagic is a character which
// makes chSep act as a normal character.
// Return TRUE on success.
inline bool GetSubString(CString& strSub, LPCTSTR lpszFullString,
int iFullStringLen, int iSubString, TCHAR chSep,
TCHAR chMagic) {
int iPos, iPosOrig, iStartPos, iEndPos, iNumMagics;
TCHAR* pcSubString;
if((lpszFullString == NULL) ||
(iFullStringLen == 0))
return FALSE;
// Find substring begin
for(iStartPos = 0; (iStartPos < iFullStringLen) && (iSubString > 0);
iStartPos++)
{
// May be separator ?
if(*(lpszFullString + iStartPos) == chSep)
{
if(((iStartPos > 0) &&
(*(lpszFullString + iStartPos - 1) != chMagic))
|| (iStartPos == 0))
{
// Sure it is a separator!
iSubString--;
}
}
}

// Return empty string when nothing found
if(iSubString > 0)
{
strSub.Empty();
return FALSE;
}

// Find substring end
iNumMagics = 0;
for(iEndPos = iStartPos;
iEndPos < iFullStringLen;
iEndPos++)
{
// Count magics
if(*(lpszFullString + iEndPos) == chMagic)
{
iNumMagics++;
}
// May be separator ?
if(*(lpszFullString + iEndPos) == chSep)
{
if(((iEndPos > 0) && (*(lpszFullString + iEndPos - 1)
!= chMagic)) || (iEndPos == 0))
{
// Sure it is the end
break;
}
}
}

// Copy substring
pcSubString = strSub.GetBufferSetLength(
iEndPos - iStartPos - iNumMagics);
iPosOrig = iStartPos;
iEndPos -= iStartPos;
if(pcSubString != NULL)
{
for(iPos = 0; iPos < iEndPos; iPos += sizeof(TCHAR))
{
if(*(lpszFullString + iPosOrig) != chMagic)
{
*(pcSubString + iPos) = *(lpszFullString + iPosOrig);
}
else
{
iPos -= sizeof(TCHAR);
iEndPos -= sizeof(TCHAR);
}
iPosOrig += sizeof(TCHAR);
}
*(pcSubString + iPos) = 0;
return TRUE;
}
return FALSE;
};
 
R

Ron Natalie

Giulio said:
I've got to take some data from a CSV file, so i need a good string parser
to recognize data to store in variables.
using google I found the code below.
that seems to be a good solution but I had some problems using it, which
libraries should I include?
It's all written to use the MFC CString class and the stupid
windows type names. If you're not using Visual Studio, you're
probably hosed, keep looking.
 
M

Moonlit

Hi,

Try http://spirit.sourceforge.net/ spirit to parse the file.

Since it uses templates there is no need to compile against a library. If it
is a simple file (like csv) you can use a relatively simple parser (without
creating a ast).

Regards, Ron AF Greve.
 
B

Bob Smith

Giulio said:
I've got to take some data from a CSV file, so i need a good string parser

the best parser you will get is a lex / yacc generated parser/lexer
If you intend to do lots of work with th eparser I suggest you to look
at those two tools, they are excellent tools for generating parsers.
/B
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top