amath/src/lib/charbuf.cpp

311 lines
6.3 KiB
C++
Raw Normal View History

2017-02-27 22:23:06 +00:00
/*-
2018-08-04 18:44:35 +00:00
* Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
2015-03-08 22:14:50 +00:00
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
2017-02-27 22:23:06 +00:00
* Project homepage:
2017-07-14 09:30:24 +00:00
* https://amath.innolan.net
2017-02-27 22:23:06 +00:00
*
2015-03-08 22:14:50 +00:00
*/
2017-02-27 22:23:06 +00:00
#include "amath.h"
#include "amathc.h"
#include "charbuf.h"
2015-03-08 22:14:50 +00:00
/**
* @brief Initialize without allocating memory.
*
*/
CharBuffer::CharBuffer()
{
2017-02-27 22:23:06 +00:00
buf = nullptr;
2015-03-08 22:14:50 +00:00
ptr = buf;
cursize = 0;
}
/**
* @brief Initialize while allocating specified amount of memory.
*
*/
CharBuffer::CharBuffer(unsigned int size)
{
cursize = (size < minimumSize ? minimumSize : size);
buf = new char[cursize];
ptr = buf;
}
CharBuffer::~CharBuffer()
{
ClearBuffer();
}
/**
* @brief Release memory in buffer.
*
*/
void CharBuffer::ClearBuffer()
{
2017-02-27 22:23:06 +00:00
if (buf != nullptr)
{
2015-03-08 22:14:50 +00:00
delete [] buf;
}
2017-02-27 22:23:06 +00:00
buf = nullptr;
2015-03-08 22:14:50 +00:00
ptr = buf;
cursize = 0;
}
/**
* @brief Release memory, allocate and copy source.
*
*/
void CharBuffer::ClearAndCopy(const char* source)
{
ClearBuffer();
cursize = AllocAndCopy(&buf, source);
2015-04-13 21:44:17 +00:00
ptr = buf + cursize - sizeof(char);
2015-03-08 22:14:50 +00:00
}
/**
* @brief Release memory and allocate new size.
*
*/
void CharBuffer::ClearAndAlloc(unsigned int size)
{
ClearBuffer();
cursize = (size < minimumSize ? minimumSize : size);
buf = new char[cursize];
ptr = buf;
}
void CharBuffer::EnsureMinimumSize()
{
2017-02-27 22:23:06 +00:00
if (buf == nullptr)
{
2015-03-08 22:14:50 +00:00
unsigned int size = minimumSize;
buf = new char[size];
ptr = buf;
}
}
/**
2017-02-27 22:23:06 +00:00
* @brief Ensure a memory block of specified size is allocated.
2015-03-08 22:14:50 +00:00
*
*/
void CharBuffer::EnsureSize(unsigned int size)
{
2017-02-27 22:23:06 +00:00
if (cursize < size)
{
2015-03-08 22:14:50 +00:00
unsigned int tempsize = cursize;
cursize = (size < minimumSize ? minimumSize : size);
2017-02-27 22:23:06 +00:00
if (buf == nullptr)
{ // Nothing allocated yet. Just allocate requested size.
2015-03-08 22:14:50 +00:00
buf = new char[cursize];
ptr = buf;
2017-02-27 22:23:06 +00:00
}
else if (buf == ptr)
{ // Already allocated but buffer is empty.
2015-03-08 22:14:50 +00:00
delete [] buf;
buf = new char[cursize];
ptr = buf;
2017-02-27 22:23:06 +00:00
}
else
{ // Buffer already in use.
2015-03-08 22:14:50 +00:00
// Make at least double size
cursize = cursize < tempsize * 2 ? tempsize * 2 : cursize;
2017-02-04 13:41:34 +00:00
unsigned int offset = (unsigned int)(ptr - buf);
2017-02-27 22:23:06 +00:00
char* temp = new char[cursize];
MemCopy(temp, buf, tempsize);
2015-03-08 22:14:50 +00:00
delete [] buf;
buf = temp;
ptr = buf + offset;
}
}
}
void CharBuffer::EnsureSize(unsigned int blocksize, unsigned int blocks)
{
2017-02-27 22:23:06 +00:00
if (cursize < blocksize * blocks)
{
if (buf == nullptr)
{
2015-03-08 22:14:50 +00:00
cursize = blocksize * blocks;
buf = new char[cursize];
ptr = buf;
2017-02-27 22:23:06 +00:00
}
else
{
2017-02-04 13:41:34 +00:00
unsigned int tptr = (unsigned int)(ptr - buf);
2017-02-27 22:23:06 +00:00
char* temp = new char[blocksize * blocks];
MemCopy(temp, buf, cursize);
2015-03-08 22:14:50 +00:00
delete [] buf;
cursize = blocksize * blocks;
buf = temp;
ptr = buf + tptr;
}
}
}
void CharBuffer::EnsureGrowth(unsigned int size)
{
2017-02-04 13:41:34 +00:00
EnsureSize((unsigned int)((ptr - buf) + size));
2015-03-08 22:14:50 +00:00
}
2017-04-12 22:50:51 +00:00
bool CharBuffer::IsEmpty() const
{
char* i = buf;
if (i == nullptr || buf == ptr)
return true;
do
{
// Blank space characters
if (*i != ' ' && *i != '\t' && *i != '\r' && *i != '\n')
return false;
i++;
}
while (i != ptr);
return true;
}
2017-02-27 22:23:06 +00:00
bool CharBuffer::Is(const char* string) const
2015-03-08 22:14:50 +00:00
{
return StrIsEqual(GetString(), string);
}
2017-02-27 22:23:06 +00:00
bool CharBuffer::Contains(const char c) const
2017-02-04 11:52:26 +00:00
{
char* i = buf;
2017-02-27 22:23:06 +00:00
if (i == nullptr || buf == ptr)
2017-02-04 11:52:26 +00:00
return false;
do
{
if (*i == c)
return true;
i++;
2017-02-27 22:23:06 +00:00
}
while (i != ptr);
2017-02-04 11:52:26 +00:00
return false;
}
2015-03-08 22:14:50 +00:00
void CharBuffer::Empty()
{
2017-02-27 22:23:06 +00:00
if (buf == nullptr)
{
2015-03-08 22:14:50 +00:00
EnsureMinimumSize();
}
ptr = buf;
}
void CharBuffer::DeleteLastChar()
{
ptr--;
}
void CharBuffer::Copy(CharBuffer* source)
{
EnsureSize(source->cursize);
2017-02-27 22:23:06 +00:00
const char* s = source->GetString();
2015-03-08 22:14:50 +00:00
ptr = buf;
2017-02-27 22:23:06 +00:00
// ReSharper disable once CppPossiblyErroneousEmptyStatements
while ((*ptr++ = *s++));
2015-03-08 22:14:50 +00:00
ptr--;
}
void CharBuffer::Append(const char c)
{
*ptr++ = c;
}
void CharBuffer::Append(const char c, unsigned int count)
{
2017-02-27 22:23:06 +00:00
if (count == 0)
{
2015-03-08 22:14:50 +00:00
return;
}
unsigned int n = count;
while (n--)
*ptr++ = c;
}
void CharBuffer::Append(const char* source)
{
2017-02-27 22:23:06 +00:00
// ReSharper disable once CppPossiblyErroneousEmptyStatements
2015-03-08 22:14:50 +00:00
while ((*ptr++ = *source++));
ptr--;
}
bool CharBuffer::RemoveTrailing(const char c)
{
2017-02-27 22:23:06 +00:00
if (ptr == buf)
{
return false;
}
2017-02-27 22:23:06 +00:00
if (*(--ptr) == c)
{
2015-03-08 22:14:50 +00:00
return true;
}
2015-04-01 12:43:50 +00:00
ptr++;
2015-03-08 22:14:50 +00:00
return false;
}
2017-02-27 22:23:06 +00:00
bool CharBuffer::RemoveTrailing(const char* string)
2015-04-13 21:44:17 +00:00
{
int len = StrLen(string) * sizeof(char);
char* s = ptr - len;
2017-02-27 22:23:06 +00:00
if (s < buf)
{
2015-04-13 21:44:17 +00:00
return false;
}
*ptr = '\0';
2017-02-27 22:23:06 +00:00
if (StrIsEqual(s, string))
{
2015-04-13 21:44:17 +00:00
ptr = s;
return true;
}
return false;
}
2017-02-27 22:23:06 +00:00
char* CharBuffer::GetString() const
2015-03-08 22:14:50 +00:00
{
*ptr = '\0';
return buf;
}