Using text file with GScanner



Hi, I am new to GTK+ and I am using it to develop a little GUI app to
download some data through the parallel port of a pc.
I am using the WIN 32 libraries with Visual C++ 6.0 and so far so good =
for
the GUI part.
Now I am trying to use the GScanner from GLIB  to parse my data and it =
is
working fine if I use a variable containing the text to parse.
The problem I have it's when I try to set-up the scanner to use a text =
file
This what I have done and I am getting the following message " test =
text:1:
error: unexpected end of file, expected symbol "
Can someone help me?

Thank you!

St=E9phan Meszaros
E-mail: meszaros@cae.ca

scanner_test.c
/* This code is based on the example from the FAQ section 8 */
/* define enumeration values to be returned for specific symbols */
enum {
  SYMBOL_MODULE =3D G_TOKEN_LAST + 1,
  SYMBOL_COMPONENT =3D G_TOKEN_LAST + 2,
  SYMBOL_START_ADDRESS =3D G_TOKEN_LAST + 3,
  SYMBOL_DATA =3D G_TOKEN_LAST + 4,
};

/* symbol array */
static const struct {
  gchar *symbol_name;
  guint  symbol_token;
} symbols[] =3D {
  { "module", SYMBOL_MODULE, },
  { "component", SYMBOL_COMPONENT, },
  { "start_address", SYMBOL_START_ADDRESS, },
  { "data", SYMBOL_DATA, },
  { NULL, 0, },
}, *symbol_p =3D symbols;

int
main (int argc, char *argv[])
{
  GScanner *scanner;
  guint expected_token;
  gint fh;
  FILE *stream;
  char inbuf[128];
  gint count =3D 0;

  /* Open a file handle. */
  fh =3D _open( "parse_it.txt", _O_TEXT );
  scanner =3D g_scanner_new (NULL);
  g_scanner_input_file(scanner,fh);
  g_scanner_sync_file_offset(scanner);
  scanner->config->symbol_2_token =3D TRUE;
	 =20
   /* load symbols into the scanner */
  while (symbol_p->symbol_name)
  {
	  g_scanner_add_symbol (scanner,
                            symbol_p->symbol_name,
                            GINT_TO_POINTER (symbol_p->symbol_token));
      symbol_p++;
  }

  /* feed in the text */
  //g_scanner_input_text (scanner, test_text, strlen (test_text));

  /* give the error handler an idea on how the input is named */
  scanner->input_name =3D "test text";

  /* scanning loop, we parse the input untill it's end is reached,
   * the scanner encountered a lexing error, or our sub routine came
   * across invalid syntax
   */
  do
    {
      /*parse_symbol is my parsing function*/
      expected_token =3D parse_symbol (scanner);
     =20
      g_scanner_peek_next_token (scanner);
    }
  while (expected_token =3D=3D G_TOKEN_NONE &&
         scanner->next_token !=3D G_TOKEN_EOF &&
         scanner->next_token !=3D G_TOKEN_ERROR);

  /* give an error message upon syntax errors */
  if (expected_token !=3D G_TOKEN_NONE)
    g_scanner_unexp_token (scanner, expected_token, NULL, "symbol", =
NULL,
NULL, TRUE);

  /* finsish parsing */
  g_scanner_destroy (scanner);
  _close( fh );

  return 0;
}



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