libStatGen Software 1
Loading...
Searching...
No Matches
CharBuffer.cpp
1/*
2 * Copyright (C) 2010 Regents of the University of Michigan
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <stdlib.h>
19#include "CharBuffer.h"
20
21CharBuffer::CharBuffer()
22 : myBuffer(NULL)
23{
24 myBuffer = (char *) malloc(DEFAULT_BUFFER_SIZE);
25 myBufferAllocatedLen = DEFAULT_BUFFER_SIZE;
26 reset();
27}
28
29
30CharBuffer::CharBuffer(int32_t initialSize)
31 : myBuffer(NULL)
32{
33 myBuffer = (char *) malloc(initialSize);
34 myBufferAllocatedLen = DEFAULT_BUFFER_SIZE;
35
36 reset();
37}
38
39
40CharBuffer::~CharBuffer()
41{
42 reset();
43 if(myBuffer != NULL)
44 {
45 free(myBuffer);
46 myBuffer = NULL;
47 }
48}
49
50
51// Copy Constructor
52CharBuffer::CharBuffer(const CharBuffer& buffer)
53 : myBuffer(NULL)
54{
55 myBuffer =
56 (char *) malloc(DEFAULT_BUFFER_SIZE);
57 myBufferAllocatedLen = DEFAULT_BUFFER_SIZE;
58
59 reset();
60
61 copy(buffer);
62}
63
64
65// Overload operator = to copy the passed in buffer into this buffer.
66CharBuffer& CharBuffer::operator = (const CharBuffer& buffer)
67{
68 copy(buffer);
69 return(*this);
70}
71
72
73// Overload operator = to copy the passed in buffer into this buffer.
74CharBuffer& CharBuffer::operator = (const std::string& stringBuffer)
75{
76 // First check lengh
77 if(prepareNewLength(stringBuffer.length()))
78 {
79 memcpy(myBuffer, stringBuffer.c_str(), stringBuffer.length());
80 }
81 // TODO: on failure of prepareNewLength, should it throw an exception?
82
83 return(*this);
84}
85
86
87bool CharBuffer::copy(const CharBuffer& buffer)
88{
89 // Check to see if the passed in value is the same as this.
90 if(this == &buffer)
91 {
92 return(true);
93 }
94
95 // Copy the buffer.
96 // First check lengh
97 prepareNewLength(buffer.myBufferLen);
98
99 memcpy(myBuffer, buffer.myBuffer, buffer.myBufferLen);
100 myBufferLen = buffer.myBufferLen;
101
102 return(true);
103}
104
105
106// Reset the buffer for a new entry, clearing out previous values.
107void CharBuffer::reset()
108{
109 myBufferLen = 0;
110 if(myBuffer != NULL)
111 {
112 myBuffer[0] = 0;
113 }
114}
115
116
117// Read from a file into the buffer. length is the amount of data to read.
118// Returns the number of bytes read.
119int CharBuffer::readFromFile(IFILE filePtr, int32_t length)
120{
121 if(filePtr == NULL)
122 {
123 return(0);
124 }
125
126 if(prepareNewLength(length))
127 {
128 return(ifread(filePtr, myBuffer, length));
129 }
130 // failed to setup the buffer, return false.
131 return(false);
132}
133
134
135// newLen is the new length that this buffer needs to be.
136bool CharBuffer::prepareNewLength(int32_t newLen)
137{
138 if(newLen < 0)
139 {
140 // Invalid length.
141 return(false);
142 }
143
144 // myBufferAllocatedLen must be bigger than new length, because the
145 // newLen position is set to 0.
146 if(myBufferAllocatedLen <= newLen)
147 {
148 // Not enough space is allocated, so allocate more space.
149 char* tmpBufferPtr = (char *)realloc(myBuffer, newLen);
150 if(tmpBufferPtr == NULL)
151 {
152 // FAILED to allocate memory
153 fprintf(stderr, "FAILED TO ALLOCATE MEMORY!!!");
154 // myStatus.addError(GlfStatus::FAIL_MEM, "Failed Memory Allocation.");
155 return(false);
156 }
157 // Successfully allocated memory, so set myRecordPtr.
158 myBuffer = tmpBufferPtr;
159 myBufferAllocatedLen = newLen;
160 }
161 myBufferLen = newLen;
162 myBuffer[newLen] = 0;
163 return(true);
164}
165
unsigned int ifread(IFILE file, void *buffer, unsigned int size)
Read up to size bytes from the file into the buffer.
Definition InputFile.h:600
Class for easily reading/writing files without having to worry about file type (uncompressed,...
Definition InputFile.h:37