aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwang_zhiqiang <wang_zhiqiang@dl.cn.nexty-ele.com>2019-04-22 15:11:27 +0800
committerwang_zhiqiang <wang_zhiqiang@dl.cn.nexty-ele.com>2019-04-22 15:11:27 +0800
commitc393aaa4ae541f1f925981a6f8ae20d46626e8b6 (patch)
tree37e421232e1b0b3de66630c8f2fcf61a78a240c6
parent46ac770dd55f51bc5d611ba3e5dd883e5f51edfa (diff)
add writeJsonFile
Change-Id: I6269845fbd3b40d697bc7c77a457b380a5906bbc
-rw-r--r--src/hs-helper.cpp41
1 files changed, 39 insertions, 2 deletions
diff --git a/src/hs-helper.cpp b/src/hs-helper.cpp
index 159668e..33ab721 100644
--- a/src/hs-helper.cpp
+++ b/src/hs-helper.cpp
@@ -283,12 +283,11 @@ std::string get_application_id(const afb_req_t request)
*
* #### Return
* 0 : read success
- * 1 : read fail
+ * -1 : read fail
*
*/
int readJsonFile(const char* file, struct json_object **obj)
{
- *obj = nullptr;
int ret = -1;
FILE *fp = fopen(file, "rb");
if(fp == nullptr) {
@@ -296,6 +295,7 @@ int readJsonFile(const char* file, struct json_object **obj)
return ret;
}
+ *obj = nullptr;
const int buf_size = 128;
char buf[buf_size];
struct json_tokener *tokener = json_tokener_new();
@@ -321,3 +321,40 @@ int readJsonFile(const char* file, struct json_object **obj)
json_tokener_free(tokener);
return ret;
}
+
+/**
+ * write to json file
+ *
+ * #### Parameters
+ * - file : output file name
+ * - obj : json_object
+ *
+ * #### Return
+ * 0 : read success
+ * -1 : read fail
+ *
+ */
+int writeJsonFile(const char* file, struct json_object *obj)
+{
+ int ret = -1;
+ FILE *fp = fopen(file, "wb");
+ if(fp == nullptr) {
+ AFB_ERROR("open %s failed", file);
+ return ret;
+ }
+
+ const char *str = json_object_to_json_string(obj);
+ size_t len = sizeof(str);
+ size_t cnt = fwrite(str, len, 1, fp);
+ if(cnt == len) {
+ ret = 0;
+ fflush(fp);
+ fsync(fileno(fp));
+ }
+ else {
+ AFB_WARNING("write to %s failed.", file);
+ }
+
+ fclose(fp);
+ return ret;
+} \ No newline at end of file