Add config.h support : will fix the LARGEFILE problem
[lttv.git] / ltt / branches / poly / ltt / parser.c
index 6b0fbd621a3b16b8b3b74881bfba084d1d7b6461..8e409aa927736ac2f9334a58d14e55e23f928626 100644 (file)
@@ -4,7 +4,9 @@ parser.c: Generate helper declarations and functions to trace events
   from an event description file.
 
 Copyright (C) 2002, Xianxiu Yang
-Copyright (C) 2002, Michel Dagenais 
+Copyright (C) 2002, Michel Dagenais
+Copyright (C) 2005, Mathieu Desnoyers
+
 This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 the Free Software Foundation; version 2 of the License.
@@ -32,16 +34,21 @@ This program is distributed in the hope that it will be useful,
    all types is maintained to facilitate the freeing of all type 
    information when the processing of an ".xml" file is finished. */
 
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include <stdlib.h> 
 #include <string.h>
 #include <stdio.h>
 #include <stdarg.h>
-#include <linux/errno.h>  
+#include <ctype.h>
+#include <linux/errno.h>
+#include <glib.h>
 
 
 #include "parser.h"
 
-
 /*****************************************************************************
  *Function name
  *    getSize    : translate from string to integer
@@ -53,19 +60,19 @@ This program is distributed in the hope that it will be useful,
 
 int getSize(parse_file *in)
 {
-  char *token;
+  gchar *token;
 
   token = getToken(in);
   if(in->type == NUMBER) {
-    if(strcmp(token,"1") == 0) return 0;
-    else if(strcmp(token,"2") == 0) return 1;
-    else if(strcmp(token,"4") == 0) return 2;
-    else if(strcmp(token,"8") == 0) return 3;
+    if(g_ascii_strcasecmp(token,"1") == 0) return 0;
+    else if(g_ascii_strcasecmp(token,"2") == 0) return 1;
+    else if(g_ascii_strcasecmp(token,"4") == 0) return 2;
+    else if(g_ascii_strcasecmp(token,"8") == 0) return 3;
   }
   else if(in->type == NAME) {
-    if(strcmp(token,"short") == 0) return 4;
-    else if(strcmp(token,"medium") == 0) return 5;
-    else if(strcmp(token,"long") == 0) return 6;
+    if(g_ascii_strcasecmp(token,"short") == 0) return 4;
+    else if(g_ascii_strcasecmp(token,"medium") == 0) return 5;
+    else if(g_ascii_strcasecmp(token,"long") == 0) return 6;
   }
   in->error(in,"incorrect size specification");
   return -1;
@@ -82,49 +89,9 @@ int getSize(parse_file *in)
 void error_callback(parse_file *in, char *msg)
 {
   if(in)
-    printf("Error in file %s, line %d: %s\n", in->name, in->lineno, msg);
+    g_printf("Error in file %s, line %d: %s\n", in->name, in->lineno, msg);
   else
     printf("%s\n",msg);
-  exit(1);
-}
-
-/*****************************************************************************
- *Function name
- *    memAlloc  : allocate memory                    
- *Input params 
- *    size      : required memory size               
- *return value 
- *    void *    : pointer to allocate memory or NULL 
- ****************************************************************************/
-
-void * memAlloc(int size)
-{
-  void * addr;
-  if(size == 0) return NULL;
-  addr = malloc(size);
-  if(!addr){
-    printf("Failed to allocate memory");    
-    exit(1);
-  }
-  return addr;   
-}
-
-/*****************************************************************************
- *Function name
- *    allocAndCopy : allocate memory and initialize it  
- *Input params 
- *    str          : string to be put in memory         
- *return value 
- *    char *       : pointer to allocate memory or NULL
- ****************************************************************************/
-
-char *allocAndCopy(char *str)
-{
-  char * addr;
-  if(str == NULL) return NULL;
-  addr = (char *)memAlloc(strlen(str)+1);
-  strcpy(addr,str);
-  return addr;
 }
 
 /**************************************************************************
@@ -142,16 +109,20 @@ char *allocAndCopy(char *str)
  *
  **************************************************************************/
 
-char * getNameAttribute(parse_file *in)
+gchar * getNameAttribute(parse_file *in)
 {
-  char * token, car;
+  gchar * token;
+  gunichar car;
+  GIOStatus status;
+  
   token = getName(in);
-  if(strcmp("name",token))in->error(in,"name was expected");
+  if(g_ascii_strcasecmp("name",token))in->error(in,"name was expected");
   getEqual(in);
   
-  car = seekNextChar(in);
-  if(car == EOF)in->error(in,"name was expected");
-  else if(car == '\"')token = getQuotedString(in);
+  status = seekNextChar(in, &car);
+  if(status == G_IO_STATUS_EOF || status == G_IO_STATUS_ERROR)
+    in->error(in,"name was expected");
+  else if(car == '\"') token = getQuotedString(in);
   else token = getName(in);
   return token;
 }
@@ -162,12 +133,12 @@ char * getFormatAttribute(parse_file *in)
 
   //format is an option
   token = getToken(in); 
