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

ascii-boxes / boxes / 6518013212

14 Oct 2023 01:37PM UTC coverage: 81.211% (-0.4%) from 81.608%
6518013212

push

github

tsjensen
remove

2349 of 3210 branches covered (0.0%)

Branch coverage included in aggregate %.

1 of 1 new or added line in 1 file covered. (100.0%)

3767 of 4321 relevant lines covered (87.18%)

7801.63 hits per line

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

55.61
/src/shape.c
1
/*
2
 * boxes - Command line filter to draw/remove ASCII boxes around text
3
 * Copyright (c) 1999-2023 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
 * Shape handling and information functions
18
 */
19

20
#include "config.h"
21

22
#include <stdlib.h>
23
#include <stdio.h>
24
#include <stdarg.h>
25
#include <string.h>
26

27
#include "bxstring.h"
28
#include "shape.h"
29
#include "boxes.h"
30
#include "tools.h"
31

32

33

34
char *shape_name[] = {
35
        "NW", "NNW", "N", "NNE", "NE", "ENE", "E", "ESE",
36
        "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW"
37
};
38

39
shape_t north_side[SHAPES_PER_SIDE] = {NW, NNW, N, NNE, NE};  /* clockwise */
40

41
shape_t east_side[SHAPES_PER_SIDE] = {NE, ENE, E, ESE, SE};
42

43
shape_t south_side[SHAPES_PER_SIDE] = {SE, SSE, S, SSW, SW};
44
shape_t south_side_rev[SHAPES_PER_SIDE] = {SW, SSW, S, SSE, SE};
45

46
shape_t west_side[SHAPES_PER_SIDE] = {SW, WSW, W, WNW, NW};
47

48
shape_t corners[NUM_CORNERS] = {NW, NE, SE, SW};
49

50
shape_t *sides[] = {north_side, east_side, south_side, west_side};
51

52

53

54
shape_t findshape(const sentry_t *sarr, const int num)
765✔
55
/*
56
 *  Find a non-empty shape and return its name
57
 *
58
 *      sarr    the shape array to check
59
 *      num     number of entries in sarr to be checked
60
 *
61
 *  RETURNS: a shape_name  on success
62
 *           num           on error (e.g. empty shape array)
63
 *
64
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
65
 */
66
{
67
    int i;
68

69
    for (i = 0; i < num; ++i) {
4,004✔
70
        if (isempty(sarr + i)) {
3,687✔
71
            continue;
3,239✔
72
        } else {
73
            break;
448✔
74
        }
75
    }
76

77
    return (shape_t) i;
765✔
78
}
79

80

81

82
int on_side(const shape_t s, const int idx)
325✔
83
/*
84
 *  Compute the side that shape s is on.
85
 *
86
 *      s    shape to look for
87
 *      idx  which occurence to return (0 == first, 1 == second (for corners)
88
 *
89
 *  RETURNS: side number (BTOP etc.)  on success
90
 *           NUM_SIDES                on error (e.g. idx==1 && s no corner)
91
 *
92
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
93
 */
94
{
95
    int side;
96
    int i;
97
    int found = 0;
325✔
98

99
    for (side = 0; side < NUM_SIDES; ++side) {
624!
100
        for (i = 0; i < SHAPES_PER_SIDE; ++i) {
2,333✔
101
            if (sides[side][i] == s) {
2,034✔
102
                if (found == idx) {
325!
103
                    return side;
325✔
104
                } else {
105
                    ++found;
×
106
                }
107
            }
108
        }
109
    }
110

111
    return NUM_SIDES;
×
112
}
113

114

115

116
int genshape(const size_t width, const size_t height, char ***chars, bxstr_t ***mbcs)
793✔
117
/*
118
 *  Generate a shape consisting of spaces only.
119
 *
120
 *      width   desired shape width
121
 *      height  desired shape height
122
 *      chars   pointer to the shape lines (should be NULL upon call)
123
 *      mbcs    pointer to the shape lines, MBCS version (should be NULL upon call)
124
 *
125
 *  Memory is allocated for the shape lines which must be freed by the caller.
126
 *
127
 *  RETURNS:  == 0   on success (memory allocated)
128
 *            != 0   on error   (no memory allocated)
129
 *
130
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
131
 */
