Skip to content

Commit 2fbc823

Browse files
committed
Unify definite/indefinite enums 1/2
1 parent 9ff623f commit 2fbc823

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

src/cbor.c

+29-8
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ cbor_item_t * cbor_load(cbor_data source,
120120
struct cbor_decoder_result decode_result;
121121
*result = (struct cbor_load_result){ .read = 0, .error = { .code = CBOR_ERR_NONE } };
122122

123-
// TODO unify errors
124-
// TODO unify returns
125123
do {
126124
if (source_size > result->read) { /* Check for overflows */
127125
decode_result = cbor_stream_decode(source + result->read, source_size - result->read, &callbacks, context);
@@ -1175,7 +1173,7 @@ cbor_item_t * cbor_new_definite_array(size_t size)
11751173
*item = (cbor_item_t){
11761174
.refcount = 1,
11771175
.type = CBOR_TYPE_ARRAY,
1178-
.metadata = { .array_metadata = { .type = _CBOR_ARRAY_METADATA_DEFINITE, .size = 0 } },
1176+
.metadata = { .array_metadata = { .type = _CBOR_METADATA_DEFINITE, .size = 0 } },
11791177
.data = malloc(sizeof(cbor_item_t *) * size)
11801178
};
11811179
return item;
@@ -1187,7 +1185,7 @@ cbor_item_t * cbor_new_indefinite_array()
11871185
*item = (cbor_item_t){
11881186
.refcount = 1,
11891187
.type = CBOR_TYPE_ARRAY,
1190-
.metadata = { .array_metadata = { .type = _CBOR_ARRAY_METADATA_INDEFINITE, .size = 0 } },
1188+
.metadata = { .array_metadata = { .type = _CBOR_METADATA_INDEFINITE, .size = 0 } },
11911189
.data = NULL /* Can be safely realloc-ed */
11921190
};
11931191
return item;
@@ -1211,6 +1209,31 @@ cbor_item_t * cbor_array_push(cbor_item_t * array, cbor_item_t * pushee)
12111209
return array;
12121210
}
12131211

1212+
size_t cbor_map_size(cbor_item_t * item)
1213+
{
1214+
assert(cbor_isa_map(item));
1215+
return item->metadata.map_metadata.size;
1216+
}
1217+
1218+
cbor_item_t * cbor_new_definite_map(size_t size)
1219+
{
1220+
cbor_item_t * item = malloc(sizeof(cbor_item_t));
1221+
*item = (cbor_item_t){
1222+
.refcount = 1,
1223+
.type = CBOR_TYPE_MAP,
1224+
.metadata = { .map_metadata = { .size = 0} },
1225+
.data = malloc(sizeof(cbor_item_t *) * size)
1226+
};
1227+
return item;
1228+
}
1229+
1230+
cbor_item_t * cbor_new_indefinite_map();
1231+
cbor_item_t * cbor_map_add(cbor_item_t * item, struct cbor_pair pair);
1232+
bool cbor_map_is_definite(cbor_item_t * item);
1233+
bool cbor_map_is_indefinite(cbor_item_t * item);
1234+
struct cbor_pair * cbor_map_handle(cbor_item_t * item);
1235+
1236+
12141237
cbor_item_t * cbor_new_tag(uint64_t value)
12151238
{
12161239
cbor_item_t * item = malloc(sizeof(cbor_item_t));
@@ -1320,8 +1343,6 @@ bool cbor_is_float(const cbor_item_t * item)
13201343
return cbor_isa_float_ctrl(item) && !cbor_float_ctrl_is_ctrl(item);
13211344
}
13221345

1323-
1324-
13251346
size_t cbor_bytestring_length(const cbor_item_t * item) {
13261347
assert(cbor_isa_bytestring(item));
13271348
return item->metadata.bytestring_metadata.length;
@@ -1352,13 +1373,13 @@ size_t cbor_array_size(cbor_item_t * item)
13521373
bool cbor_array_is_definite(cbor_item_t * item)
13531374
{
13541375
assert(cbor_isa_array(item));
1355-
return item->metadata.array_metadata.type == _CBOR_ARRAY_METADATA_DEFINITE;
1376+
return item->metadata.array_metadata.type == _CBOR_METADATA_DEFINITE;
13561377
}
13571378

13581379
bool cbor_array_is_indefinite(cbor_item_t * item)
13591380
{
13601381
assert(cbor_isa_array(item));
1361-
return item->metadata.array_metadata.type == _CBOR_ARRAY_METADATA_INDEFINITE;
1382+
return item->metadata.array_metadata.type == _CBOR_METADATA_INDEFINITE;
13621383
}
13631384

13641385
cbor_item_t ** cbor_array_handle(cbor_item_t * item)

src/cbor.h

+10-17
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,13 @@ struct _cbor_string_metadata {
103103
};
104104

105105
typedef enum {
106-
_CBOR_ARRAY_METADATA_DEFINITE,
107-
_CBOR_ARRAY_METADATA_INDEFINITE
108-
} _cbor_array_type_metadata;
106+
_CBOR_METADATA_DEFINITE,
107+
_CBOR_METADATA_INDEFINITE
108+
} _cbor_dst_metadata;
109109

110110
struct _cbor_array_metadata {
111-
size_t size;
112-
_cbor_array_type_metadata type;
111+
size_t size;
112+
_cbor_dst_metadata type;
113113
};
114114

115115
struct _cbor_map_metadata {
@@ -156,14 +156,9 @@ struct cbor_error {
156156
};
157157

158158
struct cbor_pair {
159-
cbor_item_t * key, value;
159+
cbor_item_t * key, * value;
160160
};
161161

162-
typedef struct cbor_map_iterator {
163-
struct cbor_pair data;
164-
// METADATA FOR A LINKED LIST OR TREE OR WHATEVER
165-
} cbor_map_iterator;
166-
167162
struct cbor_load_result {
168163
struct cbor_error error;
169164
size_t read;
@@ -371,14 +366,12 @@ cbor_item_t * cbor_array_push(cbor_item_t * array, cbor_item_t * pushee);
371366
*/
372367

373368
size_t cbor_map_size(cbor_item_t * item);
374-
cbor_item_t * cbor_new_map();
375-
struct cbor_map_iterator cbor_map_add(cbor_item_t * item, struct cbor_pair pair);
369+
cbor_item_t * cbor_new_definite_map(size_t size);
370+
cbor_item_t * cbor_new_indefinite_map();
371+
cbor_item_t * cbor_map_add(cbor_item_t * item, struct cbor_pair pair);
376372
bool cbor_map_is_definite(cbor_item_t * item);
377373
bool cbor_map_is_indefinite(cbor_item_t * item);
378-
struct cbor_map_iterator cbor_map_begin(cbor_item_t * item);
379-
bool cbor_map_iterator_end(struct cbor_map_iterator * iter);
380-
void cbor_map_iterator_next(struct cbor_map_iterator * iter);
381-
void cbor_map_delete(struct cbor_map_iterator * iter);
374+
struct cbor_pair * cbor_map_handle(cbor_item_t * item);
382375

383376
/*
384377
* ============================================================================

0 commit comments

Comments
 (0)