-  if(strcmp("/",token) == 0 || strcmp(">",token) == 0){
+  if(g_ascii_strcasecmp("/",token) == 0 || g_ascii_strcasecmp(">",token) == 0){
     ungetToken(in);
     return NULL;
   }
 
-  if(strcmp("format",token))in->error(in,"format was expected");
+  if(g_ascii_strcasecmp("format",token))in->error(in,"format was expected");
   getEqual(in);
   token = getQuotedString(in);
   return token;
@@ -175,7 +146,7 @@ char * getFormatAttribute(parse_file *in)
 
 int getSizeAttribute(parse_file *in)
 {
-  char * token;
+  /* skip name and equal */
   getName(in);
   getEqual(in);
 
@@ -184,7 +155,7 @@ int getSizeAttribute(parse_file *in)
 
 int getValueAttribute(parse_file *in)
 {
-  char * token;
+  /* skip name and equal */
   getName(in);
   getEqual(in);
   
@@ -197,12 +168,12 @@ char * getValueStrAttribute(parse_file *in)
   char * token;
 
   token = getToken(in); 
-  if(strcmp("/",token) == 0){
+  if(g_ascii_strcasecmp("/",token) == 0){
     ungetToken(in);
     return NULL;
   }
   
-  if(strcmp("value",token))in->error(in,"value was expected");
+  if(g_ascii_strcasecmp("value",token))in->error(in,"value was expected");
   getEqual(in);
   token = getToken(in);
   if(in->type != NUMBER) in->error(in,"number was expected");
@@ -211,22 +182,35 @@ char * getValueStrAttribute(parse_file *in)
 
 char * getDescription(parse_file *in)
 {
-  long int pos;
-  char * token, car, *str;
+  gint64 pos;
+  gchar * token, *str;
+  gunichar car;
+  GError * error = NULL;
 
-  pos = ftell(in->fp);
+  pos = in->pos;
 
   getLAnglebracket(in);
   token = getName(in);
-  if(strcmp("description",token)){
-    fseek(in->fp, pos, SEEK_SET);
+  if(g_ascii_strcasecmp("description",token)){
+    g_io_channel_seek_position(in->channel, pos-(in->pos), G_SEEK_CUR, &error);
+    if(error != NULL) {
+      g_warning("Can not seek file: \n%s\n", error->message);
+      g_error_free(error);
+    } else in->pos = pos;
+
     return NULL;
   }
   
   getRAnglebracket(in);
 
   pos = 0;
-  while((car = getc(in->fp)) != EOF) {
+  while((g_io_channel_read_unichar(in->channel, &car, &error))
+            != G_IO_STATUS_EOF) {
+    if(error != NULL) {
+      g_warning("Can not seek file: \n%s\n", error->message);
+      g_error_free(error);
+    } else in->pos++;
+
     if(car == '<') break;
     if(car == '\0') continue;
     in->buffer[pos] = car;
@@ -235,11 +219,11 @@ char * getDescription(parse_file *in)
   if(car == EOF)in->error(in,"not a valid description");
   in->buffer[pos] = '\0';
 
-  str = allocAndCopy(in->buffer);
+  str = g_strdup(in->buffer);
 
   getForwardslash(in);
   token = getName(in);
-  if(strcmp("description", token))in->error(in,"not a valid description");
+  if(g_ascii_strcasecmp("description", token))in->error(in,"not a valid description");
   getRAnglebracket(in);
 
   return str;
@@ -255,15 +239,15 @@ char * getDescription(parse_file *in)
  *    fac           : facility filled with event list
  ****************************************************************************/
 
-void parseFacility(parse_file *in, facility * fac)
+void parseFacility(parse_file *in, facility_t * fac)
 {
   char * token;
-  event *ev;
+  event_t *ev;
   
-  fac->name = allocAndCopy(getNameAttribute(in));    
+  fac->name = g_strdup(getNameAttribute(in));    
   getRAnglebracket(in);    
   
-  fac->description = allocAndCopy(getDescription(in));
+  fac->description = getDescription(in);
   
   while(1){
     getLAnglebracket(in);    
@@ -272,11 +256,11 @@ void parseFacility(parse_file *in, facility * fac)
     if(in->type == ENDFILE)
       in->error(in,"the definition of the facility is not finished");
 
-    if(strcmp("event",token) == 0){
-      ev = (event*) memAlloc(sizeof(event));
+    if(g_ascii_strcasecmp("event",token) == 0){
+      ev = (event_t*) g_new(event_t,1);
       sequence_push(&(fac->events),ev);
       parseEvent(in,ev, &(fac->unnamed_types), &(fac->named_types));    
-    }else if(strcmp("type",token) == 0){
+    }else if(g_ascii_strcasecmp("type",token) == 0){
       parseTypeDefinition(in, &(fac->unnamed_types), &(fac->named_types));
     }else if(in->type == FORWARDSLASH){
       break;
@@ -284,7 +268,7 @@ void parseFacility(parse_file *in, facility * fac)
   }
 
   token = getName(in);
-  if(strcmp("facility",token)) in->error(in,"not the end of the facility");
+  if(g_ascii_strcasecmp("facility",token)) in->error(in,"not the end of the facility");
   getRAnglebracket(in); //</facility>
 }
 
@@ -300,18 +284,17 @@ void parseFacility(parse_file *in, facility * fac)
  *    ev            : new event (parameters are passed to it)   
  ****************************************************************************/
 
-void parseEvent(parse_file *in, event * ev, sequence * unnamed_types, 
+void parseEvent(parse_file *in, event_t * ev, sequence * unnamed_types, 
                table * named_types) 
 {
   char *token;
-  type_descriptor *t;
 
   //<event name=eventtype_name>
-  ev->name = allocAndCopy(getNameAttribute(in));
+  ev->name = g_strdup(getNameAttribute(in));
   getRAnglebracket(in);  
 
   //<description>...</descriptio>
-  ev->description = allocAndCopy(getDescription(in)); 
+  ev->description = getDescription(in); 
   
   //event can have STRUCT, TYPEREF or NOTHING
   getLAnglebracket(in);
@@ -320,7 +303,7 @@ void parseEvent(parse_file *in, event * ev, sequence * unnamed_types,
   if(in->type == FORWARDSLASH){ //</event> NOTHING
     ev->type = NULL;
   }else if(in->type == NAME){
-    if(strcmp("struct",token)==0 || strcmp("typeref",token)==0){
+    if(g_ascii_strcasecmp("struct",token)==0 || g_ascii_strcasecmp("typeref",token)==0){
       ungetToken(in);
       ev->type = parseType(in,NULL, unnamed_types, named_types);
       if(ev->type->type != STRUCT && ev->type->type != NONE) 
@@ -332,7 +315,7 @@ void parseEvent(parse_file *in, event * ev, sequence * unnamed_types,
   }else in->error(in,"not a struct type");
 
   token = getName(in);
-  if(strcmp("event",token))in->error(in,"not an event definition");
+  if(g_ascii_strcasecmp("event",token))in->error(in,"not an event definition");
   getRAnglebracket(in);  //</event>
 }
 
@@ -350,16 +333,16 @@ void parseFields(parse_file *in, type_descriptor *t, sequence * unnamed_types,
                 table * named_types) 
 {
   char * token;
-  field *f;
+  type_fields *f;
 
-  f = (field *)memAlloc(sizeof(field));
+  f = g_new(type_fields,1);
   sequence_push(&(t->fields),f);
 
   //<field name=field_name> <description> <type> </field>
-  f->name = allocAndCopy(getNameAttribute(in)); 
+  f->name = g_strdup(getNameAttribute(in)); 
   getRAnglebracket(in);
 
-  f->description = allocAndCopy(getDescription(in));
+  f->description = getDescription(in);
 
   //<int size=...>
   getLAnglebracket(in);
@@ -368,7 +351,7 @@ void parseFields(parse_file *in, type_descriptor *t, sequence * unnamed_types,
   getLAnglebracket(in);
   getForwardslash(in);
   token = getName(in);
-  if(strcmp("field",token))in->error(in,"not a valid field definition");
+  if(g_ascii_strcasecmp("field",token))in->error(in,"not a valid field definition");
   getRAnglebracket(in); //</field>
 }
 
@@ -400,7 +383,7 @@ type_descriptor *parseType(parse_file *in, type_descriptor *inType,
   type_descriptor *t;
 
   if(inType == NULL) {
-    t = (type_descriptor *) memAlloc(sizeof(type_descriptor));
+    t = g_new(type_descriptor,1);
     t->type_name = NULL;
     t->type = NONE;
     t->fmt = NULL;
@@ -410,26 +393,26 @@ type_descriptor *parseType(parse_file *in, type_descriptor *inType,
 
   token = getName(in);
 
-  if(strcmp(token,"struct") == 0) {
+  if(g_ascii_strcasecmp(token,"struct") == 0) {
     t->type = STRUCT;
     getRAnglebracket(in); //<struct>
     getLAnglebracket(in); //<field name=..>
     token = getToken(in);
     sequence_init(&(t->fields));
-    while(strcmp("field",token) == 0){
+    while(g_ascii_strcasecmp("field",token) == 0){
       parseFields(in,t, unnamed_types, named_types);
       
       //next field
       getLAnglebracket(in);
       token = getToken(in);    
     }
-    if(strcmp("/",token))in->error(in,"not a valid structure definition");
+    if(g_ascii_strcasecmp("/",token))in->error(in,"not a valid structure definition");
     token = getName(in);
-    if(strcmp("struct",token)!=0)
+    if(g_ascii_strcasecmp("struct",token)!=0)
       in->error(in,"not a valid structure definition");
     getRAnglebracket(in); //</struct>
   }
-  else if(strcmp(token,"union") == 0) {
+  else if(g_ascii_strcasecmp(token,"union") == 0) {
     t->type = UNION;
     t->size = getSizeAttribute(in);
     getRAnglebracket(in); //<union typecodesize=isize>
@@ -437,20 +420,20 @@ type_descriptor *parseType(parse_file *in, type_descriptor *inType,
     getLAnglebracket(in); //<field name=..>
     token = getToken(in);
     sequence_init(&(t->fields));
-    while(strcmp("field",token) == 0){
+    while(g_ascii_strcasecmp("field",token) == 0){
       parseFields(in,t, unnamed_types, named_types);
       
       //next field
       getLAnglebracket(in);
       token = getToken(in);    
     }
-    if(strcmp("/",token))in->error(in,"not a valid union definition");
+    if(g_ascii_strcasecmp("/",token))in->error(in,"not a valid union definition");
     token = getName(in);
-    if(strcmp("union",token)!=0)
+    if(g_ascii_strcasecmp("union",token)!=0)
       in->error(in,"not a valid union definition");        
     getRAnglebracket(in); //</union>
   }
-  else if(strcmp(token,"array") == 0) {
+  else if(g_ascii_strcasecmp(token,"array") == 0) {
     t->type = ARRAY;
     t->size = getValueAttribute(in);
     getRAnglebracket(in); //<array size=n>
@@ -461,10 +444,10 @@ type_descriptor *parseType(parse_file *in, type_descriptor *inType,
     getLAnglebracket(in); //</array>
     getForwardslash(in);
     token = getName(in);
-    if(strcmp("array",token))in->error(in,"not a valid array definition");
+    if(g_ascii_strcasecmp("array",token))in->error(in,"not a valid array definition");
     getRAnglebracket(in);  //</array>
   }
-  else if(strcmp(token,"sequence") == 0) {
+  else if(g_ascii_strcasecmp(token,"sequence") == 0) {
     t->type = SEQUENCE;
     t->size = getSizeAttribute(in);
     getRAnglebracket(in); //<array lengthsize=isize>
@@ -475,32 +458,29 @@ type_descriptor *parseType(parse_file *in, type_descriptor *inType,
     getLAnglebracket(in); //</sequence>
     getForwardslash(in);
     token = getName(in);
-    if(strcmp("sequence",token))in->error(in,"not a valid sequence definition");
+    if(g_ascii_strcasecmp("sequence",token))in->error(in,"not a valid sequence definition");
     getRAnglebracket(in); //</sequence>
   }
-  else if(strcmp(token,"enum") == 0) {
+  else if(g_ascii_strcasecmp(token,"enum") == 0) {
     char * str, *str1;
     t->type = ENUM;
     sequence_init(&(t->labels));
     t->size = getSizeAttribute(in);
-    t->fmt = allocAndCopy(getFormatAttribute(in));
+    t->fmt = g_strdup(getFormatAttribute(in));
     getRAnglebracket(in);
 
     //<label name=label1 value=n/>
     getLAnglebracket(in);
     token = getToken(in); //"label" or "/"
-    while(strcmp("label",token) == 0){
-      str   = allocAndCopy(getNameAttribute(in));      
+    while(g_ascii_strcasecmp("label",token) == 0){
+      str1   = g_strdup(getNameAttribute(in));      
       token = getValueStrAttribute(in);
       if(token){
-       str1 = appendString(str,"=");
-       free(str);
-       str = appendString(str1,token);
-       free(str1);
-       sequence_push(&(t->labels),allocAndCopy(str));
-       free(str);
+             str = g_strconcat(str1,"=",token,NULL);
+       g_free(str1);
+       sequence_push(&(t->labels),str);
       }else
-       sequence_push(&(t->labels),allocAndCopy(str));
+       sequence_push(&(t->labels),str1);
 
       getForwardslash(in);
       getRAnglebracket(in);
@@ -509,44 +489,44 @@ type_descriptor *parseType(parse_file *in, type_descriptor *inType,
       getLAnglebracket(in);
       token = getToken(in); //"label" or "/"      
     }
-    if(strcmp("/",token))in->error(in, "not a valid enum definition");
+    if(g_ascii_strcasecmp("/",token))in->error(in, "not a valid enum definition");
     token = getName(in);
-    if(strcmp("enum",token))in->error(in, "not a valid enum definition");
+    if(g_ascii_strcasecmp("enum",token))in->error(in, "not a valid enum definition");
     getRAnglebracket(in); //</label>
   }
-  else if(strcmp(token,"int") == 0) {
+  else if(g_ascii_strcasecmp(token,"int") == 0) {
     t->type = INT;
     t->size = getSizeAttribute(in);
-    t->fmt  = allocAndCopy(getFormatAttribute(in));
+    t->fmt  = g_strdup(getFormatAttribute(in));
     getForwardslash(in);
     getRAnglebracket(in); 
   }
-  else if(strcmp(token,"uint") == 0) {
+  else if(g_ascii_strcasecmp(token,"uint") == 0) {
     t->type = UINT;
     t->size = getSizeAttribute(in);
-    t->fmt  = allocAndCopy(getFormatAttribute(in));
+    t->fmt  = g_strdup(getFormatAttribute(in));
     getForwardslash(in);
     getRAnglebracket(in); 
   }
-  else if(strcmp(token,"float") == 0) {
+  else if(g_ascii_strcasecmp(token,"float") == 0) {
     t->type = FLOAT;
     t->size = getSizeAttribute(in);
-    t->fmt  = allocAndCopy(getFormatAttribute(in));
+    t->fmt  = g_strdup(getFormatAttribute(in));
     getForwardslash(in);
     getRAnglebracket(in); 
   }
-  else if(strcmp(token,"string") == 0) {
+  else if(g_ascii_strcasecmp(token,"string") == 0) {
     t->type = STRING;
-    t->fmt  = allocAndCopy(getFormatAttribute(in));
+    t->fmt  = g_strdup(getFormatAttribute(in));
     getForwardslash(in);
     getRAnglebracket(in); 
   }
-  else if(strcmp(token,"typeref") == 0){
+  else if(g_ascii_strcasecmp(token,"typeref") == 0){
     // Must be a named type
     if(inType != NULL) 
       in->error(in,"Named type cannot refer to a named type");
     else {
-      free(t);
+      g_free(t);
       sequence_pop(unnamed_types);
       token = getNameAttribute(in);
       t = find_named_type(token, named_types);
@@ -569,17 +549,18 @@ type_descriptor *parseType(parse_file *in, type_descriptor *inType,
  *    type_descriptor *   : a type descriptor                       
  *****************************************************************************/
 
-type_descriptor * find_named_type(char *name, table * named_types)
+type_descriptor * find_named_type(gchar *name, table * named_types)
 { 
   type_descriptor *t;
 
   t = table_find(named_types,name);
   if(t == NULL) {
-    t = (type_descriptor *)memAlloc(sizeof(type_descriptor));
-    t->type_name = allocAndCopy(name);
+    t = g_new(type_descriptor,1);
+    t->type_name = g_strdup(name);
     t->type = NONE;
     t->fmt = NULL;
-    table_insert(named_types,allocAndCopy(name),t);
+    table_insert(named_types,t->type_name,t);
+    //    table_insert(named_types,g_strdup(name),t);
   }
   return t;
 }  
@@ -606,7 +587,7 @@ void parseTypeDefinition(parse_file * in, sequence * unnamed_types,
   getRAnglebracket(in); //<type name=type_name>
   getLAnglebracket(in); //<struct>
   token = getName(in);
-  if(strcmp("struct",token))in->error(in,"not a valid type definition");
+  if(g_ascii_strcasecmp("struct",token))in->error(in,"not a valid type definition");
   ungetToken(in);
   parseType(in,t, unnamed_types, named_types);
   
@@ -614,7 +595,7 @@ void parseTypeDefinition(parse_file * in, sequence * unnamed_types,
   getLAnglebracket(in);
   getForwardslash(in);
   token = getName(in);
-  if(strcmp("type",token))in->error(in,"not a valid type definition");  
+  if(g_ascii_strcasecmp("type",token))in->error(in,"not a valid type definition");  
   getRAnglebracket(in); //</type>
 }
 
@@ -695,18 +676,38 @@ char * getEqual(parse_file *in)
   return token;
 }
 
-char seekNextChar(parse_file *in)
+gunichar seekNextChar(parse_file *in, gunichar *car)
 {
-  char car;
-  while((car = getc(in->fp)) != EOF) {
-    if(!isspace(car)){
-      ungetc(car,in->fp);
-      return car;
+  GError * error = NULL;
+  GIOStatus status;
+
+  do {
+
+    status = g_io_channel_read_unichar(in->channel, car, &error);
+    
+    if(error != NULL) {
+      g_warning("Can not read file: \n%s\n", error->message);
+      g_error_free(error);
+      break;
     }
-  }  
-  return EOF;
+    in->pos++;
+   
+    if(!g_unichar_isspace(*car)) {
+      g_io_channel_seek_position(in->channel, -1, G_SEEK_CUR, &error);
+      if(error != NULL) {
+        g_warning("Can not seek file: \n%s\n", error->message);
+        g_error_free(error);
+      }
+      in->pos--;
+      break;
+    }
+
+  } while(status != G_IO_STATUS_EOF && status != G_IO_STATUS_ERROR);
+
+  return status;
 }
 
+
 /******************************************************************
  * Function :
  *    getToken, ungetToken
@@ -727,11 +728,11 @@ void ungetToken(parse_file * in)
   in->unget = 1;
 }
 
-char *getToken(parse_file * in)
+gchar *getToken(parse_file * in)
 {
-  FILE *fp = in->fp;
-  char car, car1;
+  gunichar car, car1;
   int pos = 0, escaped;
+  GError * error = NULL;
 
   if(in->unget == 1) {
     in->unget = 0;
@@ -740,20 +741,35 @@ char *getToken(parse_file * in)
 
   /* skip whitespace and comments */
 
-  while((car = getc(fp)) != EOF) {
+  while((g_io_channel_read_unichar(in->channel, &car, &error))
+      != G_IO_STATUS_EOF) {
+
+    if(error != NULL) {
+      g_warning("Can not read file: \n%s\n", error->message);
+      g_error_free(error);
+    } else in->pos++;
+
     if(car == '/') {
-      car1 = getc(fp); 
+      g_io_channel_read_unichar(in->channel, &car1, &error);
+      if(error != NULL) {
+        g_warning("Can not read file: \n%s\n", error->message);
+        g_error_free(error);
+      } else in->pos++;
+
       if(car1 == '*') skipComment(in);
       else if(car1 == '/') skipEOL(in);
-      else { 
-        car1 = ungetc(car1,fp);
+      else {
+        g_io_channel_seek_position(in->channel, -1, G_SEEK_CUR, &error);
+        if(error != NULL) {
+          g_warning("Can not seek file: \n%s\n", error->message);
+          g_error_free(error);
+        } else in->pos--;
         break;
       }
     }
     else if(car == '\n') in->lineno++;
-    else if(!isspace(car)) break;
+    else if(!g_unichar_isspace(car)) break;
   }
-
   switch(car) {
     case EOF:
       in->type = ENDFILE;
@@ -780,10 +796,17 @@ char *getToken(parse_file * in)
       break;
     case '"':
       escaped = 0;
-      while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) {
+      while(g_io_channel_read_unichar(in->channel, &car, &error)
+                  != G_IO_STATUS_EOF && pos < BUFFER_SIZE) {
+
+        if(error != NULL) {
+          g_warning("Can not read file: \n%s\n", error->message);
+          g_error_free(error);
+        } else in->pos++;
+
         if(car == '\\' && escaped == 0) {
-         in->buffer[pos] = car;
-         pos++;
+               in->buffer[pos] = car;
+         pos++;
           escaped = 1;
           continue;
         }
@@ -801,33 +824,67 @@ char *getToken(parse_file * in)
       in->type = QUOTEDSTRING;
       break;
     default:
-      if(isdigit(car)) {
+      if(g_unichar_isdigit(car)) {
         in->buffer[pos] = car;
         pos++;
-        while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) {
+        while(g_io_channel_read_unichar(in->channel, &car, &error)
+                    != G_IO_STATUS_EOF && pos < BUFFER_SIZE) {
+
+          if(error != NULL) {
+            g_warning("Can not read file: \n%s\n", error->message);
+            g_error_free(error);
+          } else in->pos++;
+
           if(!isdigit(car)) {
-            ungetc(car,fp);
+            g_io_channel_seek_position(in->channel, -1, G_SEEK_CUR, &error);
+            if(error != NULL) {
+              g_warning("Can not seek file: \n%s\n", error->message);
+              g_error_free(error);
+            } else in->pos--;
             break;
           }
           in->buffer[pos] = car;
           pos++;
         }
-       if(car == EOF) ungetc(car,fp);
+             if(car == EOF) {
+          g_io_channel_seek_position(in->channel, -1, G_SEEK_CUR, &error);
+          if(error != NULL) {
+            g_warning("Can not seek file: \n%s\n", error->message);
+            g_error_free(error);
+          } else in->pos--;
+        }
         if(pos == BUFFER_SIZE) in->error(in, "number token too large");
         in->type = NUMBER;
       }    
-      else if(isalpha(car)) {
+      else if(g_unichar_isalpha(car)) {
         in->buffer[0] = car;
         pos = 1;
-        while((car = getc(fp)) != EOF && pos < BUFFER_SIZE) {
-          if(!isalnum(car)) {
-            ungetc(car,fp);
+        while(g_io_channel_read_unichar(in->channel, &car, &error)
+                              != G_IO_STATUS_EOF && pos < BUFFER_SIZE) {
+
+          if(error != NULL) {
+            g_warning("Can not read file: \n%s\n", error->message);
+            g_error_free(error);
+          } else in->pos++;
+
+          if(!(g_unichar_isalnum(car) || car == '_')) {
+            g_io_channel_seek_position(in->channel, -1, G_SEEK_CUR, &error);
+            if(error != NULL) {
+              g_warning("Can not seek file: \n%s\n", error->message);
+              g_error_free(error);
+            } else in->pos--;
             break;
           }
           in->buffer[pos] = car;
           pos++;
         }
-       if(car == EOF) ungetc(car,fp);
+             if(car == EOF) {
+          g_io_channel_seek_position(in->channel, -1, G_SEEK_CUR, &error);
+          if(error != NULL) {
+            g_warning("Can not seek file: \n%s\n", error->message);
+            g_error_free(error);
+          } else in->pos--;
+        }
         if(pos == BUFFER_SIZE) in->error(in, "name token too large");
         in->type = NAME;
       }
@@ -839,14 +896,35 @@ char *getToken(parse_file * in)
 
 void skipComment(parse_file * in)
 {
-  char car;
-  while((car = getc(in->fp)) != EOF) {
+  gunichar car;
+  GError * error = NULL;
+
+  while(g_io_channel_read_unichar(in->channel, &car, &error)
+                                    != G_IO_STATUS_EOF) {
+
+    if(error != NULL) {
+      g_warning("Can not read file: \n%s\n", error->message);
+      g_error_free(error);
+    } else in->pos++;
+
     if(car == '\n') in->lineno++;
     else if(car == '*') {
-      car = getc(in->fp);
+
+      g_io_channel_read_unichar(in->channel, &car, &error);
+      if(error != NULL) {
+        g_warning("Can not read file: \n%s\n", error->message);
+        g_error_free(error);
+      } else in->pos++;
+
       if(car ==EOF) break;
       if(car == '/') return;
-      ungetc(car,in->fp);
+
+      g_io_channel_seek_position(in->channel, -1, G_SEEK_CUR, &error);
+      if(error != NULL) {
+        g_warning("Can not seek file: \n%s\n", error->message);
+        g_error_free(error);
+      } else in->pos--;
+
     }
   }
   if(car == EOF) in->error(in,"comment begining with '/*' has no ending '*/'");
@@ -854,37 +932,36 @@ void skipComment(parse_file * in)
 
 void skipEOL(parse_file * in)
 {
-  char car;
-  while((car = getc(in->fp)) != EOF) {
+  gunichar car;
+  GError * error = NULL;
+
+  while(g_io_channel_read_unichar(in->channel, &car, &error)
+                                          != G_IO_STATUS_EOF) {
     if(car == '\n') {
-      ungetc(car,in->fp);
+      g_io_channel_seek_position(in->channel, -1, G_SEEK_CUR, &error);
+      if(error != NULL) {
+        g_warning("Can not seek file: \n%s\n", error->message);
+        g_error_free(error);
+      } else in->pos--;
       break;
     }
   }
-  if(car == EOF)ungetc(car, in->fp);
-}
-
-int isalpha(char c)
-{
-  int i,j;
-  if(c == '_')return 1;
-  i = c - 'a';
-  j = c - 'A';
-  if((i>=0 && i<26) || (j>=0 && j<26)) return 1;
-  return 0;
-}
-
-int isalnum(char c)
-{
-  return (isalpha(c) || isdigit(c));
+  if(car == EOF) {
+    g_io_channel_seek_position(in->channel, -1, G_SEEK_CUR, &error);
+    if(error != NULL) {
+      g_warning("Can not seek file: \n%s\n", error->message);
+      g_error_free(error);
+    } else in->pos--;
+  }
 }
 
 /*****************************************************************************
  *Function name
  *    checkNamedTypesImplemented : check if all named types have definition
+ *    returns -1 on error, 0 if ok
  ****************************************************************************/
 
-void checkNamedTypesImplemented(table * named_types)
+int checkNamedTypesImplemented(table * named_types)
 {
   type_descriptor *t;
   int pos;
@@ -893,10 +970,13 @@ void checkNamedTypesImplemented(table * named_types)
   for(pos = 0 ; pos < named_types->values.position; pos++) {
     t = (type_descriptor *) named_types->values.array[pos];
     if(t->type == NONE){
-      sprintf(str,"named type '%s' has no definition",(char*)named_types->keys.array[pos]);
-      error_callback(NULL,str);   
+      sprintf(str,"named type '%s' has no definition",
+              (char*)named_types->keys.array[pos]);
+      error_callback(NULL,str);
+      return -1;
     }
   }
+  return 0;
 }
 
 
@@ -909,25 +989,27 @@ void checkNamedTypesImplemented(table * named_types)
  *    checksum          : checksum for the facility
  ****************************************************************************/
 
-void generateChecksum( char* facName, unsigned long * checksum, sequence * events)
+int generateChecksum(char* facName, guint32 * checksum, sequence * events)
 {
   unsigned long crc ;
   int pos;
-  event * ev;
+  event_t * ev;
   char str[256];
 
   crc = crc32(facName);
   for(pos = 0; pos < events->position; pos++){
-    ev = (event *)(events->array[pos]);
+    ev = (event_t *)(events->array[pos]);
     crc = partial_crc32(ev->name,crc);    
     if(!ev->type) continue; //event without type
     if(ev->type->type != STRUCT){
       sprintf(str,"event '%s' has a type other than STRUCT",ev->name);
       error_callback(NULL, str);
+      return -1;
     }
     crc = getTypeChecksum(crc, ev->type);
   }
   *checksum = crc;
+  return 0;
 }
 
 /*****************************************************************************
@@ -945,7 +1027,7 @@ unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor * type)
   unsigned long crc = aCrc;
   char * str = NULL, buf[16];
   int flag = 0, pos;
-  field * fld;
+  type_fields * fld;
 
   switch(type->type){
     case INT:
@@ -958,29 +1040,29 @@ unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor * type)
       str = floatOutputTypes[type->size];
       break;
     case STRING:
-      str = allocAndCopy("string");
+      str = g_strdup("string");
       flag = 1;
       break;
     case ENUM:
-      str = appendString("enum ", uintOutputTypes[type->size]);
+      str = g_strconcat("enum ", uintOutputTypes[type->size], NULL);
       flag = 1;
       break;
     case ARRAY:
-      sprintf(buf,"%d\0",type->size);
-      str = appendString("array ",buf);
+      sprintf(buf,"%d",type->size);
+      str = g_strconcat("array ",buf, NULL);
       flag = 1;
       break;
     case SEQUENCE:
-      sprintf(buf,"%d\0",type->size);
-      str = appendString("sequence ",buf);
+      sprintf(buf,"%d",type->size);
+      str = g_strconcat("sequence ",buf, NULL);
       flag = 1;
       break;
     case STRUCT:
-      str = allocAndCopy("struct");
+      str = g_strdup("struct");
       flag = 1;
       break;
     case UNION:
-      str = allocAndCopy("union");
+      str = g_strdup("union");
       flag = 1;
       break;
     default:
@@ -989,7 +1071,7 @@ unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor * type)
   }
 
   crc = partial_crc32(str,crc);
-  if(flag) free(str);
+  if(flag) g_free(str);
 
   if(type->fmt) crc = partial_crc32(type->fmt,crc);
     
@@ -997,7 +1079,7 @@ unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor * type)
     crc = getTypeChecksum(crc,type->nested_type);
   }else if(type->type == STRUCT || type->type == UNION){
     for(pos =0; pos < type->fields.position; pos++){
-      fld = (field *) type->fields.array[pos];
+      fld = (type_fields *) type->fields.array[pos];
       crc = partial_crc32(fld->name,crc);
       crc = getTypeChecksum(crc, fld->type);
     }    
@@ -1014,21 +1096,21 @@ unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor * type)
 void freeType(type_descriptor * tp)
 {
   int pos2;
-  field *f;
+  type_fields *f;
 
-  if(tp->fmt != NULL) free(tp->fmt);
+  if(tp->fmt != NULL) g_free(tp->fmt);
   if(tp->type == ENUM) {
     for(pos2 = 0; pos2 < tp->labels.position; pos2++) {
-      free(tp->labels.array[pos2]);
+      g_free(tp->labels.array[pos2]);
     }
     sequence_dispose(&(tp->labels));
   }
   if(tp->type == STRUCT) {
     for(pos2 = 0; pos2 < tp->fields.position; pos2++) {
-      f = (field *) tp->fields.array[pos2];
-      free(f->name);
-      free(f->description);
-      free(f);
+      f = (type_fields *) tp->fields.array[pos2];
+      g_free(f->name);
+      g_free(f->description);
+      g_free(f);
     }
     sequence_dispose(&(tp->fields));
   }
@@ -1040,36 +1122,35 @@ void freeNamedType(table * t)
   type_descriptor * td;
 
   for(pos = 0 ; pos < t->keys.position; pos++) {
-    free((char *)t->keys.array[pos]);
+    g_free((char *)t->keys.array[pos]);
     td = (type_descriptor*)t->values.array[pos];
     freeType(td);
-    free(td);
+    g_free(td);
   }
 }
 
 void freeTypes(sequence *t) 
 {
-  int pos, pos2;
+  int pos;
   type_descriptor *tp;
-  field *f;
 
   for(pos = 0 ; pos < t->position; pos++) {
     tp = (type_descriptor *)t->array[pos];
     freeType(tp);
-    free(tp);
+    g_free(tp);
   }
 }
 
 void freeEvents(sequence *t) 
 {
   int pos;
-  event *ev;
+  event_t *ev;
 
   for(pos = 0 ; pos < t->position; pos++) {
-    ev = (event *) t->array[pos];
-    free(ev->name);
-    free(ev->description);
-    free(ev);
+    ev = (event_t *) t->array[pos];
+    g_free(ev->name);
+    g_free(ev->description);
+    g_free(ev);
   }
 
 }
@@ -1081,13 +1162,13 @@ void sequence_init(sequence *t)
 {
   t->size = 10;
   t->position = 0;
-  t->array = (void **)memAlloc(t->size * sizeof(void *));
+  t->array = g_new(void*, t->size);
 }
 
 void sequence_dispose(sequence *t) 
 {
   t->size = 0;
-  free(t->array);
+  g_free(t->array);
   t->array = NULL;
 }
 
@@ -1097,10 +1178,10 @@ void sequence_push(sequence *t, void *elem)
 
   if(t->position >= t->size) {
     tmp = t->array;
-    t->array = (void **)memAlloc(t->size * 2 * sizeof(void *));
+    t->array = g_new(void*, 2*t->size);
     memcpy(t->array, tmp, t->size * sizeof(void *));
     t->size = t->size * 2;
-    free(tmp);
+    g_free(tmp);
   }
   t->array[t->position] = elem;
   t->position++;
@@ -1136,7 +1217,7 @@ void *table_find(table *t, char *key)
 {
   int pos;
   for(pos = 0 ; pos < t->keys.position; pos++) {
-    if(strcmp((char *)key,(char *)t->keys.array[pos]) == 0)
+    if(g_ascii_strcasecmp((char *)key,(char *)t->keys.array[pos]) == 0)
       return(t->values.array[pos]);
   }
   return NULL;
@@ -1159,17 +1240,3 @@ void *table_find_int(table *t, int *key)
 }
 
 
-/* Concatenate strings */
-
-char *appendString(char *s, char *suffix) 
-{
-  char *tmp;
-  if(suffix == NULL) return s;
-
-  tmp = (char *)memAlloc(strlen(s) + strlen(suffix) + 1);
-  strcpy(tmp,s);
-  strcat(tmp,suffix);  
-  return tmp;
-}
-
-
This page took 0.037842 seconds and 4 git commands to generate.