Skip to content

Commit 98698d0

Browse files
committed
Remove SXBP_DEBUG macro
Fixes #116
1 parent 8252018 commit 98698d0

File tree

8 files changed

+36
-69
lines changed

8 files changed

+36
-69
lines changed

saxbospiral/initialise.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ sxbp_status_t sxbp_init_spiral(sxbp_buffer_t buffer, sxbp_spiral_t* spiral) {
7474
spiral->lines = calloc(sizeof(sxbp_line_t), line_count);
7575
// check for memory allocation failure
7676
if(spiral->lines == NULL) {
77-
result.location = SXBP_DEBUG;
78-
result.diagnostic = SXBP_MALLOC_REFUSED;
77+
result = SXBP_MALLOC_REFUSED;
7978
return result;
8079
}
8180
// First line is always an UP line - this is for orientation purposes
@@ -105,7 +104,7 @@ sxbp_status_t sxbp_init_spiral(sxbp_buffer_t buffer, sxbp_spiral_t* spiral) {
105104
}
106105
}
107106
// all ok
108-
result.diagnostic = SXBP_OPERATION_OK;
107+
result = SXBP_OPERATION_OK;
109108
return result;
110109
}
111110

saxbospiral/plot.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,15 @@ sxbp_status_t sxbp_spiral_points(
7676
assert(end <= spiral.size);
7777
assert(spiral.lines != NULL);
7878
// prepare result status
79-
sxbp_status_t result = {{0, 0, 0}, 0};
79+
sxbp_status_t result;
8080
// the amount of space needed is the sum of all line lengths + 1 for end
8181
size_t size = sxbp_sum_lines(spiral, start, end) + 1;
8282
// allocate memory
8383
output->items = calloc(sizeof(sxbp_co_ord_t), size);
8484
// catch malloc error
8585
if(output->items == NULL) {
8686
// set error information then early return
87-
result.location = SXBP_DEBUG;
88-
result.diagnostic = SXBP_MALLOC_REFUSED;
87+
result = SXBP_MALLOC_REFUSED;
8988
return result;
9089
}
9190
output->size = size;
@@ -107,7 +106,7 @@ sxbp_status_t sxbp_spiral_points(
107106
}
108107
}
109108
// all good
110-
result.diagnostic = SXBP_OPERATION_OK;
109+
result = SXBP_OPERATION_OK;
111110
return result;
112111
}
113112

