File: CString.h

package info (click to toggle)
capi4hylafax 1%3A01.02.03-10sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,856 kB
  • ctags: 2,966
  • sloc: cpp: 20,126; sh: 7,703; makefile: 215; perl: 36
file content (308 lines) | stat: -rw-r--r-- 9,987 bytes parent folder | download
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*---------------------------------------------------------------------------*\

    Copyright (C) 2000 AVM GmbH. All rights reserved.

    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 the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY, without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, and WITHOUT
    ANY LIABILITY FOR ANY DAMAGES arising out of or in connection
    with the use or performance of this software. See the
    GNU General Public License for further details.

\*---------------------------------------------------------------------------*/

#ifndef _CSTRING_H_
#define _CSTRING_H_

#include "aTypes.h"
#include "aString.h"
#include <stdarg.h>

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/

#define CSTRING_RESIZE_ALLOCATE_OFFSET          100

#ifdef CSTRING_CHAR_IS_WIDE_CHAR
typedef tWiChar      tStringChar;
#else
typedef tChar        tStringChar;
#endif

typedef tStringChar *tString;

typedef char        tFormatChar;

/*===========================================================================*\
\*===========================================================================*/

class CConstString {
public:
    CConstString (tString string, tSize len = 0);   // string have to be null terminated! len ist not tested!
    virtual ~CConstString (void) {}

    tBool   IsEmpty (void);
    tSize   GetLen (void);      // GetLen & Get(Max)Size return multiples of tStringChar!
    tSize   GetSize (void);
    tSize   GetMaxSize (void);
    tString GetPointer (void);
    tBool   ErrorDetected (void);

protected:
    CConstString (void);

    tString pntr;
    tSize   curLen;
    tSize   maxSize;
    tBool   detectError;
};


/*===========================================================================*\
\*===========================================================================*/

class CDynamicString : public CConstString {
public:
    CDynamicString (void) {}
    CDynamicString (tFormatChar *string, tSize len = 0);
    CDynamicString (CConstString *pcstr);
    CDynamicString (tUInt startSize);
    virtual ~CDynamicString (void);

    tBool Set (tString       string, tSize len = 0);
    tBool Set (tFormatChar  *string, tSize len = 0);
    tBool Set (CConstString *pcstr);
    tBool Append (tString       string, tSize len = 0);
    tBool Append (tFormatChar  *string, tSize len = 0);
    tBool Append (CConstString *pcstr);

    tBool Change (tString       string, tSize startPos, tSize len = 0);
    tBool Change (tFormatChar  *string, tSize startPos, tSize len = 0);
    tBool Change (CConstString *pcstr,  tSize startPos, tSize len = 0);
    tBool Insert (tString       string, tSize startPos, tSize len = 0);
    tBool Insert (tFormatChar  *string, tSize startPos, tSize len = 0);
    tBool Insert (CConstString *pcstr,  tSize startPos, tSize len = 0);

    tBool Fill (tStringChar fillChar, tSize len, tSize startPos = 0);
    tBool FillAppend (tStringChar fillChar, tUInt len);

    tSInt Print  (tFormatChar *format, ...);
    tSInt vPrint (tFormatChar *format, va_list argptr);
    tSInt PrintAppend  (tFormatChar *format, ...);
    tSInt vPrintAppend (tFormatChar *format, va_list argptr);

    tCompare Compare (tString string, tSize len = 0);
    tBool IsIdentical (tString string, tSize len = 0);

    void Get (tString string, tSize len = 0, tSize startPos = 0);
    tStringChar GetChar (tSize pos);
    tStringChar GetLastChar (void);
    tSize FindChar (tStringChar ch, tSize startpos = 0); // not found => return = MAXVAL_tSize
    tSize FindLastChar (tStringChar ch);                 // not found => return = MAXVAL_tSize

    void  SetLen (tSize newLen);
    void  CutLen (tSize cutLen);   // Result: newlen = oldLen - cutLen
    void  Remove (tSize startPos, tSize len);
    void  RemoveChars (tString RemoveChars, tSize startPos = 0, tSize len = 0);
    void  RemoveAll (void);
    tBool Resize (tSize newMaxSize);

private:
    tSInt i_vPrintAppend (tFormatChar *format, va_list argptr, tUInt tabsize, tStringChar tabchar);
    tUInt i_Print_ltoa (tFormatChar **pBuffer, tUInt64 value, tSByte radix, tUInt *pWidth, tUInt pFlags);
    tBool DynExpand (tSize newMaxSize);
};


/*===========================================================================*\
    inline fuctions from CConstString
\*===========================================================================*/

inline CConstString::CConstString (void)
  : pntr (0),
    curLen (0),
    maxSize (0),
    detectError (vFalse) {
}

inline tBool CConstString::IsEmpty (void) {
    return (curLen == 0);
}

inline tSize CConstString::GetLen (void) {
    return curLen;
}

