summaryrefslogtreecommitdiffstats
path: root/libnetdata/line_splitter/line_splitter.c
blob: a459d3347b9556557d87704e67a0d3e9a11da643 (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
// SPDX-License-Identifier: GPL-3.0-or-later

#include "../libnetdata.h"


bool line_splitter_reconstruct_line(BUFFER *wb, void *ptr) {
    struct line_splitter *spl = ptr;
    if(!spl) return false;

    size_t added = 0;
    for(size_t i = 0; i < spl->num_words ;i++) {
        if(i) buffer_fast_strcat(wb, " ", 1);

        buffer_fast_strcat(wb, "'", 1);
        const char *s = get_word(spl->words, spl->num_words, i);
        buffer_strcat(wb, s?s:"");
        buffer_fast_strcat(wb, "'", 1);
        added++;
    }

    return added > 0;
}

inline int pluginsd_isspace(char c) {
    switch(c) {
        case ' ':
        case '\t':
        case '\r':
        case '\n':
        case '=':
            return 1;

        default:
            return 0;
    }
}

inline int config_isspace(char c) {
    switch (c) {
        case ' ':
        case '\t':
        case '\r':
        case '\n':
        case ',':
            return 1;

        default:
            return 0;
    }
}

inline int group_by_label_isspace(char c) {
    if(c == ',' || c == '|')
        return 1;

    return 0;
}

bool isspace_map_pluginsd[256] = {};
bool isspace_map_config[256] = {};
bool isspace_map_group_by_label[256] = {};

__attribute__((constructor)) void initialize_is_space_arrays(void) {
    for(int c = 0; c < 256 ; c++) {
        isspace_map_pluginsd[c] = pluginsd_isspace((char) c);
        isspace_map_config[c] = config_isspace((char) c);
        isspace_map_group_by_label[c] = group_by_label_isspace((char) c);
    }
}