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

demo.c (2096B)


      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 #ifndef unitTest
     12 #endif
     13 int MAIN(int ARGC, char** ARGV);
     14 
     15 int argc; char **argv;
     16 
     17 #ifndef unitTest
     18 // Remove main when running the unit tests
     19 #define MAIN   main
     20 #endif
     21 int MAIN(int ARGC, char** ARGV) {
     22   char **list = NULL;
     23 
     24   argc = ARGC; argv = ARGV;
     25   printf("Steps");;
     26   printf("\n");
     27   printf("- execOut \"ls\"");
     28   printf("\n");
     29   printf("- push \"libsheepy\" at the end of list");
     30   printf("\n");
     31   printf("- push CLI argument");
     32   printf("\n");
     33   printf("- print list");
     34   printf("\n");
     35   printf("- ForEach print lines containing the words two or libsheepy");
     36   printf("\n");
     37   printf("- create new list");
     38   printf("\n");
     39   printf("- Enumerate list");
     40   printf("\n");
     41   printf("");
     42   printf("\n");
     43 
     44   // run ls and store result lines in list
     45   list = execOut("ls -al ../src/dirTest.null/");
     46   // failure when list is NULL
     47   exitFailure(list);
     48 
     49   // append "libsheepy" at the end of the list
     50   pErrorNULL(listPushS(&list, "  libsheepy  "));
     51   exitFailure(list);
     52 
     53   // if there is an argument, add it to the list
     54   if (argc > 1) {
     55     pErrorNULL(listPushS(&list, argv[1]));
     56     exitFailure(list);
     57   }
     58 
     59 
     60   // print list elements
     61   pError0(listPrintS(list));
     62 
     63   printf("\nPrint lines with string \"two\" or \"libsheepy\":\n");
     64   printf("\n");
     65 
     66   // loop on list elements (lines from ls and "libsheepy")
     67   forEachCharP(list, element) {
     68     // split line with space
     69     char **splitElement = split(*element, " ");;
     70     exitFailure(list);
     71 
     72     char *lastStringInSplit = listGetS(splitElement, -1);
     73     exitFailure(lastStringInSplit);
     74 
     75     if (strEq(lastStringInSplit, "two") || strEq(lastStringInSplit, "libsheepy")) {
     76       printf("%s\n", trimS(*element));
     77     }
     78     listFreeS(splitElement);
     79   }
     80 
     81   listFreeS(list);
     82 
     83   put
     84 
     85   list = listCreateS("one", "two", "three");
     86   enumerateCharP(list, e, index) {
     87     printf("%lu - %s", index+1, *e);
     88     printf("\n");
     89   }
     90 
     91   printf("There are %zu elements in the list.", index);
     92   printf("\n");
     93 }