1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
#include "config.h"
#include <stdio.h>
#include <string.h>
/**
* jmap - map from indices to values (based on libJudy)
*
* This provides a convenient wrapper for using JudyL arrays; using
* integers or pointers as an index, Judy arrays provide an efficient
* map to integers or pointers.
*
* You define a struct for your particular index and value types using
* the JMAP_MEMBERS macro, then use the jmap routines to manipulate
* the mapping.
*
* Note: if you use an integer type for the index or value types and
* your compiler doesn't support "typeof", you will get warnings about
* mixing pointers and integers.
*
* Example:
* // Silly example of associating data with arguments by pointer and int.
* #include <string.h>
* #include <stdio.h>
* #include <ccan/jmap/jmap.h>
*
* struct opt_detail {
* bool is_long;
* size_t length; // == 1 if !is_long.
* };
*
* // Define map type for int -> argv.
* struct arg_map {
* JMAP_MEMBERS(int, char *);
* };
* // Define map type for argv -> struct opt_detail *.
* struct opt_map {
* JMAP_MEMBERS(char *, struct opt_detail *);
* };
*
* int main(int argc, char *argv[])
* {
* int i;
* // This map is equivalent to the argv[] array. Silly example.
* struct arg_map *arg = jmap_new(struct arg_map);
* struct opt_map *opt = jmap_new(struct opt_map);
* struct opt_detail *d;
*
* // Note: this is not correct for real parsing!
* for (i = 1; i < argc; i++) {
* jmap_add(arg, i, argv[i]);
* if (argv[i][0] != '-')
* continue;
* d = malloc(sizeof(*d));
* if (argv[i][1] == '-') {
* // --<stuff>
* d->is_long = true;
* d->length = strlen(argv[i]+2);
* } else {
* // -<opt1>
* d->is_long = false;
* d->length = 1;
* }
* jmap_add(opt, argv[i], d);
* }
*
* printf("Found %lu options:\n", jmap_count(opt));
* for (i = jmap_first(arg); i; i = jmap_next(arg,i)) {
* char *a = jmap_get(arg, i);
* d = jmap_get(opt, a);
* printf(" Arg %i ('%s') is a %s of %zu chars\n",
* i, a,
* d == NULL ? "normal arg"
* : d->is_long ? "long opt"
* : "short opt",
* d == NULL ? strlen(a) : d->length);
* // We no longer need it, so free it here.
* free(d);
* }
* jmap_free(opt);
* jmap_free(arg);
* return 0;
* }
* // Given "--help" output contains "Arg 1 ('--help') is a long opt of 4 chars"
* // Given "-h" output contains "Arg 1 ('-h') is a short opt of 1 chars"
* // Given "foo" output contains "Arg 1 ('foo') is a normal arg of 3 chars"
*
* License: LGPL (v2.1 or any later version)
* Author: Rusty Russell <rusty@rustcorp.com.au>
*/
int main(int argc, char *argv[])
{
if (argc != 2)
return 1;
if (strcmp(argv[1], "depends") == 0) {
printf("ccan/build_assert\n");
printf("ccan/compiler\n");
printf("ccan/tcon\n");
printf("Judy\n");
return 0;
}
if (strcmp(argv[1], "libs") == 0) {
printf("Judy\n");
return 0;
}
return 1;
}
|