DPF

DISTRHO Plugin Framework
Log | Files | Refs | Submodules | README | LICENSE

state2json.cpp (4109B)


      1 #include "extra/Base64.hpp"
      2 #include "extra/String.hpp"
      3 
      4 int main(int argc, char* argv[])
      5 {
      6     if (argc != 2)
      7     {
      8         d_stdout("usage: %s [base64-dpf-state]", argv[0]);
      9         return 1;
     10     }
     11 
     12     const std::vector<uint8_t> data(d_getChunkFromBase64String(argv[1]));
     13 
     14     if (data.empty())
     15     {
     16         printf("{}\n");
     17         return 0;
     18     }
     19 
     20     String key, value;
     21     bool firstValue = true;
     22     bool hasValue = false;
     23     bool fillingKey = true; // if filling key or value
     24     char queryingType = 'i'; // can be 'n', 's' or 'p' (none, states, parameters)
     25 
     26     const char* const buffer = reinterpret_cast<const char*>(data.data());
     27 
     28     printf("{");
     29 
     30     for (size_t i = 0; i < data.size(); ++i)
     31     {
     32         // found terminator, stop here
     33         if (buffer[i] == '\xfe')
     34         {
     35             break;
     36         }
     37 
     38         // append to temporary vars
     39         if (fillingKey)
     40         {
     41             key += buffer + i;
     42         }
     43         else
     44         {
     45             value += buffer + i;
     46             hasValue = true;
     47         }
     48 
     49         // increase buffer offset by length of string
     50         i += std::strlen(buffer + i);
     51 
     52         // if buffer offset points to null, we found the end of a string, lets check
     53         if (buffer[i] == '\0')
     54         {
     55             // special keys
     56             if (key == "__dpf_state_begin__")
     57             {
     58                 DISTRHO_SAFE_ASSERT_INT_RETURN(queryingType == 'i' || queryingType == 'n', queryingType, 1);
     59                 if (queryingType == 'n')
     60                     printf(",");
     61                 printf("\n  \"states\": {");
     62                 queryingType = 's';
     63                 key.clear();
     64                 value.clear();
     65                 firstValue = true;
     66                 hasValue = false;
     67                 continue;
     68             }
     69             if (key == "__dpf_state_end__")
     70             {
     71                 DISTRHO_SAFE_ASSERT_INT_RETURN(queryingType == 's', queryingType, 1);
     72                 printf("\n  }");
     73                 queryingType = 'n';
     74                 key.clear();
     75                 value.clear();
     76                 hasValue = false;
     77                 continue;
     78             }
     79             if (key == "__dpf_parameters_begin__")
     80             {
     81                 DISTRHO_SAFE_ASSERT_INT_RETURN(queryingType == 'i' || queryingType == 'n', queryingType, 1);
     82                 if (queryingType == 'n')
     83                     printf(",");
     84                 printf("\n  \"parameters\": {");
     85                 queryingType = 'p';
     86                 key.clear();
     87                 value.clear();
     88                 firstValue = true;
     89                 hasValue = false;
     90                 continue;
     91             }
     92             if (key == "__dpf_parameters_end__")
     93             {
     94                 DISTRHO_SAFE_ASSERT_INT_RETURN(queryingType == 'p', queryingType, 1);
     95                 printf("\n  }");
     96                 queryingType = 'x';
     97                 key.clear();
     98                 value.clear();
     99                 hasValue = false;
    100                 continue;
    101             }
    102 
    103             // no special key, swap between reading real key and value
    104             fillingKey = !fillingKey;
    105 
    106             // if there is no value yet keep reading until we have one
    107             if (! hasValue)
    108                 continue;
    109 
    110             if (key == "__dpf_program__")
    111             {
    112                 DISTRHO_SAFE_ASSERT_INT_RETURN(queryingType == 'i', queryingType, 1);
    113                 queryingType = 'n';
    114 
    115                 printf("\n  \"program\": %s", value.buffer());
    116             }
    117             else if (queryingType == 's')
    118             {
    119                 if (! firstValue)
    120                     printf(",");
    121                 // TODO safely encode value as json compatible string
    122                 printf("\n    \"%s\": %s", key.buffer(), value.buffer());
    123             }
    124             else if (queryingType == 'p')
    125             {
    126                 if (! firstValue)
    127                     printf(",");
    128                 printf("\n    \"%s\": %s", key.buffer(), value.buffer());
    129             }
    130 
    131             key.clear();
    132             value.clear();
    133             firstValue = hasValue = false;
    134         }
    135     }
    136 
    137     printf("\n}\n");
    138     return 0;
    139 }