inline tSize CConstString::GetSize (void) {
    return (GetLen()) ? curLen + 1 : 0;
}

inline tSize CConstString::GetMaxSize (void) {
    return maxSize;
}

inline tString CConstString::GetPointer (void) {
    return (GetLen()) ? pntr : 0;
}

inline tBool CConstString::ErrorDetected (void) {
    if (detectError == vFalse) {
        return vFalse;
    }
    detectError = vFalse;
    return vTrue;
}


/*===========================================================================*\
    inline fuctions from CDynamicString
\*===========================================================================*/

inline tBool CDynamicString::Set (tString string, tSize len) {
    RemoveAll();
    return Append (string, len);
}

inline tBool CDynamicString::Set (tFormatChar *string, tSize len) {
    RemoveAll();
    return Append (string, len);
}

inline tBool CDynamicString::Set (CConstString *pcstr) {
    if (pcstr->GetLen()) {
        return Set (pcstr->GetPointer(), pcstr->GetLen());
    }
    RemoveAll();
    return vTrue;
}

inline tBool CDynamicString::Append (tString string, tSize len) {
    return Change (string, GetLen(), len);
}

inline tBool CDynamicString::Append (tFormatChar *string, tSize len) {
    return Change (string, GetLen(), len);
}

inline tBool CDynamicString::Append (CConstString *pcstr) {
    return (pcstr->GetLen()) ? Change (pcstr->GetPointer(), GetLen(), pcstr->GetLen()) : vTrue;
}

inline tBool CDynamicString::Change (CConstString *pcstr, tSize startPos, tSize len) {
    return (pcstr->GetLen()) ? Change (pcstr->GetPointer(), startPos, (len) ? len : pcstr->GetLen()) : vTrue;
}

inline tBool CDynamicString::Insert (CConstString *pcstr, tSize startPos, tSize len) {
    return (pcstr->GetLen()) ? Insert (pcstr->GetPointer(), startPos, (len) ? len : pcstr->GetLen()) : vTrue;
}

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/

inline tBool CDynamicString::FillAppend (tStringChar fillChar, tUInt len) {
    return Fill (fillChar, len, GetLen());
}

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/

inline tSInt CDynamicString::Print (tFormatChar *format, ...) {
    va_list arg_ptr;
    va_start (arg_ptr, format);
    tUInt fret = vPrint (format, arg_ptr);
    va_end (arg_ptr);
    return fret;
}

inline tSInt CDynamicString::vPrint (tFormatChar *format, va_list argptr) {
    RemoveAll();
    return vPrintAppend (format, argptr);
}

inline tSInt CDynamicString::PrintAppend (tFormatChar *format, ...) {
    va_list arg_ptr;
    va_start (arg_ptr, format);
    tUInt fret = vPrintAppend (format, arg_ptr);
    va_end (arg_ptr);
    return fret;
}

inline tSInt CDynamicString::vPrintAppend (tFormatChar *format, va_list argptr) {
    return i_vPrintAppend (format, argptr, 0, 0);
}


/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/

inline tBool CDynamicString::IsIdentical (tString string, tSize len) {
    if (!len) {
        len = s_strlen (string);
    }
    return ((len == GetLen()) && (s_strcmp (string, pntr) == vEqual)) ? vTrue : vFalse;
}


/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/

inline tStringChar CDynamicString::GetChar (tSize pos) {
    //assert (pos < GetLen());
    //assert (pntr != 0);
    return (pos < GetLen()) ? pntr[pos] : '\0';
}

inline tStringChar CDynamicString::GetLastChar (void) {
    //assert (GetLen() > 0);
    //assert (pntr != 0);
    return pntr[GetLen() - 1];
}

inline void CDynamicString::SetLen (tSize newLen) {
    // assert (pntr != 0);
    // warning (newLen > curLen);
    if (curLen > newLen) {
        curLen = newLen;
        pntr[curLen] = '\0';
    }
}

inline void CDynamicString::CutLen (tSize cutLen) {
    // assert (pntr != 0);
    // warning (cutLen > curLen);
    if (curLen) {
        if (curLen > cutLen) {
            curLen -= cutLen;
        } else {
            curLen = 0;
        }
        pntr[curLen] = '\0';
    }
}

inline void CDynamicString::RemoveAll (void) {
    if (curLen) {
        // assert (pntr != 0);
        curLen  = 0;
        pntr[0] = '\0';
    }
}

/*---------------------------------------------------------------------------*\
\*---------------------------------------------------------------------------*/

inline tBool CDynamicString::DynExpand (tSize newMaxSize) {
    return Resize ((GetMaxSize() + CSTRING_RESIZE_ALLOCATE_OFFSET > newMaxSize) ?
                    GetMaxSize() + CSTRING_RESIZE_ALLOCATE_OFFSET : newMaxSize);
}

/*===========================================================================*\
\*===========================================================================*/

#endif