Add config.h support : will fix the LARGEFILE problem
[lttv.git] / ltt / branches / poly / ltt / facility.c
index b1c26baa18daec226a46d2955e7a26ed71094ef0..84baac98421ce92a6c4a5907015e908b90bba2b7 100644 (file)
@@ -1,11 +1,46 @@
+/* This file is part of the Linux Trace Toolkit viewer
+ * Copyright (C) 2003-2004 Xiangxiu Yang
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License Version 2 as
+ * published by the Free Software Foundation;
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, 
+ * MA 02111-1307, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include <stdlib.h> 
 #include <string.h>
 #include <stdio.h>
+#include <glib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+
 
-#include <ltt/LTTTypes.h>  
 #include "parser.h"
+#include <ltt/ltt.h>
+#include "ltt-private.h"
 #include <ltt/facility.h>
 
+#ifndef g_open
+#define g_open open
+#endif
+
+#define g_close close
+
 /* search for the (named) type in the table, if it does not exist
    create a new one */
 LttType * lookup_named_type(LttFacility *fac, type_descriptor * td);
@@ -15,7 +50,7 @@ void constructTypeAndFields(LttFacility * fac,type_descriptor * td,
                             LttField * fld);
 
 /* generate the facility according to the events belongin to it */
-void generateFacility(LttFacility * f, facility  * fac, 
+void generateFacility(LttFacility * f, facility_t  * fac, 
                       LttChecksum checksum);
 
 /* functions to release the memory occupied by a facility */
@@ -34,32 +69,37 @@ void freeLttNamedType(LttType * type);
  *    pathname                : the path name of the facility   
  ****************************************************************************/
 
-void ltt_facility_open(LttTrace * t, char * pathname)
+void ltt_facility_open(LttTrace * t, gchar * pathname)
 {
-  char *token;
+  gchar *token;
   parse_file in;
-  char buffer[BUFFER_SIZE];
-  facility * fac;
+  gsize length;
+  facility_t * fac;
   LttFacility * f;
   LttChecksum checksum;
+  GError * error = NULL;
+  gchar buffer[BUFFER_SIZE];
 
-  in.buffer = buffer;
+  in.buffer = &(buffer[0]);
   in.lineno = 0;
   in.error = error_callback;
   in.name = pathname;
 
-  in.fp = fopen(in.name, "r");
-  if(!in.fp ) in.error(&in,"cannot open input file");
+  in.fd = g_open(in.name, O_RDONLY, 0);
+  if(in.fd < 0 ) in.error(&in,"cannot open input file");
+
+  in.channel = g_io_channel_unix_new(in.fd);
+  in.pos = 0;
 
   while(1){
     token = getToken(&in);
     if(in.type == ENDFILE) break;
     
-    if(strcmp(token, "<")) in.error(&in,"not a facility file");
+    if(g_ascii_strcasecmp(token, "<")) in.error(&in,"not a facility file");
     token = getName(&in);
-    
-    if(strcmp("facility",token) == 0) {
-      fac = g_new(facility, 1);
+
+    if(g_ascii_strcasecmp("facility",token) == 0) {
+      fac = g_new(facility_t, 1);
       fac->name = NULL;
       fac->description = NULL;
       sequence_init(&(fac->events));
@@ -69,18 +109,19 @@ void ltt_facility_open(LttTrace * t, char * pathname)
       parseFacility(&in, fac);
 
       //check if any namedType is not defined
-      checkNamedTypesImplemented(&fac->named_types);
+      g_assert(checkNamedTypesImplemented(&fac->named_types) == 0);
     
-      generateChecksum(fac->name, &checksum, &fac->events);
+      g_assert(generateChecksum(fac->name, &checksum, &fac->events) == 0);
 
       f = g_new(LttFacility,1);    
+      f->base_id = 0;
       generateFacility(f, fac, checksum);
 
       t->facility_number++;
       g_ptr_array_add(t->facilities,f);
 
-      free(fac->name);
-      free(fac->description);
+      g_free(fac->name);
+      g_free(fac->description);
       freeEvents(&fac->events);
       sequence_dispose(&fac->events);
       freeNamedType(&fac->named_types);
@@ -91,7 +132,14 @@ void ltt_facility_open(LttTrace * t, char * pathname)
     }
     else in.error(&in,"facility token was expected");
   }
-  fclose(in.fp);
+
+  g_io_channel_shutdown(in.channel, FALSE, &error); /* No flush */
+  if(error != NULL) {
+    g_warning("Can not close file: \n%s\n", error->message);
+    g_error_free(error);
+  }
+
+  g_close(in.fd);
 }
 
 
@@ -104,7 +152,7 @@ void ltt_facility_open(LttTrace * t, char * pathname)
  *    checksum            : checksum of the facility          
  ****************************************************************************/
 
-void generateFacility(LttFacility *f, facility *fac,LttChecksum checksum)
+void generateFacility(LttFacility *f, facility_t *fac,LttChecksum checksum)
 {
   char * facilityName = fac->name;
   sequence * events = &fac->events;
@@ -128,17 +176,17 @@ void generateFacility(LttFacility *f, facility *fac,LttChecksum checksum)
     evType = g_new(LttEventType,1);
     f->events[i] = evType;
 
-    evType->name = g_strdup(((event*)(events->array[i]))->name);
-    evType->description=g_strdup(((event*)(events->array[i]))->description);
+    evType->name = g_strdup(((event_t*)(events->array[i]))->name);
+    evType->description=g_strdup(((event_t*)(events->array[i]))->description);
     
     field = g_new(LttField, 1);
     evType->root_field = field;
     evType->facility = f;
     evType->index = i;
 
-    if(((event*)(events->array[i]))->type != NULL){
+    if(((event_t*)(events->array[i]))->type != NULL){
       field->field_pos = 0;
-      type = lookup_named_type(f,((event*)(events->array[i]))->type);
+      type = lookup_named_type(f,((event_t*)(events->array[i]))->type);
       field->field_type = type;
       field->offset_root = 0;
       field->fixed_root = 1;
@@ -152,7 +200,7 @@ void generateFacility(LttFacility *f, facility *fac,LttChecksum checksum)
       field->current_element = 0;
 
       //construct field tree and type graph
-      constructTypeAndFields(f,((event*)(events->array[i]))->type,field);
+      constructTypeAndFields(f,((event_t*)(events->array[i]))->type,field);
     }else{
       evType->root_field = NULL;
       g_free(field);
@@ -222,7 +270,7 @@ void constructTypeAndFields(LttFacility * fac,type_descriptor * td,
 
     fld->child = g_new(LttField*, td->fields.position);      
     for(i=0;i<td->fields.position;i++){
-      tmpTd = ((field*)(td->fields.array[i]))->type;
+      tmpTd = ((type_fields*)(td->fields.array[i]))->type;
 
       if(flag)
        fld->field_type->element_type[i] = lookup_named_type(fac, tmpTd);
@@ -233,7 +281,7 @@ void constructTypeAndFields(LttFacility * fac,type_descriptor * td,
 
       if(flag){
        fld->child[i]->field_type->element_name 
-         = g_strdup(((field*)(td->fields.array[i]))->name);
+         = g_strdup(((type_fields*)(td->fields.array[i]))->name);
       }
 
       fld->child[i]->offset_root = -1;
@@ -266,17 +314,18 @@ void constructTypeAndFields(LttFacility * fac,type_descriptor * td,
 LttType * lookup_named_type(LttFacility *fac, type_descriptor * td)
 {
   LttType * lttType = NULL;
-  int i;
-  char * name;
+  unsigned int i=0;
+  gchar * name;
+
   if(td->type_name){
     for(i=0;i<fac->named_types_number; i++){
       if(fac->named_types[i] == NULL) break;
       name = fac->named_types[i]->type_name;
-      if(strcmp(name, td->type_name)==0){
-       lttType = fac->named_types[i];  
+      if(g_ascii_strcasecmp(name, td->type_name)==0){
+             lttType = fac->named_types[i];    
        //      if(lttType->element_name) g_free(lttType->element_name);
        //      lttType->element_name = NULL;
-       break;  
+             break;    
       }
     }
   }
@@ -293,7 +342,7 @@ LttType * lookup_named_type(LttFacility *fac, type_descriptor * td)
     lttType->element_name = NULL;
     if(td->type_name){
       lttType->type_name = g_strdup(td->type_name);
-      fac->named_types[i] = lttType;
+      fac->named_types[i] = lttType; /* i is initialized, checked. */
     }
     else{
       lttType->type_name = NULL;
@@ -328,7 +377,7 @@ int ltt_facility_close(LttFacility *f)
 
 void freeFacility(LttFacility * fac)
 {
-  int i;
+  unsigned int i;
   g_free(fac->name);  //free facility name
 
   //free event types
@@ -365,7 +414,6 @@ void freeEventtype(LttEventType * evType)
 
 void freeLttNamedType(LttType * type)
 {
-  int i;
   g_free(type->type_name);
   type->type_name = NULL;
   freeLttType(&type);
@@ -373,7 +421,7 @@ void freeLttNamedType(LttType * type)
 
 void freeLttType(LttType ** type)
 {
-  int i;
+  unsigned int i;
   if(*type == NULL) return;
   if((*type)->type_name){
     return; //this is a named type
@@ -429,7 +477,7 @@ void freeLttField(LttField * fld)
  *    char *                  : the facility's name
  ****************************************************************************/
 
-char *ltt_facility_name(LttFacility *f)
+gchar *ltt_facility_name(LttFacility *f)
 {
   return f->name;
 }
@@ -473,7 +521,7 @@ unsigned ltt_facility_base_id(LttFacility *f)
 
 unsigned ltt_facility_eventtype_number(LttFacility *f)
 {
-  return (unsigned)(f->event_number);
+  return (f->event_number);
 }
 
 /*****************************************************************************
@@ -497,22 +545,24 @@ LttEventType *ltt_facility_eventtype_get(LttFacility *f, unsigned i)
  *                     : obtain the event type according to event name
  *                       event name is unique in the facility
  *Input params
- *    f                : the facility that will be closed
+ *    f                : the facility
  *    name             : the name of the event
  *Return value
  *    LttEventType *  : the event type required  
  ****************************************************************************/
 
-LttEventType *ltt_facility_eventtype_get_by_name(LttFacility *f, char *name)
+LttEventType *ltt_facility_eventtype_get_by_name(LttFacility *f, gchar *name)
 {
-  int i;
-  LttEventType * ev;
+  unsigned int i;
+  LttEventType * ev = NULL;
+  
   for(i=0;i<f->event_number;i++){
-    ev = f->events[i];
-    if(strcmp(ev->name, name) == 0)break;      
+    LttEventType *iter_ev = f->events[i];
+    if(g_ascii_strcasecmp(iter_ev->name, name) == 0) {
+      ev = iter_ev;
+      break;
+    }
   }
-
-  if(i==f->event_number) return NULL;
-  else return ev;
+  return ev;
 }
 
This page took 0.02769 seconds and 4 git commands to generate.