-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkvp2tlv.c
227 lines (183 loc) · 6.54 KB
/
kvp2tlv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "key_list.h"
#include "kvp_parser.h"
#include "kvphash_table.h"
#include "tlv_work.h"
int main(int argc, char* argv[])
{
kvp_iterator json;
kvp_iterator dict;
bool dict_from_file = false;
kvphash_table* dict_keys = ht_create();
if(dict_keys == NULL) {
return EXIT_BAD_HASH_TABLE;
}
if(argc < 2 || argc > 4) {
printf("USAGE: %s output_TLV_file [KVP_input_file] [dict_json_file]\n", argv[0]);
printf("where output_TLV_file - tlv file for output;\n");
printf("KVP_input_file - input file with KV pairs in JSON style\n");
printf(" if it is not present - we must input KV in console;\n");
printf("dict_json_file - input file with Keys values pairs in th same JSON style\n");
printf(" if it is not present - each key will be sequentially numbered 1,2,3...;\n");
return EXIT_WRONG_ARG_COUNT;
} else if(argc == 2) {
// if argc==2 we input KVP from console
kvp_open_stream(&json, stdin);
} else {
// input from file
FILE* file_json_kvp = fopen(argv[2], "rb");
if(!file_json_kvp) {
printf("ERROR: cannot open file %s for read\n", argv[2]);
return EXIT_BAD_FILE_NAME;
}
kvp_open_stream(&json, file_json_kvp);
// if we predefine the dictionary of keys
if(argc == 4) {
// input keys values form the file
dict_from_file = true;
FILE* file_json_dict = fopen(argv[3], "rb");
if(!file_json_dict) {
printf("ERROR: cannot open file %s for read\n", argv[3]);
return EXIT_BAD_FILE_NAME;
}
kvp_open_stream(&dict, file_json_dict);
// dictionary should be as single json dict
enum kvp_json_type result = 0;
size_t len;
while(dict.type != JSON_ERROR) {
result = kvp_next(&dict);
if(result == JSON_ERROR) {
return EXIT_JSON_ERROR;
}
if(result == JSON_END) {
break;
}
void* value = ht_get(dict_keys, kvp_get_string(&dict, &len));
if(value != NULL) {
printf("We alredy had this value %s", kvp_get_string(&dict, &len));
continue;
}
// allocate space for new int and set it to count
int* val = malloc(sizeof(int));
if(val == NULL) {
return EXIT_BAD_MALLOC;
}
char* buf = malloc(len + 1);
size_t n = kvp_save_string(&dict, buf);
result = kvp_next(&dict);
if(result == JSON_ERROR) {
return EXIT_JSON_ERROR;
}
if(result == JSON_END) {
break;
}
*val = kvp_get_int(&dict);
if(ht_set(dict_keys, buf, val) == NULL) {
return EXIT_BAD_MALLOC;
}
free(buf);
}
kvp_close(&dict);
}
}
// else - if we do not predifine them
// the values should be sequentially 1,2,3,... etc
int count_keys = 0;
kvp_set_streaming(&json, false);
// open file for writing
FILE* tlv_to_write = fopen(argv[1], "wb");
if(!tlv_to_write) {
printf("ERROR: cannot open file %s for writing\n", argv[1]);
return EXIT_BAD_FILE_NAME;
}
enum kvp_json_type result = 0;
printf("read the KV pairs and write them:\n");
while(json.type != JSON_ERROR) {
result = kvp_next(&json);
if(result == JSON_ERROR) {
return EXIT_JSON_ERROR;
}
if(result == JSON_END) {
break;
}
size_t len = 0;
//printf("\nkey=%s ", kvp_get_string(&json, &len));
void* value = ht_get(dict_keys, kvp_get_string(&json, &len));
if(value == NULL) {
// no key in hashtable, increment counter
count_keys++;
// allocate space for new int and set it to count
int* pcount = malloc(sizeof(int));
if(pcount == NULL) {
return EXIT_BAD_MALLOC;
}
*pcount = count_keys;
if(ht_set(dict_keys, kvp_get_string(&json, &len), pcount) == NULL) {
return EXIT_BAD_MALLOC;
}
// free(pcount);
}
// output data into TLV file
// write_data(argv[1], ht_get(dict_keys, json->data));
tlv_write_file(NUMBER_TLV, 1, ht_get(dict_keys, kvp_get_string(&json, &len)), tlv_to_write);
result = kvp_next(&json);
if(result == JSON_ERROR) {
return EXIT_JSON_ERROR;
}
if(result == JSON_END) {
break;
}
char* buf = malloc(json.data.string_size);
kvp_get_value(&json, buf, &len);
//printf("value=%s", buf);
bool x;
int y;
// output data into TLV file
switch(json.type) {
case JSON_STRING:
tlv_write_file(STRING_TLV, json.data.string_size, json.data.string, tlv_to_write);
break;
case JSON_NUMBER: // TODO: int union
tlv_write_file(NUMBER_TLV, 1, json.data.string, tlv_to_write);
break;
case JSON_TRUE:
x = true;
tlv_write_file(BOOL_TLV, 1, &x, tlv_to_write);
break;
case JSON_FALSE:
x = false;
tlv_write_file(BOOL_TLV, 1, &x, tlv_to_write);
break;
case JSON_NULL:
y = 0;
tlv_write_file(NUMBER_TLV, 1, &y, tlv_to_write);
break;
default:
printf("Unknown type %d", (int)(json.type));
return EXIT_JSON_ERROR;
}
free(buf);
}
if(result == JSON_ERROR) {
fprintf(stderr, "error: %zu: %s\n", kvp_get_lineno(&json), kvp_get_error(&json));
return EXIT_JSON_ERROR;
} else {
printf("\n");
}
kvp_close(&json);
// Print out keys dict
hti it = ht_iterator(dict_keys);
printf("write keys values at the end:\n");
while(ht_next(&it)) {
//printf("\n %s , %d", it.key, (int)*((int*)it.value));
tlv_write_file(STRING_TLV, strlen(it.key), (void*)it.key, tlv_to_write);
tlv_write_file(NUMBER_TLV, 1, (int*)it.value, tlv_to_write);
free(it.value);
}
ht_destroy(dict_keys);
fclose(tlv_to_write);
return EXIT_NO_ERRORS;
}