diff options
Diffstat (limited to 'src/model.c')
| -rw-r--r-- | src/model.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/model.c b/src/model.c index 75ed0d4..2358b11 100644 --- a/src/model.c +++ b/src/model.c @@ -1,6 +1,7 @@ #include "internal.h" #include <stddef.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -113,6 +114,59 @@ bool cepis_health_ok(const cepis_health_t *health) strcmp(health->status, "ok") == 0; } +const cepis_faixa_t *cepis_error_faixa(const cepis_error_t *err) +{ + if (err == NULL || !err->has_faixa) + return NULL; + return &err->faixa; +} + +/* + * Copies at most `size - 1` bytes and always terminates. A municipality name + * longer than the buffer is truncated rather than rejected: the point of the + * block is to tell the caller which city they meant, and a truncated name + * still does that. + */ +static void copy_member(char *out, size_t size, const json_value_t *block, + const char *name) +{ + const char *text = json_as_string(json_object_get(block, name)); + + snprintf(out, size, "%s", text != NULL ? text : ""); +} + +bool cepis_faixa_from_json(const json_value_t *object, cepis_faixa_t *out) +{ + const json_value_t *block; + const char *localidade; + + if (out == NULL) + return false; + block = json_object_get(object, "faixa"); + if (block == NULL || block->type != JSON_OBJECT) + return false; + /* + * The municipality is the whole reason the block exists. Without it + * there is nothing to report, so an empty or non-string `localidade` + * counts as no block at all rather than as a nameless one. + */ + localidade = json_as_string(json_object_get(block, "localidade")); + if (localidade == NULL || localidade[0] == '\0') + return false; + + memset(out, 0, sizeof(*out)); + snprintf(out->localidade, sizeof(out->localidade), "%s", localidade); + copy_member(out->uf, sizeof(out->uf), block, "uf"); + copy_member(out->estado, sizeof(out->estado), block, "estado"); + copy_member(out->regiao, sizeof(out->regiao), block, "regiao"); + copy_member(out->ibge, sizeof(out->ibge), block, "ibge"); + copy_member(out->cep_ini, sizeof(out->cep_ini), block, "cep_ini"); + copy_member(out->cep_fim, sizeof(out->cep_fim), block, "cep_fim"); + if (!json_as_llong(json_object_get(block, "ceps"), &out->ceps)) + out->ceps = 0; + return true; +} + static cepis_geo_t *geo_from_json(const json_value_t *block) { const char *precisao; |