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 }
option_alternative
Definition: options.h:80
option::value_pointer
void * value_pointer
Definition: options.h:70
p
struct @0 p
alt
struct option_alternative alt[4]
Definition: test_options.c:13
option_type
option_type
Definition: options.h:43
options_next_empty
struct option * options_next_empty(struct option *ops)
Definition: options_interface.c:30
OPTION_STRING
@ OPTION_STRING
Definition: options.h:43
options.h
option::set_pointer
int * set_pointer
Definition: options.h:74
options_int
void options_int(struct option *ops, const char *name, int *p, int def_value, const char *desc)
Definition: options_interface.c:39
strdup_
char * strdup_(const char *s)
Definition: options.c:38
options_double
void options_double(struct option *ops, const char *name, double *p, double def_value, const char *desc)
Definition: options_interface.c:62
OPTION_INT
@ OPTION_INT
Definition: options.h:43
option::name
const char * name
Definition: options.h:51
option::alternative
struct option_alternative * alternative
Definition: options.h:77
ops
struct option * ops
Definition: rb_sm.c:31
options_allocate
struct option * options_allocate(int n)
Definition: options_interface.c:16
options_string
void options_string(struct option *ops, const char *name, const char **p, const char *def_value, const char *desc)
Definition: options_interface.c:72
option
Definition: options.h:49
OPTION_ALTERNATIVE
@ OPTION_ALTERNATIVE
Definition: options.h:43
option::desc
const char * desc
Definition: options.h:52
OPTION_DOUBLE
@ OPTION_DOUBLE
Definition: options.h:43
option_alternative::value
int value
Definition: options.h:82
option::type
enum option_type type
Definition: options.h:55
options_alternative
void options_alternative(struct option *ops, const char *name, struct option_alternative *alt, int *value, const char *desc)
Definition: options_interface.c:49


csm
Author(s): Andrea Censi
autogenerated on Wed Aug 17 2022 02:50:34