Skip to content
Snippets Groups Projects
Commit 29c1dc5a authored by Andrea Censi's avatar Andrea Censi
Browse files

No commit message

No commit message
parent cb392e53
No related branches found
No related tags found
No related merge requests found
# overlook is a MacBook with Leopard
#SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-long-double")
#SET(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS} -Wno-long-double")
......@@ -24,3 +24,4 @@ SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fstrict-aliasing")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pedantic")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wmissing-prototypes ")
SET(COMPILE_HSM 1)
\ No newline at end of file
......@@ -52,7 +52,7 @@ void sm_error(const char *msg, ...)
fprintf(stderr, XTERM_ERROR);
if(sm_program_name)
fputs(sm_program_name, stderr);
fprintf(stderr, "%s: ", sm_program_name);
sm_write_context();
......@@ -72,7 +72,7 @@ void sm_info(const char *msg, ...)
check_for_xterm_color();
if(sm_program_name)
fputs(sm_program_name, stderr);
fprintf(stderr, "%s: ", sm_program_name);
sm_write_context();
......@@ -94,7 +94,7 @@ void sm_debug(const char *msg, ...)
fprintf(stderr, XTERM_DEBUG);
if(sm_program_name)
fputs(sm_program_name, stderr);
fprintf(stderr, "%s: ", sm_program_name);
sm_write_context();
......
......@@ -270,6 +270,28 @@ int options_set(struct option*o, const char*value) {
}
return 1;
}
case(OPTION_ALTERNATIVE): {
int * value_pointer = (int*) o->value_pointer;
struct option_alternative * a = o->alternative;
for(; a->label; a++) {
if( !strcasecmp(a->label, value) ) {
*value_pointer = a->value;
return 1;
}
}
fprintf(stderr, "Could not recognize '%s' as one of the alternative for %s: ",
value, o->name);
for(a = o->alternative; a->label; a++) {
fprintf(stderr, "\"%s\"", a->label);
if( (a+1)->label ) fprintf(stderr, ", ");
}
fprintf(stderr, ".\n");
return 0;
}
default: {
/* XXX ERROR */
......@@ -314,20 +336,56 @@ void options_dump(struct option * options, FILE*f, int write_desc) {
char**table = malloc(sizeof(char*)*nrows*3);
int row = 0;
table[row*3 +0] = strdup_("Option name");
table[row*3 +1] = strdup_("Default");
table[row*3 +2] = strdup_("Description");
row++;
table[row*3 +0] = strdup_("-----------");
table[row*3 +1] = strdup_("-------");
table[row*3 +2] = strdup_("-----------");
row++;
if(write_desc) {
table[row*3 +0] = strdup_("Option name");
table[row*3 +1] = strdup_("Default");
table[row*3 +2] = strdup_("Description");
row++;
table[row*3 +0] = strdup_("-----------");
table[row*3 +1] = strdup_("-------");
table[row*3 +2] = strdup_("-----------");
row++;
} else {
table[row*3 +0] = strdup_("");
table[row*3 +1] = strdup_("");
table[row*3 +2] = strdup_("");
row++;
table[row*3 +0] = strdup_("");
table[row*3 +1] = strdup_("");
table[row*3 +2] = strdup_("");
row++;
}
int i;
for (i=0;i<n;i++) {
table[row*3 +0] = strdup_(options[i].name);
table[row*3 +1] = strdup_(options_value_as_string(options+i));
table[row*3 +2] = write_desc ? strdup_(options[i].desc) : strdup_("");
if( write_desc)
if(options[i].type == OPTION_ALTERNATIVE) {
char extended[1000];
strcat(extended, options[i].desc);
strcat(extended, " Possible options are: ");
struct option_alternative * a = options[i].alternative;
for(; a->label; a++) {
strcat(extended, "\"");
strcat(extended, a->label);
strcat(extended, "\"");
if(a->desc) {
strcat(extended, ": ");
strcat(extended, a->desc);
} else {
}
if((a+1)->label)
strcat(extended, ", ");
}
strcat(extended, ".");
table[row*3 +2] = strdup_(extended);
}
row ++;
}
......@@ -364,7 +422,7 @@ const char*options_value_as_string(struct option*o) {
case(OPTION_STRING): {
char** value_pointer = (char**) o->value_pointer;
sprintf(options_value_as_string_buf, "%s", *value_pointer);
sprintf(options_value_as_string_buf, "%s", *value_pointer); /* should I add "" ? */
break;
}
......@@ -373,6 +431,17 @@ const char*options_value_as_string(struct option*o) {
sprintf(options_value_as_string_buf, "%g", *value_pointer);
break;
}
case(OPTION_ALTERNATIVE): {
int * value_pointer = (int*) o->value_pointer;
struct option_alternative * a = o->alternative;
for(; a->label; a++) {
if( a->value == *value_pointer )
sprintf(options_value_as_string_buf, "%s", a->label);
}
break;
}
default:
strcpy(options_value_as_string_buf, "?");
} /* switch */
......
......@@ -7,13 +7,40 @@
extern "C" {
#endif
/**
* Utility functions to parse command line arguments.
*
* See options_example.c.
*/
enum option_type { OPTION_STRING=0, OPTION_INT=1, OPTION_DOUBLE=2 };
/** User-friendly interface */
/* Sets the banner for the help message. (pointer is kept) */
void options_banner(const char*message);
struct option;
struct option_alternative;
struct option* options_allocate(int n);
void options_int (struct option*, const char* name,
int *p, int def_value, const char*desc);
void options_double (struct option*, const char* name,
double *p, double def_value, const char*desc);
void options_string (struct option*, const char* name,
const char** p,const char*def_balue,const char*desc);
void options_alternative(struct option*, const char*name, struct option_alternative*alt,
int*value, const char*desc);
/** Returns 0 on error */
int options_parse_args(struct option*ops, int argc, const char* argv[]);
/** Returns 0 on error */
int options_parse_file(struct option*ops, const char*pwd, const char*file);
void options_print_help(struct option*ops, FILE*where);
/** Internal use */
enum option_type { OPTION_STRING=0, OPTION_INT=1, OPTION_DOUBLE=2, OPTION_ALTERNATIVE=3 };
#define OPTIONS_NAME_MAXSIZE 32
#define OPTIONS_VALUE_MAXSIZE 256
......@@ -31,45 +58,31 @@ struct option {
* the option has no parameters. Ex: in "--port 2000", "--port"
* is the name and "2000" is the value. value_pointer is interpreted
* according to the value of "type".
* type= INT: value_pointer is a "int *"
* type=STRING: value_pointer is a "char **"
* type=DOUBLE: value_pointer is a "double *"
* type=OPTION_INT: value_pointer is a "int *"
* type=OPTION_STRING: value_pointer is a "char **"
* A new string is allocated using malloc():
* *(value_pointer) = malloc( ... )
* and you should free it yourself.
* type=OPTION_DOUBLE: value_pointer is a "double *"
* type=OPTION_ALTERNATIVE: value_pointer is a "int *"
* and alternatives is set.
*/
void * value_pointer;
/** If not NULL, it is set to 1 if the option is found. */
int * set_pointer;
};
/** User-friendly interface */
/* Sets the banner for the help message. (pointer is kept) */
void options_banner(const char*message);
struct option* options_allocate(int n);
void options_int (struct option*, const char* name,
int *p, int def_value, const char*desc);
void options_double (struct option*, const char* name,
double *p, double def_value, const char*desc);
void options_string (struct option*, const char* name,
const char** p,const char*def_balue,const char*desc);
/** Returns 0 on error */
int options_parse_args(struct option*ops, int argc, const char* argv[]);
/** Returns 0 on error */
int options_parse_file(struct option*ops, const char*pwd, const char*file);
/** used only for OPTION_ALTERNATIVE */
struct option_alternative * alternative;
};
void options_print_help(struct option*ops, FILE*where);
struct option_alternative {
const char *label;
int value;
const char *desc;
};
/** Internal use */
/** Finds an option in the array. Returns 0 if not found. */
struct option * options_find(struct option*ops, const char * name);
......@@ -98,7 +111,6 @@ char * strdup_(const char *s);
int get_double(double*p, const char*s);
/** Return 1 if ok. */
int get_int(int*p, const char*s);
/* Find next empty slot in the array. XXX farlo meglio */
struct option* options_next_empty(struct option*ops);
......
......@@ -46,6 +46,19 @@ void options_int(struct option*ops, const char* name, int *p, int def_value, con
*p = def_value;
}
void options_alternative(struct option*ops, const char*name, struct option_alternative* alt,
int*value, const char*desc) {
struct option* o = options_next_empty(ops);
o->name = strdup_(name);
o->value_pointer = value;
o->set_pointer = 0;
o->desc = strdup_(desc);
o->type = OPTION_ALTERNATIVE;
o->alternative = alt;
*value = alt[0].value;
}
void options_double (struct option*ops, const char* name, double *p, double def_value, const char*desc){
struct option* o = options_next_empty(ops);
o->name = strdup_(name);
......
#include "options.h"
struct params {
int a_int;
double a_double;
const char * file;
int algo;
};
struct option_alternative alt[4] = {
{"PLICP", 3, "a new algorithm"},
{"ICP", 4, "the standard"},
{"MbICP", 5, "good for rations"}, {0,0,0}
};
int main(int argc, const char*argv[]) {
options_banner("This is a test program for the options library.");
struct params p;
struct option* ops = options_allocate(3);
options_int (ops, "i", &p.a_int, 42, "An int parameter");
options_double (ops, "d", &p.a_double, 0.42, "A double parameter");
options_string (ops, "s", &p.file , "Hello", "A file parameter");
options_alternative(ops, "algorith", alt, &p.algo, "which algorithm to use" );
if(!options_parse_args(ops, argc, argv)) {
printf("A simple program.\n\nUsage:\n");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment