1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
/*
* Variable-size, reference-counted memory buffer
*
* Copyright (C) 2003--2013 Enrico Zini <enrico@debian.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <wibble/sys/buffer.h>
#include <wibble/string.h>
#include <string.h> // memcpy
#include <stdlib.h> // malloc, free, realloc
namespace wibble {
namespace sys {
Buffer::Data::Data(size_t size) : _ref(0), _size(size)
{
_data = malloc(size);
}
Buffer::Data::Data(void* buf, size_t size, bool own)
: _ref(0), _size(size)
{
if (own)
_data = buf;
else
{
_data = malloc(size);
memcpy(_data, buf, size);
}
}
Buffer::Data::Data(const void* buf, size_t size)
: _ref(0), _size(size)
{
_data = malloc(size);
memcpy(_data, buf, size);
}
Buffer::Data::~Data()
{
if (_data)
free(_data);
}
void Buffer::Data::resize(size_t size)
{
if (size == 0)
{
if (_data)
{
free(_data);
_data = 0;
}
} else if (_data == 0) {
_data = malloc(size);
} else {
_data = realloc(_data, size);
}
_size = size;
}
/// Compare the contents of two buffers
bool Buffer::Data::operator==(const Data& d) const throw()
{
if (_size != d._size)
return false;
if (_data == 0 && d._data == 0)
return true;
if (_data == 0 || d._data == 0)
return false;
return memcmp(_data, d._data, _size) == 0;
}
/// Compare the contents of two buffers
bool Buffer::Data::operator<(const Data& d) const throw()
{
if (_size < d._size)
return true;
if (_size > d._size)
return false;
if (_data == 0 && d._data == 0)
return false;
if (_data == 0)
return true;
if (d._data == 0)
return false;
return memcmp(_data, d._data, _size) < 0;
}
std::string Buffer::print_preview(unsigned size) const
{
if (this->size() > size)
{
std::string res = str::c_escape(std::string((const char*)data(), 100));
res += "[...]";
return res;
} else {
return str::c_escape(std::string((const char*)data(), this->size()));
}
}
std::ostream& operator<<(std::ostream& o, const Buffer& b)
{
return o << b.print_preview(100);
}
}
}
// vim:set ts=4 sw=4:
|