Modifiche e correzioni generali ai assisted
This commit is contained in:
1 parent
05237cc7f1
commit
c6543cc583
13 files changed
+1645
-321
No files matched your search
+113
@@ -0,0 +1,113 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <json-c/json.h>
|
||||
|
||||
#include "../include/jsonFile.h"
|
||||
|
||||
int existsFile(const char *fname) {
|
||||
FILE *file = fopen(fname, "r");
|
||||
if (file) {
|
||||
fclose(file);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int createFile(const char *fname) {
|
||||
json_object *arr = json_object_new_array();
|
||||
if (!arr)
|
||||
return -1;
|
||||
|
||||
int res = json_object_to_file_ext(fname, arr, JSON_C_TO_STRING_PRETTY);
|
||||
json_object_put(arr);
|
||||
return (res == 0) ? 0 : -1;
|
||||
}
|
||||
|
||||
int renameDataFile(const char *oldName, const char *newName) {
|
||||
return (rename(oldName, newName) == 0) ? 0 : -1;
|
||||
}
|
||||
|
||||
static json_object *loadRawArray(const char *fname) {
|
||||
if (!existsFile(fname) && createFile(fname) != 0)
|
||||
return NULL;
|
||||
|
||||
FILE *f = fopen(fname, "r");
|
||||
if (!f)
|
||||
return NULL;
|
||||
|
||||
int empty = (fgetc(f) == EOF);
|
||||
fclose(f);
|
||||
if (empty)
|
||||
return json_object_new_array();
|
||||
|
||||
json_object *arr = json_object_from_file(fname);
|
||||
if (!arr)
|
||||
return NULL;
|
||||
|
||||
if (!json_object_is_type(arr, json_type_array)) {
|
||||
json_object_put(arr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
|
||||
json_object *loadJsonArray(const char *fname) {
|
||||
return loadRawArray(fname);
|
||||
}
|
||||
|
||||
int saveJsonArray(const char *fname, json_object *arr) {
|
||||
if (!arr)
|
||||
return -1;
|
||||
return (json_object_to_file_ext(fname, arr, JSON_C_TO_STRING_PRETTY) == 0) ? 0 : -1;
|
||||
}
|
||||
|
||||
json_object *findItemByName(json_object *arr, const char *name) {
|
||||
if (!arr || !name)
|
||||
return NULL;
|
||||
|
||||
size_t n = json_object_array_length(arr);
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
json_object *item = json_object_array_get_idx(arr, i);
|
||||
json_object *jname = NULL;
|
||||
|
||||
if (json_object_object_get_ex(item, "name", &jname) &&
|
||||
strcmp(json_object_get_string(jname), name) == 0)
|
||||
return item;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int removeItemByName(json_object *arr, const char *name) {
|
||||
if (!arr || !name)
|
||||
return -1;
|
||||
|
||||
size_t n = json_object_array_length(arr);
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
json_object *item = json_object_array_get_idx(arr, i);
|
||||
json_object *jname = NULL;
|
||||
|
||||
if (json_object_object_get_ex(item, "name", &jname) &&
|
||||
strcmp(json_object_get_string(jname), name) == 0) {
|
||||
json_object_array_del_idx(arr, (int)i, 1);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int renameItemByName(json_object *arr, const char *oldName, const char *newName) {
|
||||
if (!arr || !oldName || !newName)
|
||||
return -1;
|
||||
|
||||
json_object *item = findItemByName(arr, oldName);
|
||||
if (!item)
|
||||
return 1;
|
||||
|
||||
json_object_object_add(item, "name", json_object_new_string(newName));
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user