Joe Hildebrand | cead90a | 2015-03-22 15:13:50 -0500 | [diff] [blame] | 1 | #include <stdlib.h> |
| 2 | #include <string.h> |
| 3 | #include <assert.h> |
| 4 | |
Joe Hildebrand | 7c6c356 | 2015-03-31 00:21:21 -0600 | [diff] [blame] | 5 | #include "cn-cbor/cn-cbor.h" |
Joe Hildebrand | cead90a | 2015-03-22 15:13:50 -0500 | [diff] [blame] | 6 | |
Joe Hildebrand | 45e9ad9 | 2015-08-26 10:37:02 -0600 | [diff] [blame] | 7 | cn_cbor* cn_cbor_mapget_int(const cn_cbor* cb, int key) { |
Joe Hildebrand | cead90a | 2015-03-22 15:13:50 -0500 | [diff] [blame] | 8 | 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 Tempel | 2e513d9 | 2017-10-11 10:52:49 +0200 | [diff] [blame] | 16 | break; |
Joe Hildebrand | cead90a | 2015-03-22 15:13:50 -0500 | [diff] [blame] | 17 | 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 Hildebrand | 45e9ad9 | 2015-08-26 10:37:02 -0600 | [diff] [blame] | 29 | cn_cbor* cn_cbor_mapget_string(const cn_cbor* cb, const char* key) { |
Joe Hildebrand | cead90a | 2015-03-22 15:13:50 -0500 | [diff] [blame] | 30 | 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 Hildebrand | 0863174 | 2015-03-31 23:19:54 -0600 | [diff] [blame] | 37 | case CN_CBOR_TEXT: // fall-through |
Joe Hildebrand | cead90a | 2015-03-22 15:13:50 -0500 | [diff] [blame] | 38 | 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 Hildebrand | 45e9ad9 | 2015-08-26 10:37:02 -0600 | [diff] [blame] | 52 | cn_cbor* cn_cbor_index(const cn_cbor* cb, unsigned int idx) { |
Joe Hildebrand | cead90a | 2015-03-22 15:13:50 -0500 | [diff] [blame] | 53 | cn_cbor *cp; |
Joe Hildebrand | bc6ff46 | 2015-03-26 16:39:27 -0500 | [diff] [blame] | 54 | unsigned int i = 0; |
Joe Hildebrand | cead90a | 2015-03-22 15:13:50 -0500 | [diff] [blame] | 55 | 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 | } |