Skip to content

Instantly share code, notes, and snippets.

@toravir
Created March 5, 2019 19:14
Show Gist options
  • Select an option

  • Save toravir/a23cfc2acece37d262a4b614e09c7c8d to your computer and use it in GitHub Desktop.

Select an option

Save toravir/a23cfc2acece37d262a4b614e09c7c8d to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "yajl/yajl_tree.h"
#define CAPI_TRACE_ERR(x...) printf(x)
#define CAPI_TRACE_DBG(x...) printf(x)
#define CSCO_HAI_STATUS_INIT_ERR -1
#define CSCO_HAI_STATUS_SUCCESS 0
//#define CAPI_IS_INTEGER(v) YAJL_IS_INTEGER(v)
//#define CAPI_GET_INTEGER(v) YAJL_GET_INTEGER(v)
#define CAPI_IS_INTEGER(v) YAJL_IS_DOUBLE(v)
#define CAPI_GET_INTEGER(v) YAJL_GET_DOUBLE(v)
#define MAX_NUM_INSTANCES 4
static char errbuf[1024];
int
csco_hai_parse_cfg (const char *cfg)
{
int i;
yajl_val ynode, inst_nodes, id_node, api_port_node, api_addr_node;
const char *inst_path[] = { "instances", (const char *) 0 };
const char *id_path[] = { "id", (const char *) 0 };
const char *api_port_path[] = { "communications", "ports",
"api", (const char *) 0 };
const char *api_addr_path[] = { "communications", "addr",
(const char *) 0 };
ynode = yajl_tree_parse((const char *)cfg, errbuf, sizeof(errbuf));
if (ynode == NULL) {
CAPI_TRACE_ERR("Config parse error, err : %s\n", errbuf);
return CSCO_HAI_STATUS_INIT_ERR;
}
inst_nodes = yajl_tree_get(ynode, inst_path, yajl_t_array);
if (inst_nodes && YAJL_IS_ARRAY(inst_nodes)) {
CAPI_TRACE_DBG("num instances in cfg = %d\n", (int )inst_nodes->u.array.len);
if (inst_nodes->u.array.len > MAX_NUM_INSTANCES) {
CAPI_TRACE_ERR("Cfg parse error, Max NUM instances exceeded num instances in cfg = %d max num instances = %d\n", (
int )inst_nodes->u.array.len, MAX_NUM_INSTANCES);
goto cleanup;
}
// process each instance
for (i = 0; i < inst_nodes->u.array.len; i++) {
// get the instance id
id_node = yajl_tree_get(inst_nodes->u.array.values[i],
id_path, yajl_t_number);
if (id_node && CAPI_IS_INTEGER(id_node)) {
CAPI_TRACE_DBG("Found instance %d\n",
CAPI_GET_INTEGER(id_node));
} else {
CAPI_TRACE_ERR("Cfg parse error, instance id not found\n");
goto cleanup;
}
// set the api host
api_addr_node = yajl_tree_get(inst_nodes->u.array.values[i],
api_addr_path, yajl_t_number);
if (api_addr_node && YAJL_IS_STRING(api_addr_node)) {
CAPI_TRACE_DBG("api addr = %s\n",
YAJL_GET_STRING(api_addr_node));
}
// get the api port
api_port_node = yajl_tree_get(inst_nodes->u.array.values[i],
api_port_path, yajl_t_number);
if (api_port_node && CAPI_IS_INTEGER(api_port_node)) {
CAPI_TRACE_DBG("api port = %d\n",
CAPI_GET_INTEGER(api_port_node));
} else {
CAPI_TRACE_ERR("Config parse error, api port not found\n");
goto cleanup;
}
}
}
return CSCO_HAI_STATUS_SUCCESS;
cleanup:
CAPI_TRACE_DBG("Config parsing failed\n");
return CSCO_HAI_STATUS_INIT_ERR;
}
int main () {
const char *str = " \
{ \
\"heart-beat-timeout\" : 90, \
\"debug\" : \
{ \
\"logs\" : \
{ \
\"destination\" : \"/logs/\" \
} \
}, \
\"instances\" : \
[ \
{ \
\"id\" : 0, \
\"communications\" : \
{ \
\"ports\" : \
{ \
\"api\" : 60000 \
} \
} \
}, \
{ \
\"id\" : 1, \
\"communications\" : \
{ \
\"ports\" : \
{ \
\"api\" : 61000 \
} \
} \
} \
] \
}";
printf("Str: %s", str);
csco_hai_parse_cfg(str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment