options_interface.c
Go to the documentation of this file.
00001 #include <errno.h>
00002 #include <stdlib.h>
00003 #include <stdio.h>
00004 
00005 /* Cavillo (non impatta la portabilità. */
00006 #ifndef _GNU_SOURCE
00007 #define _GNU_SOURCE
00008 #endif
00009 
00010 #include <string.h>
00011 #include "options.h"
00012 
00016 struct option* options_allocate(int n) {
00017         n += 2; /* better safe than sorry */
00018         struct option* ops = malloc(sizeof(struct option)*n);
00019         int i; for(i=0;i<n;i++) {
00020                 ops[i].name = 0;
00021                 ops[i].type = (enum option_type) 0xbeef;
00022                 ops[i].desc = 0;
00023                 ops[i].value_pointer = 0;
00024                 ops[i].set_pointer = 0;
00025         }
00026         return ops;
00027 }
00028 
00029 /* Find next empty slot in the array. XXX farlo meglio */
00030 struct option* options_next_empty(struct option*ops) {
00031         int i; for(i=0;;i++) {
00032                 if(ops[i].name == 0)
00033                  return ops+i;
00034         }
00035 }
00036 
00037 char * strdup_(const char *s);
00038 
00039 void options_int(struct option*ops, const char* name, int *p, int def_value, const char*desc) {
00040         struct option* o =  options_next_empty(ops);
00041         o->name = strdup_(name);
00042         o->value_pointer = p;
00043         o->set_pointer = 0;
00044         o->desc = strdup_(desc);
00045         o->type = OPTION_INT;
00046         *p = def_value;
00047 }
00048 
00049 void options_alternative(struct option*ops, const char*name, struct option_alternative* alt,
00050         int*value, const char*desc) {
00051         struct option* o =  options_next_empty(ops);
00052         o->name = strdup_(name);
00053         o->value_pointer = value;
00054         o->set_pointer = 0;
00055         o->desc = strdup_(desc);
00056         o->type = OPTION_ALTERNATIVE;
00057         o->alternative = alt;
00058         *value = alt[0].value;
00059 }
00060 
00061 
00062 void options_double (struct option*ops, const char* name, double *p, double def_value, const char*desc){
00063         struct option* o =  options_next_empty(ops);
00064         o->name = strdup_(name);
00065         o->value_pointer = p;
00066         o->set_pointer = 0;
00067         o->desc = strdup_(desc);
00068         o->type = OPTION_DOUBLE;
00069         *p = def_value;
00070 }
00071 
00072 void options_string (struct option*ops, const char* name, const char** p,const char*def_value,const char*desc){
00073         struct option* o =  options_next_empty(ops);
00074         o->name = strdup_(name);
00075         o->value_pointer = p;
00076         o->set_pointer = 0;
00077         o->desc = strdup_(desc);
00078         o->type = OPTION_STRING;
00079         *p = def_value;
00080 }


csm
Author(s): Andrea Censi
autogenerated on Fri May 17 2019 02:28:33