summaryrefslogtreecommitdiff
path: root/apps/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/misc.c')
-rw-r--r--apps/misc.c66
1 files changed, 49 insertions, 17 deletions
diff --git a/apps/misc.c b/apps/misc.c
index e9f1724b1b..51e02f6a62 100644
--- a/apps/misc.c
+++ b/apps/misc.c
@@ -1217,7 +1217,9 @@ int hex_to_rgb(const char* hex, int* color)
1217 s - string (sets pointer to string, without copying) 1217 s - string (sets pointer to string, without copying)
1218 c - hex colour (RGB888 - e.g. ff00ff) 1218 c - hex colour (RGB888 - e.g. ff00ff)
1219 g - greyscale "colour" (0-3) 1219 g - greyscale "colour" (0-3)
1220 1220 valid_vals - if not NULL 1 is set in the bitplace if the item was read OK
1221 0 if not read.
1222 first item is LSB, (max 32 items! )
1221 sep - list separator (e.g. ',' or '|') 1223 sep - list separator (e.g. ',' or '|')
1222 str - string to parse, must be terminated by 0 or sep 1224 str - string to parse, must be terminated by 0 or sep
1223 ... - pointers to store the parsed values 1225 ... - pointers to store the parsed values
@@ -1229,25 +1231,29 @@ int hex_to_rgb(const char* hex, int* color)
1229/* '0'-'3' are ASCII 0x30 to 0x33 */ 1231/* '0'-'3' are ASCII 0x30 to 0x33 */
1230#define is0123(x) (((x) & 0xfc) == 0x30) 1232#define is0123(x) (((x) & 0xfc) == 0x30)
1231 1233
1232const char* parse_list(const char *fmt, const char sep, const char* str, ...) 1234const char* parse_list(const char *fmt, unsigned int *valid_vals,
1235 const char sep, const char* str, ...)
1233{ 1236{
1234 va_list ap; 1237 va_list ap;
1235 const char* p = str; 1238 const char* p = str, *f = fmt;
1236 const char** s; 1239 const char** s;
1237 int* d; 1240 int* d;
1241 bool valid;
1242 int i=0;
1238 1243
1239 va_start(ap, str); 1244 va_start(ap, str);
1240 1245 if (valid_vals)
1246 *valid_vals = 0;
1241 while (*fmt) 1247 while (*fmt)
1242 { 1248 {
1243 /* Check for separator, if we're not at the start */ 1249 /* Check for separator, if we're not at the start */
1244 if (p != str) 1250 if (f != fmt)
1245 { 1251 {
1246 if (*p != sep) 1252 if (*p != sep)
1247 goto err; 1253 goto err;
1248 p++; 1254 p++;
1249 } 1255 }
1250 1256 valid = false;
1251 switch (*fmt++) 1257 switch (*fmt++)
1252 { 1258 {
1253 case 's': /* string - return a pointer to it (not a copy) */ 1259 case 's': /* string - return a pointer to it (not a copy) */
@@ -1256,18 +1262,25 @@ const char* parse_list(const char *fmt, const char sep, const char* str, ...)
1256 *s = p; 1262 *s = p;
1257 while (*p && *p != sep) 1263 while (*p && *p != sep)
1258 p++; 1264 p++;
1259 1265 valid = (*s[0]!=sep);
1260 break; 1266 break;
1261 1267
1262 case 'd': /* int */ 1268 case 'd': /* int */
1263 d = va_arg(ap, int*); 1269 d = va_arg(ap, int*);
1264 if (!isdigit(*p)) 1270 if (!isdigit(*p))
1265 goto err; 1271 {
1266 1272 if (!valid_vals)
1267 *d = *p++ - '0'; 1273 goto err;
1268 1274 while (*p && *p != sep)
1269 while (isdigit(*p)) 1275 p++;
1270 *d = (*d * 10) + (*p++ - '0'); 1276 }
1277 else
1278 {
1279 *d = *p++ - '0';
1280 while (isdigit(*p))
1281 *d = (*d * 10) + (*p++ - '0');
1282 valid = true;
1283 }
1271 1284
1272 break; 1285 break;
1273 1286
@@ -1276,9 +1289,17 @@ const char* parse_list(const char *fmt, const char sep, const char* str, ...)
1276 d = va_arg(ap, int*); 1289 d = va_arg(ap, int*);
1277 1290
1278 if (hex_to_rgb(p, d) < 0) 1291 if (hex_to_rgb(p, d) < 0)
1279 goto err; 1292 {
1280 1293 if (!valid_vals)
1281 p += 6; 1294 goto err;
1295 while (*p && *p != sep)
1296 p++;
1297 }
1298 else
1299 {
1300 p += 6;
1301 valid = true;
1302 }
1282 1303
1283 break; 1304 break;
1284#endif 1305#endif
@@ -1288,9 +1309,17 @@ const char* parse_list(const char *fmt, const char sep, const char* str, ...)
1288 d = va_arg(ap, int*); 1309 d = va_arg(ap, int*);
1289 1310
1290 if (is0123(*p)) 1311 if (is0123(*p))
1312 {
1291 *d = *p++ - '0'; 1313 *d = *p++ - '0';
1292 else 1314 valid = true;
1315 }
1316 else if (!valid_vals)
1293 goto err; 1317 goto err;
1318 else
1319 {
1320 while (*p && *p != sep)
1321 p++;
1322 }
1294 1323
1295 break; 1324 break;
1296#endif 1325#endif
@@ -1299,6 +1328,9 @@ const char* parse_list(const char *fmt, const char sep, const char* str, ...)
1299 goto err; 1328 goto err;
1300 break; 1329 break;
1301 } 1330 }
1331 if (valid_vals && valid)
1332 *valid_vals |= (1<<i);
1333 i++;
1302 } 1334 }
1303 1335
1304 va_end(ap); 1336 va_end(ap);