2 * Copyright 2002-2003 Red Hat Inc., Durham, North Carolina.
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation on the rights to use, copy, modify, merge,
10 * publish, distribute, sublicense, and/or sell copies of the Software,
11 * and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NON-INFRINGEMENT. IN NO EVENT SHALL RED HAT AND/OR THEIR SUPPLIERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * Rickard E. (Rik) Faith <faith@redhat.com>
35 * Generic comma-delimited argument processing. */
37 #ifdef HAVE_DMX_CONFIG_H
38 #include <dmx-config.h>
41 #define DMX_ARG_TEST 0
52 /** Stores the parsed argument list. */
54 int argc
; /**< Number of arguments in argv */
55 int argm
; /**< Maximum number of arguments store-able in argv */
56 const char **argv
; /**< Arguments */
59 /** Create an (externally opaque) \a dmxArg object. */
63 dmxArg a
= malloc(sizeof(*a
));
67 a
->argv
= malloc(a
->argm
* sizeof(*a
->argv
));
72 /** Free the specified \a dmxArg object. */
78 for (i
= 0; i
< a
->argc
; i
++)
79 free((char *) a
->argv
[i
]);
84 /** Add the \a string as the next argument in the \a dmxArg object. */
86 dmxArgAdd(dmxArg a
, const char *string
)
88 if (a
->argm
<= a
->argc
+ 2)
89 a
->argv
= realloc(a
->argv
, sizeof(*a
->argv
) * (a
->argm
*= 2));
90 a
->argv
[a
->argc
++] = strdup(string
);
91 a
->argv
[a
->argc
] = NULL
;
94 /** Return the argument number \a item in the \a dmxArg object.
95 * Arguments are 0 based. NULL will be returned for values less than 0
96 * or equal to or greater than the number of arguments in the object. */
98 dmxArgV(dmxArg a
, int item
)
100 if (item
< 0 || item
>= a
->argc
)
102 return a
->argv
[item
];
105 /** Return the number of arguments in the \a dmxArg object. */
112 /** Parse a string into arguments delimited by commas. Return a new \a
113 * dmxArg object containing the arguments. */
115 dmxArgParse(const char *string
)
119 dmxArg a
= dmxArgCreate();
126 len
= strlen(string
) + 2;
128 strncpy(tmp
, string
, len
);
130 for (start
= pt
= tmp
, done
= 0; !done
&& *pt
; start
= ++pt
) {
131 for (; *pt
&& *pt
!= ','; pt
++);
138 dmxArgAdd(a
, ""); /* Final comma */
146 dmxArgPrint(dmxArg a
)
150 printf(" argc = %d\n", dmxArgC(a
));
151 for (i
= 0; i
< dmxArgC(a
); i
++)
152 printf(" argv[%d] = \"%s\"\n", i
, dmxArgV(a
, i
));
156 dmxArgTest(const char *string
)
161 printf("Testing NULL\n");
162 else if (!strlen(string
))
163 printf("Testing (empty)\n");
165 printf("Testing \"%s\"\n", string
);
167 a
= dmxArgParse(string
);
186 dmxArgTest("a,b,,c");