Fixed CSV Parsing on tools

This commit is contained in:
2023-01-28 21:01:30 -08:00
parent 104af8ff45
commit 9ebd4eb6ad
16 changed files with 345 additions and 18 deletions

View File

@ -56,23 +56,22 @@ int main(int argc, char *argv[]) {
size_t readSize = fread(buffer, 1, fileSize, file);
fclose(file);
if(readSize < fileSize) {
free(buffer);
printf("Failed to read all data from CSV\n");
free(buffer);
return 1;
}
buffer[fileSize] = '\0';
csvParse(buffer, &csv);
free(buffer);
printf("Parsed\n");
// Prepare output file for writing.
sprintf(path, "%s.language", out);
fileMkdirp(path);
file = fopen(path, "wb");
if(file == NULL) {
csvDispose(&csv);
printf("Failed to create output language file\n");
csvDispose(&csv);
return 1;
}
@ -85,14 +84,13 @@ int main(int argc, char *argv[]) {
csvDispose(&csv);
return 1;
}
char *key = csvGetCell(&csv, y, 0);
char *value = csvGetCell(&csv, y, 1);
// 23/01/14 - Replace \r in CSV.
stringRemoveAll(key, '\r');
stringRemoveAll(value, '\r');
if(strlen(key) <= 0 || strlen(value) <= 0) {
printf("Failed to parse language. Line %i has an invalid string\n", y);
fclose(file);

View File

@ -8,7 +8,7 @@
#include "csv.h"
char * csvGetCell(csv_t *csv, int32_t row, int32_t cell) {
return csv->rows[(row * CSV_ROW_COUNT_MAX) + cell];
return csv->rows[(row * CSV_COLUMN_COUNT_MAX) + cell];
}
void csvParse(char *string, csv_t *csv) {
@ -18,9 +18,9 @@ void csvParse(char *string, csv_t *csv) {
int32_t rowCellCount;
length = strlen(string);
csv->buffer = malloc(sizeof(char) * length * 2);
csv->buffer = malloc(sizeof(char) * (length+1) * CSV_COLUMN_COUNT_MAX * CSV_ROW_COUNT_MAX);
csv->cellCounts = malloc(sizeof(int32_t) * CSV_ROW_COUNT_MAX);
csv->rows = malloc(sizeof(char *) * 32 * CSV_ROW_COUNT_MAX);
csv->rows = malloc(sizeof(char*) * CSV_ROW_COUNT_MAX * CSV_COLUMN_COUNT_MAX);
i = 0;
j = 0;
@ -35,7 +35,7 @@ void csvParse(char *string, csv_t *csv) {
case CSV_PARSE_STATE_FIND_CELL:
if(c == '"') {
state = CSV_PARSE_STATE_PARSE_CELL_WITH_QUOTES;
csv->rows[(csv->rowCount * CSV_ROW_COUNT_MAX) + rowCellCount] = csv->buffer + j;
csv->rows[(csv->rowCount * CSV_COLUMN_COUNT_MAX) + rowCellCount] = csv->buffer + j;
rowCellCount++;
continue;
} else if(c == '\r' || c == '\n') {
@ -43,13 +43,13 @@ void csvParse(char *string, csv_t *csv) {
state = CSV_PARSE_STATE_LINE_END;
continue;
} else if(c == ',') {
csv->rows[(csv->rowCount * CSV_ROW_COUNT_MAX) + rowCellCount] = csv->buffer + j;
csv->rows[(csv->rowCount * CSV_COLUMN_COUNT_MAX) + rowCellCount] = csv->buffer + j;
csv->buffer[j++] = '\0';
rowCellCount++;
continue;
} else {
state = CSV_PARSE_STATE_PARSE_CELL;
csv->rows[(csv->rowCount * CSV_ROW_COUNT_MAX) + rowCellCount] = csv->buffer + j;
csv->rows[(csv->rowCount * CSV_COLUMN_COUNT_MAX) + rowCellCount] = csv->buffer + j;
csv->buffer[j++] = c;
rowCellCount++;
continue;
@ -104,6 +104,7 @@ void csvParse(char *string, csv_t *csv) {
free(NULL);
}
}
csv->buffer[j++] = '\0';
if(rowCellCount != 0) {

View File

@ -10,6 +10,7 @@
#include "string.h"
#define CSV_ROW_COUNT_MAX 128
#define CSV_COLUMN_COUNT_MAX 16
typedef enum {
CSV_PARSE_STATE_FIND_CELL,//0