Skip to content

Commit 98a7c33

Browse files
committed
merge of a pull request made on the original branch (cjlin1#14)
1 parent 4c636a2 commit 98a7c33

4 files changed

Lines changed: 93 additions & 33 deletions

File tree

lib/linear/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#linear library
22

33
SET (LINEAR_LIB_SRCS
4+
${CMAKE_CURRENT_SOURCE_DIR}/linear_locales.cpp
5+
${CMAKE_CURRENT_SOURCE_DIR}/linear_locales.h
46
${CMAKE_CURRENT_SOURCE_DIR}/linear.cpp
57
${CMAKE_CURRENT_SOURCE_DIR}/linear.h
68
${CMAKE_CURRENT_SOURCE_DIR}/tron.cpp

lib/linear/linear.cpp

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
#include <linear.h>
88
#include <tron.h>
99

10+
#include <linear_locales.h>
11+
1012
#ifdef ENABLED_OPENMP
1113
#include <omp.h>
1214
#endif
1315

14-
1516
typedef signed char schar;
1617
template <class T> static inline void swap(T& x, T& y) { T t=x; x=y; y=t; }
1718
#ifndef min
@@ -2932,12 +2933,7 @@ int save_model(const char *model_file_name, const struct model *model_)
29322933
FILE *fp = fopen(model_file_name,"w");
29332934
if(fp==NULL) return -1;
29342935

2935-
char *old_locale = setlocale(LC_ALL, NULL);
2936-
if (old_locale)
2937-
{
2938-
old_locale = strdup(old_locale);
2939-
}
2940-
setlocale(LC_ALL, "C");
2936+
locale_handle old_locale = set_c_locale();
29412937

29422938
int nr_w;
29432939
if(model_->nr_class==2 && model_->param.solver_type != MCSVM_CS)
@@ -2969,8 +2965,7 @@ int save_model(const char *model_file_name, const struct model *model_)
29692965
fprintf(fp, "\n");
29702966
}
29712967

2972-
setlocale(LC_ALL, old_locale);
2973-
free(old_locale);
2968+
restore_locale(old_locale);
29742969

29752970
if (ferror(fp) != 0 || fclose(fp) != 0) return -1;
29762971
else return 0;
@@ -2991,21 +2986,16 @@ struct model *load_model(const char *model_file_name)
29912986

29922987
model_->label = NULL;
29932988

2994-
char *old_locale = setlocale(LC_ALL, NULL);
2995-
if (old_locale)
2996-
{
2997-
old_locale = strdup(old_locale);
2998-
}
2999-
setlocale(LC_ALL, "C");
2989+
locale_handle old_locale = set_c_locale();
30002990

30012991
char cmd[81];
30022992
while(1)
30032993
{
3004-
fscanf(fp,"%80s",cmd);
2994+
int r = fscanf(fp,"%80s",cmd);
30052995
if(strcmp(cmd,"solver_type")==0)
30062996
{
3007-
fscanf(fp,"%80s",cmd);
3008-
int i;
2997+
int r = fscanf(fp,"%80s",cmd);
2998+
int i ;
30092999
for(i=0;solver_type_table[i];i++)
30103000
{
30113001
if(strcmp(solver_type_table[i],cmd)==0)
@@ -3018,26 +3008,25 @@ struct model *load_model(const char *model_file_name)
30183008
{
30193009
fprintf(stderr,"unknown solver type.\n");
30203010

3021-
setlocale(LC_ALL, old_locale);
3011+
restore_locale(old_locale);
30223012
free(model_->label);
30233013
free(model_);
3024-
free(old_locale);
30253014
return NULL;
30263015
}
30273016
}
30283017
else if(strcmp(cmd,"nr_class")==0)
30293018
{
3030-
fscanf(fp,"%d",&nr_class);
3019+
int r = fscanf(fp,"%d",&nr_class);
30313020
model_->nr_class=nr_class;
30323021
}
30333022
else if(strcmp(cmd,"nr_feature")==0)
30343023
{
3035-
fscanf(fp,"%d",&nr_feature);
3024+
int r = fscanf(fp,"%d",&nr_feature);
30363025
model_->nr_feature=nr_feature;
30373026
}
30383027
else if(strcmp(cmd,"bias")==0)
30393028
{
3040-
fscanf(fp,"%lf",&bias);
3029+
int r = fscanf(fp,"%lf",&bias);
30413030
model_->bias=bias;
30423031
}
30433032
else if(strcmp(cmd,"w")==0)
@@ -3048,16 +3037,16 @@ struct model *load_model(const char *model_file_name)
30483037
{
30493038
int nr_class = model_->nr_class;
30503039
model_->label = Malloc(int,nr_class);
3051-
for(int i=0;i<nr_class;i++)
3052-
fscanf(fp,"%d",&model_->label[i]);
3040+
for(int i=0;i<nr_class;i++) {
3041+
int r = fscanf(fp,"%d",&model_->label[i]);
3042+
}
30533043
}
30543044
else
30553045
{
30563046
fprintf(stderr,"unknown text in model file: [%s]\n",cmd);
3057-
setlocale(LC_ALL, old_locale);
3047+
restore_locale(old_locale);
30583048
free(model_->label);
30593049
free(model_);
3060-
free(old_locale);
30613050
return NULL;
30623051
}
30633052
}
@@ -3077,14 +3066,13 @@ struct model *load_model(const char *model_file_name)
30773066
model_->w=Malloc(double, w_size*nr_w);
30783067
for(i=0; i<w_size; i++)
30793068
{
3080-
int j;
3081-
for(j=0; j<nr_w; j++)
3082-
fscanf(fp, "%lf ", &model_->w[i*nr_w+j]);
3083-
fscanf(fp, "\n");
3069+
for(int j=0; j<nr_w; j++) {
3070+
int r = fscanf(fp, "%lf ", &model_->w[i*nr_w+j]);
3071+
}
3072+
int r = fscanf(fp, "\n");
30843073
}
30853074

3086-
setlocale(LC_ALL, old_locale);
3087-
free(old_locale);
3075+
restore_locale(old_locale);
30883076

30893077
if (ferror(fp) != 0 || fclose(fp) != 0) return NULL;
30903078

lib/linear/linear_locales.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <linear_locales.h>
2+
3+
#if _POSIX_VERSION >= 200809L
4+
5+
locale_handle set_c_locale()
6+
{
7+
locale_handle c_locale = newlocale(LC_ALL_MASK, "C", 0);
8+
locale_handle old_locale = uselocale(c_locale);
9+
return old_locale;
10+
}
11+
12+
void restore_locale(locale_handle locale)
13+
{
14+
locale_handle c_locale = uselocale(locale);
15+
if (c_locale && c_locale != LC_GLOBAL_LOCALE) {
16+
freelocale(c_locale);
17+
}
18+
}
19+
20+
#else
21+
22+
locale_handle set_c_locale()
23+
{
24+
locale_handle old_locale = setlocale(LC_ALL, NULL);
25+
if (old_locale) {
26+
old_locale = strdup(old_locale);
27+
}
28+
setlocale(LC_ALL, "C");
29+
return old_locale;
30+
}
31+
32+
void restore_locale(locale_handle locale)
33+
{
34+
setlocale(LC_ALL, locale);
35+
free(locale);
36+
}
37+
38+
#endif /* _POSIX_VERSION */
39+

lib/linear/linear_locales.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef __LIBLINEAR_LOCALES__
2+
#define __LIBLINEAR_LOCALES__
3+
4+
#include <locale.h>
5+
6+
#if __unix__
7+
#include <unistd.h> // For _POSIX_VERSION
8+
#endif
9+
10+
#if _POSIX_VERSION >= 200809L
11+
12+
// If possible, use the thread-safe uselocale() function
13+
typedef locale_t locale_handle ;
14+
15+
locale_handle set_c_locale();
16+
17+
void restore_locale(locale_handle locale);
18+
19+
#else
20+
21+
// But fall back to setlocale() if uselocale() is not available
22+
typedef char *locale_handle;
23+
24+
locale_handle set_c_locale();
25+
26+
void restore_locale(locale_handle locale);
27+
28+
#endif /* _POSIX_VERSION */
29+
30+
#endif /* __LIBLINEAR_LOCALES__ */
31+

0 commit comments

Comments
 (0)