summaryrefslogtreecommitdiffstats
path: root/src/model.c
blob: 2358b11a13af8d5d998122dcdd6906d105163de7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "internal.h"

#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
        const char *name;
        size_t offset;
} address_field_t;

static const address_field_t ADDRESS_FIELDS[] = {
        {"cep", offsetof(cepis_address_t, cep)},
        {"logradouro", offsetof(cepis_address_t, logradouro)},
        {"complemento", offsetof(cepis_address_t, complemento)},
        {"unidade", offsetof(cepis_address_t, unidade)},
        {"bairro", offsetof(cepis_address_t, bairro)},
        {"localidade", offsetof(cepis_address_t, localidade)},
        {"uf", offsetof(cepis_address_t, uf)},
        {"estado", offsetof(cepis_address_t, estado)},
        {"regiao", offsetof(cepis_address_t, regiao)},
        {"ibge", offsetof(cepis_address_t, ibge)},
        {"gia", offsetof(cepis_address_t, gia)},
        {"ddd", offsetof(cepis_address_t, ddd)},
        {"siafi", offsetof(cepis_address_t, siafi)},
};

#define ADDRESS_FIELD_COUNT (sizeof(ADDRESS_FIELDS) / sizeof(ADDRESS_FIELDS[0]))

static char **field_slot(cepis_address_t *address, size_t index)
{
        return (char **)((char *)address + ADDRESS_FIELDS[index].offset);
}

static cepis_address_t *address_new(void)
{
        cepis_address_t *address = calloc(1, sizeof(*address));
        size_t i;

        if (address == NULL)
                return NULL;
        for (i = 0; i < ADDRESS_FIELD_COUNT; i++) {
                char **slot = field_slot(address, i);
                *slot = cepis_strdup("");
                if (*slot == NULL) {
                        cepis_address_free(address);
                        return NULL;
                }
        }
        return address;
}

static bool set_field(cepis_address_t *address, size_t index, const char *value)
{
        char **slot = field_slot(address, index);
        char *copy = cepis_strdup(value);

        if (copy == NULL)
                return false;
        free(*slot);
        *slot = copy;
        return true;
}

void cepis_address_free(cepis_address_t *address)
{
        size_t i;

        if (address == NULL)
                return;
        for (i = 0; i < ADDRESS_FIELD_COUNT; i++)
                free(*field_slot(address, i));
        if (address->geo != NULL) {
                free(address->geo->precisao);
                free(address->geo);
        }
        free(address->raw);
        free(address);
}

void cepis_address_list_free(cepis_address_list_t *list)
{
        size_t i;

        if (list == NULL)
                return;
        for (i = 0; i < list->count; i++)
                cepis_address_free(list->items[i]);
        free(list->items);
        list->items = NULL;
        list->count = 0;
}

void cepis_health_free(cepis_health_t *health)
{
        if (health == NULL)
                return;
        free(health->status);
        free(health->dne);
        free(health->base_gerada_em);
        free(health);
}

bool cepis_geo_is_precise(const cepis_geo_t *geo)
{
        return geo != NULL && geo->precisao != NULL &&
               strcmp(geo->precisao, "logradouro") == 0;
}

bool cepis_health_ok(const cepis_health_t *health)
{
        return health != NULL && health->status != NULL &&
               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;
        cepis_geo_t *geo;
        double lat;
        double lon;
        long long raio;

        if (block == NULL || block->type != JSON_OBJECT)
                return NULL;
        if (!json_as_double(json_object_get(block, "lat"), &lat))
                return NULL;
        if (!json_as_double(json_object_get(block, "lon"), &lon))
                return NULL;
        if (!json_as_llong(json_object_get(block, "raio_m"), &raio))
                return NULL;
        precisao = json_as_string(json_object_get(block, "precisao"));
        if (precisao == NULL)
                return NULL;

        geo = calloc(1, sizeof(*geo));
        if (geo == NULL)
                return NULL;
        geo->lat = lat;
        geo->lon = lon;
        geo->raio_m = (long)raio;
        geo->precisao = cepis_strdup(precisao);
        if (geo->precisao == NULL) {
                free(geo);
                return NULL;
        }
        return geo;
}

cepis_address_t *cepis_address_from_json(const json_value_t *object,
                                         const char *raw, size_t raw_len)
{
        cepis_address_t *address;
        size_t i;

        if (object == NULL || object->type != JSON_OBJECT)
                return NULL;
        address = address_new();
        if (address == NULL)
                return NULL;

        for (i = 0; i < ADDRESS_FIELD_COUNT; i++) {
                const char *value = json_as_string(
                        json_object_get(object, ADDRESS_FIELDS[i].name));
                if (value == NULL)
                        continue;
                if (!set_field(address, i, value)) {
                        cepis_address_free(address);
                        return NULL;
                }
        }

        address->geo = geo_from_json(json_object_get(object, "geo"));

        if (raw != NULL) {
                address->raw = malloc(raw_len + 1);
                if (address->raw == NULL) {
                        cepis_address_free(address);
                        return NULL;
                }
                memcpy(address->raw, raw, raw_len);
                address->raw[raw_len] = '\0';
        }
        return address;
}

cepis_address_t *cepis_address_from_csv(const csv_row_t *header,
                                        const csv_row_t *record)
{
        cepis_address_t *address;
        size_t column;

        if (header == NULL || record == NULL)
                return NULL;
        address = address_new();
        if (address == NULL)
                return NULL;

        for (column = 0; column < header->count; column++) {
                size_t i;
                if (column >= record->count)
                        break;
                for (i = 0; i < ADDRESS_FIELD_COUNT; i++) {
                        if (strcmp(header->fields[column],
                                   ADDRESS_FIELDS[i].name) != 0)
                                continue;
                        if (!set_field(address, i, record->fields[column])) {
                                cepis_address_free(address);
                                return NULL;
                        }
                        break;
                }
        }
        return address;
}

cepis_health_t *cepis_health_from_json(const json_value_t *object)
{
        cepis_health_t *health;
        const char *text;

        if (object == NULL || object->type != JSON_OBJECT)
                return NULL;
        health = calloc(1, sizeof(*health));
        if (health == NULL)
                return NULL;

        text = json_as_string(json_object_get(object, "status"));
        health->status = cepis_strdup(text);
        text = json_as_string(json_object_get(object, "dne"));
        health->dne = cepis_strdup(text);
        text = json_as_string(json_object_get(object, "base_gerada_em"));
        health->base_gerada_em = cepis_strdup(text);

        if (health->status == NULL || health->dne == NULL ||
            health->base_gerada_em == NULL) {
                cepis_health_free(health);
                return NULL;
        }

        if (!json_as_llong(json_object_get(object, "ceps"), &health->ceps))
                health->ceps = 0;
        if (!json_as_llong(json_object_get(object, "geo"), &health->geo))
                health->geo = 0;
        return health;
}