Own fork of MbedSmartRestMain

Dependencies:   C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed

Fork of MbedSmartRestMain by Cumulocity Official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dict.h Source File

dict.h

00001 #ifndef DICH_H
00002 #define DICH_H
00003 #include <string.h>
00004 #include <stdio.h>
00005 #define MAX_KEY_LEN 50
00006 #define MAX_VALUE_LEN 50
00007 #define MAX_ITEM_SIZE 3
00008 
00009 class Dict
00010 {
00011 public:
00012         struct Item {
00013                 char key[MAX_KEY_LEN];
00014                 char value[MAX_VALUE_LEN];
00015         };
00016 
00017         Dict(): count(0) {}
00018         
00019         Dict(const Dict& d): count(0) {
00020                 *this = d;
00021         }
00022 
00023         Dict& operator= (const Dict& d) {
00024                 clear();
00025                 for (size_t i = 0; i < d.size(); ++i) {
00026                         const Item* p = d.at(i);
00027                         set(p->key, p->value);
00028                 }
00029                 return *this;
00030         }
00031 
00032         const Item* at(const size_t i) const {
00033                 if (i < count) return &items[i];
00034                 else return NULL;
00035         }
00036 
00037         const Item* get(const char *k) const {
00038                 size_t i = find(k);
00039                 if (i < count)
00040                         return &items[i];
00041                 else
00042                         return NULL;
00043         }
00044 
00045         const Item* set(const char *k, const char *v) {
00046                 size_t i = find(k);
00047                 if (i < count) {
00048                         strncpy(items[i].value, v, MAX_VALUE_LEN);
00049                         return &items[i];
00050                 } else if (count < MAX_ITEM_SIZE){
00051                         ++count;
00052                         strncpy(items[i].key, k, MAX_KEY_LEN);
00053                         strncpy(items[i].value, v, MAX_VALUE_LEN);
00054                         return &items[i];
00055                 } else
00056                         return NULL;
00057         }
00058 
00059         size_t dump(char* buf) const {
00060                 size_t l = 0;
00061                 for (size_t i = 0; i < count; ++i) {
00062                         l += sprintf(buf+l, "%s=%s;", items[i].key, items[i].value);
00063                 }
00064                 buf[l] = 0;
00065                 return l;
00066         }
00067 
00068         size_t maxSize() const { return MAX_ITEM_SIZE; }
00069         size_t size() const { return count; }
00070         bool empty() const { return count==0; }
00071         bool full() const { return count == MAX_ITEM_SIZE; }
00072         void clear() { count = 0; }
00073         virtual ~Dict() {}
00074 protected:
00075         size_t find(const char* k) const {
00076                 for (size_t i = 0; i < count; ++i) {
00077                         if (strncmp(items[i].key, k, MAX_KEY_LEN) == 0)
00078                                 return i;
00079                 }
00080                 return count;
00081         }
00082 private:
00083         Item items[MAX_ITEM_SIZE];
00084         size_t count;
00085 };
00086 
00087 #endif /* DICH_H */