@@ -129,7 +128,7 @@ sxbp_status_t sxbp_cache_spiral_points(sxbp_spiral_t* spiral, size_t limit) {
129128
assert(spiral->lines != NULL);
130129
assert(limit <= spiral->size);
131130
// prepare result status
132-
sxbp_status_t result = {{0, 0, 0}, 0};
131+
sxbp_status_t result;
133132
// the amount of space needed is the sum of all line lengths + 1 for end
134133
size_t size = sxbp_sum_lines(*spiral, 0, limit) + 1;
135134
// allocate / reallocate memory
@@ -150,8 +149,7 @@ sxbp_status_t sxbp_cache_spiral_points(sxbp_spiral_t* spiral, size_t limit) {
150149
// catch malloc failure
151150
if(spiral->co_ord_cache.co_ords.items == NULL) {
152151
// set error information then early return
153-
result.location = SXBP_DEBUG;
154-
result.diagnostic = SXBP_MALLOC_REFUSED;
152+
result = SXBP_MALLOC_REFUSED;
155153
return result;
156154
}
157155
spiral->co_ord_cache.co_ords.size = size;
@@ -180,7 +178,7 @@ sxbp_status_t sxbp_cache_spiral_points(sxbp_spiral_t* spiral, size_t limit) {
180178
*spiral, &missing, current, smallest, limit
181179
);
182180
// return errors from previous call if needed
183-
if(calculate_result.diagnostic != SXBP_OPERATION_OK) {
181+
if(calculate_result != SXBP_OPERATION_OK) {
184182
return calculate_result;
185183
}
186184
// add the missing co-ords to the cache
@@ -196,7 +194,7 @@ sxbp_status_t sxbp_cache_spiral_points(sxbp_spiral_t* spiral, size_t limit) {
196194
limit > spiral->co_ord_cache.validity
197195
) ? limit : spiral->co_ord_cache.validity;
198196
// return ok
199-
result.diagnostic = SXBP_OPERATION_OK;
197+
result = SXBP_OPERATION_OK;
200198
return result;
201199
}
202200

saxbospiral/render.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ sxbp_status_t sxbp_render_spiral(sxbp_spiral_t spiral, sxbp_bitmap_t* image) {
8686
assert(image->pixels == NULL);
8787
assert(spiral.lines != NULL);
8888
// create result status struct
89-
sxbp_status_t result = {{0, 0, 0}, 0};
89+
sxbp_status_t result;
9090
// plot co-ords of spiral into it's cache
9191
sxbp_cache_spiral_points(&spiral, spiral.size);
9292
// get the min and max bounds of the spiral's co-ords
@@ -109,8 +109,7 @@ sxbp_status_t sxbp_render_spiral(sxbp_spiral_t spiral, sxbp_bitmap_t* image) {
109109
image->pixels = malloc(image->width * sizeof(bool*));
110110
// check for malloc fail
111111
if(image->pixels == NULL) {
112-
result.location = SXBP_DEBUG;
113-
result.diagnostic = SXBP_MALLOC_REFUSED;
112+
result = SXBP_MALLOC_REFUSED;
114113
return result;
115114
}
116115
for(size_t i = 0; i < image->width; i++) {
@@ -123,8 +122,7 @@ sxbp_status_t sxbp_render_spiral(sxbp_spiral_t spiral, sxbp_bitmap_t* image) {
123122
}
124123
// now we need to free() the top-level array
125124
free(image->pixels);
126-
result.location = SXBP_DEBUG;
127-
result.diagnostic = SXBP_MALLOC_REFUSED;
125+
result = SXBP_MALLOC_REFUSED;
128126
return result;
129127
}
130128
}
@@ -155,7 +153,7 @@ sxbp_status_t sxbp_render_spiral(sxbp_spiral_t spiral, sxbp_bitmap_t* image) {
155153
}
156154
}
157155
// status ok
158-
result.diagnostic = SXBP_OPERATION_OK;
156+
result = SXBP_OPERATION_OK;
159157
return result;
160158
}
161159

saxbospiral/render_backends/png_backend.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ sxbp_status_t sxbp_write_png_image(
9999
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
100100
// catch malloc fail
101101
if(png_ptr == NULL) {
102-
result.location = SXBP_DEBUG;
103-
result.diagnostic = SXBP_MALLOC_REFUSED;
102+
result = SXBP_MALLOC_REFUSED;
104103
// cleanup
105104
cleanup_png_lib(png_ptr, info_ptr, row);
106105
return result;
@@ -109,8 +108,7 @@ sxbp_status_t sxbp_write_png_image(
109108
info_ptr = png_create_info_struct(png_ptr);
110109
// catch malloc fail
111110
if(info_ptr == NULL) {
112-
result.location = SXBP_DEBUG;
113-
result.diagnostic = SXBP_MALLOC_REFUSED;
111+
result = SXBP_MALLOC_REFUSED;
114112
// cleanup
115113
cleanup_png_lib(png_ptr, info_ptr, row);
116114
return result;
@@ -159,8 +157,7 @@ sxbp_status_t sxbp_write_png_image(
159157
row = (png_bytep) malloc(bitmap.width * sizeof(png_byte));
160158
// catch malloc fail
161159
if(row == NULL) {
162-
result.location = SXBP_DEBUG;
163-
result.diagnostic = SXBP_MALLOC_REFUSED;
160+
result = SXBP_MALLOC_REFUSED;
164161
// cleanup
165162
cleanup_png_lib(png_ptr, info_ptr, row);
166163
return result;
@@ -178,7 +175,7 @@ sxbp_status_t sxbp_write_png_image(
178175
// cleanup
179176
cleanup_png_lib(png_ptr, info_ptr, row);
180177
// status ok
181-
result.diagnostic = SXBP_OPERATION_OK;
178+
result = SXBP_OPERATION_OK;
182179
return result;
183180
}
184181

saxbospiral/saxbospiral.h

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,32 +51,13 @@ typedef uint32_t sxbp_version_hash_t;
5151
*/
5252
sxbp_version_hash_t sxbp_version_hash(sxbp_version_t version);
5353

54-
// struct for storing the location of a DEBUG invocation
55-
typedef struct sxbp_debug_t {
56-
size_t line;
57-
char* file;
58-
const char* function;
59-
} sxbp_debug_t;
60-
61-
/*
62-
* handy short-hand for debugging purposes
63-
* usage: debug_t debug = DEBUG;
64-
*/
65-
#define SXBP_DEBUG (sxbp_debug_t) { .line = __LINE__, .file = __FILE__, .function = __func__, }
66-
6754
// enum for function error information
68-
typedef enum sxbp_diagnostic_t {
55+
typedef enum sxbp_status_t {
6956
SXBP_STATE_UNKNOWN = 0, // unknown, the default state
7057
SXBP_OPERATION_FAIL, // generic failure state
7158
SXBP_MALLOC_REFUSED, // memory allocation or re-allocation was refused
7259
SXBP_IMPOSSIBLE_CONDITION, // condition thought to be impossible detected
7360
SXBP_OPERATION_OK, // no problem
74-
} sxbp_diagnostic_t;
75-
76-
// struct for storing generic diagnostics about function failure reasons
77-
typedef struct sxbp_status_t {
78-
sxbp_debug_t location; // for storing location of error
79-
sxbp_diagnostic_t diagnostic; // for storing error information (if any)
8061
} sxbp_status_t;
8162

8263
// type for representing a cartesian direction

saxbospiral/serialise.c

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,13 @@ sxbp_serialise_result_t sxbp_load_spiral(
129129
sxbp_serialise_result_t result; // build struct for returning success / failure
130130
// first, if header is too small for header + 1 line, then return early
131131
if(buffer.size < SXBP_FILE_HEADER_SIZE + SXBP_LINE_T_PACK_SIZE) {
132-
result.status.location = SXBP_DEBUG; // catch location of error
133-
result.status.diagnostic = SXBP_OPERATION_FAIL; // flag failure
132+
result.status = SXBP_OPERATION_FAIL; // flag failure
134133
result.diagnostic = SXBP_DESERIALISE_BAD_HEADER_SIZE; // failure reason
135134
return result;
136135
}
137136
// check for magic number and return early if not right
138137
if(strncmp((char*)buffer.bytes, "SAXBOSPIRAL", 11) != 0) {
139-
result.status.location = SXBP_DEBUG; // catch location of error
140-
result.status.diagnostic = SXBP_OPERATION_FAIL; // flag failure
138+
result.status = SXBP_OPERATION_FAIL; // flag failure
141139
result.diagnostic = SXBP_DESERIALISE_BAD_MAGIC_NUMBER; // failure reason
142140
return result;
143141
}
@@ -152,8 +150,7 @@ sxbp_serialise_result_t sxbp_load_spiral(
152150
// check for version compatibility
153151
if(sxbp_version_hash(buffer_version) < sxbp_version_hash(min_version)) {
154152
// check failed
155-
result.status.location = SXBP_DEBUG; // catch location of error
156-
result.status.diagnostic = SXBP_OPERATION_FAIL; // flag failure
153+
result.status = SXBP_OPERATION_FAIL; // flag failure
157154
result.diagnostic = SXBP_DESERIALISE_BAD_VERSION; // failure reason
158155
return result;
159156
}
@@ -162,8 +159,7 @@ sxbp_serialise_result_t sxbp_load_spiral(
162159
// Check that the file data section is large enough for the spiral size
163160
if((buffer.size - SXBP_FILE_HEADER_SIZE) != (SXBP_LINE_T_PACK_SIZE * spiral_size)) {
164161
// this check failed
165-
result.status.location = SXBP_DEBUG; // catch location of error
166-
result.status.diagnostic = SXBP_OPERATION_FAIL; // flag failure
162+
result.status = SXBP_OPERATION_FAIL; // flag failure
167163
result.diagnostic = SXBP_DESERIALISE_BAD_DATA_SIZE; // failure reason
168164
return result;
169165
}
@@ -176,8 +172,7 @@ sxbp_serialise_result_t sxbp_load_spiral(
176172
spiral->lines = calloc(sizeof(sxbp_line_t), spiral->size);
177173
// catch allocation error
178174
if(spiral->lines == NULL) {
179-
result.status.location = SXBP_DEBUG; // catch location of error
180-
result.status.diagnostic = SXBP_MALLOC_REFUSED; // flag failure
175+
result.status = SXBP_MALLOC_REFUSED; // flag failure
181176
return result;
182177
}
183178
// convert each serialised line segment in buffer into a line_t struct
@@ -203,7 +198,7 @@ sxbp_serialise_result_t sxbp_load_spiral(
203198
}
204199
}
205200
// return ok status
206-
result.status.diagnostic = SXBP_OPERATION_OK;
201+
result.status = SXBP_OPERATION_OK;
207202
return result;
208203
}
209204

@@ -231,8 +226,7 @@ sxbp_serialise_result_t sxbp_dump_spiral(
231226
buffer->bytes = calloc(1, buffer->size);
232227
// catch memory allocation failure
233228
if(buffer->bytes == NULL) {
234-
result.status.location = SXBP_DEBUG;
235-
result.status.diagnostic = SXBP_MALLOC_REFUSED;
229+
result.status = SXBP_MALLOC_REFUSED;
236230
return result;
237231
}
238232
// write first part of data header (magic number and version info)
@@ -267,7 +261,7 @@ sxbp_serialise_result_t sxbp_dump_spiral(
267261
}
268262
}
269263
// return ok status
270-
result.status.diagnostic = SXBP_OPERATION_OK;
264+
result.status = SXBP_OPERATION_OK;
271265
return result;
272266
}
273267

saxbospiral/solve.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ sxbp_status_t sxbp_resize_spiral(
230230
* state of which line is being resized, and what size it should be.
231231
*/
232232
// set result status
233-
sxbp_status_t result = {{0, 0, 0}, 0};
233+
sxbp_status_t result;
234234
size_t current_index = index;
235235
sxbp_length_t current_length = length;
236236
while(true) {
@@ -246,7 +246,7 @@ sxbp_status_t sxbp_resize_spiral(
246246
// update the spiral's co-ord cache, and catch any errors
247247
result = sxbp_cache_spiral_points(spiral, current_index + 1);
248248
// return if errors
249-
if(result.diagnostic != SXBP_OPERATION_OK) {
249+
if(result != SXBP_OPERATION_OK) {
250250
return result;
251251
}
252252
spiral->collides = spiral_collides(spiral, current_index);
@@ -275,7 +275,7 @@ sxbp_status_t sxbp_resize_spiral(
275275
* Return OPERATION_OK from function.
276276
*/
277277
spiral->solved_count = index + 1;
278-
result.diagnostic = SXBP_OPERATION_OK;
278+
result = SXBP_OPERATION_OK;
279279
return result;
280280
}
281281
}
@@ -314,14 +314,14 @@ sxbp_status_t sxbp_plot_spiral(
314314
// preconditional assertions
315315
assert(spiral->lines != NULL);
316316
// set up result status
317-
sxbp_status_t result = {{0, 0, 0}, 0};
317+
sxbp_status_t result;
318318
// get index of highest line to plot
319319
uint64_t max_index = (max_line > spiral->size) ? spiral->size : max_line;
320320
// calculate the length of each line within range solved_count -> max_index
321321
for(size_t i = spiral->solved_count; i < max_index; i++) {
322322
result = sxbp_resize_spiral(spiral, i, 1, perfection_threshold);
323323
// catch and return error if any
324-
if(result.diagnostic != SXBP_OPERATION_OK) {
324+
if(result != SXBP_OPERATION_OK) {
325325
return result;
326326
}
327327
// call callback if given
@@ -330,7 +330,7 @@ sxbp_status_t sxbp_plot_spiral(
330330
}
331331
}
332332
// all ok
333-
result.diagnostic = SXBP_OPERATION_OK;
333+
result = SXBP_OPERATION_OK;
334334
return result;
335335
}
336336

tests.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ bool test_sxbp_load_spiral_rejects_missing_magic_number() {
447447
sxbp_serialise_result_t serialise_result = sxbp_load_spiral(buffer, &output);
448448

449449
if(
450-
(serialise_result.status.diagnostic != SXBP_OPERATION_FAIL) ||
450+
(serialise_result.status != SXBP_OPERATION_FAIL) ||
451451
(serialise_result.diagnostic != SXBP_DESERIALISE_BAD_MAGIC_NUMBER)
452452
) {
453453
result = false;
@@ -470,7 +470,7 @@ bool test_sxbp_load_spiral_rejects_too_small_for_header() {
470470
sxbp_serialise_result_t serialise_result = sxbp_load_spiral(buffer, &output);
471471

472472
if(
473-
(serialise_result.status.diagnostic != SXBP_OPERATION_FAIL) ||
473+
(serialise_result.status != SXBP_OPERATION_FAIL) ||
474474
(serialise_result.diagnostic != SXBP_DESERIALISE_BAD_HEADER_SIZE)
475475
) {
476476
result = false;
@@ -510,7 +510,7 @@ bool test_sxbp_load_spiral_rejects_too_small_data_section() {
510510
sxbp_serialise_result_t serialise_result = sxbp_load_spiral(buffer, &output);
511511

512512
if(
513-
(serialise_result.status.diagnostic != SXBP_OPERATION_FAIL) ||
513+
(serialise_result.status != SXBP_OPERATION_FAIL) ||
514514
(serialise_result.diagnostic != SXBP_DESERIALISE_BAD_DATA_SIZE)
515515
) {
516516
result = false;
@@ -550,7 +550,7 @@ bool test_sxbp_load_spiral_rejects_wrong_version() {
550550
sxbp_serialise_result_t serialise_result = sxbp_load_spiral(buffer, &output);
551551

552552
if(
553-
(serialise_result.status.diagnostic != SXBP_OPERATION_FAIL) ||
553+
(serialise_result.status != SXBP_OPERATION_FAIL) ||
554554
(serialise_result.diagnostic != SXBP_DESERIALISE_BAD_VERSION)
555555
) {
556556
result = false;

0 commit comments

Comments
 (0)