source: trunk/src/helpers/xstring.c@ 45

Last change on this file since 45 was 45, checked in by umoeller, 24 years ago

Misc. changes.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 48.6 KB
Line 
1
2/*
3 *@@sourcefile xstring.c:
4 * string functions with memory management.
5 *
6 * Usage: All OS/2 programs.
7 *
8 * The functions in this file are intended as a replacement
9 * to the C library string functions (such as strcpy, strcat)
10 * in cases where the length of the string is unknown and
11 * dynamic memory management is desirable.
12 *
13 * Instead of char* pointers, the functions in this file
14 * operate on XSTRING structures, which in turn contain
15 * a char* pointer pointing to heap memory, which is managed
16 * automatically.
17 *
18 * Using these functions has the following advantages:
19 *
20 * -- Automatic memory management. For example, xstrcat will
21 * automatically allocate new memory if the new string
22 * does not fit into the present buffer.
23 *
24 * -- The length of the string is always known. Instead
25 * of running strlen (which consumes time), XSTRING.ulLength
26 * always contains the current length of the string.
27 *
28 * -- The functions also differentiate between allocated
29 * memory and the length of the string. That is, for
30 * iterative appends, you can pre-allocate memory to
31 * avoid excessive reallocations.
32 *
33 * Usage:
34 *
35 * 1) Allocate an XSTRING structure on the stack. Always
36 * call xstrInit on the structure, like this:
37 *
38 + XSTRING str;
39 + xstrInit(&str, 0); // no pre-allocation
40 *
41 * Alternatively, use xstrCreate to have an XSTRING
42 * allocated from the heap.
43 *
44 * Always call xstrClear(&str) to free allocated
45 * memory. Otherwise you'll get memory leaks.
46 * (For heap XSTRING's from xstrCreate, use xstrFree.)
47 *
48 * 2) To copy something into the string, use xstrcpy.
49 * To append something to the string, use xstrcat.
50 * See those functions for samples.
51 *
52 * 3) If you need the char* pointer (e.g. for a call
53 * to another function), use XSTRING.psz. However,
54 * you should ONLY modify the psz pointer yourself
55 * if the other XSTRING members are updated accordingly.
56 * You may, for example, change single characters
57 * in the psz buffer. By contrast, if you change the
58 * length of the string, you must update XSTRING.ulLength.
59 * Otherwise these functions will get into trouble.
60 *
61 * Also, you should never assume that the "psz"
62 * pointer has not changed after you have called
63 * one of the xstr* functions because these can
64 * always reallocate the buffer if more memory
65 * was needed.
66 *
67 * 4) If (and only if) you have a char* buffer which
68 * is free()'able (e.g. from strdup()), you can
69 * use xstrset to avoid duplicate copying.
70 *
71 * Function prefixes:
72 * -- xstr* extended string functions.
73 *
74 * The functions in this file used to be in stringh.c
75 * before V0.9.3 (2000-04-01). These have been largely
76 * rewritten with V0.9.6 (2000-11-01) and are now much
77 * more efficient.
78 *
79 * Note: Version numbering in this file relates to XWorkplace
80 * version numbering.
81 *
82 *@@added V0.9.3 (2000-04-01) [umoeller]
83 *@@header "helpers\xstring.h"
84 */
85
86/*
87 * Copyright (C) 1999-2000 Ulrich M”ller.
88 * This file is part of the "XWorkplace helpers" source package.
89 * This is free software; you can redistribute it and/or modify
90 * it under the terms of the GNU General Public License as published
91 * by the Free Software Foundation, in version 2 as it comes in the
92 * "COPYING" file of the XWorkplace main distribution.
93 * This program is distributed in the hope that it will be useful,
94 * but WITHOUT ANY WARRANTY; without even the implied warranty of
95 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
96 * GNU General Public License for more details.
97 */
98
99#define OS2EMX_PLAIN_CHAR
100 // this is needed for "os2emx.h"; if this is defined,
101 // emx will define PSZ as _signed_ char, otherwise
102 // as unsigned char
103
104#include <os2.h>
105
106#include <stdlib.h>
107#include <stdio.h>
108#include <string.h>
109#include <ctype.h>
110
111#include "setup.h" // code generation and debugging options
112
113#include "helpers\stringh.h"
114#include "helpers\xstring.h" // extended string helpers
115
116/*
117 *@@category: Helpers\C helpers\String management\XStrings (with memory management)
118 * See xstring.c.
119 */
120
121/*
122 *@@ xstrInit:
123 * initializes a new XSTRING. Always call this before
124 * using an XSTRING from the stack.
125 *
126 * If (ulPreAllocate != 0), memory is pre-allocated
127 * for the string, but the string will be empty
128 * (its first byte is set to '\0'). In addition,
129 * pxstr->ulDelta will be set to 10% of ulPreAllocate.
130 *
131 * This is useful if you plan to add more stuff to
132 * the string later so we don't have to reallocate
133 * all the time in xstrcat.
134 *
135 * Do not use this on an XSTRING which is already
136 * initialized (this would cause memory leaks).
137 * Use xstrcpy or xstrset instead.
138 *
139 *@@added V0.9.6 (2000-11-01) [umoeller]
140 *@@changed V0.9.9 (2001-03-09) [umoeller]: added ulDelta
141 */
142
143void xstrInit(PXSTRING pxstr, // in/out: string
144 ULONG ulPreAllocate) // in: if > 0, memory to allocate
145{
146 memset(pxstr, 0, sizeof(XSTRING));
147 if (ulPreAllocate)
148 {
149 pxstr->psz = (PSZ)malloc(ulPreAllocate);
150 pxstr->cbAllocated = ulPreAllocate;
151 // ulLength is still zero
152 *(pxstr->psz) = 0;
153
154 pxstr->ulDelta = ulPreAllocate * 10 / 100;
155 }
156 // else: pxstr->ulDelta is still 0
157}
158
159/*
160 *@@ xstrInitSet:
161 * this can be used instead of xstrInit if you
162 * have a free()'able string you want to initialize
163 * the XSTRING with.
164 *
165 * This does not create a copy of pszNew. Instead,
166 * pszNew is used as the member string in pxstr
167 * directly.
168 *
169 * Do not use this on an XSTRING which is already
170 * initialized (this would cause memory leaks).
171 * Use xstrcpy or xstrset instead.
172 *
173 * Example:
174 *
175 + XSTRING str;
176 + xstrInitSet(&str, strdup("blah"));
177 *
178 *@@added V0.9.6 (2000-11-01) [umoeller]
179 *@@changed V0.9.9 (2001-03-09) [umoeller]: added ulDelta
180 */
181
182void xstrInitSet(PXSTRING pxstr,
183 PSZ pszNew)
184{
185 if (!pszNew)
186 {
187 memset(pxstr, 0, sizeof(XSTRING));
188 }
189 else
190 {
191 pxstr->psz = pszNew;
192 pxstr->ulLength = strlen(pszNew);
193 pxstr->cbAllocated = pxstr->ulLength + 1;
194 pxstr->ulDelta = pxstr->ulLength * 10 / 100;
195 }
196}
197
198/*
199 *@@ xstrInitCopy:
200 * this can be used instead of xstrInit if you
201 * want to initialize an XSTRING with a copy
202 * of an existing string. This is a shortcut
203 * for xstrInit() and then xstrcpy().
204 *
205 * As opposed to xstrInitSet, this does create
206 * a copy of pcszSource.
207 *
208 * Do not use this on an XSTRING which is already
209 * initialized (this would cause memory leaks).
210 * Use xstrcpy or xstrset instead.
211 *
212 * Example:
213 *
214 + XSTRING str;
215 + xstrInitCopy(&str, "blah");
216 *
217 *@@added V0.9.6 (2000-11-01) [umoeller]
218 *@@changed V0.9.7 (2000-12-31) [umoeller]: added ulExtraAllocate
219 *@@changed V0.9.9 (2001-03-09) [umoeller]: added ulDelta
220 */
221
222void xstrInitCopy(PXSTRING pxstr,
223 const char *pcszSource,
224 ULONG ulExtraAllocate) // in: if > 0, extra memory to allocate
225{
226 if (pxstr)
227 {
228 memset(pxstr, 0, sizeof(XSTRING));
229
230 if (pcszSource)
231 {
232 pxstr->ulLength = strlen(pcszSource);
233
234 if (pxstr->ulLength)
235 {
236 // we do have a source string:
237 pxstr->cbAllocated = pxstr->ulLength + 1 + ulExtraAllocate;
238 pxstr->psz = (PSZ)malloc(pxstr->cbAllocated);
239 strcpy(pxstr->psz, pcszSource);
240
241 pxstr->ulDelta = pxstr->cbAllocated * 10 / 100;
242 }
243 }
244 }
245}
246
247/*
248 *@@ xstrClear:
249 * clears the specified stack XSTRING and
250 * frees allocated memory.
251 *
252 * This is the reverse to xstrInit.
253 *
254 *@@added V0.9.6 (2000-11-01) [umoeller]
255 */
256
257void xstrClear(PXSTRING pxstr) // in/out: string
258{
259 if (pxstr->psz)
260 free(pxstr->psz);
261 memset(pxstr, 0, sizeof(XSTRING));
262}
263
264/*
265 *@@ xstrReserve:
266 * this function makes sure that the specified
267 * XSTRING has at least ulBytes bytes allocated.
268 *
269 * This function is useful if you plan to do
270 * a lot of string replacements or appends and
271 * want to avoid that the buffer is reallocated
272 * with each operation. Before those operations,
273 * call this function to make room for the operations.
274 *
275 * If ulBytes is smaller than the current allocation,
276 * this function does nothing.
277 *
278 * pxstr->ulDelta has no effect here.
279 *
280 * The XSTRING must be initialized before the
281 * call.
282 *
283 * Returns the new total no. of allocated bytes.
284 *
285 *@@added V0.9.7 (2001-01-07) [umoeller]
286 *@@changed V0.9.9 (2001-03-09) [umoeller]: now using ulDelta
287 */
288
289ULONG xstrReserve(PXSTRING pxstr,
290 ULONG ulBytes)
291{
292 ULONG cbNeeded = ulBytes;
293
294 if (cbNeeded > pxstr->cbAllocated)
295 {
296 // we need more memory than we have previously
297 // allocated:
298 ULONG cbAllocate;
299 if (pxstr->ulDelta)
300 {
301 // delta specified: allocate in chunks of that
302 // V0.9.9 (2001-03-07) [umoeller]
303 ULONG cbExtra = cbNeeded - pxstr->cbAllocated;
304 cbExtra = ( (cbExtra + pxstr->ulDelta)
305 / pxstr->ulDelta
306 )
307 * pxstr->ulDelta;
308 // if we need 3 extra bytes and ulDelta is 10,
309 // this gives us 10 extra bytes
310 // if we need 3 extra bytes and ulDelta is 1000,
311 // this gives us 1000 extra bytes
312 cbAllocate = pxstr->cbAllocated + cbExtra;
313 }
314 else
315 // no delta specified:
316 cbAllocate = cbNeeded;
317 // V0.9.9 (2001-03-05) [umoeller]: use realloc;
318 // this gives the C runtime a chance to expand the
319 // existing block
320 pxstr->psz = (PSZ)realloc(pxstr->psz, cbAllocate);
321 // if pxstr->psz is NULL, realloc behaves like malloc
322 pxstr->cbAllocated = cbAllocate;
323 // ulLength is unchanged
324 }
325 // else: we have enough memory
326
327 return (pxstr->cbAllocated);
328}
329
330/*
331 *@@ xstrCreate:
332 * allocates a new XSTRING from the heap
333 * and calls xstrInit on it.
334 *
335 * Always use xstrFree to free associated
336 * resources.
337 *
338 *@@added V0.9.6 (2000-11-01) [umoeller]
339 */
340
341PXSTRING xstrCreate(ULONG ulPreAllocate)
342{
343 PXSTRING pxstr = (PXSTRING)malloc(sizeof(XSTRING));
344 if (pxstr)
345 xstrInit(pxstr, ulPreAllocate);
346
347 return (pxstr);
348}
349
350/*
351 *@@ xstrFree:
352 * frees the specified heap XSTRING, which must
353 * have been created using xstrCreate.
354 *
355 *@@added V0.9.6 (2000-11-01) [umoeller]
356 */
357
358VOID xstrFree(PXSTRING pxstr) // in/out: string
359{
360 if (pxstr)
361 {
362 xstrClear(pxstr);
363 free(pxstr);
364 }
365}
366
367/*
368 *@@ xstrset:
369 * sets the specified XSTRING to a new string
370 * without copying it.
371 *
372 * pxstr is cleared before the new string is set.
373 *
374 * This ONLY works if pszNew has been allocated from
375 * the heap using malloc() or strdup() and is thus
376 * free()'able.
377 *
378 * This assumes that exactly strlen(pszNew) + 1
379 * bytes have been allocated for pszNew, which
380 * is true if pszNew comes from strdup().
381 *
382 *@@added V0.9.6 (2000-11-01) [umoeller]
383 *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL target crash
384 */
385
386ULONG xstrset(PXSTRING pxstr, // in/out: string
387 PSZ pszNew) // in: heap PSZ to use
388{
389 if (!pxstr)
390 return (0); // V0.9.9 (2001-02-14) [umoeller]
391
392 xstrClear(pxstr);
393 pxstr->psz = pszNew;
394 if (pszNew)
395 {
396 pxstr->ulLength = strlen(pszNew);
397 pxstr->cbAllocated = pxstr->ulLength + 1;
398
399 pxstr->ulDelta = pxstr->cbAllocated * 10 / 100;
400 }
401 // else null string: cbAllocated and ulLength are 0 already
402
403 return (pxstr->ulLength);
404}
405
406/*
407 *@@ xstrcpy:
408 * copies pcszSource to pxstr, for which memory is allocated
409 * as necessary.
410 *
411 * If pxstr contains something, its contents are destroyed.
412 *
413 * With ulSourceLength, specify the length of pcszSource
414 * or 0.
415 *
416 * -- If you specify 0, this function will run
417 * strlen(pcszSource) and copy the entire source
418 * string.
419 *
420 * -- If you already know the length of pcszSource, you
421 * can speed this function up by specifying the
422 * length.
423 *
424 * -- You are required to specify ulSourceLength if you
425 * only want to copy a substring, or if pcszSource is
426 * not zero-terminated.
427 *
428 * Returns the length of the new string (excluding the null
429 * terminator), or null upon errors.
430 *
431 * Example:
432 *
433 + XSTRING str;
434 + xstrInit(&str, 0);
435 + xstrcpy(&str, "blah", 0);
436 *
437 * This sequence can be abbreviated using xstrInitCopy.
438 *
439 * Memory cost: If there's enough room in pxstr for
440 * pcszSource, none. Otherwise pxstr is reallocated
441 * to hold enough room for pcszSource.
442 *
443 *@@changed V0.9.2 (2000-04-01) [umoeller]: renamed from strhxcpy
444 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten
445 *@@changed V0.9.7 (2001-01-15) [umoeller]: added ulSourceLength
446 *@@changed V0.9.9 (2001-01-28) [lafaix]: fixed memory leak and NULL source behavior
447 *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL target crash
448 *@@changed V0.9.9 (2001-02-16) [umoeller]: now supporting non-zero-terminated pcszSource
449 *@@changed V0.9.9 (2001-03-09) [umoeller]: now using xstrReserve
450 */
451
452ULONG xstrcpy(PXSTRING pxstr, // in/out: string
453 const char *pcszSource, // in: source, can be NULL
454 ULONG ulSourceLength) // in: length of pcszSource or 0
455{
456 if (!pxstr)
457 return (0); // V0.9.9 (2001-02-14) [umoeller]
458
459 if (pcszSource)
460 {
461 // source specified:
462 if (ulSourceLength == 0)
463 // but not length:
464 ulSourceLength = strlen(pcszSource);
465 }
466 else
467 ulSourceLength = 0;
468
469 if (ulSourceLength)
470 {
471 // we do have a source string:
472 xstrReserve(pxstr,
473 // required memory:
474 ulSourceLength + 1);
475
476 memcpy(pxstr->psz,
477 pcszSource,
478 ulSourceLength);
479 *(pxstr->psz + ulSourceLength) = '\0';
480 // V0.9.9 (2001-02-16) [umoeller]
481 // we must do this or otherwise we require pcszSource
482 // to be zero-terminated... not a good idea
483 }
484 else
485 {
486 // no source specified or source is empty:
487 if (pxstr->cbAllocated)
488 // we did have a string: set to empty,
489 // but leave allocated memory intact
490 *(pxstr->psz) = 0;
491 // else
492 // we had no string previously: in that case
493 // psz and ulLength and cbAllocated are all still NULL
494 }
495
496 // in all cases, set new length
497 pxstr->ulLength = ulSourceLength;
498
499 return (pxstr->ulLength);
500}
501
502/*
503 *@@ xstrcpys:
504 * shortcut to xstrcpy if the source is an XSTRING also.
505 *
506 *@@added V0.9.9 (2001-02-14) [umoeller]
507 */
508
509ULONG xstrcpys(PXSTRING pxstr,
510 const XSTRING *pcstrSource)
511{
512 if (!pcstrSource)
513 return (0);
514
515 return (xstrcpy(pxstr, pcstrSource->psz, pcstrSource->ulLength));
516}
517
518/*
519 *@@ xstrcat:
520 * appends pcszSource to pxstr, for which memory is
521 * reallocated if necessary.
522 *
523 * If pxstr is empty, this behaves just like xstrcpy.
524 *
525 * With ulSourceLength, specify the length of pcszSource.
526 * If you specify 0, this function will run strlen(pcszSource)
527 * itself.
528 *
529 * If you already know the length of pcszSource, you can
530 * speed this function up a bit this way.
531 *
532 * You are required to specify ulSourceLength if you only want
533 * to copy a substring, or pcszSource is not zero-terminated.
534 *
535 * Returns the length of the new string (excluding the null
536 * terminator) if the string was changed, or 0 if nothing
537 * happened.
538 *
539 * Note: To append a single character, xstrcatc is faster
540 * than xstrcat.
541 *
542 * Example:
543 *
544 + XSTRING str;
545 + xstrInit(&str, 0);
546 + xstrcpy(&str, "blah", 0);
547 + xstrcat(&str, "blup", 0);
548 *
549 * After this, str.psz points to a new string containing
550 * "blahblup".
551 *
552 * Memory cost: If there's enough room in pxstr for
553 * pcszSource, none. Otherwise pxstr is reallocated
554 * to hold enough room for pcszSource.
555 *
556 *@@changed V0.9.1 (99-12-20) [umoeller]: fixed memory leak
557 *@@changed V0.9.1 (2000-01-03) [umoeller]: crashed if pszString was null; fixed
558 *@@changed V0.9.2 (2000-04-01) [umoeller]: renamed from strhxcat
559 *@@changed V0.9.3 (2000-05-11) [umoeller]: returned 0 if pszString was initially empty; fixed
560 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten
561 *@@changed V0.9.7 (2000-12-10) [umoeller]: return value was wrong
562 *@@changed V0.9.7 (2001-01-15) [umoeller]: added ulSourceLength
563 *@@changed V0.9.9 (2001-02-16) [umoeller]: now supporting non-zero-terminated pcszSource
564 *@@changed V0.9.9 (2001-03-09) [umoeller]: now using xstrReserve
565 */
566
567ULONG xstrcat(PXSTRING pxstr, // in/out: string
568 const char *pcszSource, // in: source, can be NULL
569 ULONG ulSourceLength) // in: length of pcszSource or 0
570{
571 ULONG ulrc = 0;
572
573 if (pxstr)
574 {
575 if (pcszSource)
576 {
577 if (ulSourceLength == 0)
578 ulSourceLength = strlen(pcszSource);
579
580 if (ulSourceLength)
581 {
582 // we do have a source string:
583
584 // 1) memory management
585 xstrReserve(pxstr,
586 // required memory:
587 pxstr->ulLength + ulSourceLength + 1);
588
589 // 2) append source string:
590 memcpy(pxstr->psz + pxstr->ulLength,
591 pcszSource,
592 ulSourceLength); // null terminator
593
594 *(pxstr->psz + pxstr->ulLength + ulSourceLength) = '\0';
595 // V0.9.9 (2001-02-16) [umoeller]
596 // we must do this or otherwise we require pcszSource
597 // to be zero-terminated... not a good idea
598
599 // in all cases, set new length
600 pxstr->ulLength += ulSourceLength;
601 ulrc = pxstr->ulLength; // V0.9.7 (2000-12-10) [umoeller]
602
603 } // end if (ulSourceLength)
604 }
605
606 // else no source specified or source is empty:
607 // do nothing
608 }
609
610 return (ulrc);
611}
612
613/*
614 *@@ xstrcatc:
615 * this is similar to xstrcat, except that this is
616 * for a single character. This is a bit faster than
617 * xstrcat.
618 *
619 * If "c" is \0, nothing happens.
620 *
621 * If pxstr is empty, this behaves just like xstrcpy.
622 *
623 * Returns the length of the new string (excluding the null
624 * terminator) if the string was changed, or 0 if nothing
625 * happened.
626 *
627 * Example:
628 *
629 + XSTRING str;
630 + xstrInit(&str, 0);
631 + xstrcpy(&str, "blu");
632 + xstrcatc(&str, 'p');
633 *
634 * After this, str.psz points to a new string containing
635 * "blup".
636 *
637 * Memory cost: If there's enough room in pxstr for
638 * c, none. Otherwise pxstr is reallocated
639 * to hold enough room for c.
640 *
641 *@@added V0.9.7 (2000-12-10) [umoeller]
642 *@@changed V0.9.9 (2001-03-09) [umoeller]: now using xstrReserve
643 */
644
645ULONG xstrcatc(PXSTRING pxstr, // in/out: string
646 CHAR c) // in: character to append, can be \0
647{
648 ULONG ulrc = 0;
649
650 if ((pxstr) && (c))
651 {
652 // ULONG ulSourceLength = 1;
653 // 1) memory management
654 xstrReserve(pxstr,
655 // required memory:
656 pxstr->ulLength // existing length, without null terminator
657 + 1 // new character
658 + 1); // null terminator
659 // 2) append character:
660 pxstr->psz[pxstr->ulLength] = c;
661 pxstr->psz[pxstr->ulLength + 1] = '\0';
662
663 // in all cases, set new length
664 (pxstr->ulLength)++;
665 ulrc = pxstr->ulLength;
666
667 } // end if ((pxstr) && (c))
668
669 return (ulrc);
670}
671
672/*
673 *@@ xstrcats:
674 * shortcut to xstrcat if the source is an XSTRING also.
675 *
676 *@@added V0.9.9 (2001-02-14) [umoeller]
677 */
678
679ULONG xstrcats(PXSTRING pxstr,
680 const XSTRING *pcstrSource)
681{
682 if (!pcstrSource)
683 return (0);
684
685 return (xstrcat(pxstr,
686 pcstrSource->psz,
687 pcstrSource->ulLength));
688}
689
690/*
691 *@@ xstrrpl:
692 * replaces cReplLen characters in pxstr, starting
693 * at the position ulFirstReplPos, with the string
694 * in pxstrReplaceWith.
695 *
696 * Returns the new length of the string, excluding
697 * the null terminator, or 0 if the replacement failed
698 * (e.g. because the offsets were too large).
699 *
700 * This has been extracted from xstrFindReplace because
701 * if you already know the position of a substring,
702 * you can now call this directly. This properly
703 * reallocates the string if more memory is needed.
704 *
705 * Example:
706 *
707 + XSTRING xstr, xstrReplacement;
708 + xstrInitCopy(&xstr, "This is a test string.");
709 + // positions: 0123456789012345678901
710 + // 1 2
711 +
712 + xstrInitCopy(&xstrReplacement, "stupid");
713 +
714 + xstrrpl(&xstr,
715 + 10, // position of "test"
716 + 4, // length of "test"
717 + &xstrReplacement);
718 *
719 * This would yield "This is a stupid string."
720 *
721 * Memory cost: If there's enough room in pxstr for
722 * the replacement, none. Otherwise pxstr is reallocated
723 * to hold enough room for the replacement.
724 *
725 *@@added V0.9.7 (2001-01-15) [umoeller]
726 *@@changed V0.9.9 (2001-01-29) [lafaix]: fixed unnecessary allocation when pxstr was big enough
727 *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL target crash
728 *@@changed V0.9.9 (2001-03-09) [umoeller]: now using xstrReserve
729 */
730
731ULONG xstrrpl(PXSTRING pxstr, // in/out: string
732 ULONG ulFirstReplOfs, // in: ofs of first char to replace
733 ULONG cReplLen, // in: no. of chars to replace
734 const XSTRING *pstrReplaceWith) // in: string to replace chars with
735{
736 ULONG ulrc = 0;
737
738 // security checks...
739 if ( (pxstr) // V0.9.9 (2001-02-14) [umoeller]
740 && (ulFirstReplOfs + cReplLen <= pxstr->ulLength)
741 && (pstrReplaceWith)
742 // && (pstrReplaceWith->ulLength) no, this can be empty
743 )
744 {
745 ULONG cReplaceLen = pstrReplaceWith->ulLength;
746 // can be 0!
747
748 // size of new buffer:
749 ULONG cbNeeded = pxstr->ulLength
750 + cReplaceLen
751 - cReplLen
752 + 1; // null terminator
753 // offset where pszSearch was found
754 PSZ pFound = pxstr->psz + ulFirstReplOfs;
755
756 // now check if we have enough memory...
757 if (cbNeeded > pxstr->cbAllocated)
758 {
759 // we need more memory than we have previously
760 // allocated:
761 // reallocate using ulDelta V0.9.9 (2001-03-07) [umoeller]
762 ULONG cbAllocate;
763 PSZ pszNew;
764 if (pxstr->ulDelta)
765 {
766 // delta specified: allocate in chunks of that
767 // V0.9.9 (2001-03-07) [umoeller]
768 ULONG cbExtra = cbNeeded - pxstr->cbAllocated;
769 cbExtra = ( (cbExtra + pxstr->ulDelta)
770 / pxstr->ulDelta
771 )
772 * pxstr->ulDelta;
773 // if we need 3 extra bytes and ulDelta is 10,
774 // this gives us 10 extra bytes
775 // if we need 3 extra bytes and ulDelta is 1000,
776 // this gives us 1000 extra bytes
777 cbAllocate = pxstr->cbAllocated + cbExtra;
778 }
779 else
780 // no delta specified:
781 cbAllocate = cbNeeded;
782 // allocate new buffer
783 pszNew = (PSZ)malloc(cbAllocate);
784 // end V0.9.9 (2001-03-07) [umoeller]
785
786 if (ulFirstReplOfs)
787 // "found" was not at the beginning:
788 // copy from beginning up to found-offset
789 memcpy(pszNew,
790 pxstr->psz,
791 ulFirstReplOfs); // up to "found"
792
793 if (cReplaceLen)
794 {
795 // we have a replacement:
796 // insert it next
797 memcpy(pszNew + ulFirstReplOfs,
798 pstrReplaceWith->psz,
799 cReplaceLen + 1); // include null terminator
800 }
801
802 // copy rest:
803 // pxstr frontFOUNDtail
804 // 0 1
805 // 01234567890123
806 // ³ ³ ³ ³
807 // ³ ³ ÀÄ ulFirstReplOfs + cReplLen = 10
808 // ³ ³ ³
809 // ³ ÀÄ ulFirstReplOfs = 5
810 // ³ ³
811 // pxstr->ulLength = 14
812 memcpy(pszNew + ulFirstReplOfs + cReplaceLen,
813 pFound + cReplLen,
814 // remaining bytes:
815 pxstr->ulLength - ulFirstReplOfs - cReplLen // 9
816 + 1); // null terminator
817
818 // replace old buffer with new one
819 free(pxstr->psz);
820 pxstr->psz = pszNew;
821 pxstr->ulLength = cbNeeded - 1;
822 pxstr->cbAllocated = cbAllocate; // V0.9.9 (2001-03-07) [umoeller]
823 } // end if (pxstr->cbAllocated < cbNeeded)
824 else
825 {
826 // we have enough memory left,
827 // we can just overwrite in the middle...
828 // fixed V0.9.9 (2001-01-29) [lafaix]
829
830 // calc length of string after "found"
831 ULONG cTailLength = pxstr->ulLength - ulFirstReplOfs - cReplLen;
832
833 // first, we move the end to its new location
834 // (memmove handles overlap if needed)
835 memmove(pFound + cReplaceLen,
836 pFound + cReplLen,
837 cTailLength + 1); // including null terminator
838
839 // now overwrite "found" in the middle
840 if (cReplaceLen)
841 {
842 memcpy(pFound,
843 pstrReplaceWith->psz,
844 cReplaceLen); // no null terminator
845 }
846
847 // that's it; adjust the string length now
848 pxstr->ulLength = cbNeeded - 1;
849 }
850
851 ulrc = cbNeeded - 1;
852 } // end checks
853
854 return (ulrc);
855}
856
857/*
858 *@@ xstrFindWord:
859 * searches for pstrFind in pxstr, starting at ulOfs.
860 * However, this only finds pstrFind if it's a "word",
861 * i.e. surrounded by one of the characters in the
862 * pcszBeginChars and pcszEndChars array.
863 *
864 * This is similar to strhFindWord, but this uses
865 * strhmemfind for fast searching, and it doesn't
866 * have to calculate the string lengths because these
867 * already in XSTRING.
868 *
869 * Returns 0 if no "word" was found, or the offset
870 * of the "word" in pxstr if found.
871 *
872 *@@added V0.9.6 (2000-11-12) [umoeller]
873 *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL string crashs
874 */
875
876PSZ xstrFindWord(const XSTRING *pxstr, // in: buffer to search ("haystack")
877 ULONG ulOfs, // in: where to begin search (0 = start)
878 const XSTRING *pstrFind, // in: word to find ("needle")
879 size_t *pShiftTable, // in: shift table (see strhmemfind)
880 PBOOL pfRepeatFind, // in: repeat find? (see strhmemfind)
881 const char *pcszBeginChars, // suggestion: "\x0d\x0a ()/\\-,."
882 const char *pcszEndChars) // suggestion: "\x0d\x0a ()/\\-,.:;"
883{
884 PSZ pReturn = 0;
885
886 if (pxstr && pstrFind) // V0.9.9 (2001-02-14) [umoeller]
887 {
888 ULONG ulFoundLen = pstrFind->ulLength;
889
890 if ((pxstr->ulLength) && (ulFoundLen))
891 {
892 const char *p = pxstr->psz + ulOfs;
893
894 do // while p
895 {
896 p = (PSZ)strhmemfind(p, // in: haystack
897 pxstr->ulLength - (p - pxstr->psz),
898 // remaining length of haystack
899 pstrFind->psz,
900 ulFoundLen,
901 pShiftTable,
902 pfRepeatFind);
903 if (p)
904 {
905 // string found:
906 // check if that's a word
907
908 if (strhIsWord(pxstr->psz,
909 p,
910 ulFoundLen,
911 pcszBeginChars,
912 pcszEndChars))
913 {
914 // valid end char:
915 pReturn = (PSZ)p;
916 break;
917 }
918
919 p += ulFoundLen;
920 }
921 } while (p);
922
923 }
924 }
925
926 return (pReturn);
927}
928
929/*
930 *@@ xstrFindReplace:
931 * replaces the first occurence of pstrSearch with
932 * pstrReplace in pxstr.
933 *
934 * Starting with V0.9.6, this operates entirely on
935 * XSTRING's for speed because we then know the string
936 * lengths already and can use memcpy instead of strcpy.
937 * This new version should be magnitudes faster,
938 * especially with large string bufffers.
939 *
940 * None of the pointers can be NULL, but if pstrReplace
941 * is empty, this effectively erases pstrSearch in pxstr.
942 *
943 * Returns the length of the new string (exclusing the
944 * null terminator) or 0 if pszSearch was not found
945 * (and pxstr was therefore not changed).
946 *
947 * This starts the search at *pulOfs. If
948 * (*pulOfs == 0), this starts from the beginning
949 * of pxstr.
950 *
951 * If the string was found, *pulOfs will be set to the
952 * first character after the new replacement string. This
953 * allows you to call this func again with the same strings
954 * to have several occurences replaced (see the example below).
955 *
956 * There are two wrappers around this function which
957 * work on C strings instead (however, thus losing the
958 * speed advantage):
959 *
960 * -- strhFindReplace operates on C strings only;
961 *
962 * -- xstrFindReplaceC uses C strings for the search and replace
963 * parameters.
964 *
965 * <B>Example usage:</B>
966 *
967 + XSTRING strBuf,
968 + strFind,
969 + strRepl;
970 + size_t ShiftTable[256];
971 + BOOL fRepeat = FALSE;
972 + ULONG ulOffset = 0;
973 +
974 + xstrInitCopy(&strBuf, "Test phrase 1. Test phrase 2.", 0);
975 + xstrInitSet(&strFind, "Test");
976 + xstrInitSet(&strRepl, "Dummy");
977 + while (xstrFindReplace(&str,
978 + &ulPos, // in/out: offset
979 + &strFind, // search
980 + &strRepl, // replace
981 + ShiftTable,
982 + &fRepeat))
983 + ;
984 *
985 * would replace all occurences of "Test" in str with
986 * "Dummy".
987 *
988 * Memory cost: Calls xstrrpl if pstrSearch was found.
989 *
990 *@@changed V0.9.0 [umoeller]: totally rewritten.
991 *@@changed V0.9.0 (99-11-08) [umoeller]: crashed if *ppszBuf was NULL. Fixed.
992 *@@changed V0.9.2 (2000-04-01) [umoeller]: renamed from strhxrpl
993 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten
994 *@@changed V0.9.6 (2000-11-12) [umoeller]: now using strhmemfind
995 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from xstrrpl; extracted new xstrrpl
996 */
997
998ULONG xstrFindReplace(PXSTRING pxstr, // in/out: string
999 PULONG pulOfs, // in: where to begin search (0 = start);
1000 // out: ofs of first char after replacement string
1001 const XSTRING *pstrSearch, // in: search string; cannot be NULL
1002 const XSTRING *pstrReplace, // in: replacement string; cannot be NULL
1003 size_t *pShiftTable, // in: shift table (see strhmemfind)
1004 PBOOL pfRepeatFind) // in: repeat find? (see strhmemfind)
1005{
1006 ULONG ulrc = 0; // default: not found
1007
1008 if ((pxstr) && (pstrSearch) && (pstrReplace))
1009 {
1010 ULONG cSearchLen = pstrSearch->ulLength;
1011
1012 // can we search this?
1013 if ( (*pulOfs < pxstr->ulLength)
1014 && (cSearchLen)
1015 )
1016 {
1017 // yes:
1018 ULONG ulOfs = *pulOfs;
1019 const char *pFound
1020 = (const char *)strhmemfind(pxstr->psz + ulOfs, // in: haystack
1021 pxstr->ulLength - ulOfs,
1022 pstrSearch->psz,
1023 cSearchLen,
1024 pShiftTable,
1025 pfRepeatFind);
1026 if (pFound)
1027 {
1028 ULONG ulFirstReplOfs = pFound - pxstr->psz;
1029 // found in buffer from ofs:
1030 // replace pFound with pstrReplace
1031 ulrc = xstrrpl(pxstr,
1032 ulFirstReplOfs, // where to start
1033 cSearchLen, // chars to replace
1034 pstrReplace);
1035
1036 // return new length
1037 *pulOfs = ulFirstReplOfs + pstrReplace->ulLength;
1038 } // end if (pFound)
1039 } // end if ( (*pulOfs < pxstr->ulLength) ...
1040 } // end if ((pxstr) && (pstrSearch) && (pstrReplace))
1041
1042 return (ulrc);
1043}
1044
1045/*
1046 *@@ xstrFindReplaceC:
1047 * wrapper around xstrFindReplace() which allows using
1048 * C strings for the find and replace parameters.
1049 *
1050 * This creates two temporary XSTRING's for pcszSearch
1051 * and pcszReplace and thus cannot use the shift table
1052 * for repetitive searches. As a result, this is slower
1053 * than xstrFindReplace.
1054 *
1055 * If you search with the same strings several times,
1056 * you'll be better off using xstrFindReplace() directly.
1057 *
1058 *@@added V0.9.6 (2000-11-01) [umoeller]
1059 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from xstrcrpl
1060 */
1061
1062ULONG xstrFindReplaceC(PXSTRING pxstr, // in/out: string
1063 PULONG pulOfs, // in: where to begin search (0 = start);
1064 // out: ofs of first char after replacement string
1065 const char *pcszSearch, // in: search string; cannot be NULL
1066 const char *pcszReplace) // in: replacement string; cannot be NULL
1067{
1068 XSTRING xstrFind,
1069 xstrReplace;
1070 size_t ShiftTable[256];
1071 BOOL fRepeat = FALSE;
1072 // initialize find/replace strings... note that the
1073 // C strings are not free()'able, so we MUST NOT use xstrClear
1074 // before leaving
1075 xstrInitSet(&xstrFind, (PSZ)pcszSearch);
1076 xstrInitSet(&xstrReplace, (PSZ)pcszReplace);
1077
1078 return (xstrFindReplace(pxstr, pulOfs, &xstrFind, &xstrReplace, ShiftTable, &fRepeat));
1079}
1080
1081// static encoding table for xstrEncode
1082static PSZ apszEncoding[] =
1083{
1084 "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
1085 "%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F",
1086 "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
1087 "%18", "%19", "%1A", "%1B", "%1C", "%1D", "%1E", "%1F",
1088 "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
1089 "%28", "%29", "%2A", "%2B", "%2C", "%2D", "%2E", "%2F",
1090 "%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
1091 "%38", "%39", "%3A", "%3B", "%3C", "%3D", "%3E", "%3F",
1092 "%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
1093 "%48", "%49", "%4A", "%4B", "%4C", "%4D", "%4E", "%4F",
1094 "%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
1095 "%58", "%59", "%5A", "%5B", "%5C", "%5D", "%5E", "%5F",
1096 "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
1097 "%68", "%69", "%6A", "%6B", "%6C", "%6D", "%6E", "%6F",
1098 "%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
1099 "%78", "%79", "%7A", "%7B", "%7C", "%7D", "%7E", "%7F",
1100 "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
1101 "%88", "%89", "%8A", "%8B", "%8C", "%8D", "%8E", "%8F",
1102 "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
1103 "%98", "%99", "%9A", "%9B", "%9C", "%9D", "%9E", "%9F",
1104 "%A0", "%A1", "%A2", "%A3", "%A4", "%A5", "%A6", "%A7",
1105 "%A8", "%A9", "%AA", "%AB", "%AC", "%AD", "%AE", "%AF",
1106 "%B0", "%B1", "%B2", "%B3", "%B4", "%B5", "%B6", "%B7",
1107 "%B8", "%B9", "%BA", "%BB", "%BC", "%BD", "%BE", "%BF",
1108 "%C0", "%C1", "%C2", "%C3", "%C4", "%C5", "%C6", "%C7",
1109 "%C8", "%C9", "%CA", "%CB", "%CC", "%CD", "%CE", "%CF",
1110 "%D0", "%D1", "%D2", "%D3", "%D4", "%D5", "%D6", "%D7",
1111 "%D8", "%D9", "%DA", "%DB", "%DC", "%DD", "%DE", "%DF",
1112 "%E0", "%E1", "%E2", "%E3", "%E4", "%E5", "%E6", "%E7",
1113 "%E8", "%E9", "%EA", "%EB", "%EC", "%ED", "%EE", "%EF",
1114 "%F0", "%F1", "%F2", "%F3", "%F4", "%F5", "%F6", "%F7",
1115 "%F8", "%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF"
1116};
1117
1118/*
1119 *@@ xstrEncode:
1120 * encodes characters in a string.
1121 *
1122 * This searches pxstr for all occurences of the
1123 * characters in pcszEncode (which must be a
1124 * null-terminated list of characters to be
1125 * encoded). Each occurence that is found is
1126 * replaced with "%hh", with "hh" being the
1127 * two-digit hex number of the encoded character.
1128 *
1129 * For example, to encode strings for the XCenter,
1130 * set pcszEncode to "%,();=".
1131 *
1132 * Returns the no. of characters replaced.
1133 *
1134 * NOTE: You must make sure that pcszEncode ALWAYS
1135 * contains the "%" character as well, which must
1136 * always be encoded (i.e. escaped) because it is
1137 * used for encoding the characters. Otherwise
1138 * you won't be able to decode the string again.
1139 *
1140 * Example: To encode all occurences of
1141 * "a", "b", and "c" in a string, do this:
1142 *
1143 + XSTRING str;
1144 + xstrInitCopy(&str, "Sample characters.";
1145 + xstrEncode(&str, "abc%";
1146 *
1147 * would convert str to contain:
1148 *
1149 + S%61mple %63hara%63ters.
1150 *
1151 * Memory cost: None, except for that of xstrcpy.
1152 *
1153 *@@added V0.9.9 (2001-02-28) [umoeller]
1154 *@@changed V0.9.9 (2001-03-06) [lafaix]: rewritten.
1155 */
1156
1157ULONG xstrEncode(PXSTRING pxstr, // in/out: string to convert
1158 const char *pcszEncode) // in: characters to encode (e.g. "%,();=")
1159{
1160 ULONG ulrc = 0,
1161 ul,
1162 ulEncodeLength;
1163
1164 if ( (pxstr)
1165 && (pxstr->ulLength)
1166 && (pcszEncode)
1167 && (ulEncodeLength = strlen(pcszEncode)))
1168 {
1169 PSZ pszDest = (PSZ)malloc(pxstr->ulLength * 3
1170 + 1),
1171 pszDestCurr = pszDest;
1172
1173 if (pszDest)
1174 {
1175 for (ul = 0;
1176 ul < pxstr->ulLength;
1177 ul++)
1178 {
1179 ULONG ulEncode;
1180
1181 for (ulEncode = 0;
1182 ulEncode < ulEncodeLength;
1183 ulEncode++)
1184 {
1185 if (pxstr->psz[ul] == pcszEncode[ulEncode])
1186 {
1187 // use the static encoding table for speed
1188 memcpy(pszDestCurr,
1189 apszEncoding[(unsigned char)pcszEncode[ulEncode]],
1190 3);
1191 pszDestCurr += 3;
1192 ulrc++;
1193 goto iterate;
1194 }
1195 }
1196
1197 *pszDestCurr++ = pxstr->psz[ul];
1198
1199 iterate:
1200 ;
1201 }
1202 }
1203
1204 // something was encoded; update pxstr
1205 if (ulrc)
1206 {
1207 *pszDestCurr = 0;
1208
1209 xstrcpy(pxstr, pszDest, pszDestCurr-pszDest);
1210 }
1211
1212 free(pszDest);
1213 }
1214
1215 return (ulrc);
1216}
1217
1218/*
1219 *@@ xstrDecode:
1220 * decodes a string previously encoded by xstrEncode.
1221 *
1222 * This simply assumes that all '%' characters in
1223 * pxstr introduce encodings and the next two characters
1224 * after '%' always are a hex character code. This
1225 * only recognizes hex in upper case. All this will
1226 * work properly with encodings from xstrEncode.
1227 *
1228 * Returns the no. of encodings replaced.
1229 *
1230 * Memory cost: None.
1231 *
1232 *@@added V0.9.9 (2001-02-28) [umoeller]
1233 *@@changed V0.9.9 (2001-03-06) [lafaix]: removed memory allocation
1234 */
1235
1236ULONG xstrDecode(PXSTRING pxstr) // in/out: string to be decoded
1237{
1238 ULONG ulrc = 0;
1239
1240 if ( (pxstr)
1241 && (pxstr->ulLength)
1242 )
1243 {
1244 const char *pSource = pxstr->psz;
1245 PSZ pszDest = (PSZ)pSource,
1246 pDest = (PSZ)pSource;
1247 CHAR c;
1248
1249 while ((c = *pSource++))
1250 {
1251 // pSource points to next char now
1252
1253 if (c == '%')
1254 {
1255 static char ach[] = "0123456789ABCDEF";
1256
1257 // convert two chars after '%'
1258 CHAR c2, // first char after '%' --> hi-nibble
1259 c3; // second char after '%' --> lo-nibble
1260 const char *p2, // for first char: points into ach or is NULL
1261 *p3; // for second char: points into ach or is NULL
1262 if ( (c2 = *pSource)
1263 && (p2 = strchr(ach, c2))
1264 && (c3 = *(pSource + 1))
1265 && (p3 = strchr(ach, c3))
1266 )
1267 {
1268 // both chars after '%' were valid:
1269 *pDest++ = // lo-nibble:
1270 (p3 - ach) // 0 for '0', 10 for 'A', ...
1271 // hi-nibble:
1272 + ((p2 - ach) << 4);
1273 // go on after that
1274 pSource += 2;
1275 // raise return count
1276 ulrc++;
1277 // next in loop
1278 continue;
1279 }
1280 }
1281
1282 // not encoding, or null after '%', or invalid encoding:
1283 // just copy this
1284 *pDest++ = c;
1285 } // while ((ch = *pSource++))
1286
1287 if (ulrc)
1288 {
1289 *pDest = 0;
1290 pxstr->ulLength = (pDest - pszDest);
1291 }
1292 }
1293
1294 return (ulrc);
1295}
1296
1297/*
1298 *@@ xstrConvertLineFormat:
1299 * converts between line formats.
1300 *
1301 * If (fToCFormat == CRLF2LF), all \r\n pairs are replaced
1302 * with \n chars (UNIX or C format).
1303 *
1304 * Reversely, if (fToCFormat == LF2CRLF), all \n chars
1305 * are converted to \r\n pairs (DOS and OS/2 formats).
1306 * No check is made whether this has already been done.
1307 *
1308 *@@added V0.9.7 (2001-01-15) [umoeller]
1309 */
1310
1311VOID xstrConvertLineFormat(PXSTRING pxstr,
1312 BOOL fToCFormat) // in: if CRLF2LF, to C format; if LF2CRLF, to OS/2 format.
1313{
1314 XSTRING strFind,
1315 strRepl;
1316 size_t ShiftTable[256];
1317 BOOL fRepeat = FALSE;
1318 ULONG ulOfs = 0;
1319
1320 if (fToCFormat)
1321 {
1322 // OS/2 to C:
1323 xstrInitSet(&strFind, "\r\n");
1324 xstrInitSet(&strRepl, "\n");
1325 }
1326 else
1327 {
1328 // C to OS/2:
1329 xstrInitSet(&strFind, "\n");
1330 xstrInitSet(&strRepl, "\r\n");
1331 }
1332
1333 while (xstrFindReplace(pxstr,
1334 &ulOfs,
1335 &strFind,
1336 &strRepl,
1337 ShiftTable,
1338 &fRepeat))
1339 ;
1340}
1341
1342// test case
1343
1344/* int main(void)
1345{
1346 XSTRING str,
1347 strFind,
1348 strReplace;
1349 size_t shift[256];
1350 BOOL fRepeat = FALSE;
1351 ULONG ulOfs = 0;
1352
1353 xstrInit(&str, 0);
1354 xstrInit(&strFind, 0);
1355 xstrInit(&strReplace, 0);
1356
1357 str.ulDelta = 50;
1358
1359 xstrcpy(&str, "Test string 1. Test string 2. Test string 3. !", 0);
1360 xstrcpy(&strFind, "Test", 0);
1361 xstrcpy(&strReplace, "Dummy", 0);
1362
1363 printf("Old string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1364
1365 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
1366
1367 fRepeat = FALSE;
1368 ulOfs = 0;
1369 while (xstrFindReplace(&str,
1370 &ulOfs,
1371 &strFind,
1372 &strReplace,
1373 shift, &fRepeat));
1374 ;
1375
1376 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1377
1378 printf("Appending \"blah\".\n");
1379 xstrcat(&str, "blah", 0);
1380 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1381
1382 xstrcpy(&strFind, strReplace.psz, 0);
1383 xstrClear(&strReplace);
1384
1385 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
1386
1387 fRepeat = FALSE;
1388 ulOfs = 0;
1389 while (xstrFindReplace(&str,
1390 &ulOfs,
1391 &strFind,
1392 &strReplace,
1393 shift, &fRepeat));
1394 ;
1395
1396 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1397
1398 xstrcpy(&strFind, " ", 0);
1399 xstrcpy(&strReplace, ".", 0);
1400
1401 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
1402
1403 fRepeat = FALSE;
1404 ulOfs = 0;
1405 while (xstrFindReplace(&str,
1406 &ulOfs,
1407 &strFind,
1408 &strReplace,
1409 shift, &fRepeat));
1410 ;
1411
1412 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1413
1414 xstrcpy(&strFind, ".", 0);
1415 xstrcpy(&strReplace, "*.........................*", 0);
1416
1417 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
1418
1419 fRepeat = FALSE;
1420 ulOfs = 0;
1421 while (xstrFindReplace(&str,
1422 &ulOfs,
1423 &strFind,
1424 &strReplace,
1425 shift, &fRepeat));
1426 ;
1427
1428 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1429
1430 xstrcpy(&strFind, "..........", 0);
1431 xstrcpy(&strReplace, "@", 0);
1432
1433 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
1434
1435 fRepeat = FALSE;
1436 ulOfs = 0;
1437 while (xstrFindReplace(&str,
1438 &ulOfs,
1439 &strFind,
1440 &strReplace,
1441 shift, &fRepeat));
1442 ;
1443
1444 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1445
1446 printf("Encoding @* chars.\n");
1447 xstrEncode(&str, "@*");
1448 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1449
1450 printf("Decoding @* chars.\n");
1451 xstrDecode(&str);
1452 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1453
1454 return (0);
1455} */
1456
1457
Note: See TracBrowser for help on using the repository browser.