static int64_t _config_string_to_int( const char* str ) { unsigned int length = string_length( str ); unsigned int first_nonnumeric; unsigned int dot_position; if( length < 2 ) return string_to_int64( str ); first_nonnumeric = string_find_first_not_of( str, "0123456789.", 0 ); if( ( first_nonnumeric == ( length - 1 ) ) && ( ( str[ first_nonnumeric ] == 'm' ) || ( str[ first_nonnumeric ] == 'M' ) ) ) { dot_position = string_find( str, '.', 0 ); if( dot_position != STRING_NPOS ) { if( string_find( str, '.', dot_position + 1 ) == STRING_NPOS ) return (int64_t)( string_to_real( str ) * ( REAL_C( 1024.0 ) * REAL_C( 1024.0 ) ) ); return string_to_int64( str ); //More than one dot } return string_to_int64( str ) * ( 1024LL * 1024LL ); } if( ( first_nonnumeric == ( length - 1 ) ) && ( ( str[ first_nonnumeric ] == 'k' ) || ( str[ first_nonnumeric ] == 'K' ) ) ) { dot_position = string_find( str, '.', 0 ); if( dot_position != STRING_NPOS ) { if( string_find( str, '.', dot_position + 1 ) == STRING_NPOS ) return (int64_t)( string_to_real( str ) * REAL_C( 1024.0 ) ); return string_to_int64( str ); //More than one dot } return string_to_int64( str ) * 1024LL; } return string_to_int64( str ); }
void config_parse_commandline( const char* const* cmdline, unsigned int num ) { //TODO: Implement, format --section:key=value unsigned int arg; for( arg = 0; arg < num; ++arg ) { if( string_match_pattern( cmdline[arg], "--*:*=*" ) ) { unsigned int first_sep = string_find( cmdline[arg], ':', 0 ); unsigned int second_sep = string_find( cmdline[arg], '=', 0 ); if( ( first_sep != STRING_NPOS ) && ( second_sep != STRING_NPOS ) && ( first_sep < second_sep ) ) { unsigned int section_length = first_sep - 2; unsigned int end_pos = first_sep + 1; unsigned int key_length = second_sep - end_pos; const char* section_str = cmdline[arg] + 2; const char* key_str = pointer_offset_const( cmdline[arg], end_pos ); hash_t section = hash( section_str, section_length ); hash_t key = hash( key_str, key_length ); char* value = string_substr( cmdline[arg], second_sep + 1, STRING_NPOS ); char* set_value = value; unsigned int value_length = string_length( value ); if( !value_length ) config_set_string( section, key, "" ); else if( string_equal( value, "false" ) ) config_set_bool( section, key, false ); else if( string_equal( value, "true" ) ) config_set_bool( section, key, true ); else if( ( string_find( value, '.', 0 ) != STRING_NPOS ) && ( string_find_first_not_of( value, "0123456789.", 0 ) == STRING_NPOS ) && ( string_find( value, '.', string_find( value, '.', 0 ) + 1 ) == STRING_NPOS ) ) //Exactly one "." config_set_real( section, key, string_to_real( value ) ); else if( string_find_first_not_of( value, "0123456789", 0 ) == STRING_NPOS ) config_set_int( section, key, string_to_int64( value ) ); else { if( ( value_length > 1 ) && ( value[0] == '"' ) && ( value[ value_length - 1 ] == '"' ) ) { value[ value_length - 1 ] = 0; set_value = value + 1; config_set_string( section, key, set_value ); } else { config_set_string( section, key, value ); } } log_infof( HASH_CONFIG, "Config value from command line: %.*s:%.*s = %s", section_length, section_str, key_length, key_str, set_value ); string_deallocate( value ); } } } }
float64_t stream_read_float64( stream_t* stream ) { float64_t value = 0; if( stream_is_binary( stream ) ) { stream_read( stream, &value, 8 ); if( stream && stream->swap ) byteorder_swap( &value, 8 ); } else { char* str = stream_read_string( stream ); value = string_to_real( str ); string_deallocate( str ); } return value; }
static int real(It beg, It end, T& result) noexcept { return string_to_real(beg, end, result); }
int fgetline(FILE *imgcfg, char *strchar, float *x) { char *st, *st1, *st4, *st5, *st6, *pch; char *strcha2; int k, len, rval; st=(char *)calloc(200,sizeof(char)); st1=(char *)calloc(200,sizeof(char)); st4=(char *)calloc(200,sizeof(char)); st5=(char *)calloc(200,sizeof(char)); st6=(char *)calloc(200,sizeof(char)); strcha2=(char *)calloc(200,sizeof(char)); strcpy(strcha2,strchar); strncat(strcha2," ",1); if ( fgets(st,200,imgcfg) == NULL ) { perror ("Error reading file in fgetline (1)"); } if(feof(imgcfg)==0) { strcpy(st4,""); strncat(st4,st,3); strcpy(st1,""); //strncat(st1,st+4,200); strncat(st1,st+4,120); // st1 = const_cast<char*>((std::string(st1) + std::string(st)).c_str()); while(feof(imgcfg)==0 && ( strncmp(st4,"set",3)!=0 || strncmp(st1, strcha2,strlen(strcha2))!=0)) { if ( fgets(st,200,imgcfg) == NULL ) { perror ("Error reading file in fgetline (2)"); } strcpy(st4,""); strncat(st4,st,3); strcpy(st1,""); strncat(st1,st+4,120); } if(feof(imgcfg)==0 && strncmp(st1, strcha2,strlen(strcha2))==0) { pch=(char *)memchr(st, '"', strlen(st)); strcpy(st5,pch+1); pch=(char *)memchr(st5, '"',strlen(st5)); strcpy(st6,""); strncat(st6,st5, strlen(st5)-strlen(pch)); k=0; pch=(char *)memchr(st6,',',strlen(st6)); while(pch!=NULL) { strcpy(st1,""); strncat(st1,st6, strlen(st6)-strlen(pch)); x[k]=string_to_real(st1); len=strlen(st1); k++; strcpy(st1,""); strncat(st1,st6+len+1,120); strcpy(st6,st1); pch=(char *)memchr(st6,',',strlen(st6)); } x[k]=string_to_real(st6); rewind(imgcfg); rval=0; } else { printf(" '%s' float parameter does not exist in 2dx_image.cfg\n", strchar); rewind(imgcfg); rval=1; } } else { printf(" '%s' float parameter does not exist in 2dx_image.cfg \n", strchar); rewind(imgcfg); rval=1; } // printf("::fgetline.c: parameter %s is %f\n", strchar,x[0]); free(st); free(st1); free(st4); free(st5); free(st6); free(strcha2); return(rval); }
void config_parse( stream_t* stream, hash_t filter_section, bool overwrite ) { char* buffer; hash_t section = 0; hash_t key = 0; unsigned int line = 0; #if BUILD_ENABLE_DEBUG_CONFIG log_debugf( HASH_CONFIG, "Parsing config stream: %s", stream_path( stream ) ); #endif buffer = memory_allocate_zero( 1024ULL, 0, MEMORY_TEMPORARY ); while( !stream_eos( stream ) ) { ++line; stream_read_line_buffer( stream, buffer, 1024, '\n' ); string_strip( buffer, " \t\n\r" ); if( !string_length( buffer ) || ( buffer[0] == ';' ) || ( buffer[0] == '#' ) ) continue; if( buffer[0] == '[' ) { //Section declaration unsigned int endpos = string_rfind( buffer, ']', string_length( buffer ) - 1 ); if( ( endpos == STRING_NPOS ) || ( endpos < 1 ) ) { log_warnf( HASH_CONFIG, WARNING_BAD_DATA, "Invalid section declaration on line %d in config stream '%s'", line, stream_path( stream ) ); continue; } buffer[endpos] = 0; section = hash( buffer + 1, endpos - 1 ); #if BUILD_ENABLE_DEBUG_CONFIG log_debugf( HASH_CONFIG, " config: section set to '%s' (0x%llx)", buffer + 1, section ); #endif } else if( !filter_section || ( filter_section == section ) ) { //name=value declaration char* name; char* value; unsigned int separator = string_find( buffer, '=', 0 ); if( separator == STRING_NPOS ) { log_warnf( HASH_CONFIG, WARNING_BAD_DATA, "Invalid value declaration on line %d in config stream '%s', missing assignment operator '=': %s", line, stream_path( stream ), buffer ); continue; } name = string_strip_substr( buffer, " \t", separator ); value = string_strip( buffer + separator + 1, " \t" ); if( !string_length( name ) ) { log_warnf( HASH_CONFIG, WARNING_BAD_DATA, "Invalid value declaration on line %d in config stream '%s', empty name string", line, stream_path( stream ) ); continue; } key = hash( name, string_length( name ) ); if( overwrite || !config_key( section, key, false ) ) { #if BUILD_ENABLE_DEBUG_CONFIG log_debugf( HASH_CONFIG, " config: %s (0x%llx) = %s", name, key, value ); #endif if( !string_length( value ) ) config_set_string( section, key, "" ); else if( string_equal( value, "false" ) ) config_set_bool( section, key, false ); else if( string_equal( value, "true" ) ) config_set_bool( section, key, true ); else if( ( string_find( value, '.', 0 ) != STRING_NPOS ) && ( string_find_first_not_of( value, "0123456789.", 0 ) == STRING_NPOS ) && ( string_find( value, '.', string_find( value, '.', 0 ) + 1 ) == STRING_NPOS ) ) //Exactly one "." config_set_real( section, key, string_to_real( value ) ); else if( string_find_first_not_of( value, "0123456789", 0 ) == STRING_NPOS ) config_set_int( section, key, string_to_int64( value ) ); else config_set_string( section, key, value ); } } } memory_deallocate( buffer ); }