📄 data2.cxx
字号:
#include <algorithm>#include <ctype.h>#include <math.h>#include "Data2.hxx"//#include "DataException.hxx"//#include "cpLog.h"#if defined(WIN32) || defined(__QNX__)#define strcasecmp(a,b) stricmp(a,b)#define strncasecmp(a,b,c) strnicmp(a,b,c)#endifusing namespace std;const int Data::npos = INT_MAX;Data::Data() : mLength(0), mBuf(0), mCapacity(127){ mBuf = new char[mCapacity+1]; mBuf[0] = 0;}Data::Data(int capacity, bool) : mLength(0), mBuf(0), mCapacity(capacity){ mBuf = new char[capacity+1]; mBuf[0] = 0;}Data::Data(const char* str, int length) : mLength(length), mBuf(new char[mLength+1]), mCapacity(length){ assert(str); memcpy(mBuf, str, length); mBuf[length] = 0;}Data::Data(const char* str) : mLength(0), mBuf(0), mCapacity(0){ assert(str); mLength = strlen(str); mCapacity = mLength; mBuf = new char[mLength+1]; memcpy(mBuf, str, mLength+1);}Data::Data(const string& str) : mLength(str.size()), mBuf(new char[mLength+1]), mCapacity(mLength){ memcpy(mBuf, str.c_str(), mLength+1);}Data::Data(int val) : mLength(0), mBuf(0), mCapacity(0){ if (val == 0) { mBuf = new char[2]; mBuf[0] = '0'; mBuf[1] = 0; mLength = 1; return; } bool neg = false; int value = val; if (value < 0) { value = -value; neg = true; } int c = 0; int v = value; while (v /= 10) { c++; } if (neg) { c++; } mLength = c+1; mCapacity = c+1; mBuf = new char[c+2]; mBuf[c+1] = 0; v = value; while (v) { mBuf[c--] = '0' + v%10; v /= 10; } if (neg) { mBuf[0] = '-'; }}Data::Data(double value, int precision) : mLength(0), mBuf(0), mCapacity(0){ assert(precision < 10); double v = value; bool neg = (value < 0.0); if (neg) { v = -v; } Data m((unsigned long)v); // remainder v = v - floor(v); int p = precision; while (p--) { v *= 10; } int dec = (int)floor(v+0.5); Data d; if (dec == 0) { d = "0"; } else { d.resize(precision); d.mBuf[precision] = 0; p = precision; // neglect trailing zeros bool significant = false; while (p--) { if (dec % 10 || significant) { significant = true; d.mLength++; d.mBuf[p] = '0' + (dec % 10); } else { d.mBuf[p] = 0; } dec /= 10; } } if (neg) { resize(m.length() + d.length() + 2); mBuf[0] = '-'; memcpy(mBuf+1, m.mBuf, m.length()); mBuf[1+m.length()] = '.'; memcpy(mBuf+1+m.length()+1, d.mBuf, d.length()+1); mLength = m.length() + d.length() + 2; } else { resize(m.length() + d.length() + 1); memcpy(mBuf, m.mBuf, m.length()); mBuf[m.length()] = '.'; memcpy(mBuf+m.length()+1, d.mBuf, d.length()+1); mLength = m.length() + d.length() + 1; }}Data::Data(unsigned long value) : mLength(0), mBuf(0), mCapacity(0){ if (value == 0) { mBuf = new char[2]; mBuf[0] = '0'; mBuf[1] = 0; mLength = 1; return; } int c = 0; unsigned long v = value; while (v /= 10) { c++; } mLength = c+1; mCapacity = c+1; mBuf = new char[c+2]; mBuf[c+1] = 0; v = value; while (v) { mBuf[c--] = '0' + v%10; v /= 10; }}Data::Data(char c) : mLength(1), mBuf(0), mCapacity(mLength){ mBuf = new char[2]; mBuf[0] = c; mBuf[1] = 0;}Data::Data(bool value) : mLength(0), mBuf(0), mCapacity(0){ static char* truec = "true"; static char* falsec = "false"; if (value) { mBuf = new char[5]; mLength = 4; mCapacity = 4; memcpy(mBuf, truec, 5); } else { mBuf = new char[6]; mLength = 5; mCapacity = 5; memcpy(mBuf, falsec, 6); }}Data::Data(const Data& rhs) : mLength(rhs.mLength), mBuf(new char[mLength+1]), mCapacity(mLength){ memcpy(mBuf, rhs.mBuf, mLength+1);}Data::~Data(){ delete[] mBuf;}Data&Data::operator=(const char* str){ assert(str); const int len = strlen(str); if (len > mCapacity) { resize(len); } mLength = len; memcpy(mBuf, str, mLength+1); return *this;}Data&Data::operator=( const Data& data ) { if (&data != this) { if (data.length() > mCapacity) { resize(data.length()); } mLength = data.length(); memcpy(mBuf, data.mBuf, data.mLength+1); } return *this;}const char*Data::logData() const{ return mBuf;}const char*Data::c_str() const{ return mBuf;}const char*Data::getData(char* buf, int len) const{ assert(len); strncpy(buf, mBuf, len-1); buf[len-1] = 0; return buf;}const char*Data::getData(LocalScopeAllocator& lo) const{ lo.allocate(mLength+1); memcpy(lo, mBuf, mLength+1); return lo;}charData::getChar(int i) const{ assert(i<mLength); return mBuf[i];}voidData::setchar(int i, char c){ if (i >= mLength) { resize(i+1); mLength = i+1; memset(mBuf + mLength, 0, mCapacity-mLength+1); } assert(i<mLength); mBuf[i] = c;}charData::operator[](int i) const{ assert(i<mLength); return mBuf[i];}intData::length() const{ return mLength;}voidData::setBufferSize(int newCapacity){ resize(newCapacity);}voidData::resize(int newCapacity){ if (newCapacity < mCapacity) { return; } char* oldBuf = mBuf; mBuf = new char[newCapacity+1]; for (int i = 0; i < mLength; i++) { mBuf[i] = oldBuf[i]; } mBuf[mLength] = 0; mCapacity = newCapacity; delete[] oldBuf;}boolData::operator==(const char* str) const{ assert(str); return strcmp(mBuf, str) == 0;}boolData::operator==( const Data& data) const{ return strcmp(mBuf, data.mBuf) == 0;}boolData::operator!=(const char* str) const{ assert(str); return strcmp(mBuf, str) != 0;}boolData::operator!=(const Data& data) const{ return strcmp(mBuf, data.mBuf) != 0;}booloperator==(const char* str, const Data& data){ assert(str); return strcmp(str, data.mBuf) == 0;}booloperator!=(const char* str, const Data& data){ assert(str); return strcmp(str, data.mBuf) != 0;}boolData::operator>(const Data& data) const{ return strcmp(mBuf, data.mBuf) > 0;}boolData::operator<(const Data& data) const{ return strcmp(mBuf, data.mBuf) < 0;}boolData::operator>(const char* data) const{ return strcmp(mBuf, data) > 0;}boolData::operator<(const char* data) const{ return strcmp(mBuf, data) < 0;}intData::compare(const char* str, int length) const{ return strncmp(mBuf, str, length);}intData::compare(const Data& data) const{ return strncmp(mBuf, data.mBuf, data.length());}intData::compareNoCase(const char* str, int length) const{ return strncasecmp(mBuf, str, length);}intData::compareNoCase(const Data& data) const{ return strncasecmp(mBuf, data.mBuf, data.length());}DataData::operator+(const Data& data) const{ // reserve Data ret(mLength + data.mLength, true); memcpy(ret.mBuf, mBuf, mLength); memcpy(ret.mBuf+mLength, data.mBuf, data.mLength+1); ret.mLength = mLength + data.mLength; return ret;}DataData::operator+(const char* str) const{ assert(str); // reserve int l = strlen(str); Data ret(length() + l, true); memcpy(ret.mBuf, mBuf, mLength); memcpy(ret.mBuf+mLength, str, l+1); ret.mLength = mLength + l; return ret;}DataData::operator+(const char c) const{ // reserve Data ret(length() + 1, true); memcpy(ret.mBuf, mBuf, mLength); ret.mBuf[mLength] = c; ret.mBuf[mLength+1] = 0; ret.mLength = mLength + 1; return ret;}Data&Data::operator+=(const Data& data){ if (mCapacity < mLength + data.mLength) { resize(mLength + data.mLength); } memcpy(mBuf+mLength, data.mBuf, data.mLength+1); mLength += data.mLength; return *this;}Data&Data::operator+=(const char* str){ assert(str); int l = strlen(str); if (mCapacity < mLength + l) { resize(mLength + l); } memcpy(mBuf+mLength, str, l+1); mLength += l; return *this;}Data&Data::operator+=(const char c){ if (mCapacity < mLength + 1) { resize(mLength + 1); } mBuf[mLength] = c; mBuf[mLength+1] = 0; mLength += 1; return *this;}Data&Data::lowercase(){ char* p = mBuf; while ((*p = tolower(*p)) != 0) { p++; } return *this;}Data&Data::uppercase(){ char* p = mBuf; while ((*p = toupper(*p)) != 0) { p++; } return *this;}voidData::erase(){ mLength = 0; mBuf[0] = 0;}int Data::convertInt() const{ int val = 0; char* p = mBuf; int l = mLength; int s = 1; while (isspace(*p++)) { l--; } p--; if (*p == '-') { s = -1; p++; l--; } while (l--) { char c = *p++; if ((c >= '0') && (c <= '9')) { val *= 10; val += c - '0'; } else { return s*val; } } return s*val;}long Data::convertLong() const{ long val = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -