Skip to content

Commit c5afa30

Browse files
author
wcchang
committed
Revert "Fix fscanf warning problem in svm.cpp/svm-scale.c"
This reverts commit 6575443.
1 parent 6575443 commit c5afa30

File tree

3 files changed

+68
-83
lines changed

3 files changed

+68
-83
lines changed

matlab/svmtrain.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,8 @@ int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name)
221221
// read in a problem (in svmlight format)
222222
int read_problem_dense(const mxArray *label_vec, const mxArray *instance_mat)
223223
{
224-
int i,k;
225-
int max_index, sc, label_vector_row_num;
226-
long int elements, j;
224+
int i, j, k;
225+
int elements, max_index, sc, label_vector_row_num;
227226
double *samples, *labels;
228227

229228
prob.x = NULL;
@@ -246,14 +245,13 @@ int read_problem_dense(const mxArray *label_vec, const mxArray *instance_mat)
246245
}
247246

248247
if(param.kernel_type == PRECOMPUTED)
249-
elements = (long int)prob.l * (sc + 1);
250-
248+
elements = prob.l * (sc + 1);
251249
else
252250
{
253251
for(i = 0; i < prob.l; i++)
254252
{
255253
for(k = 0; k < sc; k++)
256-
if(samples[(long int)k * prob.l + i] != 0)
254+
if(samples[k * prob.l + i] != 0)
257255
elements++;
258256
// count the '-1' element
259257
elements++;
@@ -273,10 +271,10 @@ int read_problem_dense(const mxArray *label_vec, const mxArray *instance_mat)
273271

274272
for(k = 0; k < sc; k++)
275273
{
276-
if(param.kernel_type == PRECOMPUTED || samples[(long int)k * prob.l + i] != 0)
274+
if(param.kernel_type == PRECOMPUTED || samples[k * prob.l + i] != 0)
277275
{
278276
x_space[j].index = k + 1;
279-
x_space[j].value = samples[(long int)k * prob.l + i];
277+
x_space[j].value = samples[k * prob.l + i];
280278
j++;
281279
}
282280
}
@@ -301,10 +299,9 @@ int read_problem_dense(const mxArray *label_vec, const mxArray *instance_mat)
301299

302300
int read_problem_sparse(const mxArray *label_vec, const mxArray *instance_mat)
303301
{
304-
int i;
302+
int i, j, k, low, high;
305303
mwIndex *ir, *jc;
306-
int max_index, label_vector_row_num;
307-
long elements, num_samples, j, k, low, high;
304+
int elements, max_index, num_samples, label_vector_row_num;
308305
double *samples, *labels;
309306
mxArray *instance_mat_col; // transposed instance sparse matrix
310307

@@ -331,7 +328,7 @@ int read_problem_sparse(const mxArray *label_vec, const mxArray *instance_mat)
331328
ir = mxGetIr(instance_mat_col);
332329
jc = mxGetJc(instance_mat_col);
333330

334-
num_samples = (long int)mxGetNzmax(instance_mat_col);
331+
num_samples = (int)mxGetNzmax(instance_mat_col);
335332

336333
// the number of instance
337334
prob.l = (int)mxGetN(instance_mat_col);
@@ -355,7 +352,7 @@ int read_problem_sparse(const mxArray *label_vec, const mxArray *instance_mat)
355352
{
356353
prob.x[i] = &x_space[j];
357354
prob.y[i] = labels[i];
358-
low = (long int)jc[i], high = (long int)jc[i+1];
355+
low = (int)jc[i], high = (int)jc[i+1];
359356
for(k=low;k<high;k++)
360357
{
361358
x_space[j].index = (int)ir[k] + 1;

svm-scale.c

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ void output_target(double value);
3838
void output(int index, double value);
3939
char* readline(FILE *input);
4040

41-
int clean_up(FILE *fp_restore, FILE *fp)
42-
{
43-
free(line);
44-
free(feature_max);
45-
free(feature_min);
46-
fclose(fp);
47-
fclose(fp_restore);
48-
return -1;
49-
}
50-
5141
int main(int argc,char **argv)
5242
{
5343
int i,index;
@@ -228,20 +218,16 @@ int main(int argc,char **argv)
228218

229219
if((c = fgetc(fp_restore)) == 'y')
230220
{
231-
if(fscanf(fp_restore, "%lf %lf\n", &y_lower, &y_upper) != 2
232-
&& fscanf(fp_restore, "%lf %lf\n", &y_min, &y_max) != 2)
233-
return clean_up(fp_restore, fp);
234-
221+
fscanf(fp_restore, "%lf %lf\n", &y_lower, &y_upper);
222+
fscanf(fp_restore, "%lf %lf\n", &y_min, &y_max);
235223
y_scaling = 1;
236224
}
237225
else
238226
ungetc(c, fp_restore);
239227

240228
if (fgetc(fp_restore) == 'x')
241229
{
242-
if(fscanf(fp_restore, "%lf %lf\n", &lower, &upper) != 2)
243-
return clean_up(fp_restore, fp);
244-
230+
fscanf(fp_restore, "%lf %lf\n", &lower, &upper);
245231
while(fscanf(fp_restore,"%d %lf %lf\n",&idx,&fmin,&fmax)==3)
246232
{
247233
for(i = next_index;i<idx;i++)

svm.cpp

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2753,18 +2753,33 @@ static char* readline(FILE *input)
27532753
return line;
27542754
}
27552755

2756-
#define FSCANF(_stream, _format, _var) do{ if (fscanf(_stream, _format, _var) != 1) return false; }while(0)
2757-
bool read_model_header(FILE *fp, svm_model* model)
2756+
svm_model *svm_load_model(const char *model_file_name)
27582757
{
2758+
FILE *fp = fopen(model_file_name,"rb");
2759+
if(fp==NULL) return NULL;
2760+
2761+
char *old_locale = strdup(setlocale(LC_ALL, NULL));
2762+
setlocale(LC_ALL, "C");
2763+
2764+
// read parameters
2765+
2766+
svm_model *model = Malloc(svm_model,1);
27592767
svm_parameter& param = model->param;
2768+
model->rho = NULL;
2769+
model->probA = NULL;
2770+
model->probB = NULL;
2771+
model->sv_indices = NULL;
2772+
model->label = NULL;
2773+
model->nSV = NULL;
2774+
27602775
char cmd[81];
27612776
while(1)
27622777
{
2763-
FSCANF(fp,"%80s",cmd);
2778+
fscanf(fp,"%80s",cmd);
27642779

27652780
if(strcmp(cmd,"svm_type")==0)
27662781
{
2767-
FSCANF(fp,"%80s",cmd);
2782+
fscanf(fp,"%80s",cmd);
27682783
int i;
27692784
for(i=0;svm_type_table[i];i++)
27702785
{
@@ -2777,12 +2792,19 @@ bool read_model_header(FILE *fp, svm_model* model)
27772792
if(svm_type_table[i] == NULL)
27782793
{
27792794
fprintf(stderr,"unknown svm type.\n");
2780-
return false;
2795+
2796+
setlocale(LC_ALL, old_locale);
2797+
free(old_locale);
2798+
free(model->rho);
2799+
free(model->label);
2800+
free(model->nSV);
2801+
free(model);
2802+
return NULL;
27812803
}
27822804
}
27832805
else if(strcmp(cmd,"kernel_type")==0)
27842806
{
2785-
FSCANF(fp,"%80s",cmd);
2807+
fscanf(fp,"%80s",cmd);
27862808
int i;
27872809
for(i=0;kernel_type_table[i];i++)
27882810
{
@@ -2794,54 +2816,61 @@ bool read_model_header(FILE *fp, svm_model* model)
27942816
}
27952817
if(kernel_type_table[i] == NULL)
27962818
{
2797-
fprintf(stderr,"unknown kernel function.\n");
2798-
return false;
2819+
fprintf(stderr,"unknown kernel function.\n");
2820+
2821+
setlocale(LC_ALL, old_locale);
2822+
free(old_locale);
2823+
free(model->rho);
2824+
free(model->label);
2825+
free(model->nSV);
2826+
free(model);
2827+
return NULL;
27992828
}
28002829
}
28012830
else if(strcmp(cmd,"degree")==0)
2802-
FSCANF(fp,"%d",&param.degree);
2831+
fscanf(fp,"%d",&param.degree);
28032832
else if(strcmp(cmd,"gamma")==0)
2804-
FSCANF(fp,"%lf",&param.gamma);
2833+
fscanf(fp,"%lf",&param.gamma);
28052834
else if(strcmp(cmd,"coef0")==0)
2806-
FSCANF(fp,"%lf",&param.coef0);
2835+
fscanf(fp,"%lf",&param.coef0);
28072836
else if(strcmp(cmd,"nr_class")==0)
2808-
FSCANF(fp,"%d",&model->nr_class);
2837+
fscanf(fp,"%d",&model->nr_class);
28092838
else if(strcmp(cmd,"total_sv")==0)
2810-
FSCANF(fp,"%d",&model->l);
2839+
fscanf(fp,"%d",&model->l);
28112840
else if(strcmp(cmd,"rho")==0)
28122841
{
28132842
int n = model->nr_class * (model->nr_class-1)/2;
28142843
model->rho = Malloc(double,n);
28152844
for(int i=0;i<n;i++)
2816-
FSCANF(fp,"%lf",&model->rho[i]);
2845+
fscanf(fp,"%lf",&model->rho[i]);
28172846
}
28182847
else if(strcmp(cmd,"label")==0)
28192848
{
28202849
int n = model->nr_class;
28212850
model->label = Malloc(int,n);
28222851
for(int i=0;i<n;i++)
2823-
FSCANF(fp,"%d",&model->label[i]);
2852+
fscanf(fp,"%d",&model->label[i]);
28242853
}
28252854
else if(strcmp(cmd,"probA")==0)
28262855
{
28272856
int n = model->nr_class * (model->nr_class-1)/2;
28282857
model->probA = Malloc(double,n);
28292858
for(int i=0;i<n;i++)
2830-
FSCANF(fp,"%lf",&model->probA[i]);
2859+
fscanf(fp,"%lf",&model->probA[i]);
28312860
}
28322861
else if(strcmp(cmd,"probB")==0)
28332862
{
28342863
int n = model->nr_class * (model->nr_class-1)/2;
28352864
model->probB = Malloc(double,n);
28362865
for(int i=0;i<n;i++)
2837-
FSCANF(fp,"%lf",&model->probB[i]);
2866+
fscanf(fp,"%lf",&model->probB[i]);
28382867
}
28392868
else if(strcmp(cmd,"nr_sv")==0)
28402869
{
28412870
int n = model->nr_class;
28422871
model->nSV = Malloc(int,n);
28432872
for(int i=0;i<n;i++)
2844-
FSCANF(fp,"%d",&model->nSV[i]);
2873+
fscanf(fp,"%d",&model->nSV[i]);
28452874
}
28462875
else if(strcmp(cmd,"SV")==0)
28472876
{
@@ -2855,44 +2884,17 @@ bool read_model_header(FILE *fp, svm_model* model)
28552884
else
28562885
{
28572886
fprintf(stderr,"unknown text in model file: [%s]\n",cmd);
2858-
return false;
2887+
2888+
setlocale(LC_ALL, old_locale);
2889+
free(old_locale);
2890+
free(model->rho);
2891+
free(model->label);
2892+
free(model->nSV);
2893+
free(model);
2894+
return NULL;
28592895
}
28602896
}
28612897

2862-
return true;
2863-
2864-
}
2865-
2866-
svm_model *svm_load_model(const char *model_file_name)
2867-
{
2868-
FILE *fp = fopen(model_file_name,"rb");
2869-
if(fp==NULL) return NULL;
2870-
2871-
char *old_locale = strdup(setlocale(LC_ALL, NULL));
2872-
setlocale(LC_ALL, "C");
2873-
2874-
// read parameters
2875-
2876-
svm_model *model = Malloc(svm_model,1);
2877-
model->rho = NULL;
2878-
model->probA = NULL;
2879-
model->probB = NULL;
2880-
model->sv_indices = NULL;
2881-
model->label = NULL;
2882-
model->nSV = NULL;
2883-
2884-
// read header
2885-
if (!read_model_header(fp, model))
2886-
{
2887-
setlocale(LC_ALL, old_locale);
2888-
free(old_locale);
2889-
free(model->rho);
2890-
free(model->label);
2891-
free(model->nSV);
2892-
free(model);
2893-
return NULL;
2894-
}
2895-
28962898
// read sv_coef and SV
28972899

28982900
int elements = 0;

0 commit comments

Comments
 (0)