Coverage Report

Created: 2025-07-12 06:34

/src/h2o/deps/yoml/yoml.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2014 DeNA Co., Ltd.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to
6
 * deal in the Software without restriction, including without limitation the
7
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8
 * sell copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in
12
 * all copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
 * IN THE SOFTWARE.
21
 */
22
#ifndef yoml_h
23
#define yoml_h
24
25
#ifdef __cplusplus
26
extern "C" {
27
#endif
28
29
#include <assert.h>
30
#include <stdlib.h>
31
#include <string.h>
32
33
typedef enum enum_yoml_type_t { YOML_TYPE_SCALAR, YOML_TYPE_SEQUENCE, YOML_TYPE_MAPPING, YOML__TYPE_UNRESOLVED_ALIAS } yoml_type_t;
34
35
typedef struct st_yoml_t yoml_t;
36
37
typedef struct st_yoml_sequence_t {
38
    size_t size;
39
    yoml_t *elements[];
40
} yoml_sequence_t;
41
42
typedef struct st_yoml_mapping_element_t {
43
    yoml_t *key;
44
    yoml_t *value;
45
} yoml_mapping_element_t;
46
47
typedef struct st_yoml_mapping_t {
48
    size_t size;
49
    yoml_mapping_element_t elements[];
50
} yoml_mapping_t;
51
52
struct st_yoml_t {
53
    yoml_type_t type;
54
    char *filename;
55
    size_t line;
56
    size_t column;
57
    char *anchor;
58
    char *tag;
59
    size_t _refcnt;
60
    unsigned _merged : 1;
61
    union {
62
        char *scalar;
63
        yoml_sequence_t sequence;
64
        yoml_mapping_t mapping;
65
        char *alias;
66
    } data;
67
};
68
69
static inline void yoml_free(yoml_t *node, void *(*mem_set)(void *, int, size_t))
70
0
{
71
0
    size_t i;
72
73
0
    if (node == NULL)
74
0
        return;
75
76
0
    assert(node->_refcnt > 0);
77
0
    if (--node->_refcnt == 0) {
78
0
        free(node->filename);
79
0
        free(node->anchor);
80
0
        free(node->tag);
81
0
        switch (node->type) {
82
0
        case YOML_TYPE_SCALAR:
83
0
            if (mem_set != NULL)
84
0
                mem_set(node->data.scalar, 0, strlen(node->data.scalar));
85
0
            free(node->data.scalar);
86
0
            break;
87
0
        case YOML_TYPE_SEQUENCE:
88
0
            for (i = 0; i != node->data.sequence.size; ++i) {
89
0
                yoml_free(node->data.sequence.elements[i], mem_set);
90
0
            }
91
0
            break;
92
0
        case YOML_TYPE_MAPPING:
93
0
            for (i = 0; i != node->data.mapping.size; ++i) {
94
0
                yoml_free(node->data.mapping.elements[i].key, mem_set);
95
0
                yoml_free(node->data.mapping.elements[i].value, mem_set);
96
0
            }
97
0
            break;
98
0
        case YOML__TYPE_UNRESOLVED_ALIAS:
99
0
            free(node->data.alias);
100
0
            break;
101
0
        }
102
0
        free(node);
103
0
    }
104
0
}
Unexecuted instantiation: config.c:yoml_free
Unexecuted instantiation: configurator.c:yoml_free
Unexecuted instantiation: headers_util.c:yoml_free
105
106
static inline yoml_t *yoml_find_anchor(yoml_t *node, const char *name)
107
0
{
108
0
    yoml_t *n;
109
0
    size_t i;
110
0
111
0
    if (node->anchor != NULL && strcmp(node->anchor, name) == 0)
112
0
        return node;
113
0
114
0
    switch (node->type) {
115
0
    case YOML_TYPE_SEQUENCE:
116
0
        for (i = 0; i != node->data.sequence.size; ++i)
117
0
            if ((n = yoml_find_anchor(node->data.sequence.elements[i], name)) != NULL)
118
0
                return n;
119
0
        break;
120
0
    case YOML_TYPE_MAPPING:
121
0
        for (i = 0; i != node->data.mapping.size; ++i)
122
0
            if ((n = yoml_find_anchor(node->data.mapping.elements[i].key, name)) != NULL ||
123
0
                (n = yoml_find_anchor(node->data.mapping.elements[i].value, name)) != NULL)
124
0
                return n;
125
0
        break;
126
0
    default:
127
0
        break;
128
0
    }
129
0
130
0
    return NULL;
131
0
}
Unexecuted instantiation: config.c:yoml_find_anchor
Unexecuted instantiation: configurator.c:yoml_find_anchor
Unexecuted instantiation: headers_util.c:yoml_find_anchor
132
133
static inline yoml_t *yoml_get(yoml_t *node, const char *name)
134
0
{
135
0
    size_t i;
136
137
0
    if (node->type != YOML_TYPE_MAPPING)
138
0
        return NULL;
139
0
    for (i = 0; i != node->data.mapping.size; ++i) {
140
0
        yoml_t *key = node->data.mapping.elements[i].key;
141
0
        if (key->type == YOML_TYPE_SCALAR && strcmp(key->data.scalar, name) == 0)
142
0
            return node->data.mapping.elements[i].value;
143
0
    }
144
0
    return NULL;
145
0
}
Unexecuted instantiation: config.c:yoml_get
Unexecuted instantiation: configurator.c:yoml_get
Unexecuted instantiation: headers_util.c:yoml_get
146
147
#ifdef __cplusplus
148
}
149
#endif
150
151
#endif