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. */
60 dmxArg
dmxArgCreate(void)
62 dmxArg a
= malloc(sizeof(*a
));
65 a
->argv
= malloc(a
->argm
* sizeof(*a
->argv
));
70 /** Free the specified \a dmxArg object. */
71 void dmxArgFree(dmxArg a
)
75 for (i
= 0; i
< a
->argc
; i
++) free((char *)a
->argv
[i
]);
80 /** Add the \a string as the next argument in the \a dmxArg object. */
81 void dmxArgAdd(dmxArg a
, const char *string
)
83 if (a
->argm
<= a
->argc
+ 2)
84 a
->argv
= realloc(a
->argv
, sizeof(*a
->argv
) * (a
->argm
*= 2));
85 a
->argv
[a
->argc
++] = strdup(string
);
86 a
->argv
[a
->argc
] = NULL
;
89 /** Return the argument number \a item in the \a dmxArg object.
90 * Arguments are 0 based. NULL will be returned for values less than 0
91 * or equal to or greater than the number of arguments in the object. */
92 const char *dmxArgV(dmxArg a
, int item
)
94 if (item
< 0 || item
>= a
->argc
) return NULL
;
98 /** Return the number of arguments in the \a dmxArg object. */
104 /** Parse a string into arguments delimited by commas. Return a new \a
105 * dmxArg object containing the arguments. */
106 dmxArg
dmxArgParse(const char *string
)
110 dmxArg a
= dmxArgCreate();
114 if (!string
) return a
;
116 len
= strlen(string
) + 2;
118 strncpy(tmp
, string
, len
);
120 for (start
= pt
= tmp
, done
= 0; !done
&& *pt
; start
= ++pt
) {
121 for (;*pt
&& *pt
!= ','; pt
++);
126 if (!done
) dmxArgAdd(a
, ""); /* Final comma */
133 static void dmxArgPrint(dmxArg a
)
137 printf(" argc = %d\n", dmxArgC(a
));
138 for (i
= 0; i
< dmxArgC(a
); i
++)
139 printf(" argv[%d] = \"%s\"\n", i
, dmxArgV(a
, i
));
142 static void dmxArgTest(const char *string
)
147 printf("Testing NULL\n");
148 else if (!strlen(string
))
149 printf("Testing (empty)\n");
151 printf("Testing \"%s\"\n", string
);
153 a
= dmxArgParse(string
);
171 dmxArgTest("a,b,,c");