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