1
0
Fork 0
arangodb/lib/Basics/StringBuffer.h

719 lines
28 KiB
C++

////////////////////////////////////////////////////////////////////////////////
/// @brief string buffer with formatting routines
///
/// @file
///
/// @warning You must initialise the classes by calling initialise or by
/// setting everything to 0. This can be done by using "new
/// StringBuffer()". You must call free to free the allocated
/// memory.
///
/// WARNING: this must be a POD object!
///
/// DISCLAIMER
///
/// Copyright 2004-2012 triagens GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is triAGENS GmbH, Cologne, Germany
///
/// @author Dr. Frank Celler
/// @author Copyright 2006-2012, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////
#ifndef TRIAGENS_BASICS_STRING_BUFFER_H
#define TRIAGENS_BASICS_STRING_BUFFER_H 1
#include "Basics/Common.h"
#include "BasicsC/string-buffer.h"
// -----------------------------------------------------------------------------
// string buffer with formatting routines
// -----------------------------------------------------------------------------
#ifdef STRING_BUFFER_MACROS
#define DEFSTR(a,b) static const char __STRING_ ## a [] = b
#define STR(a) __STRING_ ## a
#define LENSTR(a) (sizeof(__STRING_ ## a) - 1)
#endif
namespace triagens {
namespace basics {
// -----------------------------------------------------------------------------
// --SECTION-- class Logger
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Strings
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief string buffer with formatting routines
////////////////////////////////////////////////////////////////////////////////
class StringBuffer {
private:
StringBuffer (StringBuffer const&);
StringBuffer& operator= (StringBuffer const&);
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- constructors and destructors
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Strings
/// @{
////////////////////////////////////////////////////////////////////////////////
public:
////////////////////////////////////////////////////////////////////////////////
/// @brief initialises the string buffer
////////////////////////////////////////////////////////////////////////////////
explicit
StringBuffer (TRI_memory_zone_t* zone) {
TRI_InitStringBuffer(&_buffer, zone);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief initialises the string buffer
////////////////////////////////////////////////////////////////////////////////
StringBuffer (TRI_memory_zone_t* zone, const size_t initialSize) {
TRI_InitSizedStringBuffer(&_buffer, zone, initialSize);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief frees the string buffer
////////////////////////////////////////////////////////////////////////////////
~StringBuffer () {
TRI_DestroyStringBuffer(&_buffer);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief frees the string buffer and cleans the buffer
////////////////////////////////////////////////////////////////////////////////
void annihilate () {
TRI_AnnihilateStringBuffer(&_buffer);
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Strings
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief returns the low level buffer
////////////////////////////////////////////////////////////////////////////////
TRI_string_buffer_t* stringBuffer () {
return &_buffer;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief swaps content with another string buffer
////////////////////////////////////////////////////////////////////////////////
StringBuffer& swap (StringBuffer * other) {
TRI_SwapStringBuffer(&_buffer, &other->_buffer);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief returns pointer to the character buffer
////////////////////////////////////////////////////////////////////////////////
const char * c_str () const {
return TRI_BeginStringBuffer(&_buffer);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief returns pointer to the beginning of the character buffer
////////////////////////////////////////////////////////////////////////////////
const char * begin () const {
return TRI_BeginStringBuffer(&_buffer);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief returns pointer to the end of the character buffer
////////////////////////////////////////////////////////////////////////////////
const char * end () const {
return TRI_EndStringBuffer(&_buffer);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief returns length of the character buffer
////////////////////////////////////////////////////////////////////////////////
size_t length () const {
return TRI_LengthStringBuffer(&_buffer);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief returns true if buffer is empty
////////////////////////////////////////////////////////////////////////////////
bool empty () const {
return TRI_EmptyStringBuffer(&_buffer);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief clears the buffer
////////////////////////////////////////////////////////////////////////////////
StringBuffer& clear () {
TRI_ClearStringBuffer(&_buffer);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief assigns text from a string
////////////////////////////////////////////////////////////////////////////////
StringBuffer& operator= (string const& str) {
TRI_ReplaceStringStringBuffer(&_buffer, str.c_str(), str.length());
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief copies the string buffer
////////////////////////////////////////////////////////////////////////////////
StringBuffer& copy (StringBuffer const& source) {
TRI_CopyStringBuffer(&_buffer, &source._buffer);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief removes the first characters
////////////////////////////////////////////////////////////////////////////////
StringBuffer& erase_front (size_t len) {
TRI_EraseFrontStringBuffer(&_buffer, len);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief replaces characters
////////////////////////////////////////////////////////////////////////////////
StringBuffer& replaceText (const char * str, size_t len) {
TRI_ReplaceStringStringBuffer(&_buffer, str, len);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief replaces characters
////////////////////////////////////////////////////////////////////////////////
StringBuffer& replaceText (StringBuffer const& text) {
TRI_ReplaceStringStringBuffer(&_buffer, text.c_str(), text.length());
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- STRING AND CHARATCER APPENDERS
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// --SECTION-- public methods
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Strings
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief appends eol character
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendEol () {
TRI_AppendCharStringBuffer(&_buffer, '\n');
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends character
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendChar (char chr) {
TRI_AppendCharStringBuffer(&_buffer, chr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends characters
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendText (const char * str, size_t len) {
TRI_AppendString2StringBuffer(&_buffer, str, len);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends characters
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendText (const char * str) {
TRI_AppendStringStringBuffer(&_buffer, str);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends string
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendText (string const& str) {
TRI_AppendString2StringBuffer(&_buffer, str.c_str(), str.length());
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends a string buffer
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendText (StringBuffer const& text) {
TRI_AppendString2StringBuffer(&_buffer, text.c_str(), text.length());
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- INTEGER APPENDERS
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// --SECTION-- public functions
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Strings
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief appends integer with two digits
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendInteger2 (uint32_t attr) {
TRI_AppendInteger2StringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends integer with three digits
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendInteger3 (uint32_t attr) {
TRI_AppendInteger3StringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends integer with four digits
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendInteger4 (uint32_t attr) {
TRI_AppendInteger4StringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends integer with 8 bits
///
/// This method is implemented here in order to allow inlining.
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendInteger (int8_t attr) {
TRI_AppendInt8StringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends unsigned integer with 8 bits
///
/// This method is implemented here in order to allow inlining.
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendInteger (uint8_t attr) {
TRI_AppendUInt8StringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends integer with 16 bits
///
/// This method is implemented here in order to allow inlining.
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendInteger (int16_t attr) {
TRI_AppendInt16StringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends unsigned integer with 32 bits
///
/// This method is implemented here in order to allow inlining.
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendInteger (uint16_t attr) {
TRI_AppendUInt16StringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends integer with 32 bits
///
/// This method is implemented here in order to allow inlining.
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendInteger (int32_t attr) {
TRI_AppendInt32StringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends unsigned integer with 32 bits
///
/// This method is implemented here in order to allow inlining.
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendInteger (uint32_t attr) {
TRI_AppendUInt32StringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends integer with 64 bits
///
/// This method is implemented here in order to allow inlining.
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendInteger (int64_t attr) {
TRI_AppendInt64StringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends unsigned integer with 64 bits
///
/// This method is implemented here in order to allow inlining.
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendInteger (uint64_t attr) {
TRI_AppendUInt64StringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends size_t
///
/// This method is implemented here in order to allow inlining.
////////////////////////////////////////////////////////////////////////////////
#ifdef TRI_OVERLOAD_FUNCS_SIZE_T
StringBuffer& appendInteger (size_t attr) {
return appendInteger(sizetint_t(attr));
}
#endif
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- INTEGER OCTAL APPENDERS
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// --SECTION-- public functions
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Strings
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief appends unsigned integer with 32 bits in octal
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendOctal (uint32_t attr) {
TRI_AppendUInt32OctalStringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends unsigned integer with 64 bits in octal
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendOctal (uint64_t attr) {
TRI_AppendUInt64OctalStringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends size_t in octal
////////////////////////////////////////////////////////////////////////////////
#ifdef TRI_OVERLOAD_FUNCS_SIZE_T
StringBuffer& appendOctal (size_t attr) {
return appendOctal(sizetint_t(attr));
}
#endif
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- INTEGER HEX APPENDERS
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// --SECTION-- public functions
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Strings
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief appends unsigned integer with 32 bits in hex
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendHex (uint32_t attr) {
TRI_AppendUInt32HexStringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends unsigned integer with 64 bits in hex
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendHex (uint64_t attr) {
TRI_AppendUInt64HexStringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends size_t in hex
////////////////////////////////////////////////////////////////////////////////
#ifdef TRI_OVERLOAD_FUNCS_SIZE_T
StringBuffer& appendHex (size_t attr) {
return appendHex(sizetint_t(attr));
}
#endif
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- FLOAT APPENDERS
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// --SECTION-- public functions
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Strings
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief appends floating point number with 8 bits
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendDecimal (double attr) {
TRI_AppendDoubleStringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- DATE AND TIME APPENDERS
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// --SECTION-- public functions
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Strings
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief appends time in standard format
///
/// This method is implemented here in order to allow inlining.
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendTime (int32_t attr) {
TRI_AppendTimeStringBuffer(&_buffer, attr);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- CSV APPENDERS
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// --SECTION-- public functions
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Strings
/// @{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @brief appends csv string
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendCsvString (string const& text) {
// do not escape here, because some string - i.e. lists of identifier - have no special characters
TRI_AppendString2StringBuffer(&_buffer, text.c_str(), text.size());
TRI_AppendCharStringBuffer(&_buffer, ';');
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends csv 32-bit integer
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendCsvInteger (int32_t i) {
TRI_AppendCsvInt32StringBuffer(&_buffer, i);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends csv unisgned 32-bit integer
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendCsvInteger (uint32_t i) {
TRI_AppendCsvUInt32StringBuffer(&_buffer, i);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends csv 64-bit integer
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendCsvInteger (int64_t i) {
TRI_AppendCsvInt64StringBuffer(&_buffer, i);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends csv unsigned 64-bit integer
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendCsvInteger (uint64_t i) {
TRI_AppendCsvUInt64StringBuffer(&_buffer, i);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief appends csv double
////////////////////////////////////////////////////////////////////////////////
StringBuffer& appendCsvDouble (double d) {
TRI_AppendCsvDoubleStringBuffer(&_buffer, d);
return *this;
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// @addtogroup Strings
/// @{
////////////////////////////////////////////////////////////////////////////////
// -----------------------------------------------------------------------------
// --SECTION-- private variables
// -----------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////
/// @brief underlying C string buffer
////////////////////////////////////////////////////////////////////////////////
TRI_string_buffer_t _buffer;
};
}
}
////////////////////////////////////////////////////////////////////////////////
/// @}
////////////////////////////////////////////////////////////////////////////////
#endif
// Local Variables:
// mode: outline-minor
// outline-regexp: "^\\(/// @brief\\|/// {@inheritDoc}\\|/// @addtogroup\\|// --SECTION--\\|/// @\\}\\)"
// End: