dstrings in read_params

This commit is contained in:
Ian Jauslin 2023-11-03 16:42:27 -04:00
parent 04a15dd2c7
commit 9d232a8fca
2 changed files with 27 additions and 43 deletions

View File

@ -200,13 +200,13 @@ int dstring_cmp_str(dstring dstring, char* str){
unsigned int j; unsigned int j;
for(j=0;j<dstring.length && str[j]!='\0';j++){ for(j=0;j<dstring.length && str[j]!='\0';j++){
if(dstring.string[j]!=str[j]){ if(dstring.string[j]!=str[j]){
return(0); return(1);
} }
} }
if(j==dstring.length && str[j]=='\0'){ if(j==dstring.length && str[j]=='\0'){
return(1); return(0);
} }
return(0); return(1);
} }
// format strings // format strings

View File

@ -65,7 +65,7 @@ int print_params(nstrophy_parameters parameters, char* initfile_str, char* drivi
// read command line arguments // read command line arguments
int read_args(int argc, const char* argv[], dstring* params, unsigned int* command, unsigned int* nthreads, dstring* savefile_str, dstring* utfile_str, dstring* resumefile_str); int read_args(int argc, const char* argv[], dstring* params, unsigned int* command, unsigned int* nthreads, dstring* savefile_str, dstring* utfile_str, dstring* resumefile_str);
int set_default_params(nstrophy_parameters* parameters); int set_default_params(nstrophy_parameters* parameters);
int read_params(char* param_str, nstrophy_parameters* parameters, dstring* initfile_str, dstring* drivingfile_str); int read_params(dstring param_str, nstrophy_parameters* parameters, dstring* initfile_str, dstring* drivingfile_str);
int set_parameter(char* lhs, char* rhs, nstrophy_parameters* parameters, bool* setN1, bool* setN2, dstring* initfile_str, dstring* drivingfile_str); int set_parameter(char* lhs, char* rhs, nstrophy_parameters* parameters, bool* setN1, bool* setN2, dstring* initfile_str, dstring* drivingfile_str);
// read args from file // read args from file
int args_from_file(dstring* params, unsigned int* command, unsigned int* nthreads, dstring* savefile_str, dstring* utfile_str, char* file_str); int args_from_file(dstring* params, unsigned int* command, unsigned int* nthreads, dstring* savefile_str, dstring* utfile_str, char* file_str);
@ -134,7 +134,7 @@ int main (
// set default params // set default params
set_default_params(&parameters); set_default_params(&parameters);
// read params // read params
ret=read_params(dstring_to_str_noinit(&param_str), &parameters, &initfile_str, &drivingfile_str); ret=read_params(param_str, &parameters, &initfile_str, &drivingfile_str);
if(ret<0){ if(ret<0){
return(-1); return(-1);
} }
@ -146,7 +146,7 @@ int main (
return(-1); return(-1);
} }
// read params // read params
ret=read_params(dstring_to_str_noinit(&param_str), &parameters, &initfile_str, &drivingfile_str); ret=read_params(param_str, &parameters, &initfile_str, &drivingfile_str);
if(ret<0){ if(ret<0){
return(-1); return(-1);
} }
@ -157,7 +157,7 @@ int main (
return(-1); return(-1);
} }
// reread params // reread params
ret=read_params(dstring_to_str_noinit(&param_str), &parameters, &initfile_str, &drivingfile_str); ret=read_params(param_str, &parameters, &initfile_str, &drivingfile_str);
if(ret<0){ if(ret<0){
return(-1); return(-1);
} }
@ -505,77 +505,61 @@ int set_default_params(
// read parameters string // read parameters string
int read_params( int read_params(
char* param_str, dstring param_str,
nstrophy_parameters* parameters, nstrophy_parameters* parameters,
dstring* initfile_str, dstring* initfile_str,
dstring* drivingfile_str dstring* drivingfile_str
){ ){
int ret; int ret;
// pointer in params unsigned int i;
char* ptr;
// buffer and associated pointer // buffer and associated pointer
char *buffer_lhs, *lhs_ptr; dstring buffer_lhs;
char *buffer_rhs, *rhs_ptr; dstring buffer_rhs;
// whether N was set explicitly // whether N was set explicitly
bool setN1=false; bool setN1=false;
bool setN2=false; bool setN2=false;
// whether lhs (false is rhs) // which buffer to add to
bool lhs=true; dstring* buffer=&buffer_lhs;
// init // init
buffer_lhs=calloc(sizeof(char),strlen(param_str)); dstring_init(&buffer_lhs, param_str.length);
lhs_ptr=buffer_lhs; dstring_init(&buffer_rhs, param_str.length);
*lhs_ptr='\0';
buffer_rhs=calloc(sizeof(char),strlen(param_str));
rhs_ptr=buffer_rhs;
*rhs_ptr='\0';
for(ptr=param_str;*ptr!='\0';ptr++){ for(i=0;i<param_str.length;i++){
switch(*ptr){ switch(param_str.string[i]){
case '=': case '=':
// reset buffer // reset buffer
rhs_ptr=buffer_rhs; buffer_rhs.length=0;
*rhs_ptr='\0'; buffer=&buffer_rhs;
lhs=false;
break; break;
case ';': case ';':
//set parameter //set parameter
ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2, initfile_str, drivingfile_str); ret=set_parameter(dstring_to_str_noinit(&buffer_lhs), dstring_to_str_noinit(&buffer_rhs), parameters, &setN1, &setN2, initfile_str, drivingfile_str);
if(ret<0){ if(ret<0){
return ret; return ret;
} }
// reset buffer // reset buffer
lhs_ptr=buffer_lhs; buffer_lhs.length=0;
*lhs_ptr='\0'; buffer=&buffer_lhs;
lhs=true;
break; break;
default: default:
// add to buffer // add to buffer
if (lhs){ dstring_append(param_str.string[i],buffer);
*lhs_ptr=*ptr;
lhs_ptr++;
*lhs_ptr='\0';
}
else{
*rhs_ptr=*ptr;
rhs_ptr++;
*rhs_ptr='\0';
}
break; break;
} }
} }
// set last param // set last param
if (*param_str!='\0'){ if (param_str.length!=0){
ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2, initfile_str, drivingfile_str); ret=set_parameter(dstring_to_str_noinit(&buffer_lhs), dstring_to_str_noinit(&buffer_rhs), parameters, &setN1, &setN2, initfile_str, drivingfile_str);
if(ret<0){ if(ret<0){
return ret; return ret;
} }
} }
// free vects // free vects
free(buffer_lhs); dstring_free(buffer_lhs);
free(buffer_rhs); dstring_free(buffer_rhs);
// if N not set explicitly, set it to the smallest power of 2 that is >3*K+1 (the fft is faster on vectors whose length is a power of 2) // if N not set explicitly, set it to the smallest power of 2 that is >3*K+1 (the fft is faster on vectors whose length is a power of 2)
if (!setN1){ if (!setN1){