126 lines
4.2 KiB
C
126 lines
4.2 KiB
C
#include "json.h"
|
|
|
|
static int directory_exists(const char *path) {
|
|
struct stat info;
|
|
if (stat(path, &info) != 0) {
|
|
return 0; // 不存在或无法访问
|
|
}
|
|
return S_ISDIR(info.st_mode);
|
|
}
|
|
|
|
struct json_object* open_json_file(const char* _path){
|
|
char *buf1 = malloc(PATH_MAX);
|
|
memset(buf1,0,PATH_MAX);
|
|
ssize_t len = readlink("/proc/self/exe", buf1, PATH_MAX - 1);
|
|
if (len == -1) {
|
|
fprintf(stderr, "[Error] %s:%d: readlink() returned non-negative value. %s\n",__FILE__, __LINE__, __func__);
|
|
return NULL;
|
|
}
|
|
char *buf2 = malloc(PATH_MAX);
|
|
memset(buf2,0,PATH_MAX);
|
|
*buf2 = '/';
|
|
strcat(buf2,_path);
|
|
dirname(buf1);
|
|
strcat(buf1,buf2);
|
|
memset(buf2,0,PATH_MAX);
|
|
strncpy(buf2, buf1, PATH_MAX);
|
|
dirname(buf2);
|
|
if(!directory_exists(buf2)){
|
|
if(mkdir(buf2,0774)!=0){
|
|
fprintf(stderr, "[Error] %s:%d: Make directory failed. %s\n",__FILE__, __LINE__, __func__);
|
|
return NULL;
|
|
}
|
|
}
|
|
FILE* json_file = fopen(buf1,"r+");
|
|
if(json_file == NULL){
|
|
fprintf(stdout,"[Info] %s:%d: Read config file failed. %s : %s",__FILE__, __LINE__, __func__,strerror(errno));
|
|
json_file = fopen(buf1,"w");
|
|
if(json_file == NULL){
|
|
fprintf(stdout,"[Error] %s:%d: Create config file failed. %s : %s",__FILE__, __LINE__, __func__,strerror(errno));
|
|
return NULL;
|
|
}
|
|
fprintf(json_file,"{}");
|
|
fclose(json_file);
|
|
json_file = fopen(buf1,"r+");
|
|
}
|
|
free(buf1);free(buf2);
|
|
int file_size=0,malloced = 0;
|
|
char* json_string = malloc(malloced);
|
|
while(!feof(json_file)){
|
|
if(file_size>=malloced-1){
|
|
char* tmp_string = realloc(json_string,malloced+2048);
|
|
if(tmp_string!=NULL){
|
|
json_string = tmp_string;
|
|
}else{
|
|
fprintf(stderr, "[Error] %s:%d: Realloc failed. May system has been Out of Memory. %s\n",__FILE__, __LINE__, __func__);
|
|
return NULL;
|
|
}
|
|
memset(json_string+malloced,0,2048);
|
|
malloced+=2048;
|
|
}
|
|
json_string[file_size] = fgetc(json_file);
|
|
file_size++;
|
|
}
|
|
file_size--;
|
|
json_string[file_size] = '\0';
|
|
fclose(json_file);
|
|
char* tmp_string = realloc(json_string,file_size);
|
|
if(tmp_string!=NULL){
|
|
json_string = tmp_string;
|
|
}else{
|
|
fprintf(stderr, "[Error] %s:%d: Realloc failed. May system has been Out of Memory. %s\n",__FILE__, __LINE__, __func__);
|
|
return NULL;
|
|
}
|
|
struct json_object *obj;
|
|
obj = json_tokener_parse(json_string);
|
|
if(obj==NULL){
|
|
enum json_tokener_error jerr;
|
|
obj = json_tokener_parse_verbose("...", &jerr);
|
|
return NULL;
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
int write_json_file(const char* _path,struct json_object* _json){
|
|
char* buf1 = malloc(PATH_MAX);
|
|
memset(buf1,0,PATH_MAX);
|
|
ssize_t len = readlink("/proc/self/exe", buf1, PATH_MAX - 1);
|
|
if (len == -1) {
|
|
fprintf(stderr, "[Error] %s:%d: readlink() returned non-negative value. %s\n",__FILE__, __LINE__, __func__);
|
|
return 1;
|
|
}
|
|
char* buf2 = malloc(PATH_MAX);
|
|
memset(buf2,0,PATH_MAX);
|
|
*buf2 = '/';
|
|
strcat(buf2,_path);
|
|
dirname(buf1);
|
|
strcat(buf1,buf2);
|
|
memset(buf2,0,PATH_MAX);
|
|
strncpy(buf2, buf1, PATH_MAX);
|
|
dirname(buf2);
|
|
if(!directory_exists(buf2)){
|
|
if(mkdir(buf2,0774)!=0){
|
|
fprintf(stderr, "[Error] %s:%d: Make directory failed. %s\n",__FILE__, __LINE__, __func__);
|
|
return -1;
|
|
}
|
|
}
|
|
FILE* json_file = fopen(buf1,"w");
|
|
if(json_file == NULL){
|
|
fprintf(stderr, "[Error] %s:%d: readlink() returned non-negative value. %s\n",__FILE__, __LINE__, __func__);
|
|
return -1;
|
|
}
|
|
free(buf1);free(buf2);
|
|
fprintf(json_file, "%s", json_object_to_json_string_ext(_json, JSON_C_TO_STRING_PRETTY));
|
|
fclose(json_file);
|
|
return 0;
|
|
}
|
|
|
|
struct json_object *get_json_object_object(struct json_object *_json,const char *key,struct json_object *_default,const char *_path){
|
|
struct json_object *obj = json_object_object_get(_json, key);
|
|
if(obj==NULL){
|
|
json_object_object_add(_json,key,_default);
|
|
write_json_file(_path,_json);
|
|
obj = json_object_object_get(_json, key);
|
|
}
|
|
return obj;
|
|
} |