Loading...
Searching...
No Matches
csv.c
Go to the documentation of this file.
#include "../../test.h"
#include "arc/std/bool.h"
#include "arc/std/errno.h"
#include "arc/std/parser/csv.h"
#include <stdlib.h>
void TEST_ParserCSV_CastTypeFn(void **data, ARC_String *string){
int32_t *int32Data = (int32_t *)malloc(sizeof(int32_t));
*int32Data = (int32_t)ARC_String_ToInt64_t(string);
*data = (void *)int32Data;
}
void TEST_ParserCSV_DestroyTypeFn(void *data){
free((int32_t *)data);
}
ARC_TEST(Parser_ParserCSV_BasicTest){
ARC_Parser *parser;
ARC_ParserCSV_CreateAsParser(&parser, ARC_False, TEST_ParserCSV_CastTypeFn, TEST_ParserCSV_DestroyTypeFn);
const char *tempCString =
"1,1,2,2\n"
"2,3,4,5\n"
"4,2,4,1\n"
"7,7,7,7\n";
ARC_String *tempString;
ARC_String_CreateWithStrlen(&tempString, (char *)tempCString);
//this destroys string, so no need for cleanup
ARC_Parser_Parse(parser, &tempString);
ARC_CHECK(arc_errno == 0);
ARC_ParserCSVData *data = (ARC_ParserCSVData *)ARC_Parser_GetData(parser);
for(uint32_t y = 0; y < data->height; y++){
for(uint32_t x = 0; x < data->width; x++){
printf("%d ", *(int32_t *)(data->data[y][x]));
}
printf("\n");
}
ARC_Parser_Destroy(parser);
}
ARC_TEST(Parser_ParserCSV_BasicHeaderTest){
ARC_Parser *parser;
ARC_ParserCSV_CreateAsParser(&parser, ARC_True, TEST_ParserCSV_CastTypeFn, TEST_ParserCSV_DestroyTypeFn);
const char *tempCString =
"a,b,c,d\n"
"4,3,2,1\n"
"7,3,2,1\n"
"4,2,4,1\n"
"7,7,7,7\n";
ARC_String *tempString;
ARC_String_CreateWithStrlen(&tempString, (char *)tempCString);
//this destroys string, so no need for cleanup
ARC_Parser_Parse(parser, &tempString);
ARC_CHECK(arc_errno == 0);
ARC_ParserCSVData *data = (ARC_ParserCSVData *)ARC_Parser_GetData(parser);
for(uint32_t x = 0; x < data->width; x++){
printf("%s ", data->headers[x]->data);
}
printf("\n");
for(uint32_t y = 0; y < data->height; y++){
for(uint32_t x = 0; x < data->width; x++){
printf("%d ", *(int32_t *)(data->data[y][x]));
}
printf("\n");
}
ARC_Parser_Destroy(parser);
}
bool.h
ARC_False
#define ARC_False
Definition bool.h:12
ARC_True
#define ARC_True
Definition bool.h:11
csv.h
errno.h
arc_errno
int32_t arc_errno
Definition errno.c:5
ARC_Parser_Parse
void ARC_Parser_Parse(ARC_Parser *parser, ARC_String **data)
Definition parser.c:297
ARC_Parser_Destroy
void ARC_Parser_Destroy(ARC_Parser *parser)
destroys an ARC_Parser type
Definition parser.c:127
ARC_Parser_GetData
void * ARC_Parser_GetData(ARC_Parser *parser)
Definition parser.c:368
ARC_ParserCSV_CreateAsParser
void ARC_ParserCSV_CreateAsParser(ARC_Parser **parser, ARC_Bool header, ARC_ParserCSV_CastTypeFn castTypeFn, ARC_ParserCSV_DestroyTypeFn destroyTypeFn)
creates a parser for the Parser Lang
Definition csv.c:302
ARC_String_ToInt64_t
int64_t ARC_String_ToInt64_t(ARC_String *string)
converst substring from string to int64_t
Definition string.c:222
ARC_String_CreateWithStrlen
void ARC_String_CreateWithStrlen(ARC_String **string, char *data)
creates ARC_String type with strinlen
Definition string.c:32
ARC_ParserCSVData
defines a csv data type, data is set by the callback passed in when createing a parserCSV as parser
Definition csv.h:44
ARC_ParserCSVData::height
uint32_t height
Definition csv.h:49
ARC_ParserCSVData::width
uint32_t width
Definition csv.h:48
ARC_ParserCSVData::data
void *** data
Definition csv.h:50
ARC_ParserCSVData::headers
ARC_String ** headers
Definition csv.h:46
ARC_Parser
Definition parser.c:12
ARC_String
substring position within a string
Definition string.h:14
ARC_String::data
char * data
Definition string.h:15
test.h
ARC_CHECK
#define ARC_CHECK(TEST)
Definition test.h:33
ARC_TEST
#define ARC_TEST(MESSAGE)
Definition test.h:31
TEST_ParserCSV_CastTypeFn
void TEST_ParserCSV_CastTypeFn(void **data, ARC_String *string)
Definition csv.c:7
TEST_ParserCSV_DestroyTypeFn
void TEST_ParserCSV_DestroyTypeFn(void *data)
Definition csv.c:14