From 889b651341029b9ecb98fa78fe4d53f39d8d526d Mon Sep 17 00:00:00 2001 From: "Bi-Ruei, Chiu" Date: Mon, 25 Jan 2016 08:28:11 +0800 Subject: [PATCH] Use reentrant version of strtok(), i.e. strtok_r() or strtok_s(), to get tokens --- matlab/libsvmread.c | 14 +++++++------- matlab/svmpredict.c | 6 +++--- matlab/svmtrain.c | 6 +++--- svm-predict.c | 8 ++++---- svm-train.c | 12 ++++++------ svm.cpp | 14 +++++++------- svm.h | 5 +++++ 7 files changed, 35 insertions(+), 30 deletions(-) diff --git a/matlab/libsvmread.c b/matlab/libsvmread.c index d2fe0f5c..e378674f 100644 --- a/matlab/libsvmread.c +++ b/matlab/libsvmread.c @@ -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; @@ -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 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; @@ -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); @@ -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; diff --git a/matlab/svmpredict.c b/matlab/svmpredict.c index 96fedbc8..46dc4cd3 100644 --- a/matlab/svmpredict.c +++ b/matlab/svmpredict.c @@ -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 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); @@ -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; diff --git a/svm-train.c b/svm-train.c index 716815b4..8ac45f08 100644 --- a/svm-train.c +++ b/svm-train.c @@ -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) { @@ -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; @@ -322,7 +322,7 @@ void read_problem(const char *filename) inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has 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); @@ -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; diff --git a/svm.cpp b/svm.cpp index f31a5a0e..712e0f30 100644 --- a/svm.cpp +++ b/svm.cpp @@ -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; @@ -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;ksv_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; diff --git a/svm.h b/svm.h index 3bab5c5d..f16d9157 100644 --- a/svm.h +++ b/svm.h @@ -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