/* 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; }
/* open csv reader from file */ iCsvReader *icsv_reader_open_file(const char *filename) { #ifndef IDISABLE_FILE_SYSTEM_ACCESS iCsvReader *reader; FILE *fp; fp = fopen(filename, "rb"); if (fp == NULL) return NULL; reader = (iCsvReader*)ikmem_malloc(sizeof(iCsvReader)); if (reader == NULL) { fclose(fp); return NULL; } it_init(&reader->string, ITYPE_STR); reader->fp = fp; reader->source = NULL; reader->strings = NULL; reader->line = 0; reader->count = 0; return reader; #else return NULL; #endif }
int main(){ tree_t * tree = init_tree(100); iterator_t* it = it_init(NULL); it = it_init(tree); it_left(it); it_right(it); it_current(NULL); it_up(NULL); it_right(NULL); it_left(NULL); insert_item(NULL, 0, it, RIGHT); insert_item(tree, 200, it, 10); insert_item(tree, 30, it, LEFT); insert_item(tree, 40, it, RIGHT); insert_item(tree, 20, it, LEFT); it_right(it); it_left(it); insert_item(tree, 100500, it, RIGHT); insert_item(tree, 10, it, RIGHT); insert_item(tree, 25, it, LEFT); it_right(it); dump_tree(NULL); dump_tree(tree); foreach_t* iter = foreach_init(NULL); for (iter = foreach_init(tree); !foreach_isEnd(iter); foreach_next(iter)){ //printf("%i/%i:%i\n",iter->i, iter->nnodes, foreach_current(iter)); printf("%i\n",foreach_current(iter)); } printf("\n"); foreach_delete(iter); delete_item(tree, NULL); printf("current = %i\n", it_current(it)); it_up(it); it_left(it); printf("current = %i\n", it_current(it)); dump_tree(tree); delete_item(tree, it); printf("current = %i\n", it_current(it)); dump_tree(tree); it_right(it); delete_item(tree, it); dump_tree(tree); for (iter = foreach_init(tree); !foreach_isEnd(iter); foreach_next(iter)){ //printf("%i/%i:%i\n",iter->i, iter->nnodes, foreach_current(iter)); printf("%i\n",foreach_current(iter)); } printf("\n"); delete_tree(NULL); delete_tree(tree); it_delete(it); it_delete(NULL); foreach_delete(NULL); foreach_delete(iter); return 0; }