csv.c (1567B)
1 //: generator/pp comment config 2 //:not cScript 3 #include "../release/libsheepy.h" 4 //:end 5 6 #define internal static 7 8 #include <stdlib.h> 9 #include <stdio.h> 10 11 char *get(void **csv, size_t line, size_t col); 12 char *get2(void **csv, intmax_t line, intmax_t col); 13 #ifndef unitTest 14 #endif 15 int MAIN(int ARGC, char** ARGV); 16 17 int argc; char **argv; 18 19 char *get(void **csv, size_t line, size_t col) { 20 21 return(((char **)csv[line])[col]); 22 } 23 24 char *get2(void **csv, intmax_t line, intmax_t col) { 25 26 return(iListGetS(listGet(csv, line), col)); 27 } 28 29 #ifndef unitTest 30 // Remove main when running the unit tests 31 #define MAIN main 32 #endif 33 int MAIN(int ARGC, char** ARGV) { 34 char **list = NULL; 35 void **csv = NULL; 36 37 argc = ARGC; argv = ARGV;;// read file.csv and display values and replace comma with semicolon 38 39 // Steps 40 // load file.csv lines in list 41 // split lines and store in csv list 42 // print values seperated with semicolon 43 // display some values 44 // free csv list 45 46 // load file.csv lines in list 47 list = readText("file.csv"); 48 49 // split lines and store in csv list 50 forEachCharP(list, l) { 51 char **spl = split(*l, ","); 52 pErrorNULL(listPush(&csv, spl)); 53 } 54 listFreeS(list); 55 56 // print values seperated with semicolon 57 forEachType(void, csv, l) { 58 printf("%s\n", join((*l), ";")); 59 } 60 61 // display some values 62 put 63 printf("%s\n", get(csv, 0,0)); 64 printf("%s\n", get(csv, 0,1)); 65 put 66 printf("%s\n", get2(csv, -1,-2)); 67 printf("%s\n", get2(csv, -1,-1)); 68 69 // free csv list 70 forEachType(void, csv, l) { 71 listFreeS(*l); 72 } 73 free(csv); 74 }