@@ -4,9 +4,9 @@
------------------------------
A collection of basic c++ classes
- Version 1.3.0
+ Version 1.3.1
- (C)opyright 2005, 2006, 2007, 2008, 2009, 2010, 2011,2012 Bjoern Lemke
+ (C)opyright 2005,2006,2007,2008,2009,2010,2011,2012 Bjoern Lemke
This software comes under the GNU general public license
|
@@ -6,7 +6,7 @@
//
// Design and Implementation by Bjoern Lemke
//
-// (C)opyright 2000-2005 Bjoern Lemke
+// (C)opyright 2000-2012 Bjoern Lemke
//
// 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
@@ -45,6 +45,7 @@
#define TMPBUFSIZE 30
#define INBUFSIZE 100
+
Chain::Chain()
{
_buf = 0;
@@ -61,15 +62,25 @@
{
return;
}
-
- _buf = (char*)malloc(strlen(s) + 1);
- if ( _buf == 0 )
+
+ size_t sl = strlen(s) + 1;
+
+ if ( sl < STATICBUFSIZE )
{
- throw Exception(EXLOC, "malloc system error");
+ _buf = _staticBuf;
}
+ else
+ {
+ _buf = (char*)malloc(sl);
+ if ( _buf == 0 )
+ {
+ throw Exception(EXLOC, "malloc system error");
+ }
+ }
+
strcpy(_buf, s);
- _len=strlen(s)+1;
+ _len=sl;
}
Chain::Chain(const char *s, int len)
@@ -82,21 +93,27 @@
{
return;
}
-
- _buf = (char*)malloc(len+1);
-
- if ( _buf == 0 )
+
+ if ( len+1 < STATICBUFSIZE )
{
- throw Exception(EXLOC, "malloc system error");
+ _buf = _staticBuf;
}
-
+ else
+ {
+
+ _buf = (char*)malloc(len+1);
+
+ if ( _buf == 0 )
+ {
+ throw Exception(EXLOC, "malloc system error");
+ }
+ }
+
memcpy(_buf, s, len);
_buf[len]=0;
_len=len+1;
}
-
-
Chain::Chain(const Chain& str)
{
_len = 0;
@@ -111,12 +128,7 @@
_buf = 0;
_len = 0;
- _buf = (char*)malloc(2);
-
- if ( _buf == 0 )
- {
- throw Exception(EXLOC, "malloc system error");
- }
+ _buf = _staticBuf;
_buf[0]=c;
_buf[1]=0;
@@ -131,19 +143,15 @@
_len = sprintf(tmpBuf, "%ld", l);
_len++;
- if (_len > 0 )
+ if (_len > STATICBUFSIZE )
{
-
- _buf = (char*)malloc(_len);
-
- if ( _buf == 0 )
- {
- throw Exception(EXLOC, "malloc system error");
- }
-
- strcpy(_buf, tmpBuf);
-
+ throw Exception(EXLOC, "static buf size exceeded");
}
+
+ _buf = _staticBuf;
+
+ strcpy(_buf, tmpBuf);
+
}
Chain::Chain(unsigned long l)
@@ -153,19 +161,15 @@
_len = sprintf(tmpBuf, "%ld", l);
_len++;
- if (_len > 0 )
+ if (_len > STATICBUFSIZE )
{
-
- _buf = (char*)malloc(_len);
-
- if ( _buf == 0 )
- {
- throw Exception(EXLOC, "malloc system error");
- }
-
- strcpy(_buf, tmpBuf);
-
+ throw Exception(EXLOC, "static buf size exceeded");
}
+
+ _buf = _staticBuf;
+
+ strcpy(_buf, tmpBuf);
+
}
Chain::Chain(int i)
@@ -175,19 +179,15 @@
_len = sprintf(tmpBuf, "%d", i);
_len++;
- if (_len > 0 )
+ if (_len > STATICBUFSIZE )
{
-
- _buf = (char*)malloc(_len);
-
- if ( _buf == 0 )
- {
- throw Exception(EXLOC, "malloc system error");
- }
-
- strcpy(_buf, tmpBuf);
-
+ throw Exception(EXLOC, "static buf size exceeded");
}
+
+ _buf = _staticBuf;
+
+ strcpy(_buf, tmpBuf);
+
}
Chain::Chain(float f)
@@ -197,19 +197,15 @@
_len = sprintf(tmpBuf, "%f", f);
_len++;
- if (_len > 0 )
+ if (_len > STATICBUFSIZE )
{
-
- _buf = (char*)malloc(_len);
-
- if ( _buf == 0 )
- {
- throw Exception(EXLOC, "malloc system error");
- }
-
- strcpy(_buf, tmpBuf);
-
+ throw Exception(EXLOC, "static buf size exceeded");
}
+
+ _buf = _staticBuf;
+
+ strcpy(_buf, tmpBuf);
+
}
Chain::Chain(double d)
@@ -219,33 +215,28 @@
_len = sprintf(tmpBuf, "%f", d);
_len++;
- if (_len > 0 )
+ if (_len > STATICBUFSIZE )
{
-
- _buf = (char*)malloc(_len);
-
- if ( _buf == 0 )
- {
- throw Exception(EXLOC, "malloc system error");
- }
-
- strcpy(_buf, tmpBuf);
-
+ throw Exception(EXLOC, "static buf size exceeded");
}
+
+ _buf = _staticBuf;
+
+ strcpy(_buf, tmpBuf);
+
}
Chain::~Chain()
{
- if (_buf != 0 )
+ if (_buf != 0 && _buf != _staticBuf)
{
free(_buf);
- _buf = 0;
- _len = 0;
}
+ _buf = 0;
+ _len = 0;
}
-
void Chain::setData(char* s)
{
_buf = s;
@@ -709,7 +700,8 @@
{
if (_len > 0)
{
- free(_buf);
+ if ( _buf != 0 && _buf != _staticBuf )
+ free(_buf);
_buf = 0;
_len = 0;
}
@@ -724,20 +716,29 @@
if (_len > 0)
{
- free(_buf);
+ if ( _buf != 0 && _buf != _staticBuf )
+ free(_buf);
_buf = 0;
_len = 0;
}
- _buf = (char*)malloc(str._len + 1);
- if ( _buf == 0 )
+
+ if ( str._len + 1 < STATICBUFSIZE )
{
- throw Exception(EXLOC, "malloc system error");
+ _buf = _staticBuf;
}
else
{
- strcpy(_buf, str._buf);
- _len=str._len;
+
+ _buf = (char*)malloc(str._len + 1);
+
+ if ( _buf == 0 )
+ {
+ throw Exception(EXLOC, "malloc system error");
+ }
}
+
+ strcpy(_buf, str._buf);
+ _len=str._len;
}
return (*this);
}
@@ -755,20 +756,30 @@
}
else
{
-
- char *tmpBuf = (char*)malloc(_len + str._len -1);
-
- if ( tmpBuf == 0 )
+
+ if ( _len + str._len - 1 < STATICBUFSIZE )
{
- throw Exception(EXLOC, "malloc system error");
+ strcpy((char*)(_buf + _len - 1), str._buf);
+ _len = _len + str._len -1;
}
+ else
+ {
+ char *tmpBuf = (char*)malloc(_len + str._len -1);
+ if ( tmpBuf == 0 )
+ {
+ throw Exception(EXLOC, "malloc system error");
+ }
+
+ strcpy(tmpBuf, _buf);
+
+ if ( _buf != _staticBuf )
+ free(_buf);
+
+ _buf = tmpBuf;
+ strcpy((char*)(_buf + _len - 1), str._buf);
+ _len = _len + str._len -1;
- strcpy(tmpBuf, _buf);
- strcpy((char*)(tmpBuf + _len - 1), str._buf);
- _len = _len + str._len -1;
- free(_buf);
- _buf = tmpBuf;
-
+ }
}
return (*this);
|