bpminterface/load_struct.c

Go to the documentation of this file.
00001 
00006 #include <bpm/bpm_interface.h>
00007 #include <bpm/bpm_messages.h>
00008 
00009 #define MAX_ARGS 200
00010 
00011 int load_struct( FILE *file, char ***arg_list, char ***val_list, int *num_args ){
00012 
00013   if ( ! file ) {
00014     bpm_error( "Invalid file pointer in load_struct(...)",
00015                __FILE__, __LINE__ );
00016     return BPM_FAILURE;
00017   }
00018 
00019   if ( ! arg_list ) {
00020     bpm_error( "Invalid argument list pointer in load_struct(...)",
00021                __FILE__, __LINE__ );
00022     return BPM_FAILURE;
00023   }
00024 
00025   if ( ! val_list ) {
00026     bpm_error( "Invalid value list pointer in load_struct(...)",
00027                __FILE__, __LINE__ );
00028     return BPM_FAILURE;
00029   }
00030 
00031   if ( ! num_args ) {
00032     bpm_error( "Invalid pointer to number of arguments integer in load_struct(...)",
00033                __FILE__, __LINE__ );
00034     return BPM_FAILURE;
00035   }
00036 
00037   /*--------------------------------------------------------------------------
00038     Keep loading in lines until a non-comment is found and assume this is the
00039     start of the structure
00040     --------------------------------------------------------------------------
00041   */
00042   
00043   char line[255];
00044   char *tok;
00045   int in_struct = 0, syn_error = 0;
00046 
00047   char *temp_args[MAX_ARGS], *temp_vals[MAX_ARGS];
00048   *num_args = 0;
00049 
00050   while ( 1 ) {
00051     
00052     if ( fgets( line, 255, file ) == NULL ) {
00053       
00054       // End of file reached
00055       bpm_error( "End of file found when loading structure in load_struct(...)",
00056                  __FILE__, __LINE__ );
00057       return BPM_FAILURE;
00058     }
00059 
00060     if ( line[0] == '{' ) {
00061 
00062       // Check for open brace
00063       if (in_struct){
00064         syn_error = 1;
00065         break;
00066       }
00067 
00068       in_struct = 1;
00069     }
00070     else if ( line[0] == '}' ) {
00071       // End of definition
00072       break;
00073     }
00074     else if ( in_struct == 1 ) {
00075       
00076       // Strip whitespaces
00077       int strip_idx = 0;
00078       while ( isspace( line[strip_idx++] ) );
00079 
00080       // Check for blank lines
00081       if ((--strip_idx) == strlen(line)) continue;
00082       
00083       // Start reading in the strucutre parameters
00084       tok = strtok( &(line[strip_idx]), " \n\t" );
00085       temp_args[*num_args] = (char*) calloc( strlen(tok), sizeof(char) );
00086       strcpy( temp_args[*num_args], tok );
00087       
00088       // For the values of the parameters, read in the whole string in case
00089       // of several parameters
00090       tok = strtok( NULL, "#\n" );
00091 
00092       // strip whitespaces from front and back
00093       int strip_front_idx = 0;
00094       while ( isspace( tok[++strip_front_idx] ) );
00095 
00096       int strip_back_idx = strlen(tok);
00097       while ( isspace( tok[--strip_back_idx] ) );
00098       tok[strip_back_idx+1] = '\0';
00099 
00100       temp_vals[*num_args] = (char*) calloc( strlen(&(tok[strip_front_idx])), sizeof(char) );
00101       strcpy( temp_vals[*num_args], &(tok[strip_front_idx]) );
00102       
00103       ++(*num_args);
00104       if (*num_args == MAX_ARGS) {
00105         
00106         char buf[255];
00107         sprintf( buf, "Number of parameters in structure exceeded MAX_ARGS (%d) in load_struct", MAX_ARGS );
00108         
00109         bpm_error( buf, __FILE__, __LINE__ );
00110         return BPM_FAILURE;
00111       }
00112     }
00113     
00114   }
00115 
00116   // Transfer the parameters and values to the user supplied variables
00117   int i;
00118   *arg_list = (char**) calloc( *num_args, sizeof(char*) );
00119   *val_list = (char**) calloc( *num_args, sizeof(char*) );
00120   
00121   for ( i = 0; i < *num_args; i++ ) {
00122 
00123     (*arg_list)[i] = (char*) calloc( strlen(temp_args[i]), sizeof(char) );
00124     strcpy( (*arg_list)[i], temp_args[i] );
00125     
00126     (*val_list)[i] = (char*) calloc( strlen(temp_vals[i]), sizeof(char) );
00127     strcpy( (*val_list)[i], temp_vals[i] );
00128  
00129   }
00130   
00131   return BPM_SUCCESS;
00132 }

Generated on Thu Jan 10 10:18:04 2008 for libbpm by  doxygen 1.5.1