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