fixed csv, though there is a small memory leak

This commit is contained in:
herbglitch 2024-12-09 02:33:12 -07:00
parent 280a70c6e8
commit 2ef7a93d5a
4 changed files with 103 additions and 34 deletions

View file

@ -22,6 +22,10 @@ void ARC_ParserCSV_InitLexerRulesFn(ARC_Lexer *lexer){
}
uint32_t ARC_ParserCSV_GetStringIdFn(ARC_String *string){
if(ARC_String_EqualsCStringWithStrlen(string, "LAMBDA")){
return ARC_PARSER_TAG_LAMBDA;
}
if(ARC_String_EqualsCStringWithStrlen(string, "COMMA")){
return ARC_PARSER_CSV_CHAR_COMMA;
}
@ -116,7 +120,9 @@ void ARC_ParserCSVData_RunLineTag(ARC_Vector *dataVector, ARC_ParserTagToken *ta
ARC_ParserCSVData_RunLineTag(dataVector, childTagToken, userData);
continue;
//get the row data
case ARC_PARSER_CSV_DATA:
ARC_ParserCSVData_GetDataTag(dataVector, childTagToken, userData);
continue;
//add a new row for each new line
@ -133,8 +139,10 @@ void ARC_ParserCSVData_RunLineTag(ARC_Vector *dataVector, ARC_ParserTagToken *ta
}
void ARC_ParserCSVData_CreateFn(void **data, ARC_ParserTagToken *parsedData, void *userData){
*data = NULL;
if(data == NULL || userData == NULL){
//TODO: error here?
*data = NULL;
return;
}
@ -161,37 +169,92 @@ void ARC_ParserCSVData_CreateFn(void **data, ARC_ParserTagToken *parsedData, voi
return;
}
//get the first row of dataVector for its width
dataRowVector = (ARC_Vector *)ARC_Vector_Get(dataVector, 0);
//check that all the rows are the same size
for(uint32_t rowIndex = 1; rowIndex < ARC_Vector_GetSize(dataVector); rowIndex++){
ARC_Vector *currentRowVector = (ARC_Vector *)ARC_Vector_Get(dataVector, rowIndex);
//TODO: probs want to error
//cleanup and exit if they don't match
if(ARC_Vector_GetSize(dataRowVector) != ARC_Vector_GetSize(currentRowVector)){
//TODO: iterate and clear the vector
ARC_Vector_Destroy(dataVector);
return;
}
}
uint32_t dataStartIndex = 0;
if(csvUserData->header == ARC_True){
//TODO: headers
dataStartIndex++;
}
//init the height and width of all found rows, height starts at 1 as the first row is already found
uint32_t dataHeight = 1;
uint32_t dataWidth = 0;
//TODO: fix this for headers
//get the first non-empty row of dataVector for its width
for(; dataStartIndex < ARC_Vector_GetSize(dataVector); dataStartIndex++){
dataRowVector = (ARC_Vector *)ARC_Vector_Get(dataVector, dataStartIndex);
dataWidth = ARC_Vector_GetSize(dataRowVector);
//breakout if a valid row is found
if(dataWidth != 0){
break;
}
}
//fix this for headers
//check if a valid row if found
if(dataWidth == 0){
//TODO: iterate and clear the vector
//TODO: error here?
*data = NULL;
ARC_Vector_Destroy(dataVector);
return;
}
//check that all the rows are the same size
for(uint32_t rowIndex = dataStartIndex + 1; rowIndex < ARC_Vector_GetSize(dataVector); rowIndex++){
ARC_Vector *currentRowVector = (ARC_Vector *)ARC_Vector_Get(dataVector, rowIndex);
//skip an empty line
uint32_t currentRowVectorSize = ARC_Vector_GetSize(currentRowVector);
if(currentRowVectorSize == 0){
continue;
}
//a row was found so update the height
dataHeight++;
//TODO: probs want to error
//cleanup and exit if they don't match
if(dataWidth != currentRowVectorSize){
//TODO: iterate and clear the vector
ARC_Vector_Destroy(dataVector);
return;
}
}
//create the data that will be saved
ARC_ParserCSVData *csvData = (ARC_ParserCSVData *)malloc(sizeof(ARC_ParserCSVData));
csvData->height = ARC_Vector_GetSize(dataVector) - dataStartIndex;
csvData->width = ARC_Vector_GetSize(dataRowVector);
csvData->hasHeader = csvUserData->header;
//TODO: fix this
csvData->headers = NULL;
csvData->height = dataHeight;
csvData->width = dataWidth;
if(csvData->height == 0 || csvData->width == 0){
//TODO: error here?
free(csvData);
*data = NULL;
return;
}
//init location to copy data to
csvData->data = (void ***)malloc(sizeof(void **) * csvData->height);
//copy the data
for(uint32_t y = 0; y < csvData->height; y++){
uint32_t rowIndex = 0;
for(uint32_t y = 0; y < csvData->height; y++, rowIndex++){
ARC_Vector *currentRowVector = (ARC_Vector *)ARC_Vector_Get(dataVector, rowIndex + dataStartIndex);
//skip an empty line
uint32_t currentRowVectorSize = ARC_Vector_GetSize(currentRowVector);
if(currentRowVectorSize == 0){
y--;
continue;
}
//create the current row
csvData->data[y] = (void **)malloc(sizeof(void *) * csvData->width);
ARC_Vector *currentRowVector = (ARC_Vector *)ARC_Vector_Get(dataVector, y + dataStartIndex);
for(uint32_t x = 0; x < csvData->width; x++){
csvData->data[y][x] = ARC_Vector_Get(currentRowVector, x);
@ -204,24 +267,28 @@ void ARC_ParserCSVData_CreateFn(void **data, ARC_ParserTagToken *parsedData, voi
}
void ARC_ParserCSVData_DestroyFn(void *data, ARC_Bool clear, void *userData){
if(userData == NULL){ return; }
if(userData == NULL){
return;
}
ARC_ParserCSVUserData *csvUserData = (ARC_ParserCSVUserData *)userData;
if(data != NULL){
ARC_ParserCSVData *csvData = (ARC_ParserCSVData *)data;
//cleanup the headers
for(uint32_t x = 0; x < csvData->width; x++){
ARC_String *string = csvData->headers[x];
ARC_String_Destroy(string);
//cleanup the headers if they exist
if(csvData->hasHeader == ARC_True){
for(uint32_t x = 0; x < csvData->width; x++){
ARC_String *string = csvData->headers[x];
ARC_String_Destroy(string);
}
free(csvData->headers);
}
free(csvData->headers);
for(uint32_t y = 0; y < csvData->height; y++){
//cleanup each element in the data
for(uint32_t x = 0; x < csvData->width; x++){
csvUserData->destroyTypeFn(csvData->data[y] + x);
csvUserData->destroyTypeFn(csvData->data[y][x]);
}
//cleanup each row of data