Network Services
Fork of W5500Interface_K22F by
DNSClient/pico_string.h@0:e11e8793c3ce, 2014-08-20 (annotated)
- Committer:
- Bongjun
- Date:
- Wed Aug 20 00:28:37 2014 +0000
- Revision:
- 0:e11e8793c3ce
first release.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bongjun | 0:e11e8793c3ce | 1 | // pico_string.h 2013/8/27 |
Bongjun | 0:e11e8793c3ce | 2 | #pragma once |
Bongjun | 0:e11e8793c3ce | 3 | class pico_string { |
Bongjun | 0:e11e8793c3ce | 4 | public: |
Bongjun | 0:e11e8793c3ce | 5 | pico_string(){ |
Bongjun | 0:e11e8793c3ce | 6 | _len = 0; |
Bongjun | 0:e11e8793c3ce | 7 | _buf = (char*)malloc(1); |
Bongjun | 0:e11e8793c3ce | 8 | if (_buf) { |
Bongjun | 0:e11e8793c3ce | 9 | _buf[0] = '\0'; |
Bongjun | 0:e11e8793c3ce | 10 | } |
Bongjun | 0:e11e8793c3ce | 11 | } |
Bongjun | 0:e11e8793c3ce | 12 | ~pico_string() { |
Bongjun | 0:e11e8793c3ce | 13 | if (_buf) { |
Bongjun | 0:e11e8793c3ce | 14 | free(_buf); |
Bongjun | 0:e11e8793c3ce | 15 | } |
Bongjun | 0:e11e8793c3ce | 16 | } |
Bongjun | 0:e11e8793c3ce | 17 | bool empty() { |
Bongjun | 0:e11e8793c3ce | 18 | return _len == 0; |
Bongjun | 0:e11e8793c3ce | 19 | } |
Bongjun | 0:e11e8793c3ce | 20 | void append(const char* s, int len) { |
Bongjun | 0:e11e8793c3ce | 21 | if (_buf == NULL) { |
Bongjun | 0:e11e8793c3ce | 22 | return; |
Bongjun | 0:e11e8793c3ce | 23 | } |
Bongjun | 0:e11e8793c3ce | 24 | char* p = (char*)malloc(_len+len+1); |
Bongjun | 0:e11e8793c3ce | 25 | if (p == NULL) { |
Bongjun | 0:e11e8793c3ce | 26 | return; |
Bongjun | 0:e11e8793c3ce | 27 | } |
Bongjun | 0:e11e8793c3ce | 28 | memcpy(p, _buf, _len); |
Bongjun | 0:e11e8793c3ce | 29 | memcpy(p+_len, s, len); |
Bongjun | 0:e11e8793c3ce | 30 | p[_len+len] = '\0'; |
Bongjun | 0:e11e8793c3ce | 31 | free(_buf); |
Bongjun | 0:e11e8793c3ce | 32 | _buf = p; |
Bongjun | 0:e11e8793c3ce | 33 | } |
Bongjun | 0:e11e8793c3ce | 34 | void append(const char* s) { |
Bongjun | 0:e11e8793c3ce | 35 | append(s, strlen(s)); |
Bongjun | 0:e11e8793c3ce | 36 | } |
Bongjun | 0:e11e8793c3ce | 37 | char* c_str() { |
Bongjun | 0:e11e8793c3ce | 38 | if (_buf) { |
Bongjun | 0:e11e8793c3ce | 39 | return _buf; |
Bongjun | 0:e11e8793c3ce | 40 | } |
Bongjun | 0:e11e8793c3ce | 41 | return ""; |
Bongjun | 0:e11e8793c3ce | 42 | } |
Bongjun | 0:e11e8793c3ce | 43 | private: |
Bongjun | 0:e11e8793c3ce | 44 | char* _buf; |
Bongjun | 0:e11e8793c3ce | 45 | int _len; |
Bongjun | 0:e11e8793c3ce | 46 | }; |