132
{
133
    size_t j;
134

135
    if (width <= 0 || height <= 0 || width > LINE_MAX_BYTES) {
793!
136
        fprintf(stderr, "%s: internal error\n", PROJECT);
×
137
        return 1;
×
138
    }
139

140
    *chars = (char **) calloc(height, sizeof(char *));
793✔
141
    if (*chars == NULL) {
793!
142
        perror(PROJECT);
×
143
        return 2;
×
144
    }
145

146
    *mbcs = (bxstr_t **) calloc(height, sizeof(bxstr_t *));
793✔
147
    if (*mbcs == NULL) {
793!
148
        BFREE(*chars);
×
149
        perror(PROJECT);
×
150
        return 4;
×
151
    }
152

153
    for (j = 0; j < height; ++j) {
1,586✔
154
        (*chars)[j] = nspaces(width);
793✔
155
        (*mbcs)[j] = bxs_from_ascii((*chars)[j]);
793✔
156
    }
157

158
    return 0;
793✔
159
}
160

161

162

163
void freeshape(sentry_t *shape)
×
164
/*
165
 *  Free all memory allocated by the shape and set the struct to
166
 *  SENTRY_INITIALIZER. Do not free memory of the struct.
167
 *
168
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
169
 */
170
{
171
    size_t j;
172

173
    for (j = 0; j < shape->height; ++j) {
×
174
        BFREE (shape->chars[j]);
×
175
        bxs_free(shape->mbcs[j]);
×
176
    }
177
    BFREE (shape->chars);
×
178
    BFREE (shape->mbcs);
×
179

180
    *shape = SENTRY_INITIALIZER;
×
181
}
×
182

183

184

185
int isempty(const sentry_t *shape)
64,942✔
186
/*
187
 *  Return true if shape is empty.
188
 *
189
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
190
 */
191
{
192
    if (shape == NULL) {
64,942!
193
        return 1;
×
194
    } else if (shape->chars == NULL || shape->mbcs == NULL) {
64,942!
195
        return 1;
31,640✔
196
    } else if (shape->width == 0 || shape->height == 0) {
33,302!
197
        return 1;
×
198
    } else {
199
        return 0;
33,302✔
200
    }
201
}
202

203

204

205
int isdeepempty(const sentry_t *shape)
3,930✔
206
/*
207
 *  Return true if shape is empty, also checking if lines consist of whitespace
208
 *  only.
209
 *
210
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
211
 */
212
{
213
    size_t j;
214

215
    if (isempty(shape)) {
3,930✔
216
        return 1;
302✔
217
    }
218

219
    for (j = 0; j < shape->height; ++j) {
4,983✔
220
        if (shape->chars[j]) {
4,570!
221
            if (strspn(shape->chars[j], " \t") != shape->width) {
4,570✔
222
                return 0;
3,215✔
223
            }
224
        }
225
    }
226

227
    return 1;
413✔
228
}
229

230

231

232
size_t highest(const sentry_t *sarr, const int n, ...)
228✔
233
/*
234
 *  Return height (vert.) of highest shape in given list.
235
 *
236
 *  sarr         array of shapes to examine
237
 *  n            number of shapes following
238
 *  ...          the shapes to consider
239
 *
240
 *  RETURNS:     height in lines (may be zero)
241
 *
242
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
243
 */
244
{
245
    va_list ap;
246
    int i;
247
    size_t max = 0;                      /* current maximum height */
228✔
248

249
    #if defined(DEBUG) && 0
250
    fprintf (stderr, "highest (%d, ...)\n", n);
251
    #endif
252

253
    va_start (ap, n);
228✔
254

255
    for (i = 0; i < n; ++i) {
1,368✔
256
        shape_t r = va_arg (ap, shape_t);
1,140✔
257
        if (!isempty(sarr + r)) {
1,140✔
258
            if (sarr[r].height > max) {
716✔
259
                max = sarr[r].height;
228✔
260
            }
261
        }
262
    }
263

264
    va_end (ap);
228✔
265

266
    return max;
228✔
267
}
268

