summaryrefslogtreecommitdiff
path: root/apps/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/misc.c')
-rw-r--r--apps/misc.c108
1 files changed, 105 insertions, 3 deletions
diff --git a/apps/misc.c b/apps/misc.c
index 6f34e50a54..632b12b9aa 100644
--- a/apps/misc.c
+++ b/apps/misc.c
@@ -1098,7 +1098,7 @@ static int hex2dec(int c)
1098 (toupper(c)) - 'A' + 10); 1098 (toupper(c)) - 'A' + 10);
1099} 1099}
1100 1100
1101int hex_to_rgb(const char* hex) 1101int hex_to_rgb(const char* hex, int* color)
1102{ int ok = 1; 1102{ int ok = 1;
1103 int i; 1103 int i;
1104 int red, green, blue; 1104 int red, green, blue;
@@ -1115,11 +1115,12 @@ int hex_to_rgb(const char* hex)
1115 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]); 1115 red = (hex2dec(hex[0]) << 4) | hex2dec(hex[1]);
1116 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]); 1116 green = (hex2dec(hex[2]) << 4) | hex2dec(hex[3]);
1117 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]); 1117 blue = (hex2dec(hex[4]) << 4) | hex2dec(hex[5]);
1118 return LCD_RGBPACK(red,green,blue); 1118 *color = LCD_RGBPACK(red,green,blue);
1119 return 0;
1119 } 1120 }
1120 } 1121 }
1121 1122
1122 return 0; 1123 return -1;
1123} 1124}
1124#endif /* HAVE_LCD_COLOR */ 1125#endif /* HAVE_LCD_COLOR */
1125 1126
@@ -1200,3 +1201,104 @@ char *strip_extension(char* buffer, int buffer_size, const char *filename)
1200 1201
1201 return buffer; 1202 return buffer;
1202} 1203}
1204
1205#ifdef HAVE_LCD_BITMAP
1206/* A simplified scanf - used (at time of writing) by wps parsing functions.
1207
1208 fmt - char array specifying the format of each list option. Valid values
1209 are: d - int
1210 s - string (sets pointer to string, without copying)
1211 c - hex colour (RGB888 - e.g. ff00ff)
1212 g - greyscale "colour" (0-3)
1213
1214 sep - list separator (e.g. ',' or '|')
1215 str - string to parse, must be terminated by 0 or sep
1216 ... - pointers to store the parsed values
1217
1218 return value - pointer to char after parsed data, 0 if there was an error.
1219
1220*/
1221
1222/* '0'-'3' are ASCII 0x30 to 0x33 */
1223#define is0123(x) (((x) & 0xfc) == 0x30)
1224
1225const char* parse_list(const char *fmt, const char sep, const char* str, ...)
1226{
1227 va_list ap;
1228 const char* p = str;
1229 const char** s;
1230 int* d;
1231
1232 va_start(ap, str);
1233
1234 while (*fmt)
1235 {
1236 /* Check for separator, if we're not at the start */
1237 if (p != str)
1238 {
1239 if (*p != sep)
1240 goto err;
1241 p++;
1242 }
1243
1244 switch (*fmt++)
1245 {
1246 case 's': /* string - return a pointer to it (not a copy) */
1247 s = va_arg(ap, const char **);
1248
1249 *s = p;
1250 while (*p && *p != sep)
1251 p++;
1252
1253 break;
1254
1255 case 'd': /* int */
1256 d = va_arg(ap, int*);
1257 if (!isdigit(*p))
1258 goto err;
1259
1260 *d = *p++ - '0';
1261
1262 while (isdigit(*p))
1263 *d = (*d * 10) + (*p++ - '0');
1264
1265 break;
1266
1267#ifdef HAVE_LCD_COLOR
1268 case 'c': /* colour (rrggbb - e.g. f3c1a8) */
1269 d = va_arg(ap, int*);
1270
1271 if (hex_to_rgb(p, d) < 0)
1272 goto err;
1273
1274 p += 6;
1275
1276 break;
1277#endif
1278
1279#if LCD_DEPTH == 2 || (defined(HAVE_REMOTE_LCD) && LCD_REMOTE_DEPTH == 2)
1280 case 'g': /* greyscale colour (0-3) */
1281 d = va_arg(ap, int*);
1282
1283 if (is0123(*p))
1284 *d = *p++ - '0';
1285 else
1286 goto err;
1287
1288 break;
1289#endif
1290
1291 default: /* Unknown format type */
1292 goto err;
1293 break;
1294 }
1295 }
1296
1297 va_end(ap);
1298 return p;
1299
1300err:
1301 va_end(ap);
1302 return 0;
1303}
1304#endif