parser needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Giulio

    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(CS tring& strSub, LPCTSTR lpszFullString,
    int iFullStringLen, int iSubString, TCHAR chSep,
    TCHAR chMagic) {
    int iPos, iPosOrig, iStartPos, iEndPos, iNumMagics;
    TCHAR* pcSubString;
    if((lpszFullStr ing == NULL) ||
    (iFullStringLen == 0))
    return FALSE;
    // Find substring begin
    for(iStartPos = 0; (iStartPos < iFullStringLen) && (iSubString > 0);
    iStartPos++)
    {
    // May be separator ?
    if(*(lpszFullSt ring + iStartPos) == chSep)
    {
    if(((iStartPos > 0) &&
    (*(lpszFullStri ng + 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(*(lpszFullSt ring + iEndPos) == chMagic)
    {
    iNumMagics++;
    }
    // May be separator ?
    if(*(lpszFullSt ring + iEndPos) == chSep)
    {
    if(((iEndPos > 0) && (*(lpszFullStri ng + iEndPos - 1)
    != chMagic)) || (iEndPos == 0))
    {
    // Sure it is the end
    break;
    }
    }
    }

    // Copy substring
    pcSubString = strSub.GetBuffe rSetLength(
    iEndPos - iStartPos - iNumMagics);
    iPosOrig = iStartPos;
    iEndPos -= iStartPos;
    if(pcSubString != NULL)
    {
    for(iPos = 0; iPos < iEndPos; iPos += sizeof(TCHAR))
    {
    if(*(lpszFullSt ring + iPosOrig) != chMagic)
    {
    *(pcSubString + iPos) = *(lpszFullStrin g + 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


  • Ron Natalie

    #2
    Re: parser needed


    "Giulio" <giulio.gL.E.V. A@email.IT> wrote in message news:R_kkb.4459 8$Zh.36334@torn ado.fastwebnet. it...[color=blue]
    > 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?
    >[/color]
    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.


    Comment

    • Moonlit

      #3
      Re: parser needed

      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" <giulio.gL.E.V. A@email.IT> wrote in message
      news:R_kkb.4459 8$Zh.36334@torn ado.fastwebnet. it...[color=blue]
      > 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(CS tring& strSub, LPCTSTR lpszFullString,
      > int iFullStringLen, int iSubString, TCHAR chSep,
      > TCHAR chMagic) {
      > int iPos, iPosOrig, iStartPos, iEndPos, iNumMagics;
      > TCHAR* pcSubString;
      > if((lpszFullStr ing == NULL) ||
      > (iFullStringLen == 0))
      > return FALSE;
      > // Find substring begin
      > for(iStartPos = 0; (iStartPos < iFullStringLen) && (iSubString > 0);
      > iStartPos++)
      > {
      > // May be separator ?
      > if(*(lpszFullSt ring + iStartPos) == chSep)
      > {
      > if(((iStartPos > 0) &&
      > (*(lpszFullStri ng + 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(*(lpszFullSt ring + iEndPos) == chMagic)
      > {
      > iNumMagics++;
      > }
      > // May be separator ?
      > if(*(lpszFullSt ring + iEndPos) == chSep)
      > {
      > if(((iEndPos > 0) && (*(lpszFullStri ng + iEndPos - 1)
      > != chMagic)) || (iEndPos == 0))
      > {
      > // Sure it is the end
      > break;
      > }
      > }
      > }
      >
      > // Copy substring
      > pcSubString = strSub.GetBuffe rSetLength(
      > iEndPos - iStartPos - iNumMagics);
      > iPosOrig = iStartPos;
      > iEndPos -= iStartPos;
      > if(pcSubString != NULL)
      > {
      > for(iPos = 0; iPos < iEndPos; iPos += sizeof(TCHAR))
      > {
      > if(*(lpszFullSt ring + iPosOrig) != chMagic)
      > {
      > *(pcSubString + iPos) = *(lpszFullStrin g + 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
      >
      >[/color]


      Comment

      • Bob Smith

        #4
        Re: parser needed



        Giulio wrote:
        [color=blue]
        > I've got to take some data from a CSV file, so i need a good string parser[/color]

        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

        Comment

        Working...