-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
404 lines (336 loc) · 11.8 KB
/
Copy pathmain.c
File metadata and controls
404 lines (336 loc) · 11.8 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* /////////////////////////////////////////////////////////////////////////
* File: examples/c/example.c.cstring/main.c
*
* Purpose: Example exercising core `cstring_t` create/assign/append/
* truncate/copy/swap paths (including Windows allocator flags
* where available).
*
* Created: 7th July 2005
* Updated: 2nd August 2026
*
* ////////////////////////////////////////////////////////////////////// */
/* cstring header files */
#include <cstring/cstring.h>
/* Standard C header files */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* /////////////////////////////////////////////////////////////////////////
* compiler warnings
*/
#if defined(_MSC_VER) && \
_MSC_VER >= 1310
# pragma warning(disable : 4054)
#endif /* compiler */
/* /////////////////////////////////////////////////////////////////////////
* forward declarations
*/
static int run_unittests(void);
#if defined(WIN32)
static int on_allocFail(void *pv, size_t cb, cstring_flags_t flags, void *param);
#endif /* WIN32 */
static void play_with_string(struct cstring_t *pcs);
/* /////////////////////////////////////////////////////////////////////////
* main()
*/
int main(int argc, char *argv[])
{
#if 0
{ size_t i; for (i = 0; i < 0xFFFFFFFF; ++i){} }
#endif /* 0 */
#if !defined(__GNUC__)
fprintf(stdout, "");
fflush(stdout);
#endif /* !__GNUC__ */
if (2 == argc &&
( 0 == strcmp(argv[1], "-unittest") ||
0 == strcmp(argv[1], "--unittest")))
{
return run_unittests();
}
else
{
printf("\n========================================\n");
printf("cstring_createLen():\n");
{
struct cstring_t cs1;
CSTRING_RC rc = cstring_createLen(&cs1, "string-#1", 9);
if (CSTRING_RC_SUCCESS == rc)
{
play_with_string(&cs1);
cstring_destroy(&cs1);
}
}
#if 0
#elif defined(WIN32)
printf("\n========================================\n");
printf("cstring_createLenEx(CSTRING_F_USE_WIN32_PROCESSHEAP_MEMORY):\n");
{
struct cstring_t cs1;
CSTRING_RC rc = cstring_createLenEx(&cs1, "string-#2", 9, CSTRING_F_USE_WIN32_PROCESSHEAP_MEMORY, NULL, 0);
if (CSTRING_RC_SUCCESS == rc)
{
play_with_string(&cs1);
cstring_destroy(&cs1);
}
}
printf("\n========================================\n");
printf("cstring_createLenFn(CSTRING_F_USE_WIN32_PROCESSHEAP_MEMORY):\n");
{
struct cstring_t cs1;
CSTRING_RC rc = cstring_createLenFn(&cs1, "string-#2", 9, CSTRING_F_USE_WIN32_PROCESSHEAP_MEMORY, NULL, 0, on_allocFail, NULL);
if (CSTRING_RC_SUCCESS == rc)
{
cstring_setCapacity(&cs1, 0x7fffffff);
play_with_string(&cs1);
cstring_destroy(&cs1);
}
}
printf("\n========================================\n");
printf("cstring_createLenEx(CSTRING_F_USE_WIN32_COM_TASK_MEMORY):\n");
{
struct cstring_t cs1;
CSTRING_RC rc = cstring_createEx(&cs1, "string-#3", CSTRING_F_USE_WIN32_COM_TASK_MEMORY, NULL, 0);
if (CSTRING_RC_SUCCESS == rc)
{
play_with_string(&cs1);
cstring_destroy(&cs1);
}
}
#endif /* WIN32 */
printf("\n========================================\n");
printf("cstring_createLenEx(CSTRING_F_MEMORY_IS_FIXED | CSTRING_F_MEMORY_IS_BORROWED):\n");
{
char sz[101];
struct cstring_t cs1;
CSTRING_RC rc = cstring_createLenEx(&cs1
, "string-#4"
, 9
, CSTRING_F_MEMORY_IS_FIXED | CSTRING_F_MEMORY_IS_BORROWED
, &sz[0]
, sizeof(sz));
if (CSTRING_RC_SUCCESS == rc)
{
play_with_string(&cs1);
cstring_destroy(&cs1);
}
}
}
return EXIT_SUCCESS;
}
/* /////////////////////////////////////////////////////////////////////////
* function implementations
*/
static void play_with_string(struct cstring_t *pcs)
{
struct cstring_t cs2 = cstring_t_DEFAULT;
struct cstring_t cs3;
CSTRING_RC rc;
fprintf(stdout, " Initial contents: %.*s\n", (int)pcs->len, pcs->ptr);
fprintf(stdout, "\n");
fprintf(stdout, " Assigning another C-style string to the instance:\n");
rc = cstring_assign(pcs, "another string");
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stdout, " error: %s\n", cstring_getStatusCodeString(rc));
}
else
{
fprintf(stdout, " Contents: %.*s\n", (int)pcs->len, pcs->ptr);
}
fprintf(stdout, "\n");
fprintf(stdout, " Assigning part of another C-style string to the instance:\n");
rc = cstring_assignLen(pcs, "some more contents", 17);
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stdout, " error: %s\n", cstring_getStatusCodeString(rc));
}
else
{
fprintf(stdout, " Contents: %.*s\n", (int)pcs->len, pcs->ptr);
}
fprintf(stdout, "\n");
fprintf(stdout, " Appending another C-style string to the instance:\n");
rc = cstring_append(pcs, " - ");
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stdout, " error: %s\n", cstring_getStatusCodeString(rc));
}
else
{
fprintf(stdout, " Contents: %.*s\n", (int)pcs->len, pcs->ptr);
}
fprintf(stdout, "\n");
fprintf(stdout, " Appending part of another C-style string to the instance:\n");
rc = cstring_appendLen(pcs, "and another string", 11);
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stdout, " error: %s\n", cstring_getStatusCodeString(rc));
}
else
{
fprintf(stdout, " Contents: %.*s\n", (int)pcs->len, pcs->ptr);
}
fprintf(stdout, "\n");
fprintf(stdout, " Appending part of another C-style string to the instance:\n");
rc = cstring_append(pcs, "; and a whole lot more stringy stuff to try and precipitate a reallocation, in order to check that the 3.3 change will be tested");
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stdout, " error: %s\n", cstring_getStatusCodeString(rc));
}
else
{
fprintf(stdout, " Contents: %.*s\n", (int)pcs->len, pcs->ptr);
}
fprintf(stdout, "\n");
fprintf(stdout, " Appending part of another C-style string to the instance:\n");
rc = cstring_append(pcs, "; and a last bit to try and get the remaining reallocation");
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stdout, " error: %s\n", cstring_getStatusCodeString(rc));
}
else
{
fprintf(stdout, " Contents: %.*s\n", (int)pcs->len, pcs->ptr);
}
fprintf(stdout, "\n");
fprintf(stdout, " Truncating the instance to 10 characters:\n");
rc = cstring_truncate(pcs, 10);
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stdout, " error: %s\n", cstring_getStatusCodeString(rc));
}
else
{
fprintf(stdout, " Contents: %.*s\n", (int)pcs->len, pcs->ptr);
}
fprintf(stdout, "\n");
fprintf(stdout, " Copying the instance:\n");
rc = cstring_copy(&cs2, pcs);
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stdout, " error: %s\n", cstring_getStatusCodeString(rc));
}
else
{
fprintf(stdout, " Contents: %p %.*s\n", (void const*)pcs->ptr, (int)pcs->len, pcs->ptr);
fprintf(stdout, " Contents of copy: %p %.*s\n", (void const*)cs2.ptr, (int)cs2.len, cs2.ptr);
}
fprintf(stdout, "\n");
fprintf(stdout, " Copying the instance (by C-style string):\n");
rc = cstring_createLenEx(&cs3, pcs->ptr, pcs->len, pcs->flags, NULL, 0);
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stdout, " error: %s\n", cstring_getStatusCodeString(rc));
}
else
{
fprintf(stdout, " Contents: %p %.*s\n", (void const*)pcs->ptr, (int)pcs->len, pcs->ptr);
fprintf(stdout, " Contents of copy: %p %.*s\n", (void const*)cs3.ptr, (int)cs3.len, cs3.ptr);
}
fprintf(stdout, "\n");
fprintf(stdout, " Swapping with the copy:\n");
rc = cstring_swap(&cs2, pcs);
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stdout, " error: %s\n", cstring_getStatusCodeString(rc));
}
else
{
fprintf(stdout, " Contents: %p %.*s\n", (void const*)pcs->ptr, (int)pcs->len, pcs->ptr);
fprintf(stdout, " Contents of copy: %p %.*s\n", (void const*)cs2.ptr, (int)cs2.len, cs2.ptr);
}
fprintf(stdout, "\n");
fprintf(stdout, " Destroying the copy:\n");
cstring_destroy(&cs2);
}
static int run_unittests(void)
{
cstring_t cs1;
CSTRING_RC rc;
cstring_init(&cs1);
rc = cstring_create(&cs1, "");
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stdout, "API error: %s\n", cstring_getStatusCodeString(rc));
goto Failure;
}
else if (0 != cs1.len)
{
fprintf(stdout, "Length of empty string not 0; length=%lu\n", (unsigned long)cs1.len);
goto Failure;
}
rc = cstring_setCapacity(&cs1, 100);
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stdout, "API error: %s\n", cstring_getStatusCodeString(rc));
goto Failure;
}
else if (0 != cs1.len)
{
fprintf(stdout, "Length of empty string not 0; length=%lu\n", (unsigned long)cs1.len);
goto Failure;
}
else if (cs1.capacity < 100)
{
fprintf(stdout, "Capacity of empty string not >=100; capacity=%lu\n", (unsigned long)cs1.capacity);
goto Failure;
}
rc = cstring_assignLen(&cs1, "Lemon shoes", 11);
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stdout, "API error: %s\n", cstring_getStatusCodeString(rc));
goto Failure;
}
else if (11 != cs1.len)
{
fprintf(stdout, "Length of string not 11; length=%lu\n", (unsigned long)cs1.len);
goto Failure;
}
else if (0 != strcmp(cs1.ptr, "Lemon shoes"))
{
fprintf(stdout, "Invalid string contents; not \"Lemon shoes\", but \"%s\"\n", cs1.ptr);
goto Failure;
}
else if (cs1.capacity < 100)
{
fprintf(stdout, "Capacity of empty string not >=100; capacity=%lu\n", (unsigned long)cs1.capacity);
goto Failure;
}
rc = cstring_append(&cs1, " - and orange tie");
if (CSTRING_RC_SUCCESS != rc)
{
fprintf(stdout, "API error: %s\n", cstring_getStatusCodeString(rc));
goto Failure;
}
else if (28 != cs1.len)
{
fprintf(stdout, "Length of string not 28; length=%lu\n", (unsigned long)cs1.len);
goto Failure;
}
else if (0 != strcmp(cs1.ptr, "Lemon shoes - and orange tie"))
{
fprintf(stdout, "Invalid string contents; not \"Lemon shoes - and orange tie\", but \"%s\"\n", cs1.ptr);
goto Failure;
}
else if (cs1.capacity < 100)
{
fprintf(stdout, "Capacity of empty string not >=100; capacity=%lu\n", (unsigned long)cs1.capacity);
goto Failure;
}
fprintf(stdout, "unit-tests: passed\n");
return EXIT_SUCCESS;
Failure:
cstring_destroy(&cs1);
fprintf(stdout, "unit-tests: failed\n");
return EXIT_FAILURE;
}
#if 0
#elif defined(WIN32)
static int on_allocFail(void *pv, size_t cb, cstring_flags_t flags, void *param)
{
fprintf(stderr, "Allocation failed: pv=%p, cb=%lu, flags=0x%08x, param=%p\n", pv, (unsigned long)cb, (unsigned)flags, param);
return 0;
}
#endif /* WIN32 */
/* ///////////////////////////// end of file //////////////////////////// */