1 |
|
---|
2 | /***********************************************************************
|
---|
3 |
|
---|
4 | $Id: strips.c 911 2008-01-07 01:01:29Z gyoung $
|
---|
5 |
|
---|
6 | String strippers
|
---|
7 |
|
---|
8 | Copyright (c) 1993-98 M. Kimes
|
---|
9 | Copyright (c) 2004 Steven H.Levine
|
---|
10 |
|
---|
11 | 01 Aug 04 SHL Rework lstrip/rstrip usage
|
---|
12 | 26 Jul 06 SHL Add chop_at_crnl
|
---|
13 | 20 Aug 07 GKY Move #pragma alloc_text to end for OpenWatcom compat
|
---|
14 | 29 Dec 07 GKY Add remove_first_occurence_of_character
|
---|
15 | 29 Dec 07 GKY Add remove_last_occurence_of_character
|
---|
16 |
|
---|
17 | ***********************************************************************/
|
---|
18 |
|
---|
19 | #include <string.h>
|
---|
20 |
|
---|
21 | #include <os2.h>
|
---|
22 |
|
---|
23 | VOID chop_at_crnl(PSZ pszSrc)
|
---|
24 | {
|
---|
25 | // Chop line at CR or NL
|
---|
26 | PSZ psz = strchr(pszSrc, '\r');
|
---|
27 |
|
---|
28 | if (psz)
|
---|
29 | *psz = 0;
|
---|
30 | psz = strchr(pszSrc, '\n');
|
---|
31 | if (psz)
|
---|
32 | *psz = 0;
|
---|
33 | }
|
---|
34 |
|
---|
35 | PSZ convert_nl_to_nul(PSZ pszSrc)
|
---|
36 | {
|
---|
37 | // Convert newline to nul, return pointer to next or NULL
|
---|
38 | PSZ psz = strchr(pszSrc, '\n');
|
---|
39 |
|
---|
40 | if (psz) {
|
---|
41 | *psz = 0;
|
---|
42 | psz++;
|
---|
43 | }
|
---|
44 | return psz;
|
---|
45 | }
|
---|
46 |
|
---|
47 | void strip_trail_char(char *pszStripChars, char *pszSrc)
|
---|
48 | {
|
---|
49 | char *psz;
|
---|
50 |
|
---|
51 | if (pszSrc && *pszSrc && pszStripChars && *pszStripChars) {
|
---|
52 | psz = pszSrc + strlen(pszSrc) - 1;
|
---|
53 | // while not empty and tail char in strip list
|
---|
54 | while (*pszSrc && strchr(pszStripChars, *psz) != NULL) {
|
---|
55 | *psz = 0;
|
---|
56 | psz--;
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | void strip_lead_char(char *pszStripChars, char *pszSrc)
|
---|
62 | {
|
---|
63 | char *psz = pszSrc;
|
---|
64 |
|
---|
65 | if (pszSrc && *pszSrc && pszStripChars && *pszStripChars) {
|
---|
66 | // while lead char in strip list
|
---|
67 | while (*psz && strchr(pszStripChars, *psz) != NULL)
|
---|
68 | psz++;
|
---|
69 | if (psz != pszSrc)
|
---|
70 | memmove(pszSrc, psz, strlen(psz) + 1);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | VOID remove_first_occurence_of_character(char *pszRemoveChar, char *pszSrc)
|
---|
75 | {
|
---|
76 | PSZ pszStrLocation;
|
---|
77 |
|
---|
78 | pszStrLocation = strchr(pszSrc, *pszRemoveChar);
|
---|
79 | if (pszStrLocation)
|
---|
80 | memmove(pszStrLocation, pszStrLocation + 1, strlen(pszStrLocation) + 1);
|
---|
81 | }
|
---|
82 |
|
---|
83 | VOID remove_last_occurence_of_character(char *pszRemoveChar, char *pszSrc)
|
---|
84 | {
|
---|
85 | PSZ pszStrLocation;
|
---|
86 |
|
---|
87 | pszStrLocation = strrchr(pszSrc, *pszRemoveChar);
|
---|
88 | if (pszStrLocation)
|
---|
89 | memmove(pszStrLocation, pszStrLocation + 1, strlen(pszStrLocation) + 1);
|
---|
90 | }
|
---|
91 |
|
---|
92 | #pragma alloc_text(MISC8,chop_at_crnl,convert_nl_to_nul,strip_trail_char,strip_lead_char)
|
---|
93 | #pragma alloc_text(MISC8,remove_first_occurence_of_character,remove_last_occurence_of_character)
|
---|