My Gscanner Problem...



I would like to parse a txt data, and there is "@data" keyword in it.
What i wanna do is take the number after the keyword ,and change it into a hex number.
I cannot get the data after the "@data" keyword so far...
:(

[code]
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include <libgen.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <glib.h>

/////////////////////////////////////////////////////////////
  char DATA[50]="";
/////////////////////////////////////////////////////////////
  
/**
 * Prints token as seen by GScanner. This function allows one to
 * play with configuration parameters and observe how they impact
 * the scanner's behavior.
 */
void
parse_file (const char *filename) {
    GScanner  *scanner;
    GTokenType ttype;
    GScannerConfig config = {
        " \t\r\n",                     /* characters to skip */
        G_CSET_a_2_z "@" G_CSET_A_2_Z G_CSET_DIGITS, /* identifier start */
        G_CSET_a_2_z "_" "@" G_CSET_A_2_Z G_CSET_DIGITS, /* identifier cont. */
        "#\n",                         /* single line comment */
        FALSE,                         /* case_sensitive */
        TRUE,                          /* skip multi-line comments */
        TRUE,                          /* skip single line comments */
        FALSE,                         /* scan multi-line comments */
        TRUE,                          /* scan identifiers */
        FALSE,                         /* scan 1-char identifiers */
        FALSE,                         /* scan NULL identifiers */
        FALSE,                         /* scan symbols */
        FALSE,                         /* scan binary */
        TRUE,                          /* scan octal */
        TRUE,                          /* scan float */
        TRUE,                          /* scan hex */
        FALSE,                         /* scan hex dollar */
        TRUE,                          /* scan single quote strings */
        TRUE,                          /* scan double quite strings */
        TRUE,                          /* numbers to int */
        FALSE,                         /* int to float */
        FALSE,                         /* identifier to string */
        FALSE,                         /* char to token */
        FALSE,                         /* symbol to token */
        FALSE,                         /* scope 0 fallback */
        FALSE                          /* store int64 */
    };
    
    /* first open the file; bail if not successful */
    int fd = open (filename, O_RDONLY);
    
    if (fd < 0) {
        printf ("Failed to open file \"%s\". Skipping ...\n", filename);
        return;
    }
    
    printf ("Parsing file \"%s\":\n", filename);
    
    scanner = g_scanner_new (&config);
    g_scanner_input_file (scanner, fd);
    ttype=g_scanner_get_next_token (scanner);

    while (ttype != G_TOKEN_EOF ) {

        if( ttype!=G_TOKEN_IDENTIFIER ) // && ttype!=G_TOKEN_INT )
        {
            ttype=g_scanner_get_next_token(scanner);
            continue;
        }
        else
        {
            printf("identifier is=%s\n",scanner->value.v_identifier);    
        }
        
                if(strcmp(scanner->value.v_identifier,"@data")==0)
                {
                    ttype=g_scanner_get_next_token(scanner);
                    printf("@data is=0x%x\n",scanner->value.v_int);
                    
                        sprintf(DATA,"0x%x",scanner->value.v_int);
                        
                        printf("\n\n\ndata=0x%x\n",DATA);      
                }

        ttype=g_scanner_get_next_token(scanner);
    
    }
    /* clean up */
    g_scanner_destroy (scanner);
    close (fd);
}

/**
 * A simple program that illustrates the use of GLib
 * lexical scanner. 
 */
int 
main (int argc, char **argv) {
 
    for (int i = 1; i < argc; ++ i) {
        parse_file (argv[i]);
    }
    return (EXIT_SUCCESS);
}

[/code]

parsed txt file contains:
[code]
/*---------------------------------------------------------------------
* @fn function1
*
* @brief this is function1
* @data 0x1
*----------------------------------------------------------------------*/
/*---------------------------------------------------------------------
* @fn function2
*
* @brief this is function2
* @data 2
*----------------------------------------------------------------------*/
/*---------------------------------------------------------------------
* @fn function3
*
* @brief this is function3
* @data 0x123
*----------------------------------------------------------------------*/
/*---------------------------------------------------------------------
* @fn function4
*
* @brief this is function4
* @data 123
*----------------------------------------------------------------------*/
[/code]


Could someone give me a hand?
many thanks~




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]