Use xs_json_dump_pp() wherever possible.

This commit is contained in:
default 2023-08-03 09:02:08 +02:00
parent 2137d2f133
commit f5a3dbf8d6
6 changed files with 23 additions and 87 deletions

22
data.c
View File

@ -643,9 +643,7 @@ int _object_add(const char *id, const xs_dict *obj, int ow)
if ((f = fopen(fn, "w")) != NULL) { if ((f = fopen(fn, "w")) != NULL) {
flock(fileno(f), LOCK_EX); flock(fileno(f), LOCK_EX);
xs *j = xs_json_dumps_pp(obj, 4); xs_json_dump_pp(obj, 4, f);
fwrite(j, strlen(j), 1, f);
fclose(f); fclose(f);
/* does this object has a parent? */ /* does this object has a parent? */
@ -1190,9 +1188,7 @@ int following_add(snac *snac, const char *actor, const xs_dict *msg)
FILE *f; FILE *f;
if ((f = fopen(fn, "w")) != NULL) { if ((f = fopen(fn, "w")) != NULL) {
xs *j = xs_json_dumps_pp(msg, 4); xs_json_dump_pp(msg, 4, f);
fwrite(j, 1, strlen(j), f);
fclose(f); fclose(f);
/* get the filename of the actor object */ /* get the filename of the actor object */
@ -1893,9 +1889,7 @@ void notify_add(snac *snac, const char *type, const char *utype,
noti = xs_dict_append(noti, "objid", objid); noti = xs_dict_append(noti, "objid", objid);
if ((f = fopen(fn, "w")) != NULL) { if ((f = fopen(fn, "w")) != NULL) {
xs *j = xs_json_dumps_pp(noti, 4); xs_json_dump_pp(noti, 4, f);
fwrite(j, strlen(j), 1, f);
fclose(f); fclose(f);
} }
} }
@ -1970,9 +1964,7 @@ static xs_dict *_enqueue_put(const char *fn, xs_dict *msg)
FILE *f; FILE *f;
if ((f = fopen(tfn, "w")) != NULL) { if ((f = fopen(tfn, "w")) != NULL) {
xs *j = xs_json_dumps_pp(msg, 4); xs_json_dump_pp(msg, 4, f);
fwrite(j, strlen(j), 1, f);
fclose(f); fclose(f);
rename(tfn, fn); rename(tfn, fn);
@ -2569,8 +2561,7 @@ void srv_archive_error(const char *prefix, const xs_str *err,
if (req) { if (req) {
fprintf(f, "Request headers:\n"); fprintf(f, "Request headers:\n");
xs *j = xs_json_dumps_pp(req, 4); xs_json_dump_pp(req, 4, f);
fwrite(j, strlen(j), 1, f);
fprintf(f, "\n"); fprintf(f, "\n");
} }
@ -2579,8 +2570,7 @@ void srv_archive_error(const char *prefix, const xs_str *err,
fprintf(f, "Data:\n"); fprintf(f, "Data:\n");
if (xs_type(data) == XSTYPE_LIST || xs_type(data) == XSTYPE_DICT) { if (xs_type(data) == XSTYPE_LIST || xs_type(data) == XSTYPE_DICT) {
xs *j = xs_json_dumps_pp(data, 4); xs_json_dump_pp(data, 4, f);
fwrite(j, strlen(j), 1, f);
} }
else else
fprintf(f, "%s", data); fprintf(f, "%s", data);

8
html.c
View File

@ -1985,10 +1985,7 @@ int html_post_handler(const xs_dict *req, const char *q_path,
p_vars = xs_dict_get(req, "p_vars"); p_vars = xs_dict_get(req, "p_vars");
#if 0 #if 0
{ xs_json_dump_pp(p_vars, 4, stdout);
xs *j1 = xs_json_dumps_pp(p_vars, 4);
printf("%s\n", j1);
}
#endif #endif
if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/ if (p_path && strcmp(p_path, "admin/note") == 0) { /** **/
@ -2310,8 +2307,7 @@ int html_post_handler(const xs_dict *req, const char *q_path,
rename(fn, bfn); rename(fn, bfn);
if ((f = fopen(fn, "w")) != NULL) { if ((f = fopen(fn, "w")) != NULL) {
xs *j = xs_json_dumps_pp(snac.config, 4); xs_json_dump_pp(snac.config, 4, f);
fwrite(j, strlen(j), 1, f);
fclose(f); fclose(f);
} }
else else

24
main.c
View File

@ -198,8 +198,7 @@ int main(int argc, char *argv[])
xs *list = index_list_desc(idx, 0, 256); xs *list = index_list_desc(idx, 0, 256);
xs *tl = timeline_top_level(&snac, list); xs *tl = timeline_top_level(&snac, list);
xs *j = xs_json_dumps_pp(tl, 4); xs_json_dump_pp(tl, 4, stdout);
printf("%s\n", j);
return 0; return 0;
} }
@ -230,8 +229,7 @@ int main(int argc, char *argv[])
enqueue_message(&snac, msg); enqueue_message(&snac, msg);
if (dbglevel) { if (dbglevel) {
xs *j = xs_json_dumps_pp(msg, 4); xs_json_dump_pp(msg, 4, stdout);
printf("%s\n", j);
} }
} }
@ -249,8 +247,7 @@ int main(int argc, char *argv[])
enqueue_output_by_actor(&snac, msg, actor, 0); enqueue_output_by_actor(&snac, msg, actor, 0);
if (dbglevel) { if (dbglevel) {
xs *j = xs_json_dumps_pp(msg, 4); xs_json_dump_pp(msg, 4, stdout);
printf("%s\n", j);
} }
} }
@ -284,8 +281,7 @@ int main(int argc, char *argv[])
enqueue_output_by_actor(&snac, msg, url, 0); enqueue_output_by_actor(&snac, msg, url, 0);
if (dbglevel) { if (dbglevel) {
xs *j = xs_json_dumps_pp(msg, 4); xs_json_dump_pp(msg, 4, stdout);
printf("%s\n", j);
} }
} }
else { else {
@ -324,8 +320,7 @@ int main(int argc, char *argv[])
xs *c_msg = msg_create(&snac, msg); xs *c_msg = msg_create(&snac, msg);
if (dbglevel) { if (dbglevel) {
xs *j = xs_json_dumps_pp(c_msg, 4); xs_json_dump_pp(c_msg, 4, stdout);
printf("%s\n", j);
} }
enqueue_message(&snac, c_msg); enqueue_message(&snac, c_msg);
@ -345,8 +340,7 @@ int main(int argc, char *argv[])
printf("status: %d\n", status); printf("status: %d\n", status);
if (data != NULL) { if (data != NULL) {
xs *j = xs_json_dumps_pp(data, 4); xs_json_dump_pp(data, 4, stdout);
printf("%s\n", j);
} }
return 0; return 0;
@ -361,8 +355,7 @@ int main(int argc, char *argv[])
printf("status: %d\n", status); printf("status: %d\n", status);
if (valid_status(status)) { if (valid_status(status)) {
xs *j = xs_json_dumps_pp(data, 4); xs_json_dump_pp(data, 4, stdout);
printf("%s\n", j);
} }
return 0; return 0;
@ -405,8 +398,7 @@ int main(int argc, char *argv[])
c_msg = msg_create(&snac, msg); c_msg = msg_create(&snac, msg);
if (dbglevel) { if (dbglevel) {
xs *j = xs_json_dumps_pp(c_msg, 4); xs_json_dump_pp(c_msg, 4, stdout);
printf("%s\n", j);
} }
enqueue_message(&snac, c_msg); enqueue_message(&snac, c_msg);

View File

@ -39,8 +39,7 @@ int app_add(const char *id, const xs_dict *app)
fn = xs_str_cat(fn, ".json"); fn = xs_str_cat(fn, ".json");
if ((f = fopen(fn, "w")) != NULL) { if ((f = fopen(fn, "w")) != NULL) {
xs *j = xs_json_dumps_pp(app, 4); xs_json_dump_pp(app, 4, f);
fwrite(j, strlen(j), 1, f);
fclose(f); fclose(f);
} }
else else
@ -104,8 +103,7 @@ int token_add(const char *id, const xs_dict *token)
fn = xs_str_cat(fn, ".json"); fn = xs_str_cat(fn, ".json");
if ((f = fopen(fn, "w")) != NULL) { if ((f = fopen(fn, "w")) != NULL) {
xs *j = xs_json_dumps_pp(token, 4); xs_json_dump_pp(token, 4, f);
fwrite(j, strlen(j), 1, f);
fclose(f); fclose(f);
} }
else else
@ -170,11 +168,6 @@ int oauth_get_handler(const xs_dict *req, const char *q_path,
if (!xs_startswith(q_path, "/oauth/")) if (!xs_startswith(q_path, "/oauth/"))
return 0; return 0;
/* {
xs *j = xs_json_dumps_pp(req, 4);
printf("oauth get:\n%s\n", j);
}*/
int status = 404; int status = 404;
xs_dict *msg = xs_dict_get(req, "q_vars"); xs_dict *msg = xs_dict_get(req, "q_vars");
xs *cmd = xs_replace_n(q_path, "/oauth", "", 1); xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
@ -236,11 +229,6 @@ int oauth_post_handler(const xs_dict *req, const char *q_path,
if (!xs_startswith(q_path, "/oauth/")) if (!xs_startswith(q_path, "/oauth/"))
return 0; return 0;
/* {
xs *j = xs_json_dumps_pp(req, 4);
printf("oauth post:\n%s\n", j);
}*/
int status = 404; int status = 404;
char *i_ctype = xs_dict_get(req, "content-type"); char *i_ctype = xs_dict_get(req, "content-type");
@ -946,10 +934,6 @@ int mastoapi_get_handler(const xs_dict *req, const char *q_path,
return 0; return 0;
srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path)); srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
/* {
xs *j = xs_json_dumps_pp(req, 4);
printf("mastoapi get:\n%s\n", j);
}*/
int status = 404; int status = 404;
xs_dict *args = xs_dict_get(req, "q_vars"); xs_dict *args = xs_dict_get(req, "q_vars");
@ -1707,10 +1691,6 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
return 0; return 0;
srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path)); srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
/* {
xs *j = xs_json_dumps_pp(req, 4);
printf("mastoapi post:\n%s\n", j);
}*/
int status = 404; int status = 404;
xs *args = NULL; xs *args = NULL;
@ -1724,11 +1704,6 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
if (args == NULL) if (args == NULL)
return 400; return 400;
/* {
xs *j = xs_json_dumps_pp(args, 4);
printf("%s\n", j);
}*/
xs *cmd = xs_replace_n(q_path, "/api", "", 1); xs *cmd = xs_replace_n(q_path, "/api", "", 1);
snac snac = {0}; snac snac = {0};
@ -1785,10 +1760,6 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
if (strcmp(cmd, "/v1/statuses") == 0) { /** **/ if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
if (logged_in) { if (logged_in) {
/* post a new Note */ /* post a new Note */
/* {
xs *j = xs_json_dumps_pp(args, 4);
printf("%s\n", j);
}*/
const char *content = xs_dict_get(args, "status"); const char *content = xs_dict_get(args, "status");
const char *mid = xs_dict_get(args, "in_reply_to_id"); const char *mid = xs_dict_get(args, "in_reply_to_id");
const char *visibility = xs_dict_get(args, "visibility"); const char *visibility = xs_dict_get(args, "visibility");
@ -2009,10 +1980,6 @@ int mastoapi_post_handler(const xs_dict *req, const char *q_path,
else else
if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/ if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
if (logged_in) { if (logged_in) {
/* {
xs *j = xs_json_dumps_pp(args, 4);
printf("%s\n", j);
}*/
const xs_list *file = xs_dict_get(args, "file"); const xs_list *file = xs_dict_get(args, "file");
const char *desc = xs_dict_get(args, "description"); const char *desc = xs_dict_get(args, "description");
@ -2222,10 +2189,6 @@ int mastoapi_put_handler(const xs_dict *req, const char *q_path,
return 0; return 0;
srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path)); srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
/* {
xs *j = xs_json_dumps_pp(req, 4);
printf("mastoapi put:\n%s\n", j);
}*/
int status = 404; int status = 404;
xs *args = NULL; xs *args = NULL;

View File

@ -312,8 +312,7 @@ int snac_upgrade(xs_str **error)
FILE *f; FILE *f;
if ((f = fopen(fn, "w")) != NULL) { if ((f = fopen(fn, "w")) != NULL) {
xs *j = xs_json_dumps_pp(srv_config, 4); xs_json_dump_pp(srv_config, 4, f);
fwrite(j, strlen(j), 1, f);
fclose(f); fclose(f);
srv_log(xs_fmt("disk layout upgraded %s after %d changes", fn, changed)); srv_log(xs_fmt("disk layout upgraded %s after %d changes", fn, changed));

12
utils.c
View File

@ -194,8 +194,7 @@ int snac_init(const char *basedir)
return 1; return 1;
} }
xs *j = xs_json_dumps_pp(srv_config, 4); xs_json_dump_pp(srv_config, 4, f);
fwrite(j, strlen(j), 1, f);
fclose(f); fclose(f);
printf("Done.\n"); printf("Done.\n");
@ -276,8 +275,7 @@ int adduser(const char *uid)
return 1; return 1;
} }
else { else {
xs *j = xs_json_dumps_pp(config, 4); xs_json_dump_pp(config, 4, f);
fwrite(j, strlen(j), 1, f);
fclose(f); fclose(f);
} }
@ -292,8 +290,7 @@ int adduser(const char *uid)
return 1; return 1;
} }
else { else {
xs *j = xs_json_dumps_pp(key, 4); xs_json_dump_pp(key, 4, f);
fwrite(j, strlen(j), 1, f);
fclose(f); fclose(f);
} }
@ -319,8 +316,7 @@ int resetpwd(snac *snac)
snac->config = xs_dict_set(snac->config, "passwd", hashed_pwd); snac->config = xs_dict_set(snac->config, "passwd", hashed_pwd);
if ((f = fopen(fn, "w")) != NULL) { if ((f = fopen(fn, "w")) != NULL) {
xs *j = xs_json_dumps_pp(snac->config, 4); xs_json_dump_pp(snac->config, 4, f);
fwrite(j, strlen(j), 1, f);
fclose(f); fclose(f);
printf("New password for user %s is %s\n", snac->uid, clear_pwd); printf("New password for user %s is %s\n", snac->uid, clear_pwd);