diff options
Diffstat (limited to 'src/bufstr.h')
-rw-r--r-- | src/bufstr.h | 46 |
1 files changed, 30 insertions, 16 deletions
diff --git a/src/bufstr.h b/src/bufstr.h index 225078f..bc0ef60 100644 --- a/src/bufstr.h +++ b/src/bufstr.h @@ -29,10 +29,10 @@ class BufStr { public: - BufStr(uint size) + BufStr(size_t size) : m_size(size), m_writeOffset(0), m_spareRoom(10240), m_buf(0) { - m_buf = (char *)calloc(size,1); + m_buf = static_cast<char *>(calloc(size,1)); } ~BufStr() { @@ -43,37 +43,37 @@ class BufStr makeRoomFor(1); m_buf[m_writeOffset++]=c; } - void addArray(const char *a,uint len) + void addArray(const char *a,size_t len) { makeRoomFor(len); memcpy(m_buf+m_writeOffset,a,len); m_writeOffset+=len; } - void skip(uint s) + void skip(size_t s) { makeRoomFor(s); m_writeOffset+=s; } - void shrink( uint newlen ) + void shrink( size_t newlen ) { m_writeOffset=newlen; resize(newlen); } - void resize( uint newlen ) + void resize( size_t newlen ) { - uint oldsize = m_size; + size_t oldsize = m_size; m_size=newlen; if (m_writeOffset>=m_size) // offset out of range -> enlarge { m_size=m_writeOffset+m_spareRoom; } - m_buf = (char *)realloc(m_buf,m_size); + m_buf = static_cast<char *>(realloc(m_buf,m_size)); if (m_size>oldsize) { memset(m_buf+oldsize,0,m_size-oldsize); } } - uint size() const + size_t size() const { return m_size; } @@ -81,7 +81,7 @@ class BufStr { return m_buf; } - char &at(uint i) const + char &at(size_t i) const { return m_buf[i]; } @@ -93,28 +93,42 @@ class BufStr { return m_buf; } - uint curPos() const + size_t curPos() const { return m_writeOffset; } - void dropFromStart(uint bytes) + void dropFromStart(size_t bytes) { if (bytes>m_size) bytes=m_size; if (bytes>0) qmemmove(m_buf,m_buf+bytes,m_size-bytes); m_size-=bytes; m_writeOffset-=bytes; } + void addTerminalCharIfMissing(char c) + { + if (m_buf && m_writeOffset>1 && m_buf[m_writeOffset-2]!=c && m_buf[m_writeOffset-1]=='\0') + { + // add missing terminal character and 0 terminator + m_buf[m_writeOffset-1] = '\n'; + addChar('\0'); + } + else if (m_buf && m_writeOffset>0 && m_buf[m_writeOffset-1]!='\0' && m_buf[m_writeOffset-1]!=c) + { + // add missing terminal character without 0 terminator + addChar(c); + } + } private: - void makeRoomFor(uint size) + void makeRoomFor(size_t size) { if (m_writeOffset+size>=m_size) { resize(m_size+size+m_spareRoom); } } - uint m_size; - uint m_writeOffset; - const uint m_spareRoom; // 10Kb extra room to avoid frequent resizing + size_t m_size; + size_t m_writeOffset; + const size_t m_spareRoom; // 10Kb extra room to avoid frequent resizing char *m_buf; }; |