csv.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include <stdint.h> | ||
| 2 | #include <stdlib.h> | ||
| 3 | #include <string.h> | ||
| 4 | |||
| 5 | 953874 | static inline int64_t parse_int(const char **cursor, char delimiter) { | |
| 6 | 953874 | int64_t val = 0; | |
| 7 | 953874 | int sign = 1; | |
| 8 | 953874 | const char *c = *cursor; | |
| 9 | |||
| 10 |
4/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 953876 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 953874 times.
|
953879 | while (*c == ' ' || *c == '\t') c++; |
| 11 | |||
| 12 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 953873 times.
|
953874 | if (*c == '-') { |
| 13 | 1 | sign = -1; | |
| 14 | 1 | c++; | |
| 15 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 953873 times.
|
953873 | } else if (*c == '+') { |
| 16 | ✗ | c++; | |
| 17 | } | ||
| 18 | |||
| 19 |
4/4✓ Branch 0 taken 2937114 times.
✓ Branch 1 taken 953723 times.
✓ Branch 2 taken 2936963 times.
✓ Branch 3 taken 151 times.
|
3890837 | while (*c >= '0' && *c <= '9') { |
| 20 | 2936963 | val = val * 10 + (*c - '0'); | |
| 21 | 2936963 | c++; | |
| 22 | } | ||
| 23 | |||
| 24 |
7/8✓ Branch 0 taken 238472 times.
✓ Branch 1 taken 715407 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 238465 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
|
953879 | while (*c != delimiter && *c != '\n' && *c != '\r' && *c != '\0') c++; |
| 25 |
2/2✓ Branch 0 taken 715407 times.
✓ Branch 1 taken 238467 times.
|
953874 | if (*c == delimiter) c++; |
| 26 | |||
| 27 | 953874 | *cursor = c; | |
| 28 | 953874 | return val * sign; | |
| 29 | } | ||
| 30 | |||
| 31 | 134 | int evutils_read_csv( | |
| 32 | const char *buffer, size_t buffer_len, | ||
| 33 | char delimiter, | ||
| 34 | void **out_arrays, | ||
| 35 | int *array_types, | ||
| 36 | int *col_mapping, | ||
| 37 | int max_csv_cols, | ||
| 38 | size_t max_events, | ||
| 39 | size_t *bytes_consumed, | ||
| 40 | size_t *events_parsed | ||
| 41 | ) { | ||
| 42 | 134 | const char *cursor = buffer; | |
| 43 | 134 | const char *end = buffer + buffer_len; | |
| 44 | 134 | size_t count = 0; | |
| 45 | |||
| 46 |
4/4✓ Branch 0 taken 238582 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 238501 times.
✓ Branch 3 taken 81 times.
|
238634 | while (cursor < end && count < max_events) { |
| 47 | 238501 | const char *line_end = cursor; | |
| 48 |
4/4✓ Branch 0 taken 3890901 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 3652401 times.
✓ Branch 3 taken 238500 times.
|
3890902 | while (line_end < end && *line_end != '\n') line_end++; |
| 49 | |||
| 50 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 238500 times.
|
238501 | if (line_end == end) { |
| 51 | 1 | break; | |
| 52 | } | ||
| 53 | |||
| 54 |
3/4✓ Branch 0 taken 238469 times.
✓ Branch 1 taken 31 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 238469 times.
|
238500 | if (*cursor == '\n' || *cursor == '\r') { |
| 55 | 31 | cursor++; | |
| 56 |
3/4✓ Branch 0 taken 1 time.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
|
31 | if (cursor < end && *cursor == '\n') cursor++; |
| 57 | 31 | continue; | |
| 58 | } | ||
| 59 | |||
| 60 | 238469 | const char *line_cursor = cursor; | |
| 61 | 238469 | int col_idx = 0; | |
| 62 | |||
| 63 |
4/4✓ Branch 0 taken 953878 times.
✓ Branch 1 taken 238465 times.
✓ Branch 2 taken 953874 times.
✓ Branch 3 taken 4 times.
|
1192343 | while (line_cursor < line_end && col_idx < max_csv_cols) { |
| 64 | 953874 | int target_idx = col_mapping[col_idx]; | |
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 953874 times.
|
953874 | if (target_idx == -1) { |
| 66 | ✗ | while (line_cursor < line_end && *line_cursor != delimiter) line_cursor++; | |
| 67 | ✗ | if (line_cursor < line_end && *line_cursor == delimiter) line_cursor++; | |
| 68 | } else { | ||
| 69 | 953874 | int64_t val = parse_int(&line_cursor, delimiter); | |
| 70 | 953874 | int type = array_types[target_idx]; | |
| 71 |
2/2✓ Branch 0 taken 238468 times.
✓ Branch 1 taken 715406 times.
|
953874 | if (type == 1) { |
| 72 | 238468 | ((uint8_t*)out_arrays[target_idx])[count] = (uint8_t)val; | |
| 73 |
2/2✓ Branch 0 taken 476937 times.
✓ Branch 1 taken 238469 times.
|
715406 | } else if (type == 2) { |
| 74 | 476937 | ((uint16_t*)out_arrays[target_idx])[count] = (uint16_t)val; | |
| 75 |
1/2✓ Branch 0 taken 238469 times.
✗ Branch 1 not taken.
|
238469 | } else if (type == 8) { |
| 76 | 238469 | ((int64_t*)out_arrays[target_idx])[count] = val; | |
| 77 | } | ||
| 78 | } | ||
| 79 | 953874 | col_idx++; | |
| 80 | } | ||
| 81 | |||
| 82 | 238469 | cursor = line_end + 1; | |
| 83 | 238469 | count++; | |
| 84 | } | ||
| 85 | |||
| 86 | 134 | *bytes_consumed = cursor - buffer; | |
| 87 | 134 | *events_parsed = count; | |
| 88 | 134 | return 0; | |
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | 930216 | static inline char* itoa_positive(uint64_t val, char *buf) { | |
| 93 | 930216 | char *p = buf + 20; | |
| 94 | 930216 | *p = '\0'; | |
| 95 |
2/2✓ Branch 0 taken 116848 times.
✓ Branch 1 taken 813368 times.
|
930216 | if (val == 0) { |
| 96 | 116848 | *--p = '0'; | |
| 97 | 116848 | return p; | |
| 98 | } | ||
| 99 |
2/2✓ Branch 0 taken 2750713 times.
✓ Branch 1 taken 813368 times.
|
3564081 | while (val > 0) { |
| 100 | 2750713 | *--p = '0' + (val % 10); | |
| 101 | 2750713 | val /= 10; | |
| 102 | } | ||
| 103 | 813368 | return p; | |
| 104 | } | ||
| 105 | |||
| 106 | 232554 | static inline char* itoa_signed(int64_t val, char *buf) { | |
| 107 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 232553 times.
|
232554 | if (val < 0) { |
| 108 | 1 | char *p = itoa_positive((uint64_t)(-val), buf); | |
| 109 | 1 | *--p = '-'; | |
| 110 | 1 | return p; | |
| 111 | } | ||
| 112 | 232553 | return itoa_positive((uint64_t)val, buf); | |
| 113 | } | ||
| 114 | |||
| 115 | 31 | int evutils_write_csv( | |
| 116 | void **in_arrays, | ||
| 117 | int *array_types, | ||
| 118 | int num_columns, | ||
| 119 | char delimiter, | ||
| 120 | size_t num_events, | ||
| 121 | char *out_buffer, | ||
| 122 | size_t out_buffer_len, | ||
| 123 | size_t *bytes_written, | ||
| 124 | size_t *events_written | ||
| 125 | ) { | ||
| 126 | 31 | size_t buf_pos = 0; | |
| 127 | char num_buf[24]; | ||
| 128 | 31 | size_t i = 0; | |
| 129 | |||
| 130 |
2/2✓ Branch 0 taken 232554 times.
✓ Branch 1 taken 31 times.
|
232585 | for (; i < num_events; i++) { |
| 131 | 232554 | size_t max_req_len = (size_t)(num_columns * 22); | |
| 132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 232554 times.
|
232554 | if (buf_pos + max_req_len > out_buffer_len) { |
| 133 | ✗ | break; | |
| 134 | } | ||
| 135 | |||
| 136 |
2/2✓ Branch 0 taken 930216 times.
✓ Branch 1 taken 232554 times.
|
1162770 | for (int c = 0; c < num_columns; c++) { |
| 137 | 930216 | int type = array_types[c]; | |
| 138 | char *s; | ||
| 139 |
2/2✓ Branch 0 taken 232554 times.
✓ Branch 1 taken 697662 times.
|
930216 | if (type == 1) { |
| 140 | 232554 | s = itoa_positive(((uint8_t*)in_arrays[c])[i], num_buf); | |
| 141 |
2/2✓ Branch 0 taken 465108 times.
✓ Branch 1 taken 232554 times.
|
697662 | } else if (type == 2) { |
| 142 | 465108 | s = itoa_positive(((uint16_t*)in_arrays[c])[i], num_buf); | |
| 143 |
1/2✓ Branch 0 taken 232554 times.
✗ Branch 1 not taken.
|
232554 | } else if (type == 8) { |
| 144 | 232554 | s = itoa_signed(((int64_t*)in_arrays[c])[i], num_buf); | |
| 145 | } else { | ||
| 146 | ✗ | s = "0"; | |
| 147 | } | ||
| 148 | |||
| 149 | 930216 | size_t len = num_buf + 20 - s; | |
| 150 | 930216 | memcpy(out_buffer + buf_pos, s, len); | |
| 151 | 930216 | buf_pos += len; | |
| 152 | |||
| 153 |
2/2✓ Branch 0 taken 232554 times.
✓ Branch 1 taken 697662 times.
|
930216 | if (c == num_columns - 1) { |
| 154 | 232554 | out_buffer[buf_pos++] = '\n'; | |
| 155 | } else { | ||
| 156 | 697662 | out_buffer[buf_pos++] = delimiter; | |
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | 31 | *bytes_written = buf_pos; | |
| 162 | 31 | *events_written = i; | |
| 163 | 31 | return 0; | |
| 164 | } | ||
| 165 |