Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cn-get.c Source File

cn-get.c

00001 #include <stdlib.h>
00002 #include <string.h>
00003 #include <assert.h>
00004 
00005 #include "cn-cbor.h"
00006 
00007 cn_cbor* cn_cbor_mapget_int(const cn_cbor* cb, int key) {
00008   cn_cbor* cp;
00009   assert(cb);
00010   for (cp = cb->first_child; cp && cp->next; cp = cp->next->next) {
00011     switch(cp->type) {
00012     case CN_CBOR_UINT:
00013       if (cp->v.uint == (unsigned long)key) {
00014         return cp->next;
00015       }
00016       break;
00017     case CN_CBOR_INT:
00018       if (cp->v.sint == (long)key) {
00019         return cp->next;
00020       }
00021       break;
00022     default:
00023       ; // skip non-integer keys
00024     }
00025   }
00026   return NULL;
00027 }
00028 
00029 cn_cbor* cn_cbor_mapget_string(const cn_cbor* cb, const char* key) {
00030   cn_cbor *cp;
00031   int keylen;
00032   assert(cb);
00033   assert(key);
00034   keylen = strlen(key);
00035   for (cp = cb->first_child; cp && cp->next; cp = cp->next->next) {
00036     switch(cp->type) {
00037     case CN_CBOR_TEXT: // fall-through
00038     case CN_CBOR_BYTES:
00039       if (keylen != cp->length) {
00040         continue;
00041       }
00042       if (memcmp(key, cp->v.str, keylen) == 0) {
00043         return cp->next;
00044       }
00045     default:
00046       ; // skip non-string keys
00047     }
00048   }
00049   return NULL;
00050 }
00051 
00052 cn_cbor* cn_cbor_index(const cn_cbor* cb, unsigned int idx) {
00053   cn_cbor *cp;
00054   unsigned int i = 0;
00055   assert(cb);
00056   for (cp = cb->first_child; cp; cp = cp->next) {
00057     if (i == idx) {
00058       return cp;
00059     }
00060     i++;
00061   }
00062   return NULL;
00063 }