diff options
Diffstat (limited to 'source/csv_reader.h')
| -rw-r--r-- | source/csv_reader.h | 61 |
1 files changed, 34 insertions, 27 deletions
diff --git a/source/csv_reader.h b/source/csv_reader.h index 2b6f49c..7f5bf06 100644 --- a/source/csv_reader.h +++ b/source/csv_reader.h | |||
| @@ -8,7 +8,6 @@ enum token_flags | |||
| 8 | END_FL = 1 << 2, | 8 | END_FL = 1 << 2, |
| 9 | }; | 9 | }; |
| 10 | 10 | ||
| 11 | |||
| 12 | typedef enum token_type token_type; | 11 | typedef enum token_type token_type; |
| 13 | enum token_type | 12 | enum token_type |
| 14 | { | 13 | { |
| @@ -27,6 +26,7 @@ struct token | |||
| 27 | token *next; | 26 | token *next; |
| 28 | }; | 27 | }; |
| 29 | 28 | ||
| 29 | // NOTE(nasr): i dont think im going to use this. | ||
| 30 | typedef struct csv_row csv_row; | 30 | typedef struct csv_row csv_row; |
| 31 | struct csv_row | 31 | struct csv_row |
| 32 | { | 32 | { |
| @@ -62,8 +62,6 @@ csv_table nil_csv_table = | |||
| 62 | .row_count = 0, | 62 | .row_count = 0, |
| 63 | }; | 63 | }; |
| 64 | 64 | ||
| 65 | |||
| 66 | |||
| 67 | #endif /* ENGINE_LEXER_H */ | 65 | #endif /* ENGINE_LEXER_H */ |
| 68 | 66 | ||
| 69 | // the lexer acts as a table builder from a csv file | 67 | // the lexer acts as a table builder from a csv file |
| @@ -72,13 +70,16 @@ csv_table nil_csv_table = | |||
| 72 | internal token * | 70 | internal token * |
| 73 | tokenize_csv(string8 buffer, mem_arena *arena) | 71 | tokenize_csv(string8 buffer, mem_arena *arena) |
| 74 | { | 72 | { |
| 75 | |||
| 76 | b32 FL = TRUE; | 73 | b32 FL = TRUE; |
| 77 | 74 | ||
| 78 | if(buffer.size < 0) return NULL; | 75 | if(buffer.size < 0) return NULL; |
| 76 | |||
| 77 | token *tok = PushStruct(arena, token); | ||
| 78 | |||
| 79 | // URGENT(nasr): segfaulting because memcpy of strring value doesnt work dammit | ||
| 80 | // NOPE ITS BEECAUSE WEE DONT LOAD CSV OR SOMTHING??? | ||
| 79 | for(s32 index = 0; buffer.data[index] != '\0'; ++index) | 81 | for(s32 index = 0; buffer.data[index] != '\0'; ++index) |
| 80 | { | 82 | { |
| 81 | token *tok = PushStruct(arena, token); | ||
| 82 | u8 point = buffer.data[index]; | 83 | u8 point = buffer.data[index]; |
| 83 | 84 | ||
| 84 | s32 start = 0; | 85 | s32 start = 0; |
| @@ -86,35 +87,35 @@ tokenize_csv(string8 buffer, mem_arena *arena) | |||
| 86 | 87 | ||
| 87 | if(is_whitespace(point)) | 88 | if(is_whitespace(point)) |
| 88 | { | 89 | { |
| 89 | print("csv file is invalid"); | 90 | warn("csv file is invalid, detected whitespace"); |
| 90 | return NULL; | 91 | return NULL; |
| 91 | } | 92 | } |
| 92 | 93 | ||
| 93 | switch(point) | 94 | switch(point) |
| 94 | { | 95 | { |
| 95 | case('\n'): | 96 | case('\n'): |
| 96 | { | 97 | { |
| 97 | if(FL) tok->flags |= END_FL; | 98 | if(FL) tok->flags |= END_FL; |
| 98 | break; | 99 | break; |
| 99 | } | 100 | } |
| 100 | 101 | ||
| 101 | case(','): | 102 | case(','): |
| 102 | { | 103 | { |
| 103 | end = index - 1; | 104 | end = index - 1; |
| 104 | start = index + 1; | 105 | start = index + 1; |
| 105 | break; | 106 | break; |
| 106 | } | 107 | } |
| 107 | default: | 108 | default: |
| 108 | { | 109 | { |
| 109 | break; | 110 | break; |
| 110 | } | 111 | } |
| 111 | } | 112 | } |
| 112 | 113 | ||
| 113 | tok->lexeme = StringCast(&buffer.data[start], end - start); | 114 | tok->lexeme = StringCast(&buffer.data[start], end - start); |
| 114 | tok->next = tok; | 115 | tok->next = tok; |
| 115 | } | 116 | } |
| 116 | 117 | ||
| 117 | return NULL; | 118 | return tok; |
| 118 | } | 119 | } |
| 119 | 120 | ||
| 120 | internal void | 121 | internal void |
| @@ -124,18 +125,24 @@ read_csv(string8 buffer) | |||
| 124 | 125 | ||
| 125 | } | 126 | } |
| 126 | 127 | ||
| 127 | internal b_tree * | 128 | internal b_tree * |
| 128 | parse_csv(csv_token *tok, csv_table *table) | 129 | parse_csv(mem_arena *arena, token *tok) |
| 129 | { | 130 | { |
| 131 | b_tree *tree = PushStructZero(arena, b_tree); | ||
| 132 | b_tree_create(arena, tree); | ||
| 130 | 133 | ||
| 131 | 134 | for (; tok != NULL; tok = tok->next) | |
| 132 | for (;tok->next; tok = tok->next) | ||
| 133 | { | 135 | { |
| 134 | b_tree_node *current_btree_node = btree_node_alloc; | 136 | // skip structural tokens, only index values |
| 137 | if (tok->type != TOKEN_VALUE) | ||
| 138 | { | ||
| 139 | continue; | ||
| 140 | } | ||
| 135 | 141 | ||
| 136 | 142 | // NOTE(nasr): payload is the token itself so the caller can reach | |
| 143 | // row/col metadata without us having to copy it | ||
| 144 | b_tree_insert(arena, tree, tok->lexeme, (void *)tok); | ||
| 137 | } | 145 | } |
| 138 | 146 | ||
| 139 | return NULL; | 147 | return tree; |
| 140 | } | 148 | } |
| 141 | |||
