Skip to content

Commit 3eec294

Browse files
authored
Quiet compile (#21)
* Gitignore some dev stuff * Hid some funcs inside #defines in order to remove complaints about unused functions. Added newlines to end of files * Removed semicolons from the end of macros that define functions to remove complaint from -Wextra-semi * Empty initialisers make -pedantic unhappy * No empty function declarations * Removed forward declarations of enum types since these are forbidden by ISO C * Instead of casting to union types (a GNU extension), build a return val of the right type, and populate it appropriately
1 parent a597dd1 commit 3eec294

File tree

4 files changed

+70
-58
lines changed

4 files changed

+70
-58
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
bin
2+
obj
3+
Makefile

example.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const char *read_file(const char *path) {
2828
return (const char *)buffer;
2929
}
3030

31-
int main() {
31+
int main(void) {
3232
const char *json = read_file("../sample/reddit.json");
3333
if (json == NULL) {
3434
return -1;

json.c

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,31 @@
77
#include <stdlib.h>
88
#include <string.h>
99

10+
/**
11+
* @brief Determines whether a character `ch` is whitespace
12+
*/
13+
#define is_whitespace(ch) (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t')
14+
1015
#ifdef JSON_SKIP_WHITESPACE
11-
#define json_skip_whitespace(arg) json_skip_whitespace_actual(arg)
16+
void json_skip_whitespace(typed(json_string) * str_ptr) {
17+
while (is_whitespace(**str_ptr))
18+
(*str_ptr)++;
19+
}
1220
#else
1321
#define json_skip_whitespace(arg)
1422
#endif
1523

1624
#ifdef JSON_DEBUG
1725
#define log(str, ...) printf(str "\n", ##__VA_ARGS__)
26+
void json_debug_print(typed(json_string) str, typed(size) len) {
27+
for (size_t i = 0; i < len; i++) {
28+
if (str[i] == '\0')
29+
break;
30+
31+
putchar(str[i]);
32+
}
33+
printf("\n");
34+
}
1835
#else
1936
#define log(str, ...)
2037
#endif
@@ -71,11 +88,6 @@
7188
*/
7289
#define reallocN(ptr, type, count) (type *)realloc(ptr, (count) * sizeof(type))
7390

74-
/**
75-
* @brief Determines whether a character `ch` is whitespace
76-
*/
77-
#define is_whitespace(ch) (ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t')
78-
7991
/**
8092
* @brief Parses a JSON element {json_element_t} and moves the string
8193
* pointer to the end of the parsed element
@@ -217,7 +229,7 @@ static bool json_skip_boolean(typed(json_string) *);
217229
/**
218230
* @brief Moves a JSON string pointer beyond any whitespace
219231
*/
220-
static void json_skip_whitespace_actual(typed(json_string) *);
232+
// static void json_skip_whitespace_actual(typed(json_string) *);
221233

222234
/**
223235
* @brief Moves a JSON string pointer beyond `null` literal
@@ -281,11 +293,6 @@ static result(json_string)
281293
*/
282294
static typed(size) json_string_len(typed(json_string));
283295

284-
/**
285-
* @brief Debug print some characters from a string
286-
*/
287-
static void json_debug_print(typed(json_string) str, typed(size) len);
288-
289296
result(json_element) json_parse(typed(json_string) json_str) {
290297
if (json_str == NULL) {
291298
return result_err(json_element)(JSON_ERROR_EMPTY);
@@ -423,7 +430,10 @@ result(json_element_value) json_parse_string(typed(json_string) * str_ptr) {
423430
// Skip to beyond the string
424431
(*str_ptr) += len + 1;
425432

426-
return result_ok(json_element_value)((typed(json_element_value))output);
433+
typed(json_element_value) retval = {0};
434+
retval.as_string = output;
435+
436+
return result_ok(json_element_value)(retval);
427437
}
428438

429439
result(json_element_value) json_parse_number(typed(json_string) * str_ptr) {
@@ -438,28 +448,35 @@ result(json_element_value) json_parse_number(typed(json_string) * str_ptr) {
438448
temp_str++;
439449
}
440450

441-
typed(json_number) number = {};
451+
typed(json_number) number = {0};
452+
typed(json_number_value) val = {0};
442453

443454
if (has_decimal) {
444455
errno = 0;
445456

457+
val.as_double = strtod(*str_ptr, (char **)str_ptr);
458+
446459
number.type = JSON_NUMBER_TYPE_DOUBLE;
447-
number.value = (typed(json_number_value))strtod(*str_ptr, (char **)str_ptr);
460+
number.value = val;
448461

449462
if (errno == EINVAL || errno == ERANGE)
450463
return result_err(json_element_value)(JSON_ERROR_INVALID_VALUE);
451464
} else {
452465
errno = 0;
453466

467+
val.as_long = strtol(*str_ptr, (char **)str_ptr, 10);
468+
454469
number.type = JSON_NUMBER_TYPE_LONG;
455-
number.value =
456-
(typed(json_number_value))strtol(*str_ptr, (char **)str_ptr, 10);
470+
number.value = val;
457471

458472
if (errno == EINVAL || errno == ERANGE)
459473
return result_err(json_element_value)(JSON_ERROR_INVALID_VALUE);
460474
}
461475

462-
return result_ok(json_element_value)((typed(json_element_value))number);
476+
typed(json_element_value) retval = {0};
477+
retval.as_number = number;
478+
479+
return result_ok(json_element_value)(retval);
463480
}
464481

465482
result(json_element_value) json_parse_object(typed(json_string) * str_ptr) {
@@ -552,7 +569,10 @@ result(json_element_value) json_parse_object(typed(json_string) * str_ptr) {
552569
object->count = count;
553570
object->entries = entries;
554571

555-
return result_ok(json_element_value)((typed(json_element_value))object);
572+
typed(json_element_value) retval = {0};
573+
retval.as_object = object;
574+
575+
return result_ok(json_element_value)(retval);
556576
}
557577

558578
typed(uint64) json_key_hash(typed(json_string) str) {
@@ -623,7 +643,10 @@ result(json_element_value) json_parse_array(typed(json_string) * str_ptr) {
623643
array->count = count;
624644
array->elements = elements;
625645

626-
return result_ok(json_element_value)((typed(json_element_value))array);
646+
typed(json_element_value) retval = {0};
647+
retval.as_array = array;
648+
649+
return result_ok(json_element_value)(retval);
627650
}
628651

629652
result(json_element_value) json_parse_boolean(typed(json_string) * str_ptr) {
@@ -640,8 +663,11 @@ result(json_element_value) json_parse_boolean(typed(json_string) * str_ptr) {
640663
(*str_ptr) += 5;
641664
break;
642665
}
666+
667+
typed(json_element_value) retval = {0};
668+
retval.as_boolean = output;
643669

644-
return result_ok(json_element_value)((typed(json_element_value))output);
670+
return result_ok(json_element_value)(retval);
645671
}
646672

647673
result(json_element)
@@ -817,11 +843,6 @@ bool json_skip_boolean(typed(json_string) * str_ptr) {
817843
return false;
818844
}
819845

820-
void json_skip_whitespace_actual(typed(json_string) * str_ptr) {
821-
while (is_whitespace(**str_ptr))
822-
(*str_ptr)++;
823-
}
824-
825846
void json_skip_null(typed(json_string) * str_ptr) { (*str_ptr) += 4; }
826847

827848
void json_print(typed(json_element) * element, int indent) {
@@ -1074,19 +1095,10 @@ result(json_string)
10741095
return result_ok(json_string)((typed(json_string))output);
10751096
}
10761097

1077-
void json_debug_print(typed(json_string) str, typed(size) len) {
1078-
for (size_t i = 0; i < len; i++) {
1079-
if (str[i] == '\0')
1080-
break;
1081-
1082-
putchar(str[i]);
1083-
}
1084-
printf("\n");
1085-
}
1098+
define_result_type(json_element_type)
1099+
define_result_type(json_element_value)
1100+
define_result_type(json_element)
1101+
define_result_type(json_entry)
1102+
define_result_type(json_string)
1103+
define_result_type(size)
10861104

1087-
define_result_type(json_element_type);
1088-
define_result_type(json_element_value);
1089-
define_result_type(json_element);
1090-
define_result_type(json_entry);
1091-
define_result_type(json_string);
1092-
define_result_type(size);

json.h

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,11 @@ typedef unsigned int bool;
1313
typedef const char *typed(json_string);
1414
typedef bool typed(json_boolean);
1515

16-
typedef enum json_number_type_e typed(json_number_type);
1716
typedef union json_number_value_u typed(json_number_value);
1817
typedef signed long typed(json_number_long);
1918
typedef double typed(json_number_double);
2019
typedef struct json_number_s typed(json_number);
21-
typedef enum json_element_type_e typed(json_element_type);
2220
typedef union json_element_value_u typed(json_element_value);
23-
typedef enum json_error_e typed(json_error);
2421
typedef struct json_element_s typed(json_element);
2522
typedef struct json_entry_s typed(json_entry);
2623
typedef struct json_object_s typed(json_object);
@@ -55,19 +52,19 @@ typedef struct json_array_s typed(json_array);
5552
typed(name) result_unwrap(name)(result(name) *); \
5653
typed(json_error) result_unwrap_err(name)(result(name) *);
5754

58-
enum json_element_type_e {
55+
typedef enum json_element_type_e {
5956
JSON_ELEMENT_TYPE_STRING = 0,
6057
JSON_ELEMENT_TYPE_NUMBER,
6158
JSON_ELEMENT_TYPE_OBJECT,
6259
JSON_ELEMENT_TYPE_ARRAY,
6360
JSON_ELEMENT_TYPE_BOOLEAN,
6461
JSON_ELEMENT_TYPE_NULL
65-
};
62+
} typed(json_element_type);
6663

67-
enum json_number_type_e {
64+
typedef enum json_number_type_e {
6865
JSON_NUMBER_TYPE_LONG = 0,
6966
JSON_NUMBER_TYPE_DOUBLE,
70-
};
67+
} typed(json_number_type);
7168

7269
union json_number_value_u {
7370
typed(json_number_long) as_long;
@@ -107,19 +104,19 @@ struct json_array_s {
107104
typed(json_element) * elements;
108105
};
109106

110-
enum json_error_e {
107+
typedef enum json_error_e {
111108
JSON_ERROR_EMPTY = 0,
112109
JSON_ERROR_INVALID_TYPE,
113110
JSON_ERROR_INVALID_KEY,
114111
JSON_ERROR_INVALID_VALUE
115-
};
112+
} typed(json_error);
116113

117-
declare_result_type(json_element_type);
118-
declare_result_type(json_element_value);
119-
declare_result_type(json_element);
120-
declare_result_type(json_entry);
121-
declare_result_type(json_string);
122-
declare_result_type(size);
114+
declare_result_type(json_element_type)
115+
declare_result_type(json_element_value)
116+
declare_result_type(json_element)
117+
declare_result_type(json_entry)
118+
declare_result_type(json_string)
119+
declare_result_type(size)
123120

124121
/**
125122
* @brief Parses a JSON string into a JSON element {json_element_t}
@@ -162,4 +159,5 @@ void json_free(typed(json_element) * element);
162159
* @param error The JSON error enum {json_error_t} type
163160
* @return The string representation
164161
*/
165-
typed(json_string) json_error_to_string(typed(json_error) error);
162+
typed(json_string) json_error_to_string(typed(json_error) error);
163+

0 commit comments

Comments
 (0)