basic/json_parser/parse_all_examples.c
See examples/basic/json-parser
/*
* ZentriOS SDK LICENSE AGREEMENT | Zentri.com, 2015.
*
* Use of source code and/or libraries contained in the ZentriOS SDK is
* subject to the Zentri Operating System SDK license agreement and
* applicable open source license agreements.
*
*/
#include "zos.h"
/*************************************************************************************************
* Parse all the files in the 'json/' folder and display their contents
*/
zos_result_t parse_all_examples(void)
{
zos_result_t result;
zos_file_t *file_list;
{
};
{
}
else
{
{
if(ZOS_FAILED(result, parse_file(file->name)))
{
break;
}
}
}
zn_file_destroy_list(file_list);
ZOS_LOG("Finished");
return result;
}
/*************************************************************************************************/
{
zos_result_t result;
json_parse_context_t *context = NULL;
char buffer[128];
uint32_t handle = -1;
{
.buffer = buffer,
.buffer_len = sizeof(buffer),
.reader = file_reader
};
ZOS_LOG("\r\nParsing: %s", filename);
{
ZOS_LOG("Failed to open: %s", filename);
}
{
ZOS_LOG("Failed to initialize json parsing context");
}
{
ZOS_LOG("Failed to parse json file");
}
else
{
int tok_stack[10];
int level = 0;
memset(tok_stack, 0, sizeof(tok_stack));
ZOS_LOG("JSON file contents:");
for(const json_tok_t *tok = json_context_get_token(context, NULL, NULL); tok != NULL; tok = tok->next)
{
level = print_token(tok, level, tok_stack);
}
}
zn_file_close(handle);
json_parse_context_deinit(context);
return result;
}
/*************************************************************************************************/
{
char print_buffer[128];
char *print_ptr = print_buffer;
if(level > 0)
{
if(tok_stack[level-1] >= 0)
{
if(tok_stack[level-1] == 0)
{
--level;
}
for(int l = level; l > 0; --l)
{
*print_ptr++ = ' ';
*print_ptr++ = ' ';
*print_ptr++ = ' ';
}
--tok_stack[level-1];
}
}
{
break;
break;
break;
break;
break;
{
char str[16];
} break;
break;
sprintf(print_ptr, "NULL element");
break;
default:
break;
}
ZOS_LOG("%s", print_buffer);
return level;
}
/*************************************************************************************************
* The parser will call this until it returns an error or
* the 'bytes_read' parameter is return with a 0 value.
*/
{
uint32_t file_handle = (uint32_t)user;
uint32_t has_more_data;
zn_file_poll(file_handle, &has_more_data);
if(has_more_data == 0)
{
// no more data so set this to 0 to tell the parser to stop calling this reader
*bytes_read = 0;
}
{
}
return result;
}