diff options
Diffstat (limited to 'source/base/base_parse.c')
| -rwxr-xr-x | source/base/base_parse.c | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/source/base/base_parse.c b/source/base/base_parse.c new file mode 100755 index 0000000..4f216bf --- /dev/null +++ b/source/base/base_parse.c | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | /* | ||
| 2 | * is_numeric - Check if a string contains only digits | ||
| 3 | * @s: String to check | ||
| 4 | * | ||
| 5 | * Return: 1 if string contains only numeric characters, 0 otherwise | ||
| 6 | */ | ||
| 7 | internal b8 | ||
| 8 | is_numeric(char *s) | ||
| 9 | { | ||
| 10 | for (; *s; ++s) | ||
| 11 | { | ||
| 12 | if (*s < '0' || *s > '9') | ||
| 13 | { | ||
| 14 | return 0; | ||
| 15 | } | ||
| 16 | } | ||
| 17 | return 1; | ||
| 18 | } | ||
| 19 | |||
| 20 | /* | ||
| 21 | * TODO(nasr): checkout i think there is a buffer overflow happening somewhere | ||
| 22 | * */ | ||
| 23 | internal proc_file * | ||
| 24 | parse_proc_files(char *path, mem_arena *arena) | ||
| 25 | { | ||
| 26 | if (!path || !arena) | ||
| 27 | { | ||
| 28 | return NULL; | ||
| 29 | } | ||
| 30 | |||
| 31 | i32 fd = open(path, O_RDONLY); | ||
| 32 | if (fd < 0) | ||
| 33 | { | ||
| 34 | return NULL; | ||
| 35 | } | ||
| 36 | |||
| 37 | char *buffer = PushArray(arena, char, KiB(4)); | ||
| 38 | u64 bytes = read(fd, buffer, KiB(4)); | ||
| 39 | close(fd); | ||
| 40 | |||
| 41 | if (bytes == 0) | ||
| 42 | { | ||
| 43 | return NULL; | ||
| 44 | } | ||
| 45 | |||
| 46 | /* guessing the count to 256 because i dont want to do a double pass of the buffer */ | ||
| 47 | proc_file *pf = PushStruct(arena, proc_file); | ||
| 48 | pf->entries = PushArray(arena, proc_entry, 256); | ||
| 49 | |||
| 50 | u64 line_start = 0; | ||
| 51 | u64 delim = -1; | ||
| 52 | u64 entry_index = 0; | ||
| 53 | |||
| 54 | for (u64 index = 0; index < bytes; ++index) | ||
| 55 | { | ||
| 56 | if (buffer[index] == ':' && delim == (u64)-1) | ||
| 57 | { | ||
| 58 | delim = index; | ||
| 59 | } | ||
| 60 | else if (buffer[index] == '\n') | ||
| 61 | { | ||
| 62 | if (delim != (-1.f)) | ||
| 63 | { | ||
| 64 | u64 key_len = delim - line_start; | ||
| 65 | if (key_len >= sizeof(pf->entries[entry_index].key)) | ||
| 66 | { | ||
| 67 | key_len = sizeof(pf->entries[entry_index].key) - 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | u64 val_start = delim + 1; | ||
| 71 | while (val_start < index && (buffer[val_start] == ' ' || buffer[val_start] == '\t')) | ||
| 72 | { | ||
| 73 | val_start++; | ||
| 74 | } | ||
| 75 | |||
| 76 | u64 val_len = index - val_start; | ||
| 77 | if (val_len >= sizeof(pf->entries[entry_index].value)) | ||
| 78 | { | ||
| 79 | val_len = sizeof(pf->entries[entry_index].value) - 1; | ||
| 80 | } | ||
| 81 | |||
| 82 | MemCpy(pf->entries[entry_index].key, buffer + line_start, key_len - 1); | ||
| 83 | MemCpy(pf->entries[entry_index].value, buffer + val_start, val_len); | ||
| 84 | |||
| 85 | pf->entries[entry_index].key[key_len] = '\0'; | ||
| 86 | pf->entries[entry_index].value[val_len] = '\0'; | ||
| 87 | |||
| 88 | ++pf->count; | ||
| 89 | ++entry_index; | ||
| 90 | } | ||
| 91 | |||
| 92 | line_start = index + 1; | ||
| 93 | delim = (u64)-1; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | return (pf); | ||
| 98 | } | ||
