blob: 79d3d72ba15aee1060407f951de3b91062081353 [file] [log] [blame]
Joe Hildebrandcead90a2015-03-22 15:13:50 -05001#include <stdlib.h>
2#include <string.h>
3#include <assert.h>
4
Joe Hildebrand7c6c3562015-03-31 00:21:21 -06005#include "cn-cbor/cn-cbor.h"
Joe Hildebrandcead90a2015-03-22 15:13:50 -05006
Joe Hildebrand45e9ad92015-08-26 10:37:02 -06007cn_cbor* cn_cbor_mapget_int(const cn_cbor* cb, int key) {
Joe Hildebrandcead90a2015-03-22 15:13:50 -05008 cn_cbor* cp;
9 assert(cb);
10 for (cp = cb->first_child; cp && cp->next; cp = cp->next->next) {
11 switch(cp->type) {
12 case CN_CBOR_UINT:
13 if (cp->v.uint == (unsigned long)key) {
14 return cp->next;
15 }
Sören Tempel2e513d92017-10-11 10:52:49 +020016 break;
Joe Hildebrandcead90a2015-03-22 15:13:50 -050017 case CN_CBOR_INT:
18 if (cp->v.sint == (long)key) {
19 return cp->next;
20 }
21 break;
22 default:
23 ; // skip non-integer keys
24 }
25 }
26 return NULL;
27}
28
Joe Hildebrand45e9ad92015-08-26 10:37:02 -060029cn_cbor* cn_cbor_mapget_string(const cn_cbor* cb, const char* key) {
Joe Hildebrandcead90a2015-03-22 15:13:50 -050030 cn_cbor *cp;
31 int keylen;
32 assert(cb);
33 assert(key);
34 keylen = strlen(key);
35 for (cp = cb->first_child; cp && cp->next; cp = cp->next->next) {
36 switch(cp->type) {
Joe Hildebrand08631742015-03-31 23:19:54 -060037 case CN_CBOR_TEXT: // fall-through
Joe Hildebrandcead90a2015-03-22 15:13:50 -050038 case CN_CBOR_BYTES:
39 if (keylen != cp->length) {
40 continue;
41 }
42 if (memcmp(key, cp->v.str, keylen) == 0) {
43 return cp->next;
44 }
45 default:
46 ; // skip non-string keys
47 }
48 }
49 return NULL;
50}
51
Joe Hildebrand45e9ad92015-08-26 10:37:02 -060052cn_cbor* cn_cbor_index(const cn_cbor* cb, unsigned int idx) {
Joe Hildebrandcead90a2015-03-22 15:13:50 -050053 cn_cbor *cp;
Joe Hildebrandbc6ff462015-03-26 16:39:27 -050054 unsigned int i = 0;
Joe Hildebrandcead90a2015-03-22 15:13:50 -050055 assert(cb);
56 for (cp = cb->first_child; cp; cp = cp->next) {
57 if (i == idx) {
58 return cp;
59 }
60 i++;
61 }
62 return NULL;
63}