aboutsummaryrefslogtreecommitdiff
path: root/src/format.c
blob: 2678536aa3f9c6fcd4ac1755c5bb7c0f0c02d8a4 (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
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

#include "format.h"
#include "any_log.h"
#include "util.h"

#define format_grow() \
    do { \
        if (n + 1 >= length) { \
            length += 4; \
            parts = realloc(parts, length * sizeof(char *)); \
            assert(parts != NULL); \
            marks = realloc(marks, length * sizeof(bool)); \
            assert(marks != NULL); \
        } \
    } while (0)

bool format_init(format_t *format, const char *string, char delim)
{
    char **parts = NULL;
    uint8_t *marks = NULL;

    size_t length = 0, n = 0;
    size_t start = 0, end = 0;

    while (string[end] != '\0') {
        if (string[end] == delim) {
            // TODO: Escape formatting

            if (string[end + 1] == '{') {
                if (start != end) {
                    format_grow();
                    parts[n] = strslice(string, start, end);
                    marks[n] = false;
                    n++;
                }

                start = end += 2;
                while (string[end] != '}') {
                    if (string[end] == '\0' || (string[end] == '$' && string[end + 1] == '{')) {
                        for (size_t k = 0; k < n; k++)
                            free(parts[k]);

                        free(parts);
                        free(marks);
                        return false;
                    }
                    end++;
                }

                size_t l = end - start;
                size_t next = end--;

                while (start <= end && isspace(string[start])) start++, l--;
                while (start <= end && isspace(string[end])) end--, l--;

                parts[n] = strslice(string, start, start + l);
                marks[n] = true;
                n++;

                start = end = ++next;
                goto next;
            }
        }

        end++;
next:
    }

    if (string[end] != '\0') {
        while (string[end] != '\0') end++;

        format_grow();
        parts[n] = strslice(string, start, end);
        marks[n] = false;
        n++;
    }

    format->parts = parts;
    format->marks = marks;
    format->length = n;

    return true;
}

bool format_remark(format_t *format, const char *label, const format_pair_t *pairs)
{
    int errors = 0;
    for (size_t i = 0; i < format->length; i++) {
        // Skip regular strings
        if (!format->marks[i]) continue;

        for (size_t j = 0; pairs[j].key != NULL; j++) {
            if (!strcmp(pairs[j].key, format->parts[i])) {
                format->marks[i] = pairs[j].mark;
                goto next;
            }
        }

        errors++;
        if (label != NULL)
            log_error("Unknown format option '%s' for %s", format->parts[i], label);
next:
    }

    return errors == 0;
}

void format_free(format_t *format)
{
    for (size_t i = 0; i < format->length; i++)
        free(format->parts[i]);

    free(format->parts);
    free(format->marks);
}