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

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

misc updates

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