-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.c
143 lines (125 loc) · 3.24 KB
/
config.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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netdb.h>
#include<signal.h>
#include<fcntl.h>
#include "json.h"
#include "config.h"
int isCommentLine(char *line){
char *pc = line;
while(*pc != '\0'){
if(*pc == '\t' || *pc == ' ') {
pc++;
continue;
}else{
if (*pc == '/') {
if(*(pc+1) == '/') {
return 1;
} else{
return 0;
}
}else{
return 0;
}
}
}
return 0;
}
json_object* getCleanConfig(char *filename) {
FILE *fd;
char buffer[102400]={0};
char *pline = NULL;
ssize_t read;
//printf("\n==============>Parse Config: %s\n", filename);
fd = fopen(filename, "r");
if(!fd) {
printf("ERROR: Failed to open file %s\n", filename);
return NULL;
}
while((read = getline(&pline, &read, fd)) != -1){
//printf("%s\n", pline);
if (isCommentLine(pline)){
continue;
}else{
strcat(buffer, pline);
strcat(buffer, "\n");
}
}
free(pline);
fclose(fd);
//printf("\n==============>Clean Config: %s\n", buffer);
json_object *json = NULL;
json = json_tokener_parse(buffer);
if (json) {
//printf("JSON parse OK.\n");
//printf(cJSON_Print(json));
//printf("\n");
}else{
printf("Failed to parse config\n");
return NULL;
}
return json;
}
int writeConfig(json_object *json, char *filename) {
FILE *fd;
fd = fopen(filename, "w+");
if (fd) {
char *output;
output = json_object_to_json_string_ext(json, JSON_C_TO_STRING_PRETTY);
if (output) {
size_t len = strlen(output);
fwrite(output, len, 1, fd);
}
}else{
printf("ERROR: Failed to open file %s\n", filename);
return -1;
}
fclose(fd);
return 0;
}
json_object *get_config_param(json_object *config, char *name){
json_object *authDelegate = json_object_object_get(config, "authDelegate");
if(authDelegate == NULL) {
return NULL;
}
if (strcmp(name, "clientSecret") == 0) {
json_object *clientSecret = json_object_object_get(authDelegate, "clientSecret");
return clientSecret;
}else if (strcmp(name, "deviceSerialNumber") == 0) {
json_object *deviceSerialNumber = json_object_object_get(authDelegate, "deviceSerialNumber");
return deviceSerialNumber;
}else if (strcmp(name, "refreshToken") == 0) {
json_object *refreshToken = json_object_object_get(authDelegate, "refreshToken");
return refreshToken;
}else if (strcmp(name, "clientId") == 0) {
json_object *clientId = json_object_object_get(authDelegate, "clientId");
return clientId;
}else if (strcmp(name, "productId") == 0) {
json_object *productId = json_object_object_get(authDelegate, "productId");
return productId;
}
return NULL;
}
char *get_config_param_value(json_object *config, char *name){
json_object *param;
param = get_config_param(config, name);
return param? json_object_get_string(param): NULL;
}
int update_config_param(json_object *config, char *name, char *value) {
json_object *authDelegate = json_object_object_get(config, "authDelegate");
if(authDelegate == NULL) {
return -1;
}
if (strcmp(name, "refreshToken") == 0){
json_object_object_del(authDelegate, "refreshToken");
json_object_object_add(authDelegate, "refreshToken", json_object_new_string(value));
return 0;
}
return -1;
}