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

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

Misc. fixes.

  • Property svn:eol-style set to CRLF
  • Property svn:keywords set to Author Date Id Revision
File size: 49.4 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 first
694 * "cReplaceWithLen" characters from pcszReplaceWith.
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 *@@changed V0.9.11 (2001-04-22) [umoeller]: replaced replacement XSTRING with PCSZ
730 */
731
732ULONG xstrrpl(PXSTRING pxstr, // in/out: string
733 ULONG ulFirstReplOfs, // in: ofs of first char to replace
734 ULONG cReplLen, // in: no. of chars to replace
735 const char *pcszReplaceWith, // in: string to replace chars with
736 ULONG cReplaceWithLen) // in: length of replacement string
737 // (this MUST be specified; if 0, chars are removed only)
738{
739 ULONG ulrc = 0;
740
741 // security checks...
742 if ( (pxstr) // V0.9.9 (2001-02-14) [umoeller]
743 && (ulFirstReplOfs + cReplLen <= pxstr->ulLength)
744 && (pcszReplaceWith)
745 )
746 {
747 // size of new buffer:
748 ULONG cbNeeded = pxstr->ulLength // existing
749 + cReplaceWithLen // plus replacement string length
750 - cReplLen // minus replaced characters
751 + 1; // plus null terminator
752 // offset where pszSearch was found
753 PSZ pFound = pxstr->psz + ulFirstReplOfs;
754
755 // now check if we have enough memory...
756 if (cbNeeded > pxstr->cbAllocated)
757 {
758 // we need more memory than we have previously
759 // allocated:
760 // reallocate using ulDelta V0.9.9 (2001-03-07) [umoeller]
761 ULONG cbAllocate;
762 PSZ pszNew;
763 if (pxstr->ulDelta)
764 {
765 // delta specified: allocate in chunks of that
766 // V0.9.9 (2001-03-07) [umoeller]
767 ULONG cbExtra = cbNeeded - pxstr->cbAllocated;
768 cbExtra = ( (cbExtra + pxstr->ulDelta)
769 / pxstr->ulDelta
770 )
771 * pxstr->ulDelta;
772 // if we need 3 extra bytes and ulDelta is 10,
773 // this gives us 10 extra bytes
774 // if we need 3 extra bytes and ulDelta is 1000,
775 // this gives us 1000 extra bytes
776 cbAllocate = pxstr->cbAllocated + cbExtra;
777 }
778 else
779 // no delta specified:
780 cbAllocate = cbNeeded;
781 // allocate new buffer
782 pszNew = (PSZ)malloc(cbAllocate);
783 // end V0.9.9 (2001-03-07) [umoeller]
784
785 if (ulFirstReplOfs)
786 // "found" was not at the beginning:
787 // copy from beginning up to found-offset
788 memcpy(pszNew,
789 pxstr->psz,
790 ulFirstReplOfs); // up to "found"
791
792 if (cReplaceWithLen)
793 {
794 // we have a replacement:
795 // insert it next
796
797 /* memcpy(pszNew + ulFirstReplOfs,
798 pstrReplaceWith->psz,
799 cReplaceWithLen + 1); // include null terminator
800 */
801 // no, we no longer can be sure that pcszReplaceWith is
802 // null terminated, so terminate explicitly
803 // V0.9.11 (2001-04-22) [umoeller]
804
805 memcpy(pszNew + ulFirstReplOfs,
806 pcszReplaceWith,
807 cReplaceWithLen);
808 *(pszNew + ulFirstReplOfs + cReplaceWithLen) = '\0';
809 }
810
811 // copy rest:
812 // pxstr frontFOUNDtail
813 // 0 1
814 // 01234567890123
815 // ³ ³ ³ ³
816 // ³ ³ ÀÄ ulFirstReplOfs + cReplLen = 10
817 // ³ ³ ³
818 // ³ ÀÄ ulFirstReplOfs = 5
819 // ³ ³
820 // pxstr->ulLength = 14
821 memcpy(pszNew + ulFirstReplOfs + cReplaceWithLen,
822 pFound + cReplLen,
823 // remaining bytes:
824 pxstr->ulLength - ulFirstReplOfs - cReplLen // 9
825 + 1); // null terminator
826
827 // replace old buffer with new one
828 free(pxstr->psz);
829 pxstr->psz = pszNew;
830 pxstr->ulLength = cbNeeded - 1;
831 pxstr->cbAllocated = cbAllocate; // V0.9.9 (2001-03-07) [umoeller]
832 } // end if (pxstr->cbAllocated < cbNeeded)
833 else
834 {
835 // we have enough memory left,
836 // we can just overwrite in the middle...
837 // fixed V0.9.9 (2001-01-29) [lafaix]
838
839 // calc length of string after "found"
840 ULONG cTailLength = pxstr->ulLength - ulFirstReplOfs - cReplLen;
841
842 // first, we move the end to its new location
843 // (memmove handles overlap if needed)
844 memmove(pFound + cReplaceWithLen,
845 pFound + cReplLen,
846 cTailLength + 1); // including null terminator
847
848 // now overwrite "found" in the middle
849 if (cReplaceWithLen)
850 {
851 memcpy(pFound,
852 pcszReplaceWith,
853 cReplaceWithLen); // no null terminator
854 }
855
856 // that's it; adjust the string length now
857 pxstr->ulLength = cbNeeded - 1;
858 }
859
860 ulrc = cbNeeded - 1;
861 } // end checks
862
863 return (ulrc);
864}
865
866/*
867 *@@ xstrFindWord:
868 * searches for pstrFind in pxstr, starting at ulOfs.
869 * However, this only finds pstrFind if it's a "word",
870 * i.e. surrounded by one of the characters in the
871 * pcszBeginChars and pcszEndChars array.
872 *
873 * This is similar to strhFindWord, but this uses
874 * strhmemfind for fast searching, and it doesn't
875 * have to calculate the string lengths because these
876 * already in XSTRING.
877 *
878 * Returns 0 if no "word" was found, or the offset
879 * of the "word" in pxstr if found.
880 *
881 *@@added V0.9.6 (2000-11-12) [umoeller]
882 *@@changed V0.9.9 (2001-02-14) [umoeller]: fixed NULL string crashs
883 */
884
885PSZ xstrFindWord(const XSTRING *pxstr, // in: buffer to search ("haystack")
886 ULONG ulOfs, // in: where to begin search (0 = start)
887 const XSTRING *pstrFind, // in: word to find ("needle")
888 size_t *pShiftTable, // in: shift table (see strhmemfind)
889 PBOOL pfRepeatFind, // in: repeat find? (see strhmemfind)
890 const char *pcszBeginChars, // suggestion: "\x0d\x0a ()/\\-,."
891 const char *pcszEndChars) // suggestion: "\x0d\x0a ()/\\-,.:;"
892{
893 PSZ pReturn = 0;
894
895 if (pxstr && pstrFind) // V0.9.9 (2001-02-14) [umoeller]
896 {
897 ULONG ulFoundLen = pstrFind->ulLength;
898
899 if ((pxstr->ulLength) && (ulFoundLen))
900 {
901 const char *p = pxstr->psz + ulOfs;
902
903 do // while p
904 {
905 p = (PSZ)strhmemfind(p, // in: haystack
906 pxstr->ulLength - (p - pxstr->psz),
907 // remaining length of haystack
908 pstrFind->psz,
909 ulFoundLen,
910 pShiftTable,
911 pfRepeatFind);
912 if (p)
913 {
914 // string found:
915 // check if that's a word
916
917 if (strhIsWord(pxstr->psz,
918 p,
919 ulFoundLen,
920 pcszBeginChars,
921 pcszEndChars))
922 {
923 // valid end char:
924 pReturn = (PSZ)p;
925 break;
926 }
927
928 p += ulFoundLen;
929 }
930 } while (p);
931
932 }
933 }
934
935 return (pReturn);
936}
937
938/*
939 *@@ xstrFindReplace:
940 * replaces the first occurence of pstrSearch with
941 * pstrReplace in pxstr.
942 *
943 * Starting with V0.9.6, this operates entirely on
944 * XSTRING's for speed because we then know the string
945 * lengths already and can use memcpy instead of strcpy.
946 * This new version should be magnitudes faster,
947 * especially with large string bufffers.
948 *
949 * None of the pointers can be NULL, but if pstrReplace
950 * is empty, this effectively erases pstrSearch in pxstr.
951 *
952 * Returns the length of the new string (exclusing the
953 * null terminator) or 0 if pszSearch was not found
954 * (and pxstr was therefore not changed).
955 *
956 * This starts the search at *pulOfs. If
957 * (*pulOfs == 0), this starts from the beginning
958 * of pxstr.
959 *
960 * If the string was found, *pulOfs will be set to the
961 * first character after the new replacement string. This
962 * allows you to call this func again with the same strings
963 * to have several occurences replaced (see the example below).
964 *
965 * There are two wrappers around this function which
966 * work on C strings instead (however, thus losing the
967 * speed advantage):
968 *
969 * -- strhFindReplace operates on C strings only;
970 *
971 * -- xstrFindReplaceC uses C strings for the search and replace
972 * parameters.
973 *
974 * <B>Example usage:</B>
975 *
976 + XSTRING strBuf,
977 + strFind,
978 + strRepl;
979 + size_t ShiftTable[256];
980 + BOOL fRepeat = FALSE;
981 + ULONG ulOffset = 0;
982 +
983 + xstrInitCopy(&strBuf, "Test phrase 1. Test phrase 2.", 0);
984 + xstrInitSet(&strFind, "Test");
985 + xstrInitSet(&strRepl, "Dummy");
986 + while (xstrFindReplace(&str,
987 + &ulPos, // in/out: offset
988 + &strFind, // search
989 + &strRepl, // replace
990 + ShiftTable,
991 + &fRepeat))
992 + ;
993 *
994 * would replace all occurences of "Test" in str with
995 * "Dummy".
996 *
997 * Memory cost: Calls xstrrpl if pstrSearch was found.
998 *
999 *@@changed V0.9.0 [umoeller]: totally rewritten.
1000 *@@changed V0.9.0 (99-11-08) [umoeller]: crashed if *ppszBuf was NULL. Fixed.
1001 *@@changed V0.9.2 (2000-04-01) [umoeller]: renamed from strhxrpl
1002 *@@changed V0.9.6 (2000-11-01) [umoeller]: rewritten
1003 *@@changed V0.9.6 (2000-11-12) [umoeller]: now using strhmemfind
1004 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from xstrrpl; extracted new xstrrpl
1005 */
1006
1007ULONG xstrFindReplace(PXSTRING pxstr, // in/out: string
1008 PULONG pulOfs, // in: where to begin search (0 = start);
1009 // out: ofs of first char after replacement string
1010 const XSTRING *pstrSearch, // in: search string; cannot be NULL
1011 const XSTRING *pstrReplace, // in: replacement string; cannot be NULL
1012 size_t *pShiftTable, // in: shift table (see strhmemfind)
1013 PBOOL pfRepeatFind) // in: repeat find? (see strhmemfind)
1014{
1015 ULONG ulrc = 0; // default: not found
1016
1017 if ((pxstr) && (pstrSearch) && (pstrReplace))
1018 {
1019 ULONG cSearchLen = pstrSearch->ulLength;
1020
1021 // can we search this?
1022 if ( (*pulOfs < pxstr->ulLength)
1023 && (cSearchLen)
1024 )
1025 {
1026 // yes:
1027 ULONG ulOfs = *pulOfs;
1028 const char *pFound
1029 = (const char *)strhmemfind(pxstr->psz + ulOfs, // in: haystack
1030 pxstr->ulLength - ulOfs,
1031 pstrSearch->psz,
1032 cSearchLen,
1033 pShiftTable,
1034 pfRepeatFind);
1035 if (pFound)
1036 {
1037 ULONG ulFirstReplOfs = pFound - pxstr->psz;
1038 // found in buffer from ofs:
1039 // replace pFound with pstrReplace
1040 ulrc = xstrrpl(pxstr,
1041 ulFirstReplOfs, // where to start
1042 cSearchLen, // chars to replace
1043 pstrReplace->psz,
1044 pstrReplace->ulLength); // adjusted V0.9.11 (2001-04-22) [umoeller]
1045
1046 // return new length
1047 *pulOfs = ulFirstReplOfs + pstrReplace->ulLength;
1048 } // end if (pFound)
1049 } // end if ( (*pulOfs < pxstr->ulLength) ...
1050 } // end if ((pxstr) && (pstrSearch) && (pstrReplace))
1051
1052 return (ulrc);
1053}
1054
1055/*
1056 *@@ xstrFindReplaceC:
1057 * wrapper around xstrFindReplace() which allows using
1058 * C strings for the find and replace parameters.
1059 *
1060 * This creates two temporary XSTRING's for pcszSearch
1061 * and pcszReplace and thus cannot use the shift table
1062 * for repetitive searches. As a result, this is slower
1063 * than xstrFindReplace.
1064 *
1065 * If you search with the same strings several times,
1066 * you'll be better off using xstrFindReplace() directly.
1067 *
1068 *@@added V0.9.6 (2000-11-01) [umoeller]
1069 *@@changed V0.9.7 (2001-01-15) [umoeller]: renamed from xstrcrpl
1070 */
1071
1072ULONG xstrFindReplaceC(PXSTRING pxstr, // in/out: string
1073 PULONG pulOfs, // in: where to begin search (0 = start);
1074 // out: ofs of first char after replacement string
1075 const char *pcszSearch, // in: search string; cannot be NULL
1076 const char *pcszReplace) // in: replacement string; cannot be NULL
1077{
1078 XSTRING xstrFind,
1079 xstrReplace;
1080 size_t ShiftTable[256];
1081 BOOL fRepeat = FALSE;
1082 // initialize find/replace strings... note that the
1083 // C strings are not free()'able, so we MUST NOT use xstrClear
1084 // before leaving
1085 xstrInitSet(&xstrFind, (PSZ)pcszSearch);
1086 xstrInitSet(&xstrReplace, (PSZ)pcszReplace);
1087
1088 return (xstrFindReplace(pxstr, pulOfs, &xstrFind, &xstrReplace, ShiftTable, &fRepeat));
1089}
1090
1091// static encoding table for xstrEncode
1092static PSZ apszEncoding[] =
1093{
1094 "%00", "%01", "%02", "%03", "%04", "%05", "%06", "%07",
1095 "%08", "%09", "%0A", "%0B", "%0C", "%0D", "%0E", "%0F",
1096 "%10", "%11", "%12", "%13", "%14", "%15", "%16", "%17",
1097 "%18", "%19", "%1A", "%1B", "%1C", "%1D", "%1E", "%1F",
1098 "%20", "%21", "%22", "%23", "%24", "%25", "%26", "%27",
1099 "%28", "%29", "%2A", "%2B", "%2C", "%2D", "%2E", "%2F",
1100 "%30", "%31", "%32", "%33", "%34", "%35", "%36", "%37",
1101 "%38", "%39", "%3A", "%3B", "%3C", "%3D", "%3E", "%3F",
1102 "%40", "%41", "%42", "%43", "%44", "%45", "%46", "%47",
1103 "%48", "%49", "%4A", "%4B", "%4C", "%4D", "%4E", "%4F",
1104 "%50", "%51", "%52", "%53", "%54", "%55", "%56", "%57",
1105 "%58", "%59", "%5A", "%5B", "%5C", "%5D", "%5E", "%5F",
1106 "%60", "%61", "%62", "%63", "%64", "%65", "%66", "%67",
1107 "%68", "%69", "%6A", "%6B", "%6C", "%6D", "%6E", "%6F",
1108 "%70", "%71", "%72", "%73", "%74", "%75", "%76", "%77",
1109 "%78", "%79", "%7A", "%7B", "%7C", "%7D", "%7E", "%7F",
1110 "%80", "%81", "%82", "%83", "%84", "%85", "%86", "%87",
1111 "%88", "%89", "%8A", "%8B", "%8C", "%8D", "%8E", "%8F",
1112 "%90", "%91", "%92", "%93", "%94", "%95", "%96", "%97",
1113 "%98", "%99", "%9A", "%9B", "%9C", "%9D", "%9E", "%9F",
1114 "%A0", "%A1", "%A2", "%A3", "%A4", "%A5", "%A6", "%A7",
1115 "%A8", "%A9", "%AA", "%AB", "%AC", "%AD", "%AE", "%AF",
1116 "%B0", "%B1", "%B2", "%B3", "%B4", "%B5", "%B6", "%B7",
1117 "%B8", "%B9", "%BA", "%BB", "%BC", "%BD", "%BE", "%BF",
1118 "%C0", "%C1", "%C2", "%C3", "%C4", "%C5", "%C6", "%C7",
1119 "%C8", "%C9", "%CA", "%CB", "%CC", "%CD", "%CE", "%CF",
1120 "%D0", "%D1", "%D2", "%D3", "%D4", "%D5", "%D6", "%D7",
1121 "%D8", "%D9", "%DA", "%DB", "%DC", "%DD", "%DE", "%DF",
1122 "%E0", "%E1", "%E2", "%E3", "%E4", "%E5", "%E6", "%E7",
1123 "%E8", "%E9", "%EA", "%EB", "%EC", "%ED", "%EE", "%EF",
1124 "%F0", "%F1", "%F2", "%F3", "%F4", "%F5", "%F6", "%F7",
1125 "%F8", "%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF"
1126};
1127
1128/*
1129 *@@ xstrEncode:
1130 * encodes characters in a string.
1131 *
1132 * This searches pxstr for all occurences of the
1133 * characters in pcszEncode (which must be a
1134 * null-terminated list of characters to be
1135 * encoded). Each occurence that is found is
1136 * replaced with "%hh", with "hh" being the
1137 * two-digit hex number of the encoded character.
1138 *
1139 * For example, to encode strings for the XCenter,
1140 * set pcszEncode to "%,();=".
1141 *
1142 * Returns the no. of characters replaced.
1143 *
1144 * NOTE: You must make sure that pcszEncode ALWAYS
1145 * contains the "%" character as well, which must
1146 * always be encoded (i.e. escaped) because it is
1147 * used for encoding the characters. Otherwise
1148 * you won't be able to decode the string again.
1149 *
1150 * Example: To encode all occurences of
1151 * "a", "b", and "c" in a string, do this:
1152 *
1153 + XSTRING str;
1154 + xstrInitCopy(&str, "Sample characters.";
1155 + xstrEncode(&str, "abc%";
1156 *
1157 * would convert str to contain:
1158 *
1159 + S%61mple %63hara%63ters.
1160 *
1161 * Memory cost: None, except for that of xstrcpy.
1162 *
1163 *@@added V0.9.9 (2001-02-28) [umoeller]
1164 *@@changed V0.9.9 (2001-03-06) [lafaix]: rewritten.
1165 */
1166
1167ULONG xstrEncode(PXSTRING pxstr, // in/out: string to convert
1168 const char *pcszEncode) // in: characters to encode (e.g. "%,();=")
1169{
1170 ULONG ulrc = 0,
1171 ul,
1172 ulEncodeLength;
1173
1174 if ( (pxstr)
1175 && (pxstr->ulLength)
1176 && (pcszEncode)
1177 && (ulEncodeLength = strlen(pcszEncode)))
1178 {
1179 PSZ pszDest = (PSZ)malloc(pxstr->ulLength * 3
1180 + 1),
1181 pszDestCurr = pszDest;
1182
1183 if (pszDest)
1184 {
1185 for (ul = 0;
1186 ul < pxstr->ulLength;
1187 ul++)
1188 {
1189 ULONG ulEncode;
1190
1191 for (ulEncode = 0;
1192 ulEncode < ulEncodeLength;
1193 ulEncode++)
1194 {
1195 if (pxstr->psz[ul] == pcszEncode[ulEncode])
1196 {
1197 // use the static encoding table for speed
1198 memcpy(pszDestCurr,
1199 apszEncoding[(unsigned char)pcszEncode[ulEncode]],
1200 3);
1201 pszDestCurr += 3;
1202 ulrc++;
1203 goto iterate;
1204 }
1205 }
1206
1207 *pszDestCurr++ = pxstr->psz[ul];
1208
1209 iterate:
1210 ;
1211 }
1212 }
1213
1214 // something was encoded; update pxstr
1215 if (ulrc)
1216 {
1217 *pszDestCurr = 0;
1218
1219 xstrcpy(pxstr, pszDest, pszDestCurr-pszDest);
1220 }
1221
1222 free(pszDest);
1223 }
1224
1225 return (ulrc);
1226}
1227
1228/*
1229 *@@ xstrDecode:
1230 * decodes a string previously encoded by xstrEncode.
1231 *
1232 * This simply assumes that all '%' characters in
1233 * pxstr introduce encodings and the next two characters
1234 * after '%' always are a hex character code. This
1235 * only recognizes hex in upper case. All this will
1236 * work properly with encodings from xstrEncode.
1237 *
1238 * Returns the no. of encodings replaced.
1239 *
1240 * Memory cost: None.
1241 *
1242 *@@added V0.9.9 (2001-02-28) [umoeller]
1243 *@@changed V0.9.9 (2001-03-06) [lafaix]: removed memory allocation
1244 */
1245
1246ULONG xstrDecode(PXSTRING pxstr) // in/out: string to be decoded
1247{
1248 ULONG ulrc = 0;
1249
1250 if ( (pxstr)
1251 && (pxstr->ulLength)
1252 )
1253 {
1254 const char *pSource = pxstr->psz;
1255 PSZ pszDest = (PSZ)pSource,
1256 pDest = (PSZ)pSource;
1257 CHAR c;
1258
1259 while ((c = *pSource++))
1260 {
1261 // pSource points to next char now
1262
1263 if (c == '%')
1264 {
1265 static char ach[] = "0123456789ABCDEF";
1266
1267 // convert two chars after '%'
1268 CHAR c2, // first char after '%' --> hi-nibble
1269 c3; // second char after '%' --> lo-nibble
1270 const char *p2, // for first char: points into ach or is NULL
1271 *p3; // for second char: points into ach or is NULL
1272 if ( (c2 = *pSource)
1273 && (p2 = strchr(ach, c2))
1274 && (c3 = *(pSource + 1))
1275 && (p3 = strchr(ach, c3))
1276 )
1277 {
1278 // both chars after '%' were valid:
1279 *pDest++ = // lo-nibble:
1280 (p3 - ach) // 0 for '0', 10 for 'A', ...
1281 // hi-nibble:
1282 + ((p2 - ach) << 4);
1283 // go on after that
1284 pSource += 2;
1285 // raise return count
1286 ulrc++;
1287 // next in loop
1288 continue;
1289 }
1290 }
1291
1292 // not encoding, or null after '%', or invalid encoding:
1293 // just copy this
1294 *pDest++ = c;
1295 } // while ((ch = *pSource++))
1296
1297 if (ulrc)
1298 {
1299 *pDest = 0;
1300 pxstr->ulLength = (pDest - pszDest);
1301 }
1302 }
1303
1304 return (ulrc);
1305}
1306
1307/*
1308 *@@ xstrConvertLineFormat:
1309 * converts between line formats.
1310 *
1311 * If (fToCFormat == CRLF2LF), all \r\n pairs are replaced
1312 * with \n chars (UNIX or C format).
1313 *
1314 * Reversely, if (fToCFormat == LF2CRLF), all \n chars
1315 * are converted to \r\n pairs (DOS and OS/2 formats).
1316 * No check is made whether this has already been done.
1317 *
1318 *@@added V0.9.7 (2001-01-15) [umoeller]
1319 */
1320
1321VOID xstrConvertLineFormat(PXSTRING pxstr,
1322 BOOL fToCFormat) // in: if CRLF2LF, to C format; if LF2CRLF, to OS/2 format.
1323{
1324 XSTRING strFind,
1325 strRepl;
1326 size_t ShiftTable[256];
1327 BOOL fRepeat = FALSE;
1328 ULONG ulOfs = 0;
1329
1330 if (fToCFormat)
1331 {
1332 // OS/2 to C:
1333 xstrInitSet(&strFind, "\r\n");
1334 xstrInitSet(&strRepl, "\n");
1335 }
1336 else
1337 {
1338 // C to OS/2:
1339 xstrInitSet(&strFind, "\n");
1340 xstrInitSet(&strRepl, "\r\n");
1341 }
1342
1343 while (xstrFindReplace(pxstr,
1344 &ulOfs,
1345 &strFind,
1346 &strRepl,
1347 ShiftTable,
1348 &fRepeat))
1349 ;
1350}
1351
1352// test case
1353
1354/* int main(void)
1355{
1356 XSTRING str,
1357 strFind,
1358 strReplace;
1359 size_t shift[256];
1360 BOOL fRepeat = FALSE;
1361 ULONG ulOfs = 0;
1362
1363 xstrInit(&str, 0);
1364 xstrInit(&strFind, 0);
1365 xstrInit(&strReplace, 0);
1366
1367 str.ulDelta = 50;
1368
1369 xstrcpy(&str, "Test string 1. Test string 2. Test string 3. !", 0);
1370 xstrcpy(&strFind, "Test", 0);
1371 xstrcpy(&strReplace, "Dummy", 0);
1372
1373 printf("Old string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1374
1375 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
1376
1377 fRepeat = FALSE;
1378 ulOfs = 0;
1379 while (xstrFindReplace(&str,
1380 &ulOfs,
1381 &strFind,
1382 &strReplace,
1383 shift, &fRepeat));
1384 ;
1385
1386 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1387
1388 printf("Appending \"blah\".\n");
1389 xstrcat(&str, "blah", 0);
1390 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1391
1392 xstrcpy(&strFind, strReplace.psz, 0);
1393 xstrClear(&strReplace);
1394
1395 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
1396
1397 fRepeat = FALSE;
1398 ulOfs = 0;
1399 while (xstrFindReplace(&str,
1400 &ulOfs,
1401 &strFind,
1402 &strReplace,
1403 shift, &fRepeat));
1404 ;
1405
1406 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1407
1408 xstrcpy(&strFind, " ", 0);
1409 xstrcpy(&strReplace, ".", 0);
1410
1411 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
1412
1413 fRepeat = FALSE;
1414 ulOfs = 0;
1415 while (xstrFindReplace(&str,
1416 &ulOfs,
1417 &strFind,
1418 &strReplace,
1419 shift, &fRepeat));
1420 ;
1421
1422 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1423
1424 xstrcpy(&strFind, ".", 0);
1425 xstrcpy(&strReplace, "*.........................*", 0);
1426
1427 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
1428
1429 fRepeat = FALSE;
1430 ulOfs = 0;
1431 while (xstrFindReplace(&str,
1432 &ulOfs,
1433 &strFind,
1434 &strReplace,
1435 shift, &fRepeat));
1436 ;
1437
1438 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1439
1440 xstrcpy(&strFind, "..........", 0);
1441 xstrcpy(&strReplace, "@", 0);
1442
1443 printf("Replacing \"%s\" with \"%s\".\n", strFind.psz, strReplace.psz);
1444
1445 fRepeat = FALSE;
1446 ulOfs = 0;
1447 while (xstrFindReplace(&str,
1448 &ulOfs,
1449 &strFind,
1450 &strReplace,
1451 shift, &fRepeat));
1452 ;
1453
1454 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1455
1456 printf("Encoding @* chars.\n");
1457 xstrEncode(&str, "@*");
1458 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1459
1460 printf("Decoding @* chars.\n");
1461 xstrDecode(&str);
1462 printf("New string is: \"%s\" (%d/%d/%d)\n", str.psz, str.ulLength, str.cbAllocated, str.ulDelta);
1463
1464 return (0);
1465} */
1466
1467
Note: See TracBrowser for help on using the repository browser.