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

b2j.c (1143B)


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