• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ascii-boxes / boxes / 11084497071

28 Sep 2024 01:00PM UTC coverage: 87.312% (-1.6%) from 88.939%
11084497071

push

github

tsjensen
Remove unused function array_contains() from 'tools' module

3132 of 3809 branches covered (82.23%)

Branch coverage included in aggregate %.

5091 of 5609 relevant lines covered (90.76%)

175335.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.82
/src/query.c
1
/*
2
 * boxes - Command line filter to draw/remove ASCII boxes around text
3
 * Copyright (c) 1999-2024 Thomas Jensen and the boxes contributors
4
 *
5
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
6
 * License, version 3, as published by the Free Software Foundation.
7
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
8
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
9
 * details.
10
 * You should have received a copy of the GNU General Public License along with this program.
11
 * If not, see <https://www.gnu.org/licenses/>.
12
 *
13
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
14
 */
15

16
/*
17
 * Functions related to querying the design list by tag.
18
 */
19

20
#include "config.h"
21
#include <stdio.h>
22
#include <string.h>
23
#include <strings.h>
24

25
#include "boxes.h"
26
#include "list.h"
27
#include "logging.h"
28
#include "tools.h"
29
#include "query.h"
30

31

32

33
#define QUERY_ALL "(all)"
34

35

36

37
static int validate_tag(char *tag)
38✔
38
{
39
    if (strcmp(tag, QUERY_ALL) == 0) {
38✔
40
        return 1;
4✔
41
    }
42
    return tag_is_valid(tag);
34✔
43
}
19✔
44

45

46

47
char **parse_query(char *optarg)
26✔
48
{
49
    /* CAUTION: This function cannot use `opt`, because it is involved in its construction. */
50

51
    char **query = NULL;
26✔
52
    char *dup = strdup(optarg);   /* required because strtok() modifies its input */
26✔
53

54
    int contains_positive_element = 0;
26✔
55
    size_t num_expr = 0;
26✔
56
    for (char *q = strtok(dup, ","); q != NULL; q = strtok(NULL, ","))
58✔
57
    {
58
        char *trimmed = trimdup(q, q + strlen(q) - 1);
38✔
59
        if (strlen(trimmed) == 0) {
38✔
60
            BFREE(trimmed);
×
61
            continue;
×
62
        }
63

64
        if (trimmed[0] != '-') {
38✔
65
            contains_positive_element = 1;
26✔
66
        }
13✔
67

68
        char *raw_tag = (trimmed[0] == '+' || trimmed[0] == '-') ? (trimmed + 1) : trimmed;
38✔
69
        if (!validate_tag(raw_tag)) {
38✔
70
            fprintf(stderr, "%s: not a tag -- %s\n", PROJECT, raw_tag);
2✔
71
            BFREE(trimmed);
2✔
72
            BFREE(query);
2✔
73
            return NULL;
2✔
74
        }
75
        if (query != NULL) {
36✔
76
            for (size_t i = 0; query[i] != NULL; ++i) {
24✔
77
                char *restag = (query[i][0] == '+' || query[i][0] == '-') ? (query[i] + 1) : query[i];
14✔
78
                if (strcasecmp(restag, raw_tag) == 0) {
14✔
79
                    fprintf(stderr, "%s: duplicate query expression -- %s\n", PROJECT, trimmed);
4✔
80
                    BFREE(trimmed);
4✔
81
                    BFREE(query);
4✔
82
                    return NULL;
4✔
83
                }
84
            }
5✔
85
        }
5✔
86

87
        ++num_expr;
32✔
88
        query = (char **) realloc(query, (num_expr + 1) * sizeof(char *));
32✔
89
        if (query == NULL) {
32✔
90
            perror(PROJECT);
×
91
            break;
×
92
        }
93
        query[num_expr - 1] = trimmed;
32✔
94
        query[num_expr] = NULL;
32✔
95
    }
16✔
96
    BFREE(dup);
20✔
97

98
    if (num_expr == 0) {
20✔
99
        fprintf(stderr, "%s: empty tag query -- %s\n", PROJECT, optarg);
2✔
100
        return NULL;
2✔
101
    }
102

103
    if (!contains_positive_element) {
18✔
104
        ++num_expr;
4✔
105
        query = (char **) realloc(query, (num_expr + 1) * sizeof(char *));
4✔
106
        if (query == NULL) {
4!
107
            perror(PROJECT);
×
108
        }
109
        else {
110
            query[num_expr - 1] = QUERY_ALL;
4✔
111
            query[num_expr] = NULL;
4✔
112
        }
113
    }
2✔
114

115
    return query;
18✔
116
}
13✔
117

118

119

120
static int filter_by_tag(char **tags)
208✔
121
{
122
    if (is_debug_logging(MAIN)) {
208!
123
        log_debug(__FILE__, MAIN, "filter_by_tag(");
×
124
        for (size_t tidx = 0; tags[tidx] != NULL; ++tidx) {
×
125
            log_debug_cont(MAIN, "%s%s", tidx > 0 ? ", " : "", tags[tidx]);
×
126
        }
127
    }
128

129
    int result = array_contains0(opt.query, QUERY_ALL);
208✔
130
    if (opt.query != NULL) {
208!
131
        for (size_t qidx = 0; opt.query[qidx] != NULL; ++qidx) {
422✔
132
            if (opt.query[qidx][0] == '+') {
238✔
133
                result = array_contains0(tags, opt.query[qidx] + 1);
20✔
134
                if (!result) {
20✔
135
                    break;
10✔
136
                }
137
            }
5✔
138
            else if (opt.query[qidx][0] == '-') {
218✔
139
                if (array_contains0(tags, opt.query[qidx] + 1)) {
28✔
140
                    result = 0;
14✔
141
                    break;
14✔
142
                }
143
            }
7✔
144
            else if (array_contains0(tags, opt.query[qidx])) {
190✔
145
                result = 1;
16✔
146
            }
8✔
147
        }
107✔
148
    }
104✔
149

150
    log_debug_cont(MAIN, ") -> %d\n", result);
208✔
151
    return result;
208✔
152
}
153

154

155

156
int query_by_tag()
18✔
157
{
158
    design_t **list = sort_designs_by_name();                 /* temp list for sorting */
18✔
159
    if (list == NULL) {
18!
160
        return 1;
×
161
    }
162
    for (int i = 0; i < num_designs; ++i) {
226✔
163
        if (filter_by_tag(list[i]->tags)) {
208✔
164
            fprintf(opt.outfile, "%s%s", list[i]->name, opt.eol);
172✔
165
            for (size_t aidx = 0; list[i]->aliases[aidx] != NULL; ++aidx) {
204✔
166
                fprintf(opt.outfile, "%s (alias)%s", list[i]->aliases[aidx], opt.eol);
32✔
167
            }
16✔
168
        }
86✔
169
    }
104✔
170
    BFREE(list);
18✔
171
    return 0;
18✔
172
}
9✔
173

174

175
/* vim: set sw=4: */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc