/* open csv reader from memory */ iCsvReader *icsv_reader_open_memory(const char *text, ilong size) { iCsvReader *reader; reader = (iCsvReader*)ikmem_malloc(sizeof(iCsvReader)); if (reader == NULL) { return NULL; } it_init(&reader->string, ITYPE_STR); #ifndef IDISABLE_FILE_SYSTEM_ACCESS reader->fp = NULL; #endif reader->source = NULL; reader->strings = NULL; reader->line = 0; reader->count = 0; reader->source = istring_list_split(text, size, "\n", 1); if (reader->source == NULL) { ikmem_free(reader); return NULL; } return reader; }
// open csv writer from file: if filename is NULL, it will open in memory iCsvWriter *icsv_writer_open(const char *filename, int append) { iCsvWriter *writer; writer = (iCsvWriter*)ikmem_malloc(sizeof(iCsvWriter)); if (writer == NULL) return NULL; if (filename != NULL) { void *fp = NULL; #ifndef IDISABLE_FILE_SYSTEM_ACCESS writer->fp = fopen(filename, append? "a" : "w"); if (writer->fp && append) { fseek(writer->fp, 0, SEEK_END); } fp = writer->fp; #endif if (fp == NULL) { ikmem_free(writer); return NULL; } writer->mode = 1; } else { writer->mode = 2; #ifndef IDISABLE_FILE_SYSTEM_ACCESS writer->fp = NULL; #endif } writer->strings = istring_list_new(); if (writer->strings == NULL) { #ifndef IDISABLE_FILE_SYSTEM_ACCESS if (writer->fp) { fclose(writer->fp); } #endif ikmem_free(writer); return NULL; } it_init(&writer->string, ITYPE_STR); it_init(&writer->output, ITYPE_STR); return writer; }
// close csv writer void icsv_writer_close(iCsvWriter *writer) { if (writer) { if (writer->strings) { istring_list_delete(writer->strings); writer->strings = NULL; } #ifndef IDISABLE_FILE_SYSTEM_ACCESS if (writer->fp) { fclose(writer->fp); writer->fp = NULL; } #endif writer->mode = 0; it_destroy(&writer->string); it_destroy(&writer->output); ikmem_free(writer); } }
void icsv_reader_close(iCsvReader *reader) { if (reader) { if (reader->strings) { istring_list_delete(reader->strings); reader->strings = NULL; } if (reader->source) { istring_list_delete(reader->source); reader->source = NULL; } #ifndef IDISABLE_FILE_SYSTEM_ACCESS if (reader->fp) { fclose(reader->fp); reader->fp = NULL; } #endif reader->line = 0; reader->count = 0; it_destroy(&reader->string); ikmem_free(reader); } }
void xfree(void *ptr) { if (kmem_turnon) ikmem_free(ptr); else free(ptr); }