
Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
util/dict.h
- Committer:
- xinlei
- Date:
- 2015-05-07
- Revision:
- 98:e369fc75c000
- Parent:
- 95:5dfdc8568e9f
- Child:
- 100:dbcd3bc51758
File content as of revision 98:e369fc75c000:
#ifndef DICH_H #define DICH_H #include <string.h> #define MAX_KEY_LEN 50 #define MAX_VALUE_LEN 50 #define MAX_ITEM_SIZE 3 class Dict { public: struct Item { char key[MAX_KEY_LEN]; char value[MAX_VALUE_LEN]; }; Dict(): count(0) {} const Item* at(const size_t i) const { if (i < count) return &items[i]; else return NULL; } const Item* get(const char *k) const { size_t i = find(k); if (i < count) return &items[i]; else return NULL; } const Item* set(const char *k, const char *v) { size_t i = find(k); if (i < count) { strncpy(items[i].value, v, MAX_VALUE_LEN); return &items[i]; } else if (count < MAX_ITEM_SIZE){ ++count; strncpy(items[i].key, k, MAX_KEY_LEN); strncpy(items[i].value, v, MAX_VALUE_LEN); return &items[i]; } else return NULL; } size_t maxSize() const { return MAX_ITEM_SIZE; } size_t size() const { return count; } bool empty() const { return count==0; } bool full() const { return count == MAX_ITEM_SIZE; } void clear() { count = 0; } size_t dump(char* buf) { size_t l = 0; for (size_t i = 0; i < count; ++i) { l + sprintf(buf+l, "%s=%s;", items[i].key, items[i].value); } buf[l] = 0; return l; } virtual ~Dict() {} protected: size_t find(const char* k) const { for (size_t i = 0; i < count; ++i) { if (strncmp(items[i].key, k, MAX_KEY_LEN) == 0) return i; } return count; } private: Item items[MAX_ITEM_SIZE]; size_t count; }; #endif /* DICH_H */