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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/*
* Copyright (C) 2010 Regents of the University of Michigan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include "CharBuffer.h"
CharBuffer::CharBuffer()
: myBuffer(NULL)
{
myBuffer = (char *) malloc(DEFAULT_BUFFER_SIZE);
myBufferAllocatedLen = DEFAULT_BUFFER_SIZE;
reset();
}
CharBuffer::CharBuffer(int32_t initialSize)
: myBuffer(NULL)
{
myBuffer = (char *) malloc(initialSize);
myBufferAllocatedLen = DEFAULT_BUFFER_SIZE;
reset();
}
CharBuffer::~CharBuffer()
{
reset();
if(myBuffer != NULL)
{
free(myBuffer);
myBuffer = NULL;
}
}
// Copy Constructor
CharBuffer::CharBuffer(const CharBuffer& buffer)
: myBuffer(NULL)
{
myBuffer =
(char *) malloc(DEFAULT_BUFFER_SIZE);
myBufferAllocatedLen = DEFAULT_BUFFER_SIZE;
reset();
copy(buffer);
}
// Overload operator = to copy the passed in buffer into this buffer.
CharBuffer& CharBuffer::operator = (const CharBuffer& buffer)
{
copy(buffer);
return(*this);
}
// Overload operator = to copy the passed in buffer into this buffer.
CharBuffer& CharBuffer::operator = (const std::string& stringBuffer)
{
// First check lengh
if(prepareNewLength(stringBuffer.length()))
{
memcpy(myBuffer, stringBuffer.c_str(), stringBuffer.length());
}
// TODO: on failure of prepareNewLength, should it throw an exception?
return(*this);
}
bool CharBuffer::copy(const CharBuffer& buffer)
{
// Check to see if the passed in value is the same as this.
if(this == &buffer)
{
return(true);
}
// Copy the buffer.
// First check lengh
prepareNewLength(buffer.myBufferLen);
memcpy(myBuffer, buffer.myBuffer, buffer.myBufferLen);
myBufferLen = buffer.myBufferLen;
return(true);
}
// Reset the buffer for a new entry, clearing out previous values.
void CharBuffer::reset()
{
myBufferLen = 0;
if(myBuffer != NULL)
{
myBuffer[0] = 0;
}
}
// Read from a file into the buffer. length is the amount of data to read.
// Returns the number of bytes read.
int CharBuffer::readFromFile(IFILE filePtr, int32_t length)
{
if(filePtr == NULL)
{
return(0);
}
if(prepareNewLength(length))
{
return(ifread(filePtr, myBuffer, length));
}
// failed to setup the buffer, return false.
return(false);
}
// newLen is the new length that this buffer needs to be.
bool CharBuffer::prepareNewLength(int32_t newLen)
{
if(newLen < 0)
{
// Invalid length.
return(false);
}
// myBufferAllocatedLen must be bigger than new length, because the
// newLen position is set to 0.
if(myBufferAllocatedLen <= newLen)
{
// Not enough space is allocated, so allocate more space.
char* tmpBufferPtr = (char *)realloc(myBuffer, newLen);
if(tmpBufferPtr == NULL)
{
// FAILED to allocate memory
fprintf(stderr, "FAILED TO ALLOCATE MEMORY!!!");
// myStatus.addError(GlfStatus::FAIL_MEM, "Failed Memory Allocation.");
return(false);
}
// Successfully allocated memory, so set myRecordPtr.
myBuffer = tmpBufferPtr;
myBufferAllocatedLen = newLen;
}
myBufferLen = newLen;
myBuffer[newLen] = 0;
return(true);
}
|