Fixed leak in MString when assigning from another MString.
This commit is contained in:
1 parent
423af4eb52
commit
b44a00862b
31 files changed
+268
-4807
No files matched your search
+75
-105
@@ -5,127 +5,111 @@
|
||||
#include <iomanip>
|
||||
#include <stdlib.h>
|
||||
|
||||
namespace coreutils
|
||||
{
|
||||
|
||||
MString::MString()
|
||||
{
|
||||
namespace coreutils {
|
||||
|
||||
MString::MString() {
|
||||
data = NULL;
|
||||
length = 0;
|
||||
cursor = NULL;
|
||||
setSize(0);
|
||||
}
|
||||
|
||||
MString::MString(const char *data)
|
||||
{
|
||||
|
||||
MString::MString(const char *data) {
|
||||
setSize(strlen(data));
|
||||
memcpy(this->data, data, length);
|
||||
cursor = this->data;
|
||||
}
|
||||
|
||||
MString::MString(char *data, size_t length)
|
||||
{
|
||||
|
||||
MString::MString(char *data, size_t length) {
|
||||
setSize(length);
|
||||
memcpy(this->data, data, length);
|
||||
cursor = this->data;
|
||||
}
|
||||
|
||||
MString::MString(unsigned char *data, size_t length)
|
||||
{
|
||||
|
||||
MString::MString(unsigned char *data, size_t length) {
|
||||
setSize(length);
|
||||
memcpy(this->data, data, length);
|
||||
cursor = this->data;
|
||||
}
|
||||
|
||||
MString::MString(const char *data, size_t length)
|
||||
{
|
||||
|
||||
MString::MString(const char *data, size_t length) {
|
||||
setSize(length);
|
||||
memcpy(this->data, data, length);
|
||||
cursor = this->data;
|
||||
}
|
||||
|
||||
MString::MString(const MString &mstring)
|
||||
{
|
||||
|
||||
MString::MString(const MString &mstring) {
|
||||
setSize(mstring.length);
|
||||
memcpy(data, mstring.data, mstring.length);
|
||||
cursor = data;
|
||||
// Log(LOG_DEBUG_2) << "MString Copy Constructor: ";
|
||||
}
|
||||
|
||||
MString::MString(ZString &zstring)
|
||||
{
|
||||
setSize(zstring.getLength());
|
||||
memcpy(data, zstring.getData(), zstring.getLength());
|
||||
|
||||
MString::MString(MString &mstring) {
|
||||
setSize(mstring.getLength());
|
||||
memcpy(data, mstring.getData(), mstring.getLength());
|
||||
cursor = data;
|
||||
}
|
||||
|
||||
MString::MString(ZString zstring)
|
||||
{
|
||||
setSize(zstring.getLength());
|
||||
memcpy(data, zstring.getData(), zstring.getLength());
|
||||
cursor = data;
|
||||
}
|
||||
|
||||
MString::MString(std::string data)
|
||||
{
|
||||
|
||||
// MString::MString(MString mstring) {
|
||||
// setSize(mstring.getLength());
|
||||
// memcpy(data, mstring.getData(), mstring.getLength());
|
||||
// cursor = data;
|
||||
// printf("MString Copy Constructor %p\n", this);
|
||||
// }
|
||||
|
||||
MString::MString(std::string data) {
|
||||
setSize(data.length());
|
||||
memcpy(this->data, (char *)data.c_str(), data.length());
|
||||
cursor = this->data;
|
||||
}
|
||||
|
||||
MString::MString(std::FILE *file)
|
||||
{
|
||||
|
||||
MString::MString(std::FILE *file) {
|
||||
setSize(1000000);
|
||||
this->length = fread(this->data, 1000000, 1000000, file);
|
||||
cursor = this->data;
|
||||
}
|
||||
|
||||
MString::~MString()
|
||||
{
|
||||
|
||||
MString::~MString() {
|
||||
if (data)
|
||||
free(data);
|
||||
free(data);
|
||||
}
|
||||
|
||||
MString &MString::operator=(coreutils::ZString &value)
|
||||
{
|
||||
|
||||
MString &MString::operator=(coreutils::MString &value) {
|
||||
setSize(value.getLength());
|
||||
memcpy(data, value.getData(), value.getLength());
|
||||
length = value.getLength();
|
||||
return *this;
|
||||
}
|
||||
|
||||
MString &MString::operator=(const char *value)
|
||||
{
|
||||
|
||||
MString &MString::operator=(const char *value) {
|
||||
int len = strlen(value);
|
||||
setSize(len);
|
||||
memcpy(data, value, len);
|
||||
return *this;
|
||||
}
|
||||
|
||||
MString &MString::operator=(char value)
|
||||
{
|
||||
|
||||
MString &MString::operator=(char value) {
|
||||
int len = 1;
|
||||
setSize(1);
|
||||
*data = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
MString &MString::operator<<(const char *value)
|
||||
{
|
||||
|
||||
MString &MString::operator<<(const char *value) {
|
||||
int temp = length;
|
||||
int len = strlen(value);
|
||||
setSize(len + length);
|
||||
memcpy(data + temp, value, len);
|
||||
return *this;
|
||||
}
|
||||
|
||||
MString &MString::operator<<(const int value)
|
||||
{
|
||||
|
||||
MString &MString::operator<<(const int value) {
|
||||
std::stringstream temp;
|
||||
temp << value;
|
||||
*this << temp.str();
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
// MString& MString::operator<<(coreutils::ZString &zstring) {
|
||||
// int temp = length;
|
||||
// int len = length + zstring.getLength();
|
||||
@@ -133,100 +117,86 @@ namespace coreutils
|
||||
// memcpy(data + temp, zstring.getData(), zstring.getLength());
|
||||
// return *this;
|
||||
// }
|
||||
|
||||
MString &MString::operator<<(coreutils::ZString zstring)
|
||||
{
|
||||
|
||||
MString &MString::operator<<(coreutils::ZString zstring) {
|
||||
int temp = length;
|
||||
int len = length + zstring.getLength();
|
||||
setSize(len);
|
||||
memcpy(data + temp, zstring.getData(), zstring.getLength());
|
||||
return *this;
|
||||
}
|
||||
|
||||
MString &MString::operator<<(std::string value)
|
||||
{
|
||||
|
||||
MString &MString::operator<<(std::string value) {
|
||||
int temp = length;
|
||||
int len = length + value.length();
|
||||
setSize(len);
|
||||
memcpy(data + temp, value.c_str(), value.length());
|
||||
return *this;
|
||||
}
|
||||
|
||||
void MString::insert(char ch, int offset)
|
||||
{
|
||||
|
||||
void MString::insert(char ch, int offset) {
|
||||
setSize(length + 1);
|
||||
for (int ix = length; ix >= offset; ix--)
|
||||
getData()[ix + 1] = getData()[ix];
|
||||
getData()[ix + 1] = getData()[ix];
|
||||
data[offset] = ch;
|
||||
}
|
||||
|
||||
void MString::insert(ZString &value, int offset)
|
||||
{
|
||||
|
||||
void MString::insert(ZString &value, int offset) {
|
||||
setSize(length + value.getLength());
|
||||
for (int ix = length; ix >= offset; ix--)
|
||||
getData()[ix + value.getLength()] = getData()[ix];
|
||||
getData()[ix + value.getLength()] = getData()[ix];
|
||||
std::memcpy(data + offset, value.getData(), value.getLength());
|
||||
}
|
||||
|
||||
void MString::insert(std::string value, int offset)
|
||||
{
|
||||
|
||||
void MString::insert(std::string value, int offset) {
|
||||
setSize(length + value.size());
|
||||
for (int ix = length; ix >= offset; ix--)
|
||||
getData()[ix + value.size()] = getData()[ix];
|
||||
getData()[ix + value.size()] = getData()[ix];
|
||||
std::memcpy(data + offset, value.c_str(), value.size());
|
||||
}
|
||||
|
||||
void MString::replace(char ch, int offset)
|
||||
{
|
||||
|
||||
void MString::replace(char ch, int offset) {
|
||||
}
|
||||
|
||||
void MString::replace(ZString &value, int offset)
|
||||
{
|
||||
|
||||
void MString::replace(ZString &value, int offset) {
|
||||
}
|
||||
|
||||
void MString::replace(std::string value, int offset)
|
||||
{
|
||||
|
||||
void MString::replace(std::string value, int offset) {
|
||||
}
|
||||
|
||||
void MString::remove(int offset, int length)
|
||||
{
|
||||
|
||||
void MString::remove(int offset, int length) {
|
||||
// (data + this->length) > (data + offset + length) ? length: length; // TODO: Need to calculate correct length.
|
||||
for (int ix = offset; ix < this->length; ix++)
|
||||
getData()[ix] = getData()[ix + length];
|
||||
getData()[ix] = getData()[ix + length];
|
||||
setSize(this->length - length);
|
||||
}
|
||||
|
||||
MString &MString::write(char ch)
|
||||
{
|
||||
|
||||
MString &MString::write(char ch) {
|
||||
setSize(length + 1);
|
||||
*(data + length - 1) = ch;
|
||||
return *this;
|
||||
}
|
||||
|
||||
MString &MString::write(ZString &value)
|
||||
{
|
||||
|
||||
MString &MString::write(ZString &value) {
|
||||
int len = length;
|
||||
setSize(length + value.getLength());
|
||||
memcpy(data + len, value.getData(), value.getLength());
|
||||
return *this;
|
||||
}
|
||||
|
||||
void MString::setSize(int size)
|
||||
{
|
||||
|
||||
void MString::setSize(int size) {
|
||||
int cursorOffset = cursor - data;
|
||||
int newBufferSize = ((size / 256) + 1) * 256;
|
||||
if (bufferSize != newBufferSize)
|
||||
{
|
||||
if (bufferSize != newBufferSize) {
|
||||
bufferSize = newBufferSize;
|
||||
data = (char *)realloc(data, bufferSize);
|
||||
cursor = data + cursorOffset;
|
||||
}
|
||||
length = size;
|
||||
}
|
||||
|
||||
int MString::offset()
|
||||
{
|
||||
|
||||
int MString::offset() {
|
||||
return cursor - data;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user