This code sample will show how to handle device configurations from a JSON file.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include "openicc_config.h"
#include "openicc_config_internal.h"
int main(int argc, char ** argv)
{
const char * file_name = argc > 1 ? argv[1] : "../test.json";
char * text = 0;
int size = 0;
char ** keys = 0;
char ** values = 0;
int i,j, n = 0, devices_n, flags;
char * json, * device_class;
const char * devices_filter[] = {OPENICC_DEVICE_CAMERA,NULL},
* old_device_class = NULL,
* d = NULL;
int output = 0;
const char * file = NULL;
int help = 0;
int verbose = 0;
const char * export = NULL;
const char * loc = setlocale(LC_ALL,"");
oyjlOptionChoice_s i_choices[] = {{"openicc.json", _("openicc.json"), _("openicc.json"), ""},
{"","","",""}};
oyjlOptionChoice_s o_choices[] = {{"0", _("Print All"), _("Print All"), ""},
{"1", _("Print Camera"), _("Print Camera JSON"), ""},
{"2", _("Print None"), _("Print None"), ""},
{"","","",""}};
oyjlOption_s oarray[] = {
{
"oiwi", 0,
"i",
"input", NULL, _(
"input"), _(
"Set Input"), NULL, _(
"FILENAME"), oyjlOPTIONTYPE_CHOICE, {.choices.list =
openiccMemDup( i_choices,
sizeof(i_choices) )}, oyjlSTRING, {.s = &file} },
{
"oiwi", 0,
"o",
"output", NULL, _(
"output"), _(
"Control Output"), NULL,
"0|1|2", oyjlOPTIONTYPE_CHOICE, {.choices.list =
openiccMemDup( o_choices,
sizeof(o_choices) )}, oyjlINT, {.i = &output} },
{"oiwi", 0, "h", "help", NULL, _("help"), _("Help"), NULL, NULL, oyjlOPTIONTYPE_NONE, {}, oyjlINT, {.i = &help} },
{"oiwi", 0, "v", "verbose", NULL, _("verbose"), _("verbose"), NULL, NULL, oyjlOPTIONTYPE_NONE, {}, oyjlINT, {.i = &verbose} },
{"oiwi", 0, "X", "export", NULL, NULL, NULL, NULL, NULL, oyjlOPTIONTYPE_CHOICE, {}, oyjlSTRING,{.s=&export} },
{"",0,0,0,0,0,0,0, NULL, oyjlOPTIONTYPE_END, {},0,{}}
};
oyjlOptionGroup_s groups[] = {
{"oiwg", 0, _("Mode"), _("Actual mode"), NULL, "i", "o,v", "i,o" },
{"oiwg", 0, _("Misc"), _("General options"), NULL, "", "", "X,v,h" },
{"",0,0,0,0,0,0,0}
};
oyjlUiHeaderSection_s sections[] = {
{"oihs", "version", NULL, "1.0", NULL},
{"oihs", "documentation", NULL, "", _("The example tool demonstrates the usage of the libOpenIcc config and options API's.")},
{"",0,0,0,0}};
oyjlUi_s * ui = oyjlUi_Create( argc, (const char**) argv,
"openicc-config-read", "OpenICC Config Read", _("Short example tool using libOpenIcc"), "openicc-logo",
sections, oarray, groups, NULL );
if(!ui) return 0;
if(file) file_name = file;
text = openiccReadFile( file_name, &size );
if(!text)
{
oyjlOptions_PrintHelp( ui->opts, ui, verbose, "%s example tool", argv[0] );
return 0;
}
fprintf(stderr, "Found %d devices.\n", devices_n );
if(output == 0)
for(i = 0; i < devices_n; ++i)
{
&keys, &values, malloc,free );
if(i)
fprintf( stderr,"\n");
n = 0; if(keys) while(keys[n]) ++n;
fprintf( stderr, "[%d] device class:\"%s\" with %d keys/values pairs\n", i, d, n);
for( j = 0; j < n; ++j )
{
fprintf(stderr, "%s:\"%s\"\n", keys[j], values[j]);
free(keys[j]);
free(values[j]);
}
free(keys); free(values);
}
i = 1;
old_device_class, &json, malloc,free );
fprintf( stderr, "\ndevice class[%d]: \"%s\"\n", i, device_class);
if(output == 0)
printf( "%s\n", json );
free(json);
fprintf(stderr, "Found %d %s devices.\n", devices_n, devices_filter[0] );
old_device_class = NULL;
for(i = 0; i < devices_n; ++i)
{
flags = 0;
if(i != 0)
flags |= OPENICC_CONFIGS_SKIP_HEADER;
if(i != devices_n - 1)
flags |= OPENICC_CONFIGS_SKIP_FOOTER;
old_device_class, &json, malloc,free );
old_device_class = d;
if(output <= 1)
printf( "%s\n", json );
free(json);
}
return 0;
}