diff options
Diffstat (limited to 'source/csv_reader.h')
| -rw-r--r-- | source/csv_reader.h | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/source/csv_reader.h b/source/csv_reader.h new file mode 100644 index 0000000..6c8e625 --- /dev/null +++ b/source/csv_reader.h | |||
| @@ -0,0 +1,145 @@ | |||
| 1 | #ifndef ENGINE_LEXER_H | ||
| 2 | #define ENGINE_LEXER_H | ||
| 3 | |||
| 4 | typedef enum token_flags token_flags; | ||
| 5 | enum token_flags | ||
| 6 | { | ||
| 7 | START_FL = 1 << 1, | ||
| 8 | END_FL = 1 << 2, | ||
| 9 | }; | ||
| 10 | |||
| 11 | |||
| 12 | typedef enum token_type token_type; | ||
| 13 | enum token_type | ||
| 14 | { | ||
| 15 | // first 255 tokens for ascii characters | ||
| 16 | TOKEN_UNDEFINED = 255, | ||
| 17 | TOKEN_IDENTIFIER, | ||
| 18 | TOKEN_VALUE, | ||
| 19 | }; | ||
| 20 | |||
| 21 | typedef struct token token; | ||
| 22 | struct token | ||
| 23 | { | ||
| 24 | string8 lexeme; | ||
| 25 | token_type type; | ||
| 26 | token_flags flags; | ||
| 27 | token *next; | ||
| 28 | }; | ||
| 29 | |||
| 30 | typedef struct csv_row csv_row; | ||
| 31 | struct csv_row | ||
| 32 | { | ||
| 33 | // array of size col_count, points into mmap buffer | ||
| 34 | string8 *fields; | ||
| 35 | s32 count; | ||
| 36 | }; | ||
| 37 | |||
| 38 | typedef struct csv_table csv_table; | ||
| 39 | struct csv_table | ||
| 40 | { | ||
| 41 | // first row, col names | ||
| 42 | // all data rows | ||
| 43 | string8 *headers; | ||
| 44 | csv_row *rows; | ||
| 45 | s32 col_count; | ||
| 46 | s32 row_count; | ||
| 47 | }; | ||
| 48 | |||
| 49 | read_only global_variable | ||
| 50 | csv_row nil_csv_row = | ||
| 51 | { | ||
| 52 | .fields = &nil_string, | ||
| 53 | .count = 0, | ||
| 54 | }; | ||
| 55 | |||
| 56 | read_only global_variable | ||
| 57 | csv_table nil_csv_table = | ||
| 58 | { | ||
| 59 | .headers = &nil_string, | ||
| 60 | .rows = &nil_csv_row, | ||
| 61 | .col_count = 0, | ||
| 62 | .row_count = 0, | ||
| 63 | }; | ||
| 64 | |||
| 65 | |||
| 66 | // the lexer acts as a table builder from a csv file | ||
| 67 | // and parsing indivudal rows and columns | ||
| 68 | // the next step would be building a the b-tree | ||
| 69 | internal token * | ||
| 70 | tokenize_csv(string8 buffer, mem_arena *arena) | ||
| 71 | { | ||
| 72 | |||
| 73 | b32 FL = TRUE; | ||
| 74 | |||
| 75 | if(buffer.size < 0) return NULL; | ||
| 76 | for(s32 index = 0; buffer.data[index] != '\0'; ++index) | ||
| 77 | { | ||
| 78 | token *tok = PushStruct(arena, token); | ||
| 79 | u8 point = buffer.data[index]; | ||
| 80 | |||
| 81 | s32 start = 0; | ||
| 82 | s32 end = 0; | ||
| 83 | |||
| 84 | if(is_whitespace(point)) | ||
| 85 | { | ||
| 86 | print("csv file is invalid"); | ||
| 87 | return NULL; | ||
| 88 | } | ||
| 89 | |||
| 90 | switch(point) | ||
| 91 | { | ||
| 92 | case('\n'): | ||
| 93 | { | ||
| 94 | if(FL) tok->flags |= END_FL; | ||
| 95 | break; | ||
| 96 | } | ||
| 97 | |||
| 98 | case(','): | ||
| 99 | { | ||
| 100 | end = index - 1; | ||
| 101 | start = index + 1; | ||
| 102 | break; | ||
| 103 | } | ||
| 104 | default: | ||
| 105 | { | ||
| 106 | break; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | tok->lexeme = StringCast(&buffer.data[start], end - start); | ||
| 111 | tok->next = tok; | ||
| 112 | } | ||
| 113 | |||
| 114 | return NULL; | ||
| 115 | } | ||
| 116 | |||
| 117 | internal void | ||
| 118 | strip_new_line(string8 buffer) | ||
| 119 | { | ||
| 120 | |||
| 121 | for (u64 index = 0; index < buffer.size; index++) | ||
| 122 | { | ||
| 123 | |||
| 124 | } | ||
| 125 | |||
| 126 | return; | ||
| 127 | |||
| 128 | } | ||
| 129 | |||
| 130 | internal void | ||
| 131 | read_csv(string8 buffer) | ||
| 132 | { | ||
| 133 | // printf("\nsize:%lu\ndata %s\n", buffer.size, buffer.data); | ||
| 134 | |||
| 135 | } | ||
| 136 | |||
| 137 | internal csv_table * | ||
| 138 | parse_csv(token *tokens, csv_table *table) | ||
| 139 | { | ||
| 140 | |||
| 141 | return NULL; | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | #endif /* ENGINE_LEXER_H */ | ||