269

270

271
size_t widest(const sentry_t *sarr, const int n, ...)
228✔
272
/*
273
 *  Return width (horiz.) of widest shape in given list.
274
 *
275
 *  sarr         array of shapes to examine
276
 *  n            number of shapes following
277
 *  ...          the shapes to consider
278
 *
279
 *  RETURNS:     width in chars (may be zero)
280
 *
281
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
282
 */
283
{
284
    va_list ap;
285
    int i;
286
    size_t max = 0;                      /* current maximum width */
228✔
287

288
    #if defined(DEBUG) && 0
289
    fprintf (stderr, "widest (%d, ...)\n", n);
290
    #endif
291

292
    va_start (ap, n);
228✔
293

294
    for (i = 0; i < n; ++i) {
1,368✔
295
        shape_t r = va_arg (ap, shape_t);
1,140✔
296
        if (!isempty(sarr + r)) {
1,140✔
297
            if (sarr[r].width > max) {
704✔
298
                max = sarr[r].width;
228✔
299
            }
300
        }
301
    }
302

303
    va_end (ap);
228✔
304

305
    return max;
228✔
306
}
307

308

309

310
shape_t leftmost(const int aside, const int cnt)
×
311
/*
312
 *  Return leftmost existing shape in specification for side aside
313
 *  (BTOP or BBOT), skipping cnt shapes. Corners are not considered.
314
 *
315
 *  RETURNS:    shape       if shape was found
316
 *              NUM_SHAPES  on error (e.g. cnt too high)
317
 *
318
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
319
 */
320
{
321
    int c = 0;
×
322
    int s;
323

324
    if (cnt < 0) {
×
325
        return NUM_SHAPES;
×
326
    }
327

328
    if (aside == BTOP) {
×
329
        s = 0;
×
330
        do {
331
            ++s;
×
332
            while (s < SHAPES_PER_SIDE - 1 &&
×
333
                    isempty(opt.design->shape + north_side[s])) {
×
334
                        ++s;
×
335
            }
336
            if (s == SHAPES_PER_SIDE - 1) {
×
337
                return NUM_SHAPES;
×
338
            }
339
        } while (c++ < cnt);
×
340
        return north_side[s];
×
341
    }
342

343
    else if (aside == BBOT) {
×
344
        s = SHAPES_PER_SIDE - 1;
×
345
        do {
346
            --s;
×
347
            while (s && isempty(opt.design->shape + south_side[s])) {
×
348
                --s;
×
349
            }
350
            if (!s) {
×
351
                return NUM_SHAPES;
×
352
            }
353
        } while (c++ < cnt);
×
354
        return south_side[s];
×
355
    }
356

357
    return NUM_SHAPES;
×
358
}
359

360

361

362
/**
363
 * Return true if the shapes on the given side consist entirely of spaces - and spaces only, tabs are considered
364
 * non-empty.
365
 *
366
 * @param sarr pointer to shape list of design to check
367
 * @param aside the box side (one of `BTOP` etc.)
368
 * @return == 0: side is not empty;
369
 *         \!= 0: side is empty
370
 */
371
int empty_side(sentry_t *sarr, const int aside)
1,149✔
372
{
373
    int i;
374

375
    for (i = 0; i < SHAPES_PER_SIDE; ++i) {
1,721✔
376
        if (isdeepempty(sarr + sides[aside][i])) {
1,627✔
377
            continue;
572✔
378
        } else {
379
            return 0;
1,055✔
380
        }                    /* side is not empty */
381
    }
382

383
    return 1;                            /* side is empty */
94✔
384
}
385

386

387

388
/* 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

© 2025 Coveralls, Inc