Skip to content

Commit

Permalink
Use reentrant version of strtok(), i.e. strtok_r() or strtok_s(), to …
Browse files Browse the repository at this point in the history
…get tokens
  • Loading branch information
brchiu committed Jan 25, 2016
1 parent 2fdc614 commit 889b651
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 30 deletions.
14 changes: 7 additions & 7 deletions matlab/libsvmread.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void read_problem(const char *filename, int nlhs, mxArray *plhs[])
int max_index, min_index, inst_max_index;
size_t elements, k, i, l=0;
FILE *fp = fopen(filename,"r");
char *endptr;
char *endptr, *r = (char *)NULL;
mwIndex *ir, *jc;
double *labels, *samples;

Expand All @@ -83,11 +83,11 @@ void read_problem(const char *filename, int nlhs, mxArray *plhs[])
int index = 0;

inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0
strtok(line," \t"); // label
strtok_r(line," \t",&r); // label
while (1)
{
idx = strtok(NULL,":"); // index:value
val = strtok(NULL," \t");
idx = strtok_r(NULL,":",&r); // index:value
val = strtok_r(NULL," \t",&r);
if(val == NULL)
break;

Expand Down Expand Up @@ -131,7 +131,7 @@ void read_problem(const char *filename, int nlhs, mxArray *plhs[])

readline(fp);

label = strtok(line," \t\n");
label = strtok_r(line," \t\n",&r);
if(label == NULL)
{
mexPrintf("Empty line at line %d\n",i+1);
Expand All @@ -149,8 +149,8 @@ void read_problem(const char *filename, int nlhs, mxArray *plhs[])
// features
while(1)
{
idx = strtok(NULL,":");
val = strtok(NULL," \t");
idx = strtok_r(NULL,":",&r);
val = strtok_r(NULL," \t",&r);
if(val == NULL)
break;

Expand Down
6 changes: 3 additions & 3 deletions matlab/svmpredict.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,12 @@ void mexFunction( int nlhs, mxArray *plhs[],
if(nrhs==4)
{
int i, argc = 1;
char cmd[CMD_LEN], *argv[CMD_LEN/2];
char cmd[CMD_LEN], *argv[CMD_LEN/2], *r = (char *)NULL;

// put options in argv[]
mxGetString(prhs[3], cmd, mxGetN(prhs[3]) + 1);
if((argv[argc] = strtok(cmd, " ")) != NULL)
while((argv[++argc] = strtok(NULL, " ")) != NULL)
if((argv[argc] = strtok_r(cmd, " ", &r)) != NULL)
while((argv[++argc] = strtok_r(NULL, " ", &r)) != NULL)
;

for(i=1;i<argc;i++)
Expand Down
6 changes: 3 additions & 3 deletions matlab/svmtrain.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name)
{
int i, argc = 1;
char cmd[CMD_LEN];
char *argv[CMD_LEN/2];
char *argv[CMD_LEN/2], *r = (char *)NULL;
void (*print_func)(const char *) = print_string_matlab; // default printing to matlab display

// default values
Expand Down Expand Up @@ -137,8 +137,8 @@ int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name)
{
// put options in argv[]
mxGetString(prhs[2], cmd, mxGetN(prhs[2]) + 1);
if((argv[argc] = strtok(cmd, " ")) != NULL)
while((argv[++argc] = strtok(NULL, " ")) != NULL)
if((argv[argc] = strtok_r(cmd, " ", &r)) != NULL)
while((argv[++argc] = strtok_r(NULL, " ", &r)) != NULL)
;
}

Expand Down
8 changes: 4 additions & 4 deletions svm-predict.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ void predict(FILE *input, FILE *output)
{
int i = 0;
double target_label, predict_label;
char *idx, *val, *label, *endptr;
char *idx, *val, *label, *endptr, *r = (char *)NULL;
int inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0

label = strtok(line," \t\n");
label = strtok_r(line," \t\n",&r);
if(label == NULL) // empty line
exit_input_error(total+1);

Expand All @@ -96,8 +96,8 @@ void predict(FILE *input, FILE *output)
x = (struct svm_node *) realloc(x,max_nr_attr*sizeof(struct svm_node));
}

idx = strtok(NULL,":");
val = strtok(NULL," \t");
idx = strtok_r(NULL,":",&r);
val = strtok_r(NULL," \t",&r);

if(val == NULL)
break;
Expand Down
12 changes: 6 additions & 6 deletions svm-train.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ void read_problem(const char *filename)
size_t elements, j;
FILE *fp = fopen(filename,"r");
char *endptr;
char *idx, *val, *label;
char *idx, *val, *label, *r = (char *)NULL;

if(fp == NULL)
{
Expand All @@ -296,12 +296,12 @@ void read_problem(const char *filename)
line = Malloc(char,max_line_len);
while(readline(fp)!=NULL)
{
char *p = strtok(line," \t"); // label
char *p = strtok_r(line," \t", &r); // label

// features
while(1)
{
p = strtok(NULL," \t");
p = strtok_r(NULL," \t", &r);
if(p == NULL || *p == '\n') // check '\n' as ' ' may be after the last feature
break;
++elements;
Expand All @@ -322,7 +322,7 @@ void read_problem(const char *filename)
inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0
readline(fp);
prob.x[i] = &x_space[j];
label = strtok(line," \t\n");
label = strtok_r(line," \t\n", &r);
if(label == NULL) // empty line
exit_input_error(i+1);

Expand All @@ -332,8 +332,8 @@ void read_problem(const char *filename)

while(1)
{
idx = strtok(NULL,":");
val = strtok(NULL," \t");
idx = strtok_r(NULL,":",&r);
val = strtok_r(NULL," \t",&r);

if(val == NULL)
break;
Expand Down
14 changes: 7 additions & 7 deletions svm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2914,14 +2914,14 @@ svm_model *svm_load_model(const char *model_file_name)

max_line_len = 1024;
line = Malloc(char,max_line_len);
char *p,*endptr,*idx,*val;
char *p,*endptr,*idx,*val,*r = (char *)NULL;;

while(readline(fp)!=NULL)
{
p = strtok(line,":");
p = strtok_r(line,":",&r);
while(1)
{
p = strtok(NULL,":");
p = strtok_r(NULL,":",&r);
if(p == NULL)
break;
++elements;
Expand All @@ -2947,18 +2947,18 @@ svm_model *svm_load_model(const char *model_file_name)
readline(fp);
model->SV[i] = &x_space[j];

p = strtok(line, " \t");
p = strtok_r(line, " \t", &r);
model->sv_coef[0][i] = strtod(p,&endptr);
for(int k=1;k<m;k++)
{
p = strtok(NULL, " \t");
p = strtok_r(NULL, " \t", &r);
model->sv_coef[k][i] = strtod(p,&endptr);
}

while(1)
{
idx = strtok(NULL, ":");
val = strtok(NULL, " \t");
idx = strtok_r(NULL, ":", &r);
val = strtok_r(NULL, " \t", &r);

if(val == NULL)
break;
Expand Down
5 changes: 5 additions & 0 deletions svm.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ int svm_check_probability_model(const struct svm_model *model);

void svm_set_print_string_function(void (*print_func)(const char *));

/* There is no strtok_r in Windows. */
#if defined(_WIN32) || defined(_WIN64)
#define strtok_r strtok_s
#endif

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 889b651

Please sign in to comment.