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