···11+/*****************************************************************************
22+ * pce *
33+ *****************************************************************************/
44+55+/*****************************************************************************
66+ * File name: src/drivers/options.c *
77+ * Created: 2009-10-17 by Hampa Hug <hampa@hampa.ch> *
88+ * Copyright: (C) 2009-2010 Hampa Hug <hampa@hampa.ch> *
99+ *****************************************************************************/
1010+1111+/*****************************************************************************
1212+ * This program is free software. You can redistribute it and / or modify it *
1313+ * under the terms of the GNU General Public License version 2 as published *
1414+ * by the Free Software Foundation. *
1515+ * *
1616+ * This program is distributed in the hope that it will be useful, but *
1717+ * WITHOUT ANY WARRANTY, without even the implied warranty of *
1818+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
1919+ * Public License for more details. *
2020+ *****************************************************************************/
2121+2222+2323+#include <config.h>
2424+2525+#include <stdlib.h>
2626+#include <string.h>
2727+2828+#include <drivers/options.h>
2929+3030+3131+static
3232+const char *drv_skip_option (const char *str)
3333+{
3434+ while (*str != 0) {
3535+ if (*str == ':') {
3636+ if (*(str + 1) == ':') {
3737+ str += 2;
3838+ }
3939+ else {
4040+ return (str + 1);
4141+ }
4242+ }
4343+ else {
4444+ str += 1;
4545+ }
4646+ }
4747+4848+ return (NULL);
4949+}
5050+5151+static
5252+const char *drv_skip_space (const char *str)
5353+{
5454+ while (1) {
5555+ if (*str == ' ') {
5656+ str += 1;
5757+ }
5858+ else if (*str == '\t') {
5959+ str += 1;
6060+ }
6161+ else {
6262+ return (str);
6363+ }
6464+ }
6565+}
6666+6767+static
6868+const char *drv_get_option_value (const char *str, const char *name)
6969+{
7070+ str = drv_skip_space (str);
7171+7272+ while (*name != 0) {
7373+ if (*str != *name) {
7474+ return (NULL);
7575+ }
7676+7777+ str += 1;
7878+ name += 1;
7979+ }
8080+8181+ str = drv_skip_space (str);
8282+8383+ if (*str == '=') {
8484+ str = drv_skip_space (str + 1);
8585+8686+ return (str);
8787+ }
8888+8989+ return (NULL);
9090+}
9191+9292+static
9393+char *drv_option_dup (const char *str)
9494+{
9595+ unsigned i, j, n;
9696+ char *ret;
9797+9898+ n = 0;
9999+ while (str[n] != 0) {
100100+ if (str[n] == ':') {
101101+ if (str[n + 1] == ':') {
102102+ n += 2;
103103+ }
104104+ else {
105105+ break;
106106+ }
107107+ }
108108+ else {
109109+ n += 1;
110110+ }
111111+ }
112112+113113+ while (n > 0) {
114114+ if (str[n - 1] == ' ') {
115115+ n -= 1;
116116+ }
117117+ else if (str[n - 1] == '\t') {
118118+ n -= 1;
119119+ }
120120+ else {
121121+ break;
122122+ }
123123+ }
124124+125125+ ret = malloc (n + 1);
126126+ if (ret == NULL) {
127127+ return (NULL);
128128+ }
129129+130130+ i = 0;
131131+ j = 0;
132132+133133+ while (i < n) {
134134+ if ((str[i] == ':') && (str[i + 1] == ':')) {
135135+ ret[j] = ':';
136136+ i += 2;
137137+ }
138138+ else {
139139+ ret[j] = str[i];
140140+ i += 1;
141141+ }
142142+143143+ j += 1;
144144+ }
145145+146146+ ret[j] = 0;
147147+148148+ return (ret);
149149+}
150150+151151+char *drv_get_option (const char *str, const char *name)
152152+{
153153+ const char *val;
154154+155155+ while (str != NULL) {
156156+ val = drv_get_option_value (str, name);
157157+158158+ if (val != NULL) {
159159+ return (drv_option_dup (val));
160160+ }
161161+162162+ str = drv_skip_option (str);
163163+ }
164164+165165+ return (NULL);
166166+}
167167+168168+int drv_get_option_bool (const char *str, const char *name, int def)
169169+{
170170+ int r;
171171+ char *s;
172172+173173+ s = drv_get_option (str, name);
174174+175175+ if (s == NULL) {
176176+ return (def);
177177+ }
178178+179179+ r = def;
180180+181181+ if (strcmp (s, "1") == 0) {
182182+ r = 1;
183183+ }
184184+ else if (strcmp (s, "true") == 0) {
185185+ r = 1;
186186+ }
187187+ else if (strcmp (s, "yes") == 0) {
188188+ r = 1;
189189+ }
190190+ else if (strcmp (s, "0") == 0) {
191191+ r = 0;
192192+ }
193193+ else if (strcmp (s, "false") == 0) {
194194+ r = 0;
195195+ }
196196+ else if (strcmp (s, "no") == 0) {
197197+ r = 0;
198198+ }
199199+200200+ free (s);
201201+202202+ return (r);
203203+}
204204+205205+unsigned long drv_get_option_uint (const char *str, const char *name, unsigned long def)
206206+{
207207+ unsigned long val;
208208+ char *end;
209209+ char *s;
210210+211211+ s = drv_get_option (str, name);
212212+213213+ if (s == NULL) {
214214+ return (def);
215215+ }
216216+217217+ val = strtoul (s, &end, 0);
218218+219219+ if ((end != NULL) && (*end != 0)) {
220220+ free (s);
221221+ return (def);
222222+ }
223223+224224+ free (s);
225225+226226+ return (val);
227227+}
+48
src/drivers/options.h
···11+/*****************************************************************************
22+ * pce *
33+ *****************************************************************************/
44+55+/*****************************************************************************
66+ * File name: src/drivers/options.h *
77+ * Created: 2009-10-17 by Hampa Hug <hampa@hampa.ch> *
88+ * Copyright: (C) 2009-2010 Hampa Hug <hampa@hampa.ch> *
99+ *****************************************************************************/
1010+1111+/*****************************************************************************
1212+ * This program is free software. You can redistribute it and / or modify it *
1313+ * under the terms of the GNU General Public License version 2 as published *
1414+ * by the Free Software Foundation. *
1515+ * *
1616+ * This program is distributed in the hope that it will be useful, but *
1717+ * WITHOUT ANY WARRANTY, without even the implied warranty of *
1818+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General *
1919+ * Public License for more details. *
2020+ *****************************************************************************/
2121+2222+2323+#ifndef PCE_DRIVERS_OPTIONS_H
2424+#define PCE_DRIVERS_OPTIONS_H 1
2525+2626+2727+/*!***************************************************************************
2828+ * @short Get a named driver option
2929+ * @return The option value or NULL if the option is not defined in str
3030+ *
3131+ * The returned string is owned by the caller.
3232+ *****************************************************************************/
3333+char *drv_get_option (const char *str, const char *name);
3434+3535+/*!***************************************************************************
3636+ * @short Get a named boolean driver option
3737+ @ @return The option value
3838+ *****************************************************************************/
3939+int drv_get_option_bool (const char *str, const char *name, int def);
4040+4141+/*!***************************************************************************
4242+ * @short Get a named integer driver option
4343+ @ @return The option value
4444+ *****************************************************************************/
4545+unsigned long drv_get_option_uint (const char *str, const char *name, unsigned long def);
4646+4747+4848+#endif