coma

create simple config files from JSON input
git clone git://git.hanetzok.net/coma
Log | Files | Refs | README

commit 9660bb58ff30e675c743abb5a5459172b5153248
parent f967889ad5d3ca14be7cc5a8160794abb1ae736a
Author: Markus Hanetzok <markus@hanetzok.net>
Date:   Tue, 11 Nov 2025 21:22:48 +0100

refactor redundant code & add TODO in README

Diffstat:
MREADME | 4++++
Mcoma.c | 27+++++++++++++--------------
2 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/README b/README @@ -15,3 +15,7 @@ reads from stdin and prints to stdout. Example usage: curl https://example.com/api/call | coma > example.conf + +TODO +---- +- Distinguish between ints and floats diff --git a/coma.c b/coma.c @@ -3,6 +3,8 @@ #include <string.h> #include <cjson/cJSON.h> +#define SEPARATOR "=" + #define BUFFSIZE 2048 #define VERSION "0.1" @@ -21,6 +23,13 @@ void die(char *msg, int rc) { exit(rc); } +void stradd(char *output, char *key, char *value) { + strcat(output, key); + strcat(output, SEPARATOR); + strcat(output, value); + strcat(output, "\n"); +} + int main (int argc, char *argv[]) { int i; for (i = 1; i < argc; i++) { @@ -48,28 +57,18 @@ int main (int argc, char *argv[]) { char *key = node->string; cJSON *item = cJSON_GetObjectItemCaseSensitive(json, key); if (cJSON_IsString(item) && (item->valuestring != NULL)) { - strcat(output, key); - strcat(output, "="); - strcat(output, item->valuestring); - strcat(output, "\n"); + stradd(output, key, item->valuestring); } if (cJSON_IsNumber(item) && (item->valuedouble != 0)) { char numstr[64]; snprintf(numstr, 64, "%f", item->valuedouble); - strcat(output, key); - strcat(output, "="); - strcat(output, numstr); - strcat(output, "\n"); + stradd(output, key, numstr); } if (cJSON_IsTrue(item)) { - strcat(output, key); - strcat(output, "=true"); - strcat(output, "\n"); + stradd(output, key, "true"); } if (cJSON_IsFalse(item)) { - strcat(output, key); - strcat(output, "=false"); - strcat(output, "\n"); + stradd(output, key, "false"); } }