1 2 3 4 5 6 7 8 9 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| char* string_format(const char *format, ...) { va_list args; va_start(args, format); int len = vsnprintf(NULL, 0, format, args); va_end(args); if (len < 0) return NULL; char *result = (char*)malloc(len + 1); if (!result) return NULL; va_start(args, format); vsnprintf(result, len + 1, format, args); va_end(args); return result; }
typedef struct { char ***rows; int *column_counts; int row_count; int capacity; } CSVData;
CSVData* csv_parse(const char *csv_string) { if (!csv_string) return NULL; CSVData *csv = (CSVData*)malloc(sizeof(CSVData)); if (!csv) return NULL; csv->capacity = 10; csv->rows = (char***)malloc(csv->capacity * sizeof(char**)); csv->column_counts = (int*)malloc(csv->capacity * sizeof(int)); csv->row_count = 0; if (!csv->rows || !csv->column_counts) { free(csv->rows); free(csv->column_counts); free(csv); return NULL; } StringTokens *lines = string_split(csv_string, "\n"); if (!lines) { free(csv->rows); free(csv->column_counts); free(csv); return NULL; } for (int i = 0; i < lines->count; i++) { if (strlen(lines->tokens[i]) == 0) continue; if (csv->row_count >= csv->capacity) { csv->capacity *= 2; csv->rows = (char***)realloc(csv->rows, csv->capacity * sizeof(char**)); csv->column_counts = (int*)realloc(csv->column_counts, csv->capacity * sizeof(int)); } StringTokens *columns = string_split_advanced(lines->tokens[i], ',', '"', '\\'); if (columns) { csv->rows[csv->row_count] = (char**)malloc(columns->count * sizeof(char*)); for (int j = 0; j < columns->count; j++) { csv->rows[csv->row_count][j] = strdup(columns->tokens[j]); } csv->column_counts[csv->row_count] = columns->count; csv->row_count++; tokens_destroy(columns); } } tokens_destroy(lines); return csv; }
typedef enum { JSON_NULL, JSON_BOOL, JSON_NUMBER, JSON_STRING, JSON_ARRAY, JSON_OBJECT } JsonType;
typedef struct JsonValue { JsonType type; union { int bool_value; double number_value; char *string_value; struct { struct JsonValue **items; int count; } array_value; struct { char **keys; struct JsonValue **values; int count; } object_value; } data; } JsonValue;
const char* skip_whitespace(const char *json) { while (*json && isspace(*json)) { json++; } return json; }
JsonValue* json_parse_string(const char **json) { const char *start = ++(*json); while (**json && **json != '"') { if (**json == '\\') { (*json)++; } (*json)++; } if (**json != '"') return NULL; int len = *json - start; JsonValue *value = (JsonValue*)malloc(sizeof(JsonValue)); if (!value) return NULL; value->type = JSON_STRING; value->data.string_value = (char*)malloc(len + 1); if (!value->data.string_value) { free(value); return NULL; } memcpy(value->data.string_value, start, len); value->data.string_value[len] = '\0'; (*json)++; return value; }
JsonValue* json_parse_number(const char **json) { char *end; double number = strtod(*json, &end); if (end == *json) return NULL; JsonValue *value = (JsonValue*)malloc(sizeof(JsonValue)); if (!value) return NULL; value->type = JSON_NUMBER; value->data.number_value = number; *json = end; return value; }
|