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