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;
for(j=0;j<dstring.length && str[j]!='\0';j++){
if(dstring.string[j]!=str[j]){
return(0);
return(1);
}
}
if(j==dstring.length && str[j]=='\0'){
return(1);
return(0);
}
return(0);
return(1);
}
// format strings

View File

@ -65,7 +65,7 @@ int print_params(nstrophy_parameters parameters, char* initfile_str, char* drivi
// 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 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);
// 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);
@ -134,7 +134,7 @@ int main (
// set default params
set_default_params(&parameters);
// 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){
return(-1);
}
@ -146,7 +146,7 @@ int main (
return(-1);
}
// 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){
return(-1);
}
@ -157,7 +157,7 @@ int main (
return(-1);
}
// 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){
return(-1);
}
@ -505,77 +505,61 @@ int set_default_params(
// read parameters string
int read_params(
char* param_str,
dstring param_str,
nstrophy_parameters* parameters,
dstring* initfile_str,
dstring* drivingfile_str
){
int ret;
// pointer in params
char* ptr;
unsigned int i;
// buffer and associated pointer
char *buffer_lhs, *lhs_ptr;
char *buffer_rhs, *rhs_ptr;
dstring buffer_lhs;
dstring buffer_rhs;
// whether N was set explicitly
bool setN1=false;
bool setN2=false;
// whether lhs (false is rhs)
bool lhs=true;
// which buffer to add to
dstring* buffer=&buffer_lhs;
// init
buffer_lhs=calloc(sizeof(char),strlen(param_str));
lhs_ptr=buffer_lhs;
*lhs_ptr='\0';
buffer_rhs=calloc(sizeof(char),strlen(param_str));
rhs_ptr=buffer_rhs;
*rhs_ptr='\0';
dstring_init(&buffer_lhs, param_str.length);
dstring_init(&buffer_rhs, param_str.length);
for(ptr=param_str;*ptr!='\0';ptr++){
switch(*ptr){
for(i=0;i<param_str.length;i++){
switch(param_str.string[i]){
case '=':
// reset buffer
rhs_ptr=buffer_rhs;
*rhs_ptr='\0';
lhs=false;
buffer_rhs.length=0;
buffer=&buffer_rhs;
break;
case ';':
//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){
return ret;
}
// reset buffer
lhs_ptr=buffer_lhs;
*lhs_ptr='\0';
lhs=true;
buffer_lhs.length=0;
buffer=&buffer_lhs;
break;
default:
// add to buffer
if (lhs){
*lhs_ptr=*ptr;
lhs_ptr++;
*lhs_ptr='\0';
}
else{
*rhs_ptr=*ptr;
rhs_ptr++;
*rhs_ptr='\0';
}
dstring_append(param_str.string[i],buffer);
break;
}
}
// set last param
if (*param_str!='\0'){
ret=set_parameter(buffer_lhs, buffer_rhs, parameters, &setN1, &setN2, initfile_str, drivingfile_str);
if (param_str.length!=0){
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){
return ret;
}
}
// free vects
free(buffer_lhs);
free(buffer_rhs);
dstring_free(buffer_lhs);
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 (!setN1){