Own fork of MbedSmartRestMain
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
util/dict.h@102:ede6611e064e, 2015-05-08 (annotated)
- Committer:
- xinlei
- Date:
- Fri May 08 13:03:41 2015 +0000
- Revision:
- 102:ede6611e064e
- Parent:
- 100:dbcd3bc51758
- Child:
- 116:5de54f09f754
hotfix: validate configuration content.
Who changed what in which revision?
User | Revision | Line number | New 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 | 100:dbcd3bc51758 | 4 | #include <stdio.h> |
xinlei | 95:5dfdc8568e9f | 5 | #define MAX_KEY_LEN 50 |
xinlei | 95:5dfdc8568e9f | 6 | #define MAX_VALUE_LEN 50 |
xinlei | 98:e369fc75c000 | 7 | #define MAX_ITEM_SIZE 3 |
xinlei | 95:5dfdc8568e9f | 8 | |
xinlei | 95:5dfdc8568e9f | 9 | class Dict |
xinlei | 95:5dfdc8568e9f | 10 | { |
xinlei | 95:5dfdc8568e9f | 11 | public: |
xinlei | 95:5dfdc8568e9f | 12 | struct Item { |
xinlei | 95:5dfdc8568e9f | 13 | char key[MAX_KEY_LEN]; |
xinlei | 95:5dfdc8568e9f | 14 | char value[MAX_VALUE_LEN]; |
xinlei | 95:5dfdc8568e9f | 15 | }; |
xinlei | 95:5dfdc8568e9f | 16 | |
xinlei | 95:5dfdc8568e9f | 17 | Dict(): count(0) {} |
xinlei | 100:dbcd3bc51758 | 18 | |
xinlei | 100:dbcd3bc51758 | 19 | Dict(const Dict& d): count(0) { |
xinlei | 100:dbcd3bc51758 | 20 | *this = d; |
xinlei | 100:dbcd3bc51758 | 21 | } |
xinlei | 100:dbcd3bc51758 | 22 | |
xinlei | 100:dbcd3bc51758 | 23 | Dict& operator= (const Dict& d) { |
xinlei | 100:dbcd3bc51758 | 24 | clear(); |
xinlei | 100:dbcd3bc51758 | 25 | for (size_t i = 0; i < d.size(); ++i) { |
xinlei | 100:dbcd3bc51758 | 26 | const Item* p = d.at(i); |
xinlei | 100:dbcd3bc51758 | 27 | set(p->key, p->value); |
xinlei | 100:dbcd3bc51758 | 28 | } |
xinlei | 100:dbcd3bc51758 | 29 | return *this; |
xinlei | 100:dbcd3bc51758 | 30 | } |
xinlei | 95:5dfdc8568e9f | 31 | |
xinlei | 95:5dfdc8568e9f | 32 | const Item* at(const size_t i) const { |
xinlei | 95:5dfdc8568e9f | 33 | if (i < count) return &items[i]; |
xinlei | 95:5dfdc8568e9f | 34 | else return NULL; |
xinlei | 95:5dfdc8568e9f | 35 | } |
xinlei | 95:5dfdc8568e9f | 36 | |
xinlei | 95:5dfdc8568e9f | 37 | const Item* get(const char *k) const { |
xinlei | 95:5dfdc8568e9f | 38 | size_t i = find(k); |
xinlei | 95:5dfdc8568e9f | 39 | if (i < count) |
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 | const Item* set(const char *k, const char *v) { |
xinlei | 95:5dfdc8568e9f | 46 | size_t i = find(k); |
xinlei | 95:5dfdc8568e9f | 47 | if (i < count) { |
xinlei | 95:5dfdc8568e9f | 48 | strncpy(items[i].value, v, MAX_VALUE_LEN); |
xinlei | 95:5dfdc8568e9f | 49 | return &items[i]; |
xinlei | 95:5dfdc8568e9f | 50 | } else if (count < MAX_ITEM_SIZE){ |
xinlei | 95:5dfdc8568e9f | 51 | ++count; |
xinlei | 95:5dfdc8568e9f | 52 | strncpy(items[i].key, k, MAX_KEY_LEN); |
xinlei | 95:5dfdc8568e9f | 53 | strncpy(items[i].value, v, MAX_VALUE_LEN); |
xinlei | 95:5dfdc8568e9f | 54 | return &items[i]; |
xinlei | 95:5dfdc8568e9f | 55 | } else |
xinlei | 95:5dfdc8568e9f | 56 | return NULL; |
xinlei | 95:5dfdc8568e9f | 57 | } |
xinlei | 95:5dfdc8568e9f | 58 | |
xinlei | 100:dbcd3bc51758 | 59 | size_t dump(char* buf) const { |
xinlei | 100:dbcd3bc51758 | 60 | size_t l = 0; |
xinlei | 100:dbcd3bc51758 | 61 | for (size_t i = 0; i < count; ++i) { |
xinlei | 102:ede6611e064e | 62 | l += sprintf(buf+l, "%s=%s;\r\n", items[i].key, items[i].value); |
xinlei | 100:dbcd3bc51758 | 63 | } |
xinlei | 100:dbcd3bc51758 | 64 | buf[l] = 0; |
xinlei | 100:dbcd3bc51758 | 65 | return l; |
xinlei | 100:dbcd3bc51758 | 66 | } |
xinlei | 100:dbcd3bc51758 | 67 | |
xinlei | 95:5dfdc8568e9f | 68 | size_t maxSize() const { return MAX_ITEM_SIZE; } |
xinlei | 95:5dfdc8568e9f | 69 | size_t size() const { return count; } |
xinlei | 95:5dfdc8568e9f | 70 | bool empty() const { return count==0; } |
xinlei | 95:5dfdc8568e9f | 71 | bool full() const { return count == MAX_ITEM_SIZE; } |
xinlei | 98:e369fc75c000 | 72 | void clear() { count = 0; } |
xinlei | 95:5dfdc8568e9f | 73 | virtual ~Dict() {} |
xinlei | 95:5dfdc8568e9f | 74 | protected: |
xinlei | 95:5dfdc8568e9f | 75 | size_t find(const char* k) const { |
xinlei | 95:5dfdc8568e9f | 76 | for (size_t i = 0; i < count; ++i) { |
xinlei | 95:5dfdc8568e9f | 77 | if (strncmp(items[i].key, k, MAX_KEY_LEN) == 0) |
xinlei | 95:5dfdc8568e9f | 78 | return i; |
xinlei | 95:5dfdc8568e9f | 79 | } |
xinlei | 95:5dfdc8568e9f | 80 | return count; |
xinlei | 95:5dfdc8568e9f | 81 | } |
xinlei | 95:5dfdc8568e9f | 82 | private: |
xinlei | 95:5dfdc8568e9f | 83 | Item items[MAX_ITEM_SIZE]; |
xinlei | 95:5dfdc8568e9f | 84 | size_t count; |
xinlei | 95:5dfdc8568e9f | 85 | }; |
xinlei | 95:5dfdc8568e9f | 86 | |
xinlei | 100:dbcd3bc51758 | 87 | #endif /* DICH_H */ |