blob: a968ba661b33966ce502f153d9dc99679d248ced [file] [log] [blame]
anjana_sreekumar@infosys.com991c2062020-01-08 11:42:57 +05301/*
2 * Copyright (c) 2003-2018, Great Software Laboratory Pvt. Ltd.
3 * Copyright (c) 2017 Intel Corporation
4 * Copyright (c) 2019, Infosys Ltd.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include <stdio.h>
20#include <string.h>
21#include <stdlib.h>
22#include <netinet/in.h>
23#include <arpa/inet.h>
24
25#include "log.h"
26
27FILE *json_fp;
28
29static char *
30seek_to_tag(char *name)
31{
32 char *tmp = name;
33 char tag[64] = {0};
34 char last_iteration = 0;
35
36 rewind(json_fp);
37
38 tmp = strchr(name, '.');
39 strncpy(tag, name, tmp-name);
40 ++tmp;
41
42 while(1) {
43 char tag_not_found = 1;
44 char *line =(char *) calloc(1, 128);
45 char *entry;
46 char *tmp2;
47
48 while(tag_not_found) {
49 if (NULL == fgets(line, 128, json_fp)) return NULL;
50 if(NULL != (entry = strstr(line, tag))) {
51 tag_not_found = 0; /*found*/
52 if(last_iteration) return line;
53 break;
54 }
55 if(feof(json_fp)) return NULL;
56 }
57#if 0
58 if(NULL == strchr(tmp, '.')) {
59 /*This is the last tag*/
60 return line;
61 }
62#endif
63 tmp2 = strchr(tmp, '.');
64 if(NULL == tmp2) {
65 last_iteration = 1;
66 tmp2 = tmp+strlen(tmp);
67 }
68 memset(tag, 0, 64);
69 strncpy(tag, tmp, tmp2-tmp);
70 tmp = tmp2+1;
71 tag_not_found = 1;/*seach for next tag*/
72 }
73
74}
75
76char *
77get_string_scalar(char *path)
78{
79 char *entry = seek_to_tag(path);
80 char *value = calloc(1, 128);
81
82 if(NULL == entry) {
83 log_msg(LOG_ERROR, "%s: entry not found\n", path);
84 free(value);
85 return NULL;
86 }
87
88 if (NULL == value)
89 {
90 log_msg(LOG_ERROR,"calloc function fail");
91 return NULL;
92 }
93 char *tmp = strchr(entry, ':');
94 sscanf(tmp, ": \"%[^\"]", value);
95 log_msg(LOG_INFO, "%s = %s\n", path, value);
96 return value;
97}
98
99int
100get_int_scalar(char *path)
101{
102 char *entry = seek_to_tag(path);
103 unsigned int i = 0;
104
105 if(NULL == entry) {
106 log_msg(LOG_ERROR, "%s: entry not found\n", path);
107 return -1;
108 }
109 char *tmp = strchr(entry, ':');
110
111 i = atoi(tmp+1);
112
113 free(entry);
114 log_msg(LOG_INFO, "%s = %d\n", path, i);
115 return i;
116}
117
118
119int
120get_ip_scalar(char *path)
121{
122 char *val = get_string_scalar(path);
123 unsigned int addr = 0;
124
125 if(NULL == val) addr = -1;
126 else addr = inet_addr(val);
127
128 free(val);
129 return ntohl(addr);
130}
131
132int
133load_json(char *filename)
134{
135 json_fp = fopen(filename, "r");
136
137 if(NULL == json_fp) {
138 log_msg(LOG_ERROR, "Error opening json file\n");
139 perror("Error:");
140 return -1;
141 }
142
143 return 0;
144}
145
146/** TEST CODE
147void
148main()
149{
150
151 load_json("test.json");
152 get_string_scalar("root.scalar1");
153 get_string_scalar("root.scalar2");
154 get_string_scalar("root.scalarx");
155 get_string_scalar("root.scalar3");
156 get_string_scalar("root.scalar4");
157 get_string_scalar("root.nested.scalar5");
158}
159**/