473,324 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

parser needed

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;
};
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 09/10/2003
Jul 19 '05 #1
3 2416

"Giulio" <gi*************@email.IT> wrote in message news:R_******************@tornado.fastwebnet.it...
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.
Jul 19 '05 #2
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.

"Giulio" <gi*************@email.IT> wrote in message
news:R_******************@tornado.fastwebnet.it...
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;
};
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 09/10/2003

Jul 19 '05 #3


Giulio wrote:
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

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Adrian | last post by:
Hello everyone, I am quite new when it comes to things XML. I realize that a parser is in fact needed for the xml, but I am not quite sure what I need. I do know how to use apache, mysql, php...
16
by: Mike | last post by:
Does anyone know of a minimal/mini/tiny/small xml parser in c? I'm looking for something small that accepts a stream or string, builds a c structure, and then returns an opaque pointer to that...
6
by: Jan Danielsson | last post by:
Hello all, I guess this is a question for people who have written a parser. Does an XML parser ever need to be recursive? I mean like: &fo&bar;o; I know this particular example is in the...
6
by: Angelic Devil | last post by:
I'm building a file parser but I have a problem I'm not sure how to solve. The files this will parse have the potential to be huge (multiple GBs). There are distinct sections of the file that I...
3
by: cr88192 | last post by:
for various reasons, I added an imo ugly hack to my xml parser. basically, I wanted the ability to have binary payload within the xml parse trees. this was partly because I came up with a binary...
32
by: Weiguang Shi | last post by:
Hi, Is there a tool that, given a struct definition, generates a function that parses binary data of this struct and a command that can be used to construct binary data according to...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
18
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: ...
5
by: PatlaDJ | last post by:
Java SAX parser, please need a clue how to get the raw XML code of the currently parsing event... needed for logging, debugging purposes. Here's and example, letting me clarify exactly what i...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.