- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
typedef struct {
uint32_t id;
char * title;
} dbrow_t;
typedef struct {
char * context;
dbrow_t dbrow;
} parser_context_t;
typedef enum {
bad_char = 0,
identifer,
number,
state_end_context,
state_end_line
} type_t;
type_t ch_d[0x100] = { [0] = state_end_line, [1 ... 0xff] = bad_char, ['0' ... '9'] = number, ['a' ... 'z'] = identifer, ['\n'] = state_end_context};
inline char * do_other_type(char * p, type_t type) { while(ch_d[*(++p)] == type); return p;}
static inline uint64_t bad_char_headler(parser_context_t * context) { context->context = do_other_type(context->context, bad_char); return 1;}
static inline uint64_t number_headler(parser_context_t * context) {
uint32_t id = 0; char * p = context->context;
while(({ id *= 10; id += (*p - '0'); ch_d[*(++p)];}) == number);
context->dbrow.id = id; context->context = p;
return 1;
}
static inline uint64_t identifer_headler(parser_context_t * context) {
char * p = context->context, * title = context->dbrow.title;
char * end_identifer = do_other_type(p, identifer);
memcpy(title, p, (end_identifer - p)); *(title + (end_identifer - p)) = 0;
context->context = end_identifer;
return 1;
}
static inline uint64_t end_context_headler(parser_context_t * context) {
context->context = do_other_type(context->context, state_end_context);
if(context->dbrow.id && *context->dbrow.title)
fprintf(stderr, "id = %u, title = %s\n", context->dbrow.id, context->dbrow.title);
context->dbrow.id = 0, *context->dbrow.title = 0;
return 1;
}
static inline uint64_t end_line_headler(parser_context_t * context) { return 0; }
typedef uint64_t(*headler_ptr_t)(parser_context_t *);
headler_ptr_t headlers[] = { [bad_char] = bad_char_headler, [identifer] = identifer_headler, [number] = number_headler, [state_end_context] = end_context_headler, [state_end_line] = end_line_headler };
int main(void) {
char * content = strcpy(malloc(50), "1|raz\n11|dva\n21|tri\n31|shestb\n5000|test\n|||\n||\n|\n");
parser_context_t context = (parser_context_t){ .context = content, .dbrow = (dbrow_t){ .title = malloc(1000)}};
while(headlers[ch_d[*(context.context)]](&context));
return 0;
}