1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
|
#ifdef ECERE_STATIC
public import static "ecere"
#endif
#include <stdarg.h>
public class DynamicString : Array<char>
{
minAllocSize = 1024;
DynamicString()
{
Add('\0');
}
property String
{
set
{
DynamicString s { };
if(value)
{
int len = strlen(value) + 1;
s.size = len;
memcpy(s.array, value, len);
}
return s;
}
get { return array; }
}
const char * OnGetString(char * tempString, void * fieldData, bool * needClass)
{
return array;
}
bool OnGetDataFromString(const char * string)
{
this = (DynamicString)(char *)string;
return true;
}
void concat(const String s)
{
int len = strlen(s);
if(len)
{
int pos = size-1;
if(pos == -1) { Add('\0'); pos = 0; }
size += len;
memcpy(&(this[pos]), s, len+1);
}
}
void concatf(const char * format, ...)
{
// TODO: improve this to vsprinf directly in the Array<char> instead of calling concat
char string[MAX_F_STRING];
va_list args;
va_start(args, format);
vsnprintf(string, sizeof(string), format, args);
string[sizeof(string)-1] = 0;
va_end(args);
concat(string);
}
void concatx(typed_object object, ...)
{
// TODO: improve this to work directly on the Array<char> instead of calling PrintStdArgsToBuffer
char string[MAX_F_STRING];
va_list args;
//int len;
va_start(args, object);
/*len = */PrintStdArgsToBuffer(string, sizeof(string), object, args);
concat(string);
va_end(args);
}
void copySingleBlankReplTrim(String s, char replace, bool trim)
{
privateCommonCopyLenSingleBlankReplTrim(s, replace, trim, strlen(s));
}
void copyLenSingleBlankReplTrim(String s, char replace, bool trim, int copyLen)
{
privateCommonCopyLenSingleBlankReplTrim(s, replace, trim, Min(strlen(s), copyLen));
}
void privateCommonCopyLenSingleBlankReplTrim(String s, char replace, bool trim, int len)
{
int c, d;
bool wasBlank = trim;
size = len + 1;
for(c = d = 0; c < len; c++)
{
if(isblank(s[c]))
{
if(!wasBlank)
{
wasBlank = true;
/*array*/this[d++] = replace ? replace : s[c];
}
}
else
{
/*array*/this[d++] = s[c];
if(wasBlank)
wasBlank = false;
}
}
if(!trim || (len && !isblank(/*array*/this[d])))
d++;
/*array*/this[d] = '\0';
size = d + 1;
}
}
|