I am currently writing a simple cursor library in c for parsing fix structured strings.
For example, it allows to parse the following string into to previously defined variables as follows (github):
char *test_string = "27,19";
S_CCURSOR_HANDLER cursor = {0};
ret = ccursor_init(&cursor, test_string, strlen(test_string));
uint8_t value_a = 0;
uint8_t value_b = 0;
// parse int values
ret &= ccursor_read_u8(&cursor, &value_a);
ret &= ccursor_skip_char(&cursor, ',');
ret &= ccursor_read_u8(&cursor, &value_b);
// check if empty
ret &= ccursor_is_empty(&cursor);
This works fine, but to make the interface a little bit more convenient, I would like to also provide some macro based definition.
For example: CCURSOR_PARSER(macro_parser, "27,19", U8(value_a) U8(value_b));
My problem now is, that I am to stupid to get the macro logic right.
Currently, I have the following macro code:
#define U8_EVAL(_var, _cur)
uint8_t _var = 0;
_cur##_ret &= ccursor_read_u8(&_cur, &_var);
#define U8(_var) U8_EVAL(_var, _cur)
#define CCURSOR_PARSER(_cur, _str, _pattern)
bool _cur##_ret = false;
S_CCURSOR_HANDLER _cur = {0};
_cur##_ret = ccursor_init(&_cur, _str, strlen(_str));
_pattern
This does not compile, because it complains that _cur
is the first use within the U8 to U8_EVAL macro expansion. My general question is actually, if I can somehow inject the value of _cur
from the parent macro CCURSOR_PARSER
into all the sub macros such as U8, U16, etc.
The simplest solution would be to extend U8 with the _cur (U8(_var, _cur)
), but this would not make the user code nice; it would expand to CCURSOR_PARSER(macro_parser, "27,19", U8(value_a, macro_parser) U8(value_b, macro_parser));
what is not my goal.
Is not there a way to pass through the _cur to all sub macros?
Thanks in advise