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"
int main(int argc, char ** argv)
{
const char * file_name = argc > 1 ? argv[1] : "../test.json";
FILE * fp = NULL;
char * text = 0;
size_t 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;
setlocale(LC_ALL,"");
fp = fopen(file_name,"rb");
if(fp)
{
fseek(fp,0L,SEEK_END);
size = ftell (fp);
rewind(fp);
if(size)
{
text = malloc(size+1);
if(text)
fread(text, sizeof(char), size, fp);
text[size] = '\000';
}
} else
{
fprintf( stderr, "Usage: %s openicc.json\n\n", argv[0] );
return 0;
}
fprintf(stderr, "Found %d devices.\n", devices_n );
for(i = 0; i < devices_n; ++i)
{
&keys, &values, malloc );
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 );
fprintf( stderr, "\ndevice class[%d]: \"%s\"\n", i, device_class);
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 );
old_device_class = d;
printf( "%s\n", json );
free(json);
}
return 0;
}