libsheepy

C lib for handling text files, strings and json like data structure with an object oriented system
git clone https://spartatek.se/git/libsheepy.git
Log | Files | Refs | README | LICENSE

csvStatic.c (1672B)


      1 //
      2 
      3 #include "../release/libsheepyObject.h"
      4 
      5 #define internal static
      6 
      7 #include <stdlib.h>
      8 #include <stdio.h>
      9 
     10 char *get(smallArrayt *csv, size_t line, size_t col);
     11 int MAIN(int ARGC, char** ARGV);
     12 
     13 int argc; char **argv;
     14 
     15 char *get(smallArrayt *csv, size_t line, size_t col) {
     16 
     17   smallArrayt *lin = getG(csv, lin, line);
     18   char *s = getG(lin, s, col);
     19   free(lin);
     20   return(s);
     21 }
     22 
     23 #ifndef unitTest
     24 // Remove main when running the unit tests
     25 #define MAIN   main
     26 #endif
     27 int MAIN(int ARGC, char** ARGV) {
     28   int dummy;
     29 
     30   argc = ARGC; argv = ARGV;;// read file.csv and display values and replace comma with semicolon
     31 
     32   // Steps
     33   // load file.csv lines in list
     34   // split lines and store in csv list
     35   // print values seperated with semicolon
     36   // display some values
     37   // free csv list
     38 
     39   // load file.csv lines in list
     40   createAllocateSmallArray(csv);
     41   createAllocateSmallArray(list);
     42   readFileG(list, "file.csv");
     43 
     44   // split lines and store in csv list
     45   iter(list, L) {
     46     castS(l,L);
     47     smallArrayt *spl = splitG(l, ',');
     48     // push and free spl baset object, the sArray in spl is stored in the sArray in csv
     49     pushNFreeG(csv, spl);
     50   }
     51   terminateO(list);
     52 
     53   // print values seperated with semicolon
     54   forEachSmallArray(csv, L2) {
     55     cast(smallArrayt *, l2, L2);
     56     char *s = joinSG(l2, ";");
     57     printf("%s\n", s);
     58     // joinS allocated s, free s now
     59     free(s);
     60     // always free the loop element
     61     free(l2);
     62   }
     63 
     64   // display some values
     65   put
     66   printf("%s\n", get(csv, 0,0));
     67   printf("%s\n", get(csv, 0,1));
     68   put
     69   printf("%s\n", get(csv, -1,-2));
     70   printf("%s\n", get(csv, -1,-1));
     71 
     72   // free csv list
     73   terminateO(csv);
     74 
     75   finalizeLibsheepy();
     76 }