aboutsummaryrefslogtreecommitdiff
path: root/src/stdlib/simpleparse.h
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-08-23 19:28:08 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-08-23 19:28:08 -0400
commitfcda36561d668f43bac91ea31cd55cbbd605d330 (patch)
treeeb74c0b17df584af0fd8154422ad924e04c96cc2 /src/stdlib/simpleparse.h
parent414b0c7472c87c5a013029aefef49e2dbc41e700 (diff)
Autoformat everything with clang-format
Diffstat (limited to 'src/stdlib/simpleparse.h')
-rw-r--r--src/stdlib/simpleparse.h58
1 files changed, 35 insertions, 23 deletions
diff --git a/src/stdlib/simpleparse.h b/src/stdlib/simpleparse.h
index da077e20..d35a8b3b 100644
--- a/src/stdlib/simpleparse.h
+++ b/src/stdlib/simpleparse.h
@@ -22,30 +22,42 @@
#include "mapmacro.h"
-typedef struct { char c; } some_char_t;
-#define PARSE_SOME_OF(chars) ((some_char_t*)chars)
+typedef struct {
+ char c;
+} some_char_t;
+#define PARSE_SOME_OF(chars) ((some_char_t *)chars)
#define PARSE_WHITESPACE PARSE_SOME_OF(" \t\r\n\v")
-typedef enum {PARSE_LITERAL, PARSE_LONG, PARSE_DOUBLE, PARSE_BOOL, PARSE_STRING, PARSE_SOME_OF} parse_type_e;
-
-typedef struct { parse_type_e type; void *dest; } parse_element_t;
-
-#define _parse_type(dest) _Generic((dest), \
- some_char_t*: PARSE_SOME_OF, \
- const char*: PARSE_LITERAL, \
- char*: PARSE_LITERAL, \
- const char**: PARSE_STRING, \
- char**: PARSE_STRING, \
- double*: PARSE_DOUBLE, \
- long*: PARSE_LONG, \
- bool*: PARSE_BOOL)
-
-#define as_void_star(x) ((void*)x)
-#define strparse(str, ...) simpleparse(str, sizeof((const void*[]){__VA_ARGS__})/sizeof(void*), (parse_type_e[]){MAP_LIST(_parse_type, __VA_ARGS__)}, (void*[]){MAP_LIST(as_void_star, __VA_ARGS__)})
-#define fparse(file, ...) ({ char *_file_contents = NULL; size_t _capacity; \
- ssize_t _just_read = getdelim(&_file_contents, &_capacity, '\0', file); \
- const char *_parse_err = _just_read > 0 ? strparse(_file_contents, __VA_ARGS__) : "No such file"; \
- if (_file_contents) free(_file_contents); \
- _parse_err; })
+typedef enum { PARSE_LITERAL, PARSE_LONG, PARSE_DOUBLE, PARSE_BOOL, PARSE_STRING, PARSE_SOME_OF } parse_type_e;
+
+typedef struct {
+ parse_type_e type;
+ void *dest;
+} parse_element_t;
+
+#define _parse_type(dest) \
+ _Generic((dest), \
+ some_char_t *: PARSE_SOME_OF, \
+ const char *: PARSE_LITERAL, \
+ char *: PARSE_LITERAL, \
+ const char **: PARSE_STRING, \
+ char **: PARSE_STRING, \
+ double *: PARSE_DOUBLE, \
+ long *: PARSE_LONG, \
+ bool *: PARSE_BOOL)
+
+#define as_void_star(x) ((void *)x)
+#define strparse(str, ...) \
+ simpleparse(str, sizeof((const void *[]){__VA_ARGS__}) / sizeof(void *), \
+ (parse_type_e[]){MAP_LIST(_parse_type, __VA_ARGS__)}, (void *[]){MAP_LIST(as_void_star, __VA_ARGS__)})
+#define fparse(file, ...) \
+ ({ \
+ char *_file_contents = NULL; \
+ size_t _capacity; \
+ ssize_t _just_read = getdelim(&_file_contents, &_capacity, '\0', file); \
+ const char *_parse_err = _just_read > 0 ? strparse(_file_contents, __VA_ARGS__) : "No such file"; \
+ if (_file_contents) free(_file_contents); \
+ _parse_err; \
+ })
const char *simpleparse(const char *str, int n, parse_type_e types[n], void *destinations[n]);