Don't test font if the banner is disabled.
[fswebcam.git] / parse.c
blob5ebe5b90aa0a9bdf37156d4398743721719c7181
1 /* fswebcam - FireStorm.cx's webcam generator */
2 /*===========================================================*/
3 /* Copyright (C)2005-2006 Philip Heron <phil@firestorm.cx> */
4 /* */
5 /* This program is distributed under the terms of the GNU */
6 /* General Public License, version 2. You may use, modify, */
7 /* and redistribute it under the terms of this license. A */
8 /* copy should be included with this source. */
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <errno.h>
18 #include "parse.h"
20 #define ARG_EOS (-1) /* End of string */
21 #define ARG_EOA (-2) /* End of argument */
23 typedef struct {
25 char *s;
26 char *sep;
27 char opts;
29 char quote;
31 } arg_t;
33 int arggetc(arg_t *arg)
35 if(*arg->s == '\0') return(ARG_EOS);
37 if((!(arg->opts & ARG_NO_ESCAPE) && *arg->s == '\\') &&
38 !(arg->quote && *(arg->s + 1) != '"'))
40 if(*(++arg->s) == '\0') return(ARG_EOS);
41 return((int) *(arg->s++));
44 if(!(arg->opts & ARG_NO_QUOTE) && *arg->s == '"')
46 arg->s++;
47 arg->quote = !arg->quote;
49 return(arggetc(arg));
52 if(arg->quote) return((int) *(arg->s++));
54 if(strchr(arg->sep, *arg->s))
56 arg->s++;
57 return(ARG_EOA);
60 return((int) *(arg->s++));
63 int argopts(arg_t *arg, char *src, char *sep, int opts)
65 memset(arg, 0, sizeof(arg_t));
66 arg->s = src;
67 arg->sep = sep;
68 arg->opts = opts;
69 return(0);
72 int argncpy(char *dst, size_t n, char *src, char *sep, int arg, int opt)
74 arg_t s;
75 char *d;
76 size_t l;
77 int r;
79 argopts(&s, src, sep, opt);
81 d = dst;
82 *d = '\0';
83 l = 0;
85 while((r = arggetc(&s)) != ARG_EOS)
87 if(r == ARG_EOA)
89 if(!(opt & ARG_NO_TRIM) && !l) continue;
90 if(!arg--) return(0);
92 d = dst;
93 *d = '\0';
94 l = 0;
96 continue;
99 if(n > l)
101 *(d++) = r;
102 *d = '\0';
103 l++;
107 if(!arg && !(!(opt & ARG_NO_TRIM) && !l)) return(0);
109 return(-1);
112 int arglen(char *src, char *sep, int arg, int opt)
114 arg_t s;
115 size_t l;
116 int r;
118 argopts(&s, src, sep, opt);
120 l = 0;
122 while((r = arggetc(&s)) != ARG_EOS)
124 if(r == ARG_EOA)
126 if(!(opt & ARG_NO_TRIM) && !l) continue;
127 if(!arg--) return(l);
129 l = 0;
131 continue;
134 l++;
137 if(!arg && !(!(opt & ARG_NO_TRIM) && !l)) return(l);
139 return(-1);
142 char *argdup(char *src, char *sep, int arg, int opt)
144 char *dst;
145 int l;
147 l = arglen(src, sep, arg, opt);
148 if(l < 0) return(NULL);
150 dst = malloc(++l);
151 if(!dst) return(NULL);
153 argncpy(dst, l, src, sep, arg, opt);
155 return(dst);
158 int argcount(char *src, char *sep, int opt)
160 int count = 0;
162 while(arglen(src, sep, count, opt) >= 0)
163 count++;
165 return(count);
168 long argtol(char *src, char *sep, int arg, int opt, int base)
170 char *str;
171 long val;
173 str = argdup(src, sep, arg, opt);
174 if(!str) return(-1);
176 /* Catch any errors. */
177 errno = 0;
179 val = strtol(str, NULL, base);
180 if(errno) val = -1;
181 free(str);
183 return(val);
186 int parse_font(char *src, char **font, int *fontsize)
188 char *p;
189 size_t l;
191 if(!src || !font || !fontsize) return(-1);
193 p = strchr(src, ':');
195 if(p) l = p - src;
196 else l = strlen(src);
198 if(l)
200 *font = (char *) calloc(l + 1, 1);
201 if(!*font) return(-1);
202 strncpy(*font, src, l);
205 if(p)
207 p++;
208 if(*p) *fontsize = atoi(p);
211 return(0);
214 char *strtrim(char *str, char *ws)
216 char *s = str;
218 str += strspn(str, ws);
220 while(*str != '\0' && strspn(str, ws) != strlen(str))
221 *(s++) = *(str++);
223 *s = '\0';
225 return(str);