From: rofu Date: Tue, 26 Jan 2010 15:20:42 +0000 (+0000) Subject: Change global variable liblinear_print_string to static, and add a interface function X-Git-Tag: v160~17 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=683c6a198a46f78608276837c039a41c31a145ed;p=liblinear Change global variable liblinear_print_string to static, and add a interface function to assign the print_function. --- diff --git a/README b/README index 8c3d3a1..91e88e8 100644 --- a/README +++ b/README @@ -421,10 +421,11 @@ Library Usage This function frees the memory used by a parameter set. -- Variable: extern void (*liblinear_print_string) (const char *); +- Function: void set_print_string_function(void (*print_func)(const char *)); - Users can specify their output format by - liblinear_print_string = &your_print_function; + Users can specify their output format by a function. Use + svm_set_print_string_function(NULL); + for default printing to stdout. Building Windows Binaries ========================= diff --git a/linear.cpp b/linear.cpp index 9e29d66..fbd032a 100644 --- a/linear.cpp +++ b/linear.cpp @@ -27,7 +27,7 @@ static void print_string_stdout(const char *s) fflush(stdout); } -void (*liblinear_print_string) (const char *) = &print_string_stdout; +static void (*liblinear_print_string) (const char *) = &print_string_stdout; #if 1 static void info(const char *fmt,...) @@ -2091,3 +2091,11 @@ void get_labels(const model *model_, int* label) label[i] = model_->label[i]; } +void set_print_string_function(void (*print_func) (const char*)) +{ + if (print_func == NULL) + liblinear_print_string = &print_string_stdout; + else + liblinear_print_string = print_func; +} + diff --git a/linear.h b/linear.h index 51ff608..799260d 100644 --- a/linear.h +++ b/linear.h @@ -60,7 +60,7 @@ void get_labels(const struct model *model_, int* label); void destroy_model(struct model *model_); void destroy_param(struct parameter *param); const char *check_parameter(const struct problem *prob, const struct parameter *param); -extern void (*liblinear_print_string) (const char *); +void set_print_string_function(void (*print_func) (const char*)); #ifdef __cplusplus } diff --git a/matlab/train.c b/matlab/train.c index 8a6b919..7eec36b 100644 --- a/matlab/train.c +++ b/matlab/train.c @@ -16,9 +16,7 @@ typedef int mwIndex; #define Malloc(type,n) (type *)malloc((n)*sizeof(type)) #define INF HUGE_VAL -void print_null(const char *s){} - -void (*liblinear_default_print_string) (const char *); +void print_null(const char *s) {} void exit_with_help() { @@ -87,6 +85,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]; + void (*print_func)(const char*) = NULL; // default values param.solver_type = L2R_L2LOSS_SVC_DUAL; @@ -99,11 +98,6 @@ int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name) col_format_flag = 0; bias = -1; - // train loaded only once under matlab - if(liblinear_default_print_string == NULL) - liblinear_default_print_string = liblinear_print_string; - else - liblinear_print_string = liblinear_default_print_string; if(nrhs <= 1) return 1; @@ -162,7 +156,7 @@ int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name) param.weight[param.nr_weight-1] = atof(argv[i]); break; case 'q': - liblinear_print_string = &print_null; + print_func = &print_null; i--; break; default: @@ -171,6 +165,8 @@ int parse_command_line(int nrhs, const mxArray *prhs[], char *model_file_name) } } + set_print_string_function(print_func); + if(param.eps == INF) { if(param.solver_type == L2R_LR || param.solver_type == L2R_L2LOSS_SVC) diff --git a/train.c b/train.c index f3e37db..6f7ea4f 100644 --- a/train.c +++ b/train.c @@ -135,6 +135,7 @@ void do_cross_validation() void parse_command_line(int argc, char **argv, char *input_file_name, char *model_file_name) { int i; + void (*print_func)(const char*) = NULL; // default values param.solver_type = L2R_L2LOSS_SVC_DUAL; @@ -189,7 +190,7 @@ void parse_command_line(int argc, char **argv, char *input_file_name, char *mode break; case 'q': - liblinear_print_string = &print_null; + print_func = &print_null; i--; break; @@ -200,6 +201,8 @@ void parse_command_line(int argc, char **argv, char *input_file_name, char *mode } } + set_print_string_function(print_func); + // determine filenames if(i>=argc) exit_with_help();