1 /* 2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef PATH_BUFFER_H 6 #define PATH_BUFFER_H 7 8 9 #include <string.h> 10 11 #include <algorithm> 12 13 14 namespace { 15 16 struct PathBuffer { 17 PathBuffer() 18 : 19 fBuffer(NULL), 20 fSize(0), 21 fLength(0) 22 { 23 } 24 25 PathBuffer(char* buffer, size_t size, size_t length = 0) 26 { 27 SetTo(buffer, size, length); 28 } 29 30 void SetTo(char* buffer, size_t size, size_t length = 0) 31 { 32 fBuffer = buffer; 33 fSize = size; 34 fLength = length; 35 if (fLength < fSize) 36 fBuffer[fLength] = '\0'; 37 } 38 39 bool Append(const char* toAppend, size_t length) 40 { 41 if (length > 0 && fLength + 1 < fSize) { 42 size_t toCopy = std::min(length, fSize - fLength - 1); 43 memcpy(fBuffer + fLength, toAppend, toCopy); 44 fBuffer[fLength + toCopy] = '\0'; 45 } 46 47 fLength += length; 48 return fLength < fSize; 49 } 50 51 bool Append(const char* toAppend) 52 { 53 return Append(toAppend, strlen(toAppend)); 54 } 55 56 bool Append(char c) 57 { 58 return Append(&c, 1); 59 } 60 61 size_t Length() const 62 { 63 return fLength; 64 } 65 66 private: 67 char* fBuffer; 68 size_t fSize; 69 size_t fLength; 70 }; 71 72 } 73 74 75 #endif // PATH_BUFFER_H 76