00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <eh_utils.h>
00022
00023 #if defined( OLD_SYMBOL_TABLE )
00024
00025 guint eh_str_case_hash(gconstpointer key);
00026 gboolean eh_str_case_equal(gconstpointer a, gconstpointer b);
00027
00028 void eh_symbol_table_free_label( gpointer label )
00029 {
00030 eh_free( label );
00031 }
00032
00033 Symbol_table *eh_create_symbol_table(void)
00034 {
00035 return (Symbol_table*)g_hash_table_new_full( &eh_str_case_hash ,
00036 &eh_str_case_equal ,
00037 &eh_symbol_table_free_label ,
00038 &eh_symbol_table_free_label );
00039 }
00040
00041 guint eh_str_case_hash(gconstpointer key)
00042 {
00043 char *new_key = g_strdup((char*)key);
00044 guint ans;
00045
00046 g_strup(new_key);
00047 ans = g_str_hash(new_key);
00048 eh_free(new_key);
00049
00050 return ans;
00051 }
00052
00053 void eh_destroy_key( gpointer key , gpointer value , gpointer user_data)
00054 {
00055 eh_free(key);
00056 }
00057
00058
00059 #include <stdarg.h>
00060
00061 void __eh_symbol_table_insert( char *key, char *value, Symbol_table *t )
00062 {
00063 eh_symbol_table_insert( t , g_strdup(key) , g_strdup(value) );
00064 }
00065
00066 Symbol_table* eh_symbol_table_dup( Symbol_table* t )
00067 {
00068 return eh_symbol_table_copy( NULL , t );
00069 }
00070
00071 Symbol_table* eh_symbol_table_copy( Symbol_table* dest , Symbol_table* src )
00072 {
00073 if ( !dest )
00074 dest = eh_create_symbol_table();
00075
00076 g_hash_table_foreach( src , (GHFunc)&__eh_symbol_table_insert , dest );
00077
00078 return dest;
00079 }
00080
00081 Symbol_table *eh_merge_symbol_table( Symbol_table* table1 , ... )
00082 {
00083 va_list args;
00084 Symbol_table *rtn, *table;
00085
00086 rtn = eh_create_symbol_table();
00087 g_hash_table_foreach( table1 , (GHFunc)&__eh_symbol_table_insert , rtn );
00088
00089 va_start( args , table1 );
00090 table = va_arg( args , Symbol_table* );
00091 while ( table )
00092 {
00093 g_hash_table_foreach( table , (GHFunc)&__eh_symbol_table_insert , rtn );
00094 table = va_arg( args , Symbol_table* );
00095 }
00096 va_end( args );
00097 return rtn;
00098 }
00099
00100 gboolean eh_str_case_equal(gconstpointer a, gconstpointer b)
00101 {
00102 return (g_strcasecmp((char*)a,(char*)b)==0)?TRUE:FALSE;
00103 }
00104
00105 void eh_symbol_table_insert(Symbol_table *t, char *key, char *value)
00106 {
00107 g_hash_table_insert((GHashTable*)t,g_strdup(key),g_strdup(value));
00108 }
00109
00110 void eh_symbol_table_replace( Symbol_table* t , char* key , char* value )
00111 {
00112 g_hash_table_replace((GHashTable*)t,g_strdup(key),g_strdup(value));
00113 }
00114
00115 char*
00116 eh_symbol_table_lookup(Symbol_table *t, char *key)
00117 {
00118 return (char*)g_hash_table_lookup((GHashTable*)t,key);
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131 }
00132
00133 typedef struct
00134 {
00135 FILE *fp;
00136 int max_key_len;
00137 }
00138 aligned_st;
00139 void eh_print_symbol_aligned( char *key, char *value, aligned_st *st );
00140 void eh_get_max_key_len( char *key , char *value , int *max_len );
00141
00142 void eh_print_symbol(char *key, char *value, FILE *fp)
00143 {
00144 fprintf(fp,"%s : %s\n",key,value);
00145 }
00146
00147 void eh_print_symbol_aligned( char *key, char *value, aligned_st *st )
00148 {
00149 int len = st->max_key_len-strlen(key)+1;
00150 char *fill = g_strnfill( len , ' ' );
00151 fprintf(st->fp,"%s%s: %s\n",key,fill,value);
00152 eh_free(fill);
00153 }
00154
00155 void eh_get_max_key_len( char *key , char *value , int *max_len )
00156 {
00157 int len = strlen(key);
00158 if ( len > *max_len )
00159 *max_len = len;
00160 }
00161
00162 void eh_print_symbol_table( Symbol_table *t , FILE *fp )
00163 {
00164 g_hash_table_foreach( t , (GHFunc)&eh_print_symbol , fp );
00165 }
00166
00167 void eh_print_symbol_table_aligned( Symbol_table *t , FILE *fp )
00168 {
00169 int max_len=0;
00170 aligned_st st;
00171 g_hash_table_foreach( t , (GHFunc)&eh_get_max_key_len , &max_len );
00172 st.fp = fp;
00173 st.max_key_len = max_len;
00174 g_hash_table_foreach( t , (GHFunc)&eh_print_symbol_aligned , &st );
00175 }
00176
00177 gssize eh_symbol_table_size( Symbol_table *t )
00178 {
00179 return g_hash_table_size( t );
00180 }
00181
00182 Symbol_table* eh_destroy_symbol_table(Symbol_table *t)
00183 {
00184 if ( t )
00185 g_hash_table_destroy( (GHashTable*)t );
00186 return NULL;
00187 }
00188
00189 void eh_free_symbol_table_labels( Symbol_table *t )
00190 {
00191 if ( t )
00192 {
00193 g_hash_table_foreach( (GHashTable*)t , &eh_destroy_key , NULL );
00194 }
00195 }
00196
00197 #endif
00198
00199 #if defined( RECORD_FILE )
00200
00201 GHashTable *eh_scan_record_file(const char *filename)
00202 {
00203 GScanner *s;
00204 char *record_name;
00205 Eh_symbol_table symbol_table;
00206 GHashTable *record_table;
00207 GSList *list;
00208
00209 s = eh_open_scanner(filename);
00210
00211 record_table = g_hash_table_new( &g_str_hash , &g_str_equal );
00212
00213 while ( !g_scanner_eof(s) )
00214 {
00215 symbol_table = eh_symbol_table_new();
00216 record_name = eh_scan_next_record(s,symbol_table);
00217 if ( !record_name )
00218 break;
00219 list = (GSList*)g_hash_table_lookup(record_table,record_name);
00220 list = g_slist_append(list,symbol_table);
00221 g_hash_table_insert(record_table,record_name,list);
00222 }
00223
00224
00225 eh_symbol_table_destroy( symbol_table );
00226
00227 eh_close_scanner( s );
00228
00229 return record_table;
00230 }
00231
00232 void eh_destroy_record_file( GHashTable *table )
00233 {
00234 g_hash_table_destroy( table );
00235 }
00236
00237 Symbol_table *eh_scan_record_file_for( const char *filename , const char *rec_name )
00238 {
00239 GHashTable *records;
00240 GSList *list;
00241 Symbol_table *a;
00242
00243
00244
00245 records = eh_scan_record_file( filename );
00246
00247
00248 list = g_hash_table_lookup( records , rec_name );
00249 a = g_slist_nth_data( list , 0 );
00250
00251 g_slist_remove( list , a );
00252 g_slist_free( list );
00253 g_hash_table_remove( records , rec_name );
00254
00255
00256 eh_destroy_record_file( records );
00257
00258 return a;
00259 }
00260
00261 Symbol_table* eh_scan_key_file_for( const gchar* file ,
00262 const gchar* name ,
00263 Symbol_table *tab )
00264 {
00265 Symbol_table* new_tab;
00266
00267
00268
00269
00270
00271
00272 {
00273 Eh_key_file key_file = eh_key_file_scan( file );
00274
00275 if ( eh_key_file_has_group( key_file , name ) )
00276 new_tab = eh_key_file_get_symbol_table( key_file , name );
00277 else
00278 new_tab = NULL;
00279
00280 eh_key_file_destroy( key_file );
00281 }
00282
00283 if ( tab && new_tab )
00284 eh_symbol_table_copy( tab , new_tab );
00285
00286 return new_tab;
00287 }
00288
00289 #endif
00290
00291 #include <errno.h>
00292
00293 GScanner *eh_open_scanner(const char *filename , GError** error )
00294 {
00295 GScanner* s = NULL;
00296
00297 eh_require( filename );
00298 eh_return_val_if_fail( error==NULL || *error==NULL , NULL );
00299
00300 {
00301 int fd = g_open( filename , O_RDONLY );
00302
00303 if ( fd != -1 )
00304 {
00305 GScannerConfig config;
00306
00307 config.cset_skip_characters = g_strdup( " \t\n" );
00308
00309 config.cset_identifier_first = g_strconcat( G_CSET_a_2_z ,
00310 "^%.+-,_0123456789" ,
00311 G_CSET_A_2_Z ,
00312 NULL );
00313 config.cset_identifier_nth = g_strconcat( G_CSET_a_2_z ,
00314 G_CSET_A_2_Z ,
00315 "^%.+-,_0123456789" ,
00316 G_CSET_LATINS ,
00317 G_CSET_LATINC ,
00318 NULL );
00319 config.cpair_comment_single = g_strdup( "#\n" );
00320
00321 config.skip_comment_multi = TRUE;
00322 config.skip_comment_single = TRUE;
00323 config.scan_comment_multi = TRUE;
00324 config.scan_identifier = 1;
00325 config.scan_identifier_1char = 1;
00326 config.scan_identifier_NULL = 1;
00327 config.scan_symbols = 1;
00328 config.scan_binary = FALSE;
00329 config.scan_octal = FALSE;
00330 config.scan_float = FALSE;
00331 config.scan_hex = FALSE;
00332 config.scan_hex_dollar = FALSE;
00333 config.scan_string_sq = TRUE;
00334 config.scan_string_dq = TRUE;
00335 config.numbers_2_int = TRUE;
00336 config.int_2_float = TRUE;
00337 config.identifier_2_string = TRUE;
00338 config.char_2_token = TRUE;
00339 config.symbol_2_token = FALSE;
00340 config.scope_0_fallback = TRUE;
00341
00342 s = g_scanner_new(&config);
00343 s->input_name = filename;
00344
00345 g_scanner_input_file( s , fd );
00346 }
00347 else
00348 eh_set_file_error_from_errno( error , filename , errno );
00349 }
00350
00351 return s;
00352 }
00353
00354 #include <unistd.h>
00355
00356 void eh_close_scanner( GScanner *s )
00357 {
00358 close( s->input_fd );
00359
00360 eh_free( s->config->cset_skip_characters );
00361 eh_free( s->config->cset_identifier_first );
00362 eh_free( s->config->cset_identifier_nth );
00363 eh_free( s->config->cpair_comment_single );
00364
00365 g_scanner_destroy( s );
00366 }
00367
00368 #ifdef OLD
00369
00370 GPtrArray *eh_scan_data_file( const char *filename ,
00371 const char *delimeter ,
00372 gboolean row_major ,
00373 gboolean with_header )
00374 {
00375 GScanner *s;
00376 GPtrArray *data=g_ptr_array_new();
00377 Eh_data_record *entry;
00378
00379 s = eh_open_scanner(filename);
00380
00381 do
00382 {
00383 entry = eh_create_data_record( );
00384
00385 if ( with_header )
00386 eh_scan_next_record(s,entry->symbol_table);
00387
00388 eh_scan_data_record( s , delimeter , row_major , entry->data );
00389
00390 g_ptr_array_add( data , entry );
00391
00392 }
00393 while ( entry->data->len > 0 );
00394
00395
00396 g_ptr_array_remove( data , entry );
00397 eh_free_data_record( entry );
00398 eh_close_scanner( s );
00399
00400 return data;
00401 }
00402
00403 GPtrArray *eh_scan_data_record(GScanner *s, const char *delimeter, gboolean row_major, GPtrArray *data)
00404 {
00405 double d_val;
00406 int i, n_rows;
00407 char *data_line=NULL;
00408 char **data_vals;
00409 GArray *vals;
00410
00411 if ( !g_scanner_scope_lookup_symbol(s,0,"---") )
00412 g_scanner_scope_add_symbol(s,0,"---",g_strdup("---") );
00413
00414
00415
00416
00417
00418
00419
00420 eh_debug( "Find the next record" );
00421 {
00422 char *record_name = eh_seek_record_start(s);
00423 if ( !record_name )
00424 return NULL;
00425 eh_free( record_name );
00426 }
00427
00428 data_line = eh_scan_ascii_data_line(s);
00429 if ( data_line )
00430 data_vals = g_strsplit(data_line,delimeter,-1);
00431 else
00432 eh_error( "Trouble reading first line of data." );
00433 eh_free( data_line );
00434
00435 for ( n_rows=0 ; data_vals[n_rows] ; n_rows++ );
00436
00437 if ( row_major )
00438 {
00439 do
00440 {
00441 vals = g_array_new(FALSE,FALSE,sizeof(double));
00442 for ( i=0 ; i<n_rows && data_vals[i] ; i++ )
00443 {
00444 d_val = g_strtod(data_vals[i],NULL);
00445 g_array_append_val(vals,d_val);
00446 }
00447 g_ptr_array_add(data,vals);
00448 g_strfreev(data_vals);
00449 data_line = eh_scan_ascii_data_line(s);
00450 if ( data_line )
00451 data_vals = g_strsplit(data_line,delimeter,0);
00452 else
00453 data_vals = NULL;
00454 eh_free( data_line );
00455 }
00456 while ( data_vals );
00457 }
00458 else
00459 {
00460 for ( i=0 ; i<n_rows ; i++ )
00461 g_ptr_array_add(data,g_array_new(FALSE,FALSE,sizeof(double)));
00462 do
00463 {
00464 for ( i=0 ; i<n_rows && data_vals[i] ; i++ )
00465 {
00466 d_val = g_strtod(data_vals[i],NULL);
00467 g_array_append_val((GArray*)g_ptr_array_index(data,i),d_val);
00468 }
00469 g_strfreev(data_vals);
00470 data_line = eh_scan_ascii_data_line(s);
00471 if ( data_line )
00472 data_vals = g_strsplit(data_line,delimeter,0);
00473 else
00474 data_vals = NULL;
00475 eh_free( data_line );
00476 }
00477 while ( data_vals );
00478 }
00479
00480 return data;
00481 }
00482
00483 GPtrArray *eh_print_data_table(GPtrArray *data, const char *delimeter, gboolean row_major, FILE *fp )
00484 {
00485 int i, j, n;
00486 GArray *row;
00487
00488 if ( row_major )
00489 {
00490 for ( i=0 ; i<data->len ; i++ )
00491 {
00492 row = g_ptr_array_index( data , i );
00493 for ( j=0 ; j<row->len-1 ; j++ )
00494 fprintf(fp,"%f%c ",g_array_index(row,double,j),*delimeter);
00495 fprintf(fp,"%f\n",g_array_index(row,double,j));
00496 }
00497 }
00498 else
00499 {
00500 row = g_ptr_array_index( data , 0 );
00501 n = row->len;
00502 for ( i=0 ; i<n ; i++ )
00503 {
00504 for ( j=0 ; j<data->len-1 ; j++ )
00505 {
00506 row = g_ptr_array_index( data , j );
00507 fprintf(fp,"%f%c ",g_array_index(row,double,i),*delimeter);
00508 }
00509 row = g_ptr_array_index( data , j );
00510 fprintf(fp,"%f\n",g_array_index(row,double,i));
00511 }
00512 }
00513 return data;
00514 }
00515
00516 #endif
00517 gboolean eh_scanner_eol( GScanner *s );
00518
00519 double* eh_scan_ascii_data_line_dbl( GScanner* s , const char* delim , gssize *len )
00520 {
00521 double* row = NULL;
00522 gssize n_cols = 0;
00523
00524 if ( s )
00525 {
00526 gchar* str = eh_scan_ascii_data_line( s );
00527
00528 if ( str )
00529 {
00530 gssize i;
00531 gchar** data_vals = g_strsplit( str , delim , -1 );
00532
00533 n_cols = g_strv_length( data_vals );
00534 row = eh_new( double , n_cols );
00535
00536 for ( i=0 ; i<n_cols ; i++ )
00537 row[i] = g_ascii_strtod( data_vals[i] , NULL );
00538
00539 g_strfreev( data_vals );
00540
00541 eh_free( str );
00542 }
00543 else
00544 eh_error( "Trouble reading data at line %d (%s)" ,
00545 g_scanner_cur_line(s) ,
00546 (s->input_name)?(s->input_name):("unknown") );
00547 }
00548
00549 *len = n_cols;
00550
00551 return row;
00552 }
00553
00554 char *eh_scan_ascii_data_line(GScanner *s)
00555 {
00556 char *skip = g_strdup(" \t");
00557 char *old_skip;
00558 GString *entry_string;
00559 char *entry;
00560 char c[2];
00561 c[1] = '\0';
00562
00563 old_skip = s->config->cset_skip_characters;
00564 s->config->cset_skip_characters = skip;
00565
00566 if ( g_scanner_eof(s)
00567 || eh_scanner_eor(s) )
00568 {
00569 eh_free( skip );
00570 s->config->cset_skip_characters = old_skip;
00571 return NULL;
00572 }
00573 else
00574 entry_string = g_string_new("");
00575
00576 while ( eh_scanner_eol(s) && !g_scanner_eof(s) );
00577
00578 if ( g_scanner_eof(s) )
00579 {
00580 eh_free( skip );
00581 s->config->cset_skip_characters = old_skip;
00582 g_string_free( entry_string , TRUE );
00583 return NULL;
00584 }
00585
00586 while ( g_scanner_get_next_token(s)!='\n'
00587 && g_scanner_cur_token(s)!='\r'
00588 && !g_scanner_eof(s)
00589 && !eh_scanner_eor(s) )
00590 {
00591 if ( g_scanner_cur_token(s) == G_TOKEN_STRING )
00592 {
00593 g_string_append(entry_string,g_scanner_cur_value(s).v_string);
00594 }
00595 else if ( g_scanner_cur_token(s) < G_TOKEN_NONE )
00596 {
00597 c[0] = g_scanner_cur_token(s);
00598 g_string_append(entry_string,c);
00599 }
00600 else
00601 {
00602 g_scanner_unexp_token(s,G_TOKEN_STRING,NULL,NULL,NULL,"eh_scan_ascii_data_line",TRUE);
00603
00604 g_string_free(entry_string,TRUE);
00605 eh_free( skip );
00606 s->config->cset_skip_characters = old_skip;
00607 return NULL;
00608 }
00609 }
00610
00611 entry = entry_string->str;
00612 g_string_free( entry_string , FALSE );
00613
00614 eh_free( skip );
00615 s->config->cset_skip_characters = old_skip;
00616
00617 return entry;
00618 }
00619
00620 char *eh_scan_next_record( GScanner *s , Eh_symbol_table symbol_table )
00621 {
00622 char *record_name;
00623
00624 if ( !g_scanner_scope_lookup_symbol(s,0,"---") )
00625 g_scanner_scope_add_symbol(s,0,"---",g_strdup("---") );
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635 record_name = eh_seek_record_start(s);
00636 if ( !record_name )
00637 return NULL;
00638
00639
00640
00641 {
00642 char *label, *entry;
00643
00644 while ( !g_scanner_eof(s) )
00645 {
00646 if ( g_scanner_peek_next_token(s) == G_TOKEN_STRING )
00647 {
00648 label = eh_scan_label(s);
00649 entry = eh_scan_entry(s);
00650
00651 eh_symbol_table_insert(symbol_table,label,entry);
00652
00653 eh_free( label );
00654 eh_free( entry );
00655 }
00656 else if ( eh_scanner_eor(s) )
00657 break;
00658 else if ( g_scanner_peek_next_token(s) == G_TOKEN_EOF )
00659 break;
00660 else if ( !eh_scanner_eol(s) )
00661 {
00662 g_scanner_unexp_token(s,G_TOKEN_STRING,NULL,NULL,NULL,"eh_scan_next_record",TRUE);
00663 eh_exit(-1);
00664 }
00665 }
00666 }
00667
00668 return record_name;
00669 }
00670
00671 gboolean
00672 eh_scanner_eor(GScanner *s)
00673 {
00674 if ( g_scanner_peek_next_token(s) == G_TOKEN_SYMBOL )
00675 {
00676
00677 if ( strncmp((char*)(s->next_value.v_symbol),"---",3)==0 )
00678 return TRUE;
00679 else
00680 {
00681 g_scanner_unexp_token(s,G_TOKEN_STRING,NULL,NULL,NULL,"eh_scanner_eor",TRUE);
00682 eh_exit(-1);
00683 }
00684 }
00685 else if ( g_scanner_peek_next_token(s) == G_TOKEN_LEFT_BRACE )
00686 return TRUE;
00687
00688 return FALSE;
00689 }
00690
00691 gboolean eh_scanner_eol( GScanner *s )
00692 {
00693 if ( g_scanner_peek_next_token(s) != '\n'
00694 && g_scanner_peek_next_token(s) != '\r' )
00695 return FALSE;
00696 else
00697 {
00698 g_scanner_get_next_token(s);
00699 eh_scanner_eol(s);
00700 return TRUE;
00701 }
00702 }
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719 char*
00720 eh_scan_label(GScanner *s)
00721 {
00722 char *first = g_strconcat(G_CSET_a_2_z," _0123456789",G_CSET_A_2_Z,NULL);
00723 char *nth = g_strconcat(G_CSET_a_2_z,G_CSET_A_2_Z," _0123456789",G_CSET_LATINS,G_CSET_LATINC,NULL);
00724 char* comment = g_strdup( "()" );
00725 GString *label = g_string_new("");
00726 char *old_first, *old_nth, *old_comment;
00727 char* ans;
00728 char c[2];
00729 c[1] = '\0';
00730
00731 old_first = s->config->cset_identifier_first;
00732 old_nth = s->config->cset_identifier_nth;
00733 old_comment = s->config->cpair_comment_single;
00734
00735 s->config->cset_identifier_first = first;
00736 s->config->cset_identifier_nth = nth;
00737 s->config->cpair_comment_single = comment;
00738
00739 while ( g_scanner_peek_next_token(s) != ':' && g_scanner_peek_next_token(s) != G_TOKEN_EOF )
00740 {
00741 if ( g_scanner_get_next_token(s) == G_TOKEN_STRING )
00742 g_string_append(label,g_scanner_cur_value(s).v_string);
00743 else if ( g_scanner_cur_token(s) < G_TOKEN_NONE )
00744 {
00745 c[0] = g_scanner_cur_token(s);
00746 g_string_append(label,c);
00747 }
00748 else
00749 {
00750 g_scanner_unexp_token(s,G_TOKEN_STRING,NULL,NULL,NULL,"get_label",TRUE);
00751 eh_exit(-1);
00752 }
00753 }
00754 if ( g_scanner_peek_next_token(s) == G_TOKEN_EOF )
00755 g_scanner_unexp_token(s,(GTokenType)':',NULL,NULL,NULL,"get_label",TRUE);
00756
00757 g_scanner_get_next_token(s);
00758
00759 s->config->cset_identifier_first = old_first;
00760 s->config->cset_identifier_nth = old_nth;
00761 s->config->cpair_comment_single = old_comment;
00762
00763 eh_free( comment );
00764 eh_free( nth );
00765 eh_free( first );
00766
00767 ans = label->str;
00768
00769 g_string_free( label , FALSE );
00770
00771 return g_strstrip(ans);
00772 }
00773
00774 char *eh_scan_entry(GScanner *s)
00775 {
00776 char *skip = g_strdup(" \t");
00777 char *first = g_strconcat(G_CSET_a_2_z,"_0123456789",G_CSET_A_2_Z,NULL);
00778 char *nth = g_strconcat(G_CSET_a_2_z,G_CSET_A_2_Z,"_0123456789",G_CSET_LATINS,G_CSET_LATINC,NULL);
00779 char *old_skip, *old_first, *old_nth;
00780 GString *entry = g_string_new("");
00781 char* ans;
00782 char c[2];
00783 c[1] = '\0';
00784
00785 old_first = s->config->cset_identifier_first;
00786 old_nth = s->config->cset_identifier_nth;
00787 old_skip = s->config->cset_skip_characters;
00788
00789 s->config->cset_skip_characters = skip;
00790 s->config->cset_identifier_first = first;
00791 s->config->cset_identifier_nth = nth;
00792
00793 while ( g_scanner_peek_next_token(s) != '\n' && g_scanner_peek_next_token(s) != G_TOKEN_EOF )
00794 {
00795 if ( g_scanner_get_next_token(s) == G_TOKEN_STRING )
00796 g_string_append(entry,g_scanner_cur_value(s).v_string);
00797 else if ( g_scanner_cur_token(s) < G_TOKEN_NONE )
00798 {
00799 c[0] = g_scanner_cur_token(s);
00800 g_string_append(entry,c);
00801 }
00802 else
00803 {
00804 g_scanner_unexp_token(s,G_TOKEN_STRING,NULL,NULL,NULL,"get_entry",TRUE);
00805 eh_exit(-1);
00806 }
00807 }
00808
00809 g_scanner_get_next_token(s);
00810
00811 s->config->cset_skip_characters = old_skip;
00812 s->config->cset_identifier_first = old_first;
00813 s->config->cset_identifier_nth = old_nth;
00814
00815 eh_free( skip );
00816 eh_free( nth );
00817 eh_free( first );
00818
00819 ans = entry->str;
00820
00821 g_string_free( entry , FALSE );
00822
00823 return g_strstrip(ans);
00824 }
00825
00826 #include <string.h>
00827
00828 char *eh_seek_record_start(GScanner *s)
00829 {
00830 gchar* ans;
00831 char c[2];
00832 GString *record_name = g_string_new("");
00833 c[1] = '\0';
00834
00835 while ( !g_scanner_eof(s) )
00836 {
00837 if ( g_scanner_get_next_token(s) == G_TOKEN_SYMBOL )
00838 {
00839 if ( strncmp((char*)g_scanner_cur_value(s).v_symbol,"---",3)==0 )
00840 break;
00841 }
00842 else if ( g_scanner_cur_token(s)==G_TOKEN_LEFT_BRACE )
00843 break;
00844 }
00845
00846 if ( g_scanner_eof(s) )
00847 {
00848 g_string_free( record_name , TRUE );
00849 return NULL;
00850 }
00851
00852 while ( !g_scanner_eof(s) )
00853 {
00854 if ( g_scanner_get_next_token(s) == G_TOKEN_STRING )
00855 {
00856 g_string_append(record_name,g_scanner_cur_value(s).v_string);
00857 }
00858 else if ( g_scanner_cur_token(s) == G_TOKEN_RIGHT_BRACE )
00859 break;
00860 else if ( g_scanner_cur_token(s) == G_TOKEN_SYMBOL )
00861 {
00862 if ( strncmp((char*)g_scanner_cur_value(s).v_symbol,"---",3)==0 )
00863 break;
00864 }
00865 else if ( g_scanner_cur_token(s) < G_TOKEN_NONE )
00866 {
00867 c[0] = g_scanner_cur_token(s);
00868 g_string_append(record_name,c);
00869 }
00870 else
00871 {
00872 g_scanner_unexp_token(s,G_TOKEN_STRING,NULL,NULL,NULL,"get_next_record",TRUE);
00873 eh_exit(-1);
00874 }
00875 }
00876
00877 while ( eh_scanner_eol(s) && !g_scanner_eof(s) );
00878
00879 ans = record_name->str;
00880
00881 g_string_free( record_name , FALSE );
00882
00883 return ans;
00884 }
00885
00886 #ifdef OLD
00887
00888 Eh_data_record *eh_create_data_record()
00889 {
00890 Eh_data_record *p=eh_new( Eh_data_record , 1 );
00891 p->symbol_table = eh_create_symbol_table();
00892 p->data = g_ptr_array_new();
00893 return p;
00894 }
00895
00896 void eh_free_data_record(Eh_data_record *p)
00897 {
00898 eh_destroy_symbol_table( p->symbol_table );
00899 g_ptr_array_free( p->data , FALSE );
00900 eh_free( p );
00901 }
00902
00903 void eh_print_data_record( Eh_data_record *p , char *rec_name , char *delimeter , gboolean row_major , gboolean with_header , FILE *fp )
00904 {
00905 if ( with_header )
00906 {
00907 fprintf( fp , "[ %s ]\n" , rec_name );
00908 eh_print_symbol_table_aligned( p->symbol_table , fp );
00909 fprintf(fp,"[ data ]\n");
00910 }
00911 eh_print_data_table( p->data , delimeter , row_major , fp );
00912 }
00913
00914 int
00915 eh_get_data_record_size( Eh_data_record *p , int dim )
00916 {
00917 if ( dim==0 )
00918 return p->data->len;
00919 else if ( dim==1 )
00920 return ((GArray*)g_ptr_array_index(p->data,0))->len;
00921 else
00922 {
00923 eh_info( "dimension argument must be 0 or 1 : eh_get_data_record_size" );
00924 eh_exit(-1);
00925 }
00926 eh_require_not_reached();
00927 return -1;
00928 }
00929
00930 Symbol_table *eh_get_data_record_sym_table(Eh_data_record *p)
00931 {
00932 return p->symbol_table;
00933 }
00934
00935 GArray *eh_get_data_record_row(Eh_data_record *p,int row)
00936 {
00937 if ( row < eh_get_data_record_size(p,0) )
00938 return (GArray*)g_ptr_array_index(p->data,row);
00939 else
00940 eh_error( "eh_get_data_record_row: Row is out of bounds: %d >= %d" ,
00941 row , eh_get_data_record_size(p,0) );
00942 eh_require_not_reached();
00943 return NULL;
00944 }
00945
00946 void eh_set_data_record_row( Eh_data_record *p , int row , GArray *a )
00947 {
00948 g_ptr_array_index( p->data , row ) = a;
00949 }
00950
00951 void eh_add_data_record_row( Eh_data_record *p , GArray *a )
00952 {
00953 g_ptr_array_add( p->data , a );
00954 }
00955
00956 void eh_add_data_record_label( Eh_data_record *p , char *label , char *value )
00957 {
00958 eh_symbol_table_insert( p->symbol_table , label , value );
00959 }
00960
00961 void eh_interpolate_data_record_rows( Eh_data_record *p , int row , GArray *x )
00962 {
00963 GArray *x1, *y1, *y;
00964 int i, len1, len = eh_get_data_record_size( p , 0 );
00965
00966 x1 = eh_get_data_record_row( p , row );
00967 len1 = x1->len;
00968
00969
00970 eh_set_data_record_row( p , row , x );
00971
00972 for ( i=0 ; i<len ; i++ )
00973 {
00974 if ( i!=row )
00975 {
00976
00977 y = g_array_new( FALSE , FALSE , sizeof(double) );
00978 g_array_set_size( y , x->len );
00979
00980
00981 y1 = eh_get_data_record_row( p , i );
00982
00983
00984 interpolate( (double*)(x1->data) , (double*)(y1->data) , len1 ,
00985 (double*)(x->data) , (double*)(y->data) , x->len );
00986
00987
00988 g_array_free( y1 , FALSE );
00989 eh_set_data_record_row( p , i , y );
00990 }
00991 }
00992
00993
00994
00995 g_array_free( x1 , FALSE );
00996 }
00997
00998 Eh_record_file *eh_open_record_file( const char *filename )
00999 {
01000 Eh_record_file *rec_file = eh_new0( Eh_record_file , 1 );
01001
01002 rec_file->filename = g_strdup( filename );
01003 rec_file->records = eh_scan_record_file( filename );
01004
01005 return rec_file;
01006 }
01007
01008 void eh_close_record_file( Eh_record_file *rec_file )
01009 {
01010 if ( rec_file )
01011 {
01012 eh_free( rec_file->filename );
01013 eh_free( rec_file );
01014 }
01015 }
01016
01017 Eh_symbol_table *eh_get_record_from_record_file( Eh_record_file *rec_file , const char *name )
01018 {
01019 GSList *rec_list = g_hash_table_lookup( rec_file->records , name );
01020 return g_slist_nth_data( rec_list , 0 );
01021 }
01022
01023 char *eh_get_value_from_record_file( Eh_record_file *rec_file , const char *rec_name , const char *label )
01024 {
01025 Eh_symbol_table *record = eh_get_record_from_record_file( rec_file , rec_name );
01026 return eh_symbol_table_lookup( record , (char*)label );
01027 }
01028
01029 #endif
01030
01031 Eh_data_file*
01032 eh_open_data_file( const char *filename , Eh_data_file_attr *attr , GError** error )
01033 {
01034 Eh_data_file* data_file = NULL;
01035 GError* tmp_err = NULL;
01036
01037 eh_return_val_if_fail( error==NULL || *error==NULL , NULL );
01038
01039 data_file = eh_new0( Eh_data_file , 1 );
01040
01041 if ( attr==NULL )
01042 {
01043 data_file->delimeter = ",";
01044 data_file->fast_dim = EH_FAST_DIM_COL;
01045 data_file->row_major = FALSE;
01046 data_file->with_header = TRUE;
01047 }
01048 else
01049 {
01050 data_file->delimeter = attr->delimeter;
01051 data_file->fast_dim = EH_FAST_DIM_COL;
01052 data_file->row_major = attr->row_major;
01053 data_file->with_header = attr->with_header;
01054 }
01055
01056 data_file->filename = g_strdup( filename );
01057 data_file->records = eh_data_record_scan_file( filename ,
01058 data_file->delimeter ,
01059 data_file->fast_dim ,
01060 data_file->with_header ,
01061 &tmp_err );
01062
01063 if ( tmp_err )
01064 {
01065 g_propagate_error( error , tmp_err );
01066
01067 eh_free( data_file->filename );
01068 eh_free( data_file );
01069
01070 data_file = NULL;
01071 }
01072
01073 return data_file;
01074 }
01075
01076 void eh_close_data_file( Eh_data_file *data_file )
01077 {
01078 if ( data_file )
01079 {
01080 gssize i;
01081 eh_free( data_file->filename );
01082 for ( i=0 ; data_file->records[i] ; i++ )
01083 eh_data_record_destroy( data_file->records[i] );
01084 eh_free( data_file->records );
01085 }
01086 }
01087
01088 int eh_get_data_file_size( Eh_data_file *data_file )
01089 {
01090 gssize len=0;
01091 eh_require( data_file )
01092 {
01093 for ( len=0 ; data_file->records[len] ; len++ );
01094 }
01095 return len;
01096 }
01097
01098 #ifdef OLD
01099 Eh_data_record *eh_get_data_from_file( Eh_data_file *data_file , int i )
01100 {
01101 return (Eh_data_record*)g_ptr_array_index( data_file->records , i );
01102 }
01103 #endif
01104