Coverage Report

Created: 2025-07-11 06:33

/src/PROJ/src/param.cpp
Line
Count
Source (jump to first uncovered line)
1
/* put parameters in linked list and retrieve */
2
3
#include <ctype.h>
4
#include <stddef.h>
5
#include <stdio.h>
6
#include <stdlib.h>
7
#include <string.h>
8
9
#include "proj.h"
10
#include "proj_internal.h"
11
12
/* create parameter list entry */
13
3.60M
paralist *pj_mkparam(const char *str) {
14
3.60M
    paralist *newitem;
15
16
3.60M
    if ((newitem = (paralist *)malloc(sizeof(paralist) + strlen(str))) !=
17
3.60M
        nullptr) {
18
3.60M
        newitem->used = 0;
19
3.60M
        newitem->next = nullptr;
20
3.60M
        if (*str == '+')
21
1.24k
            ++str;
22
3.60M
        (void)strcpy(newitem->param, str);
23
3.60M
    }
24
3.60M
    return newitem;
25
3.60M
}
26
27
/* As pj_mkparam, but payload ends at first whitespace, rather than at end of
28
 * <str> */
29
1.63k
paralist *pj_mkparam_ws(const char *str, const char **next_str) {
30
1.63k
    paralist *newitem;
31
1.63k
    size_t len = 0;
32
33
1.63k
    if (nullptr == str)
34
0
        return nullptr;
35
36
    /* Find start and length of string */
37
2.97k
    while (isspace(*str))
38
1.34k
        str++;
39
1.63k
    if (*str == '+')
40
0
        str++;
41
1.63k
    bool in_string = false;
42
24.3k
    for (; str[len] != '\0'; len++) {
43
24.0k
        if (in_string) {
44
0
            if (str[len] == '"' && str[len + 1] == '"') {
45
0
                len++;
46
0
            } else if (str[len] == '"') {
47
0
                in_string = false;
48
0
            }
49
24.0k
        } else if (str[len] == '=' && str[len + 1] == '"') {
50
0
            in_string = true;
51
24.0k
        } else if (isspace(str[len])) {
52
1.34k
            break;
53
1.34k
        }
54
24.0k
    }
55
56
1.63k
    if (next_str)
57
1.63k
        *next_str = str + len;
58
59
    /* Use calloc to automagically 0-terminate the copy */
60
1.63k
    newitem = (paralist *)calloc(1, sizeof(paralist) + len + 1);
61
1.63k
    if (nullptr == newitem)
62
0
        return nullptr;
63
1.63k
    memcpy(newitem->param, str, len);
64
65
1.63k
    newitem->used = 0;
66
1.63k
    newitem->next = nullptr;
67
68
1.63k
    return newitem;
69
1.63k
}
70
71
/**************************************************************************************/
72
9.76M
paralist *pj_param_exists(paralist *list, const char *parameter) {
73
    /***************************************************************************************
74
        Determine whether a given parameter exists in a paralist. If it does,
75
    return a pointer to the corresponding list element - otherwise return 0.
76
77
        In support of the pipeline syntax, the search is terminated once a
78
    "+step" list element is reached, in which case a 0 is returned, unless the
79
    parameter searched for is actually "step", in which case a pointer to the
80
    "step" list element is returned.
81
82
        This function is equivalent to the pj_param (...) call with the "opt"
83
    argument set to the parameter name preceeeded by a 't'. But by using this
84
    one, one avoids writing the code allocating memory for a new copy of
85
    parameter name, and prepending the t (for compile time known names, this is
86
    obviously not an issue).
87
    ***************************************************************************************/
88
9.76M
    paralist *next = list;
89
9.76M
    const char *c = strchr(parameter, '=');
90
9.76M
    size_t len = strlen(parameter);
91
9.76M
    if (c)
92
0
        len = c - parameter;
93
9.76M
    if (list == nullptr)
94
0
        return nullptr;
95
96
101M
    for (next = list; next; next = next->next) {
97
93.3M
        if (0 == strncmp(parameter, next->param, len) &&
98
93.3M
            (next->param[len] == '=' || next->param[len] == 0)) {
99
1.28M
            next->used = 1;
100
1.28M
            return next;
101
1.28M
        }
102
92.0M
        if (0 == strcmp(parameter, "step"))
103
0
            return nullptr;
104
92.0M
    }
105
106
8.47M
    return nullptr;
107
9.76M
}
108
109
/************************************************************************/
110
/*                              pj_param()                              */
111
/*                                                                      */
112
/*      Test for presence or get parameter value.  The first            */
113
/*      character in `opt' is a parameter type which can take the       */
114
/*      values:                                                         */
115
/*                                                                      */
116
/*       `t' - test for presence, return TRUE/FALSE in PROJVALUE.i      */
117
/*       `i' - integer value returned in PROJVALUE.i                    */
118
/*       `d' - simple valued real input returned in PROJVALUE.f         */
119
/*       `r' - degrees (DMS translation applied), returned as           */
120
/*             radians in PROJVALUE.f                                   */
121
/*       `s' - string returned in PROJVALUE.s                           */
122
/*       `b' - test for t/T/f/F, return in PROJVALUE.i                  */
123
/*                                                                      */
124
/*      Search is terminated when "step" is found, in which case        */
125
/*      0 is returned, unless "step" was the target searched for.       */
126
/*                                                                      */
127
/************************************************************************/
128
129
6.48M
PROJVALUE pj_param(PJ_CONTEXT *ctx, paralist *pl, const char *opt) {
130
131
6.48M
    int type;
132
6.48M
    unsigned l;
133
6.48M
    PROJVALUE value = {0};
134
135
6.48M
    if (ctx == nullptr)
136
0
        ctx = pj_get_default_ctx();
137
138
6.48M
    type = *opt++;
139
140
6.48M
    if (nullptr == strchr("tbirds", type)) {
141
0
        fprintf(stderr, "invalid request to pj_param, fatal\n");
142
0
        exit(1);
143
0
    }
144
145
6.48M
    pl = pj_param_exists(pl, opt);
146
6.48M
    if (type == 't') {
147
1.78M
        value.i = pl != nullptr;
148
1.78M
        return value;
149
1.78M
    }
150
151
    /* Not found */
152
4.69M
    if (nullptr == pl) {
153
        /* Return value after the switch, so that the return path is */
154
        /* taken in all cases */
155
4.33M
        switch (type) {
156
727k
        case 'b':
157
727k
        case 'i':
158
727k
            value.i = 0;
159
727k
            break;
160
974k
        case 'd':
161
1.44M
        case 'r':
162
1.44M
            value.f = 0.;
163
1.44M
            break;
164
2.15M
        case 's':
165
2.15M
            value.s = nullptr;
166
2.15M
            break;
167
4.33M
        }
168
4.33M
        return value;
169
4.33M
    }
170
171
    /* Found parameter - now find its value */
172
367k
    pl->used |= 1;
173
367k
    l = (int)strlen(opt);
174
367k
    opt = pl->param + l;
175
367k
    if (*opt == '=')
176
338k
        ++opt;
177
178
367k
    switch (type) {
179
408
    case 'i': /* integer input */
180
408
        value.i = atoi(opt);
181
1.59k
        for (const char *ptr = opt; *ptr != '\0'; ++ptr) {
182
1.18k
            if (!(*ptr >= '0' && *ptr <= '9')) {
183
645
                proj_context_errno_set(ctx,
184
645
                                       PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
185
645
                value.i = 0;
186
645
            }
187
1.18k
        }
188
408
        break;
189
101k
    case 'd': /* simple real input */
190
101k
        value.f = pj_atof(opt);
191
101k
        break;
192
50.5k
    case 'r': /* degrees input */
193
50.5k
        value.f = dmstor_ctx(ctx, opt, nullptr);
194
50.5k
        break;
195
188k
    case 's': /* char string */
196
188k
        value.s = (char *)opt;
197
188k
        break;
198
25.7k
    case 'b': /* boolean */
199
25.7k
        switch (*opt) {
200
5
        case 'F':
201
5
        case 'f':
202
5
            value.i = 0;
203
5
            break;
204
25.5k
        case '\0':
205
25.5k
        case 'T':
206
25.5k
        case 't':
207
25.5k
            value.i = 1;
208
25.5k
            break;
209
121
        default:
210
121
            proj_context_errno_set(ctx, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
211
121
            value.i = 0;
212
121
            break;
213
25.7k
        }
214
25.7k
        break;
215
367k
    }
216
367k
    return value;
217
367k
}