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

y2j.c (1245B)


      1 //
      2 
      3 #include "../release/libsheepyObject.h"
      4 
      5 #define internal static
      6 
      7 #include <stdlib.h>
      8 #include <stdio.h>
      9 
     10 #ifndef unitTest
     11 #endif
     12 int MAIN(int ARGC, char** ARGV);
     13 
     14 int argc; char **argv;
     15 
     16 #ifndef unitTest
     17 // Remove main when running the unit tests
     18 #define MAIN   main
     19 #endif
     20 int MAIN(int ARGC, char** ARGV) {
     21   int dummy UNUSED;
     22 
     23   argc = ARGC; argv = ARGV;;// load yml in command parameter and convert to yml
     24 
     25   // Steps
     26   // create yml object
     27   // load text to file
     28   // parse
     29   // write in yml format
     30 
     31   if (argc < 2) {
     32     puts(BLD RED "Usage: " RST "y2j <file.yml>");
     33     XFAILURE
     34   }
     35 
     36   char *iFName = argv[1];
     37   char *oFName = dupG(iFName);
     38   char *p      = findG(oFName, ".");
     39   if (p) {
     40     // remove extension
     41     *p = 0;
     42   }
     43   pErrorNULL(appendG(&oFName, ".json"));
     44 
     45   // create yml object
     46   createAllocateSmallJson(json);
     47 
     48   // parse yml
     49   // load text to file
     50   createAllocateSmallString(file);
     51   readFileG(file, iFName);
     52 
     53   // parse
     54   parseYMLG(json, ssGet(file));
     55 
     56   // write in json format
     57   if (fileExistsG(oFName)) {
     58     puts(BLD RED "Overwriting: " RST);
     59     puts(oFName);
     60   }
     61   char *jsons = stringifyG(json,2); {
     62   writeFileG(jsons, oFName);
     63 
     64   free(jsons);
     65   free(oFName);
     66   terminateG(json);
     67   terminateG(file);
     68 }
     69   }