options_interface.c
Go to the documentation of this file.
1 #include <errno.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 
5 /* Cavillo (non impatta la portabilità. */
6 #ifndef _GNU_SOURCE
7 #define _GNU_SOURCE
8 #endif
9 
10 #include <string.h>
11 #include "options.h"
12 
16 struct option* options_allocate(int n) {
17  n += 2; /* better safe than sorry */
18  struct option* ops = malloc(sizeof(struct option)*n);
19  int i; for(i=0;i<n;i++) {
20  ops[i].name = 0;
21  ops[i].type = (enum option_type) 0xbeef;
22  ops[i].desc = 0;
23  ops[i].value_pointer = 0;
24  ops[i].set_pointer = 0;
25  }
26  return ops;
27 }
28 
29 /* Find next empty slot in the array. XXX farlo meglio */
31  int i; for(i=0;;i++) {
32  if(ops[i].name == 0)
33  return ops+i;
34  }
35 }
36 
37 char * strdup_(const char *s);
38 
39 void options_int(struct option*ops, const char* name, int *p, int def_value, const char*desc) {
40  struct option* o = options_next_empty(ops);
41  o->name = strdup_(name);
42  o->value_pointer = p;
43  o->set_pointer = 0;
44  o->desc = strdup_(desc);
45  o->type = OPTION_INT;
46  *p = def_value;
47 }
48 
49 void options_alternative(struct option*ops, const char*name, struct option_alternative* alt,
50  int*value, const char*desc) {
51  struct option* o = options_next_empty(ops);
52  o->name = strdup_(name);
53  o->value_pointer = value;
54  o->set_pointer = 0;
55  o->desc = strdup_(desc);
57  o->alternative = alt;
58  *value = alt[0].value;
59 }
60 
61 
62 void options_double (struct option*ops, const char* name, double *p, double def_value, const char*desc){
63  struct option* o = options_next_empty(ops);
64  o->name = strdup_(name);
65  o->value_pointer = p;
66  o->set_pointer = 0;
67  o->desc = strdup_(desc);
68  o->type = OPTION_DOUBLE;
69  *p = def_value;
70 }
71 
72 void options_string (struct option*ops, const char* name, const char** p,const char*def_value,const char*desc){
73  struct option* o = options_next_empty(ops);
74  o->name = strdup_(name);
75  o->value_pointer = p;
76  o->set_pointer = 0;
77  o->desc = strdup_(desc);
78  o->type = OPTION_STRING;
79  *p = def_value;
80 }
int * set_pointer
Definition: options.h:74
char * strdup_(const char *s)
Definition: options.c:38
struct option * options_allocate(int n)
option_type
Definition: options.h:43
struct option * ops
Definition: rb_sm.c:31
struct option * options_next_empty(struct option *ops)
struct option_alternative alt[4]
Definition: test_options.c:13
Definition: options.h:49
void options_alternative(struct option *ops, const char *name, struct option_alternative *alt, int *value, const char *desc)
void options_string(struct option *ops, const char *name, const char **p, const char *def_value, const char *desc)
struct @0 p
const char * name
Definition: options.h:51
struct option_alternative * alternative
Definition: options.h:77
void * value_pointer
Definition: options.h:70
void options_int(struct option *ops, const char *name, int *p, int def_value, const char *desc)
void options_double(struct option *ops, const char *name, double *p, double def_value, const char *desc)
enum option_type type
Definition: options.h:55
const char * desc
Definition: options.h:52


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23