commit f5a3dbf

default  ·  2023-08-03 07:02:08 +0000 UTC
parent 2137d2f
Use xs_json_dump_pp() wherever possible.
6 files changed,  +23, -87
M data.c
M html.c
M main.c
M data.c
+6, -16
 1@@ -643,9 +643,7 @@ int _object_add(const char *id, const xs_dict *obj, int ow)
 2     if ((f = fopen(fn, "w")) != NULL) {
 3         flock(fileno(f), LOCK_EX);
 4 
 5-        xs *j = xs_json_dumps_pp(obj, 4);
 6-
 7-        fwrite(j, strlen(j), 1, f);
 8+        xs_json_dump_pp(obj, 4, f);
 9         fclose(f);
10 
11         /* does this object has a parent? */
12@@ -1190,9 +1188,7 @@ int following_add(snac *snac, const char *actor, const xs_dict *msg)
13     FILE *f;
14 
15     if ((f = fopen(fn, "w")) != NULL) {
16-        xs *j = xs_json_dumps_pp(msg, 4);
17-
18-        fwrite(j, 1, strlen(j), f);
19+        xs_json_dump_pp(msg, 4, f);
20         fclose(f);
21 
22         /* get the filename of the actor object */
23@@ -1893,9 +1889,7 @@ void notify_add(snac *snac, const char *type, const char *utype,
24         noti = xs_dict_append(noti, "objid", objid);
25 
26     if ((f = fopen(fn, "w")) != NULL) {
27-        xs *j = xs_json_dumps_pp(noti, 4);
28-
29-        fwrite(j, strlen(j), 1, f);
30+        xs_json_dump_pp(noti, 4, f);
31         fclose(f);
32     }
33 }
34@@ -1970,9 +1964,7 @@ static xs_dict *_enqueue_put(const char *fn, xs_dict *msg)
35     FILE *f;
36 
37     if ((f = fopen(tfn, "w")) != NULL) {
38-        xs *j = xs_json_dumps_pp(msg, 4);
39-
40-        fwrite(j, strlen(j), 1, f);
41+        xs_json_dump_pp(msg, 4, f);
42         fclose(f);
43 
44         rename(tfn, fn);
45@@ -2569,8 +2561,7 @@ void srv_archive_error(const char *prefix, const xs_str *err,
46         if (req) {
47             fprintf(f, "Request headers:\n");
48 
49-            xs *j = xs_json_dumps_pp(req, 4);
50-            fwrite(j, strlen(j), 1, f);
51+            xs_json_dump_pp(req, 4, f);
52 
53             fprintf(f, "\n");
54         }
55@@ -2579,8 +2570,7 @@ void srv_archive_error(const char *prefix, const xs_str *err,
56             fprintf(f, "Data:\n");
57 
58             if (xs_type(data) == XSTYPE_LIST || xs_type(data) == XSTYPE_DICT) {
59-                xs *j = xs_json_dumps_pp(data, 4);
60-                fwrite(j, strlen(j), 1, f);
61+                xs_json_dump_pp(data, 4, f);
62             }
63             else
64                 fprintf(f, "%s", data);
M html.c
+2, -6
 1@@ -1985,10 +1985,7 @@ int html_post_handler(const xs_dict *req, const char *q_path,
 2     p_vars = xs_dict_get(req, "p_vars");
 3 
 4 #if 0
 5-    {
 6-        xs *j1 = xs_json_dumps_pp(p_vars, 4);
 7-        printf("%s\n", j1);
 8-    }
 9+    xs_json_dump_pp(p_vars, 4, stdout);
10 #endif
11 
12     if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
13@@ -2310,8 +2307,7 @@ int html_post_handler(const xs_dict *req, const char *q_path,
14         rename(fn, bfn);
15 
16         if ((f = fopen(fn, "w")) != NULL) {
17-            xs *j = xs_json_dumps_pp(snac.config, 4);
18-            fwrite(j, strlen(j), 1, f);
19+            xs_json_dump_pp(snac.config, 4, f);
20             fclose(f);
21         }
22         else
M main.c
+8, -16
 1@@ -198,8 +198,7 @@ int main(int argc, char *argv[])
 2         xs *list = index_list_desc(idx, 0, 256);
 3         xs *tl   = timeline_top_level(&snac, list);
 4 
 5-        xs *j    = xs_json_dumps_pp(tl, 4);
 6-        printf("%s\n", j);
 7+        xs_json_dump_pp(tl, 4, stdout);
 8 
 9         return 0;
10     }
11@@ -230,8 +229,7 @@ int main(int argc, char *argv[])
12             enqueue_message(&snac, msg);
13 
14             if (dbglevel) {
15-                xs *j = xs_json_dumps_pp(msg, 4);
16-                printf("%s\n", j);
17+                xs_json_dump_pp(msg, 4, stdout);
18             }
19         }
20 
21@@ -249,8 +247,7 @@ int main(int argc, char *argv[])
22             enqueue_output_by_actor(&snac, msg, actor, 0);
23 
24             if (dbglevel) {
25-                xs *j = xs_json_dumps_pp(msg, 4);
26-                printf("%s\n", j);
27+                xs_json_dump_pp(msg, 4, stdout);
28             }
29         }
30 
31@@ -284,8 +281,7 @@ int main(int argc, char *argv[])
32             enqueue_output_by_actor(&snac, msg, url, 0);
33 
34             if (dbglevel) {
35-                xs *j = xs_json_dumps_pp(msg, 4);
36-                printf("%s\n", j);
37+                xs_json_dump_pp(msg, 4, stdout);
38             }
39         }
40         else {
41@@ -324,8 +320,7 @@ int main(int argc, char *argv[])
42         xs *c_msg = msg_create(&snac, msg);
43 
44         if (dbglevel) {
45-            xs *j = xs_json_dumps_pp(c_msg, 4);
46-            printf("%s\n", j);
47+            xs_json_dump_pp(c_msg, 4, stdout);
48         }
49 
50         enqueue_message(&snac, c_msg);
51@@ -345,8 +340,7 @@ int main(int argc, char *argv[])
52         printf("status: %d\n", status);
53 
54         if (data != NULL) {
55-            xs *j = xs_json_dumps_pp(data, 4);
56-            printf("%s\n", j);
57+            xs_json_dump_pp(data, 4, stdout);
58         }
59 
60         return 0;
61@@ -361,8 +355,7 @@ int main(int argc, char *argv[])
62         printf("status: %d\n", status);
63 
64         if (valid_status(status)) {
65-            xs *j = xs_json_dumps_pp(data, 4);
66-            printf("%s\n", j);
67+            xs_json_dump_pp(data, 4, stdout);
68         }
69 
70         return 0;
71@@ -405,8 +398,7 @@ int main(int argc, char *argv[])
72         c_msg = msg_create(&snac, msg);
73 
74         if (dbglevel) {
75-            xs *j = xs_json_dumps_pp(c_msg, 4);
76-            printf("%s\n", j);
77+            xs_json_dump_pp(c_msg, 4, stdout);
78         }
79 
80         enqueue_message(&snac, c_msg);
+2, -39
  1@@ -39,8 +39,7 @@ int app_add(const char *id, const xs_dict *app)
  2     fn = xs_str_cat(fn, ".json");
  3 
  4     if ((f = fopen(fn, "w")) != NULL) {
  5-        xs *j = xs_json_dumps_pp(app, 4);
  6-        fwrite(j, strlen(j), 1, f);
  7+        xs_json_dump_pp(app, 4, f);
  8         fclose(f);
  9     }
 10     else
 11@@ -104,8 +103,7 @@ int token_add(const char *id, const xs_dict *token)
 12     fn = xs_str_cat(fn, ".json");
 13 
 14     if ((f = fopen(fn, "w")) != NULL) {
 15-        xs *j = xs_json_dumps_pp(token, 4);
 16-        fwrite(j, strlen(j), 1, f);
 17+        xs_json_dump_pp(token, 4, f);
 18         fclose(f);
 19     }
 20     else
 21@@ -170,11 +168,6 @@ int oauth_get_handler(const xs_dict *req, const char *q_path,
 22     if (!xs_startswith(q_path, "/oauth/"))
 23         return 0;
 24 
 25-/*    {
 26-        xs *j = xs_json_dumps_pp(req, 4);
 27-        printf("oauth get:\n%s\n", j);
 28-    }*/
 29-
 30     int status   = 404;
 31     xs_dict *msg = xs_dict_get(req, "q_vars");
 32     xs *cmd      = xs_replace_n(q_path, "/oauth", "", 1);
 33@@ -236,11 +229,6 @@ int oauth_post_handler(const xs_dict *req, const char *q_path,
 34     if (!xs_startswith(q_path, "/oauth/"))
 35         return 0;
 36 
 37-/*    {
 38-        xs *j = xs_json_dumps_pp(req, 4);
 39-        printf("oauth post:\n%s\n", j);
 40-    }*/
 41-
 42     int status   = 404;
 43 
 44     char *i_ctype = xs_dict_get(req, "content-type");
 45@@ -946,10 +934,6 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
 46         return 0;
 47 
 48     srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
 49-/*    {
 50-        xs *j = xs_json_dumps_pp(req, 4);
 51-        printf("mastoapi get:\n%s\n", j);
 52-    }*/
 53 
 54     int status    = 404;
 55     xs_dict *args = xs_dict_get(req, "q_vars");
 56@@ -1707,10 +1691,6 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
 57         return 0;
 58 
 59     srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
 60-/*    {
 61-        xs *j = xs_json_dumps_pp(req, 4);
 62-        printf("mastoapi post:\n%s\n", j);
 63-    }*/
 64 
 65     int status    = 404;
 66     xs *args      = NULL;
 67@@ -1724,11 +1704,6 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
 68     if (args == NULL)
 69         return 400;
 70 
 71-/*    {
 72-        xs *j = xs_json_dumps_pp(args, 4);
 73-        printf("%s\n", j);
 74-    }*/
 75-
 76     xs *cmd = xs_replace_n(q_path, "/api", "", 1);
 77 
 78     snac snac = {0};
 79@@ -1785,10 +1760,6 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
 80     if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
 81         if (logged_in) {
 82             /* post a new Note */
 83-/*    {
 84-        xs *j = xs_json_dumps_pp(args, 4);
 85-        printf("%s\n", j);
 86-    }*/
 87             const char *content    = xs_dict_get(args, "status");
 88             const char *mid        = xs_dict_get(args, "in_reply_to_id");
 89             const char *visibility = xs_dict_get(args, "visibility");
 90@@ -2009,10 +1980,6 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
 91     else
 92     if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
 93         if (logged_in) {
 94-/*    {
 95-        xs *j = xs_json_dumps_pp(args, 4);
 96-        printf("%s\n", j);
 97-    }*/
 98             const xs_list *file = xs_dict_get(args, "file");
 99             const char *desc    = xs_dict_get(args, "description");
100 
101@@ -2222,10 +2189,6 @@ int mastoapi_put_handler(const xs_dict *req, const char *q_path,
102         return 0;
103 
104     srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
105-/*    {
106-        xs *j = xs_json_dumps_pp(req, 4);
107-        printf("mastoapi put:\n%s\n", j);
108-    }*/
109 
110     int status    = 404;
111     xs *args      = NULL;
+1, -2
 1@@ -312,8 +312,7 @@ int snac_upgrade(xs_str **error)
 2         FILE *f;
 3 
 4         if ((f = fopen(fn, "w")) != NULL) {
 5-            xs *j = xs_json_dumps_pp(srv_config, 4);
 6-            fwrite(j, strlen(j), 1, f);
 7+            xs_json_dump_pp(srv_config, 4, f);
 8             fclose(f);
 9 
10             srv_log(xs_fmt("disk layout upgraded %s after %d changes", fn, changed));
+4, -8
 1@@ -194,8 +194,7 @@ int snac_init(const char *basedir)
 2         return 1;
 3     }
 4 
 5-    xs *j = xs_json_dumps_pp(srv_config, 4);
 6-    fwrite(j, strlen(j), 1, f);
 7+    xs_json_dump_pp(srv_config, 4, f);
 8     fclose(f);
 9 
10     printf("Done.\n");
11@@ -276,8 +275,7 @@ int adduser(const char *uid)
12         return 1;
13     }
14     else {
15-        xs *j = xs_json_dumps_pp(config, 4);
16-        fwrite(j, strlen(j), 1, f);
17+        xs_json_dump_pp(config, 4, f);
18         fclose(f);
19     }
20 
21@@ -292,8 +290,7 @@ int adduser(const char *uid)
22         return 1;
23     }
24     else {
25-        xs *j = xs_json_dumps_pp(key, 4);
26-        fwrite(j, strlen(j), 1, f);
27+        xs_json_dump_pp(key, 4, f);
28         fclose(f);
29     }
30 
31@@ -319,8 +316,7 @@ int resetpwd(snac *snac)
32     snac->config = xs_dict_set(snac->config, "passwd", hashed_pwd);
33 
34     if ((f = fopen(fn, "w")) != NULL) {
35-        xs *j = xs_json_dumps_pp(snac->config, 4);
36-        fwrite(j, strlen(j), 1, f);
37+        xs_json_dump_pp(snac->config, 4, f);
38         fclose(f);
39 
40         printf("New password for user %s is %s\n", snac->uid, clear_pwd);