Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Committer:
xinlei
Date:
Mon Apr 27 10:50:21 2015 +0000
Revision:
95:5dfdc8568e9f
Child:
98:e369fc75c000
LCDDisplay now is singleton

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xinlei 95:5dfdc8568e9f 1 #ifndef DICH_H
xinlei 95:5dfdc8568e9f 2 #define DICH_H
xinlei 95:5dfdc8568e9f 3 #include <string.h>
xinlei 95:5dfdc8568e9f 4 #define MAX_KEY_LEN 50
xinlei 95:5dfdc8568e9f 5 #define MAX_VALUE_LEN 50
xinlei 95:5dfdc8568e9f 6 #define MAX_ITEM_SIZE 5
xinlei 95:5dfdc8568e9f 7
xinlei 95:5dfdc8568e9f 8 class Dict
xinlei 95:5dfdc8568e9f 9 {
xinlei 95:5dfdc8568e9f 10 public:
xinlei 95:5dfdc8568e9f 11 struct Item {
xinlei 95:5dfdc8568e9f 12 char key[MAX_KEY_LEN];
xinlei 95:5dfdc8568e9f 13 char value[MAX_VALUE_LEN];
xinlei 95:5dfdc8568e9f 14 };
xinlei 95:5dfdc8568e9f 15
xinlei 95:5dfdc8568e9f 16 Dict(): count(0) {}
xinlei 95:5dfdc8568e9f 17
xinlei 95:5dfdc8568e9f 18 const Item* at(const size_t i) const {
xinlei 95:5dfdc8568e9f 19 if (i < count) return &items[i];
xinlei 95:5dfdc8568e9f 20 else return NULL;
xinlei 95:5dfdc8568e9f 21 }
xinlei 95:5dfdc8568e9f 22
xinlei 95:5dfdc8568e9f 23 const Item* get(const char *k) const {
xinlei 95:5dfdc8568e9f 24 size_t i = find(k);
xinlei 95:5dfdc8568e9f 25 if (i < count)
xinlei 95:5dfdc8568e9f 26 return &items[i];
xinlei 95:5dfdc8568e9f 27 else
xinlei 95:5dfdc8568e9f 28 return NULL;
xinlei 95:5dfdc8568e9f 29 }
xinlei 95:5dfdc8568e9f 30
xinlei 95:5dfdc8568e9f 31 const Item* set(const char *k, const char *v) {
xinlei 95:5dfdc8568e9f 32 size_t i = find(k);
xinlei 95:5dfdc8568e9f 33 if (i < count) {
xinlei 95:5dfdc8568e9f 34 strncpy(items[i].value, v, MAX_VALUE_LEN);
xinlei 95:5dfdc8568e9f 35 return &items[i];
xinlei 95:5dfdc8568e9f 36 } else if (count < MAX_ITEM_SIZE){
xinlei 95:5dfdc8568e9f 37 ++count;
xinlei 95:5dfdc8568e9f 38 strncpy(items[i].key, k, MAX_KEY_LEN);
xinlei 95:5dfdc8568e9f 39 strncpy(items[i].value, v, MAX_VALUE_LEN);
xinlei 95:5dfdc8568e9f 40 return &items[i];
xinlei 95:5dfdc8568e9f 41 } else
xinlei 95:5dfdc8568e9f 42 return NULL;
xinlei 95:5dfdc8568e9f 43 }
xinlei 95:5dfdc8568e9f 44
xinlei 95:5dfdc8568e9f 45 size_t maxSize() const { return MAX_ITEM_SIZE; }
xinlei 95:5dfdc8568e9f 46 size_t maxKeyLen() const { return MAX_KEY_LEN; }
xinlei 95:5dfdc8568e9f 47 size_t maxValueLen() const { return MAX_VALUE_LEN; }
xinlei 95:5dfdc8568e9f 48 size_t size() const { return count; }
xinlei 95:5dfdc8568e9f 49 bool empty() const { return count==0; }
xinlei 95:5dfdc8568e9f 50 bool full() const { return count == MAX_ITEM_SIZE; }
xinlei 95:5dfdc8568e9f 51 virtual ~Dict() {}
xinlei 95:5dfdc8568e9f 52 protected:
xinlei 95:5dfdc8568e9f 53 size_t find(const char* k) const {
xinlei 95:5dfdc8568e9f 54 for (size_t i = 0; i < count; ++i) {
xinlei 95:5dfdc8568e9f 55 if (strncmp(items[i].key, k, MAX_KEY_LEN) == 0)
xinlei 95:5dfdc8568e9f 56 return i;
xinlei 95:5dfdc8568e9f 57 }
xinlei 95:5dfdc8568e9f 58 return count;
xinlei 95:5dfdc8568e9f 59 }
xinlei 95:5dfdc8568e9f 60 private:
xinlei 95:5dfdc8568e9f 61 Item items[MAX_ITEM_SIZE];
xinlei 95:5dfdc8568e9f 62 size_t count;
xinlei 95:5dfdc8568e9f 63 };
xinlei 95:5dfdc8568e9f 64
xinlei 95:5dfdc8568e9f 65 #endif /* DICH_H */