I'm going to use these classes in a program I'm working on, so I want to see if they're correct or could be improved. https://pastebin.com/qx7ccteT
#include <cstdio>
#include <cstdlib>
class FileHelper
{
public:
FILE* f = NULL;
char buffer[8192];
int bufferPosition = 0;
int bytesRead = 0;
FileHelper(const char* filePath)
{
f = std::fopen(filePath, "rb");
if (!f)
{
throw "FileHelper fopen failure in constructor";
}
}
~FileHelper()
{
if (f)
{
std::fclose(f);
f = NULL;
}
}
bool getCharacter(char& ch)
{
if (bufferPosition == 8192 || bufferPosition == bytesRead)
{
bufferPosition = 0;
bytesRead = fread(buffer, sizeof(char), 8192, f);
if (!bytesRead)
{
return false;
}
}
ch = buffer[bufferPosition++];
return true;
}
};
template <typename T>
class ArrayHelper
{
public:
T* arr = NULL;
size_t capacity = 0;
size_t arrPosition = 0;
ArrayHelper()
{
arr = (T*)std::malloc(8 * sizeof(T));
if (!arr)
{
throw "ArrayHelper malloc failure in constructor";
}
capacity = 8;
}
~ArrayHelper()
{
if (arr)
{
std::free(arr);
arr = NULL;
}
}
void append(T value)
{
if (arrPosition == capacity)
{
// python's overallocation
int newCapacity = ((size_t)(capacity + 1) + ((capacity + 1) >> 3) + 6) & ~(size_t)3;
T* temp = arr;
arr = (T*)std::realloc(arr, newCapacity * sizeof(T));
if (!arr)
{
arr = temp;
throw "ArrayHelper realloc failure in append method";
}
capacity = newCapacity;
}
arr[arrPosition++] = value;
}
void clear()
{
arrPosition = 0;
}
// copies given to PointerArrayHelper
T* copy()
{
if (!arrPosition)
{
return NULL;
}
T* arrCopy = (T*)std::malloc(arrPosition * sizeof(T));
if (!arrCopy)
{
throw "ArrayHelper malloc failure in copy method";
}
for (size_t i = 0; i < arrPosition; i++)
{
arrCopy[i] = arr[i];
}
return arrCopy;
}
};
template <typename T>
class PointerArrayHelper
{
public:
T* arr = NULL;
size_t capacity = 0;
size_t arrPosition = 0;
PointerArrayHelper()
{
arr = (T*)std::malloc(8 * sizeof(T));
if (!arr)
{
throw "PointerArrayHelper malloc failure in constructor";
}
capacity = 8;
}
~PointerArrayHelper()
{
if (arr)
{
for (size_t i = 0; i < arrPosition; i++)
{
if (arr[i])
{
std::free(arr[i]);
arr[i] = NULL;
}
}
std::free(arr);
arr = NULL;
}
}
template <typename U>
void append(ArrayHelper<U>& arrHelper)
{
if (arrPosition == capacity)
{
// python's overallocation
int newCapacity = ((size_t)(capacity + 1) + ((capacity + 1) >> 3) + 6) & ~(size_t)3;
T* temp = arr;
arr = (T*)std::realloc(arr, newCapacity * sizeof(T));
if (!arr)
{
arr = temp;
throw "PointerArrayHelper realloc failure in append method";
}
capacity = newCapacity;
}
arr[arrPosition++] = arrHelper.copy();
}
void shrink()
{
T* temp = arr;
arr = (T*)std::realloc(arr, arrPosition * sizeof(T));
if (!arr)
{
arr = temp;
throw "PointerArrayHelper realloc failure in shrink method";
}
capacity = arrPosition;
}
};
int main()
{
try
{
FileHelper fh("test.txt");
PointerArrayHelper<char*> ptrArrHelper;
ArrayHelper<char> arrHelper;
char ch = '\0';
while (fh.getCharacter(ch))
{
if (ch == '\n')
{
arrHelper.append('\0');
ptrArrHelper.append(arrHelper);
arrHelper.clear();
}
else
{
arrHelper.append(ch);
}
}
if (ch != '\n')
{
arrHelper.append('\0');
ptrArrHelper.append(arrHelper);
}
ptrArrHelper.shrink();
for (size_t i = 0; i < ptrArrHelper.capacity; i++)
{
std::printf("%s\n", ptrArrHelper.arr[i]);
}
}
catch (char const* e)
{
std::printf("%s\n", e);
}
return 0;
}
std::fstream
andstd::vector
? \$\endgroup\$if (bufferPosition == 8192 || bufferPosition == bytesRead)
to justif (bufferPosition == bytesRead)
? \$\endgroup\$FILE*
wrapper. \$\endgroup\$