Line data Source code
1 : /* RCS $Id: dmstring.c,v 1.2 2007-01-18 09:29:40 vg Exp $
2 : --
3 : -- SYNOPSIS
4 : -- String handling code
5 : --
6 : -- DESCRIPTION
7 : -- Routines to handle string manipulation. This code is not specific
8 : -- to dmake and has/and will be used in other programs. The string
9 : -- "" is considered the NULL string, if NIL(char) is received instead
10 : -- undefined results may occurr. (In reality NIL(char) is checked for
11 : -- but in general it is not safe to assume NIL(char) == NULL)
12 : --
13 : -- AUTHOR
14 : -- Dennis Vadura, dvadura@dmake.wticorp.com
15 : --
16 : -- WWW
17 : -- http://dmake.wticorp.com/
18 : --
19 : -- COPYRIGHT
20 : -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
21 : --
22 : -- This program is NOT free software; you can redistribute it and/or
23 : -- modify it under the terms of the Software License Agreement Provided
24 : -- in the file <distribution-root>/readme/license.txt.
25 : --
26 : -- LOG
27 : -- Use cvs log to obtain detailed change logs.
28 : */
29 :
30 : #include "extern.h"
31 :
32 : PUBLIC char *
33 5749381 : DmStrJoin( src, data, n, fr )/*
34 : ===============================
35 : Join data to src according to value of n.
36 :
37 : n = -1 - return strcat( src, data )
38 : n >= 0 - return strncat( src, data, n )
39 :
40 : FREE original src if fr == TRUE, else leave it alone */
41 :
42 : char *src;
43 : char *data;
44 : int n;
45 : int fr;
46 : {
47 : char *t;
48 : int l;
49 5749381 : int flag = FALSE;
50 :
51 : DB_ENTER( "DmStrJoin" );
52 :
53 5749381 : if( src == NIL(char) ) { src = ""; flag = TRUE; }
54 5749381 : if( data == NIL(char) ) data = "";
55 : DB_PRINT( "str", ("Joining [%s] [%s] %d", src, data, n) );
56 :
57 5749381 : if( n == -1 ) n = strlen( data );
58 :
59 5749381 : l = strlen( src ) + n + 1;
60 5749381 : if( (t = MALLOC( l, char )) == NIL(char) ) No_ram();
61 :
62 5749381 : strcpy( t, src );
63 5749381 : if (n) strncat( t, data, n );
64 5749381 : t[ l-1 ] = '\0';
65 :
66 5749381 : if( !flag && fr ) FREE( src );
67 :
68 : DB_PRINT( "str", ("Result [%s]", t) );
69 5749381 : DB_RETURN( t );
70 : }
71 :
72 :
73 :
74 :
75 : PUBLIC char *
76 104658 : DmStrAdd( src, data, fr )/*
77 : ===========================
78 : append data to src with space in between if src is not NIL(char) or ""
79 : and free both src and data if fr == TRUE, otherwise leave them be */
80 :
81 : char *src;
82 : char *data;
83 : int fr;
84 : {
85 : char *t;
86 : int l;
87 : int sflag;
88 : int dflag;
89 :
90 : DB_ENTER( "DmStrAdd" );
91 :
92 104658 : sflag = dflag = fr;
93 :
94 104658 : if( src == NIL(char) ) { src = ""; sflag = FALSE; }
95 104658 : if( data == NIL(char) ) { data = ""; dflag = FALSE; }
96 : DB_PRINT( "str", ("Adding [%s] [%s] %d", src, data, fr) );
97 :
98 104658 : l = strlen(src) + strlen(data) + 1;
99 104658 : if( *src ) l++;
100 :
101 104658 : if( (t = MALLOC( l, char )) == NIL(char) ) No_ram();
102 :
103 104658 : strcpy( t, src );
104 :
105 104658 : if( *data )
106 : {
107 104656 : if( *src ) strcat( t, " " );
108 104656 : strcat( t, data );
109 : }
110 :
111 104658 : if( sflag ) FREE( src );
112 104658 : if( dflag ) FREE( data );
113 :
114 : DB_PRINT( "str", ("Result [%s]", t) );
115 104658 : DB_RETURN( t );
116 : }
117 :
118 :
119 :
120 : PUBLIC char *
121 45124 : DmStrApp( src1, src2 )/*
122 : ========================
123 : Append two strings together, and return the result with a space between
124 : the two strings. FREE the first string if it is not NIL and always
125 : leave the second string be. */
126 : char *src1;
127 : char *src2;
128 : {
129 45124 : src2 = DmStrAdd( src1, src2, FALSE );
130 45124 : if( src1 != NIL(char) ) FREE( src1 );
131 45124 : return( src2 );
132 : }
133 :
134 :
135 : PUBLIC char *
136 6330758 : DmStrDup( str )/*
137 : ================= Duplicate the contents of a string, by using malloc */
138 : char *str;
139 : {
140 : char *t;
141 :
142 6330758 : if( str == NIL(char) ) return( NIL(char) );
143 :
144 6291405 : if( (t = MALLOC( strlen( str )+1, char )) == NIL(char) ) No_ram();
145 6291405 : strcpy( t, str );
146 :
147 6291405 : return( t );
148 : }
149 :
150 :
151 :
152 : PUBLIC char *
153 0 : DmStrDup2( str )/*
154 : ==================
155 : This function is used solely to properly quote command line arguments when
156 : they are reinserted int MAKEMACROS so that they can be used further in
157 : a processing line. */
158 : char *str;
159 : {
160 : char *t;
161 : size_t size;
162 : size_t alloced;
163 : char *tmp;
164 : char *dest;
165 0 : int seen_equal = 0;
166 :
167 0 : if(str == NIL(char)) return(NIL(char));
168 0 : size = strlen(str) + 1;
169 0 : alloced = size + 2; /* for two quotes */
170 :
171 0 : for(tmp = str; *tmp; tmp++)
172 0 : if(*tmp == '"')
173 0 : alloced++;
174 :
175 0 : if((t = MALLOC(alloced, char)) == NIL(char)) No_ram();
176 :
177 0 : for(tmp = str, dest = t; *tmp; tmp++, dest++) {
178 0 : if(*tmp == '=' && !seen_equal) {
179 0 : seen_equal = 1;
180 0 : *dest++ = *tmp;
181 0 : *dest = '"';
182 0 : continue;
183 : }
184 0 : if(*tmp == '"')
185 0 : *dest++ = '\\';
186 0 : *dest = *tmp;
187 : }
188 :
189 0 : if(!seen_equal)
190 0 : Fatal("DmStrDup2 invoked without argument of form x=y\n");
191 :
192 0 : *dest++ = '"';
193 0 : *dest = 0;
194 :
195 0 : return t;
196 : }
197 :
198 :
199 :
200 : PUBLIC char *
201 3912627 : DmStrPbrk( s1, s2 )/*
202 : ====================
203 : find first occurrence of char in s2 in string s1.
204 : Returns a pointer to the first occurrence. NOTE '\0' is considered part
205 : of s2 and a pointer to it is returned if no other chars match. */
206 :
207 : char *s1;
208 : char *s2;
209 : {
210 : register char *t;
211 :
212 3912627 : if( s1 == NIL(char) || s2 == NIL(char) ) return( "" );
213 :
214 3912627 : for( t=s1; *t && (strchr( s2, *t ) == NIL(char)); t++ );
215 3912627 : return( t );
216 : }
217 :
218 :
219 :
220 :
221 : PUBLIC char *
222 3840391 : DmStrSpn( s1, s2 )/*
223 : ====================
224 : return pointer to first char in s1 that does not belong to s2.
225 : Returns the pointer if match found, else returns pointer to null char
226 : in s1. (ie. "" ) */
227 :
228 : char *s1;
229 : char *s2;
230 : {
231 : register char *t;
232 :
233 3840391 : if( s1 == NIL(char) || s2 == NIL(char) ) return( "" );
234 :
235 3840391 : for( t=s1; *t && (strchr( s2, *t ) != NIL(char)); t++ );
236 3840391 : return( t );
237 : }
238 :
239 :
240 :
241 :
242 : PUBLIC char *
243 1806104 : DmStrStr( s1, s2 )/*
244 : ==================== find first occurrence in s1 of s2 */
245 : char *s1;
246 : char *s2;
247 : {
248 : register char *s;
249 : register char *p;
250 : register char *r;
251 :
252 1806104 : if( s1 != NIL(char) && s2 != NIL(char) )
253 668323340 : for( s=s1; *s; s++ )
254 666720938 : if( *s == *s2 )
255 : {
256 289998 : for( r=s+1, p = s2+1; *p && (*r == *p); r++, p++ );
257 289998 : if( !*p ) return( s );
258 : }
259 :
260 1602402 : return( NIL(char) );
261 : }
262 :
263 :
264 :
265 : PUBLIC char *
266 2035648 : DmSubStr( s, e )/*
267 : ==================
268 : Return the string between the two pointers s and e, not including the
269 : char that e points to. NOTE: This routine assumes that s and e point
270 : into the same string. */
271 :
272 : char *s;
273 : char *e;
274 : {
275 : char save;
276 2035648 : int len = e-s;
277 :
278 2035648 : if( len < 0 || len > strlen(s) )
279 0 : Fatal( "Internal Error: SubStr fails consistency test" );
280 :
281 2035648 : save = *e;
282 2035648 : *e = '\0';
283 2035648 : s = DmStrDup( s );
284 2035648 : *e = save;
285 :
286 2035648 : return( s );
287 : }
288 :
289 :
290 : /* Provide "missing" string function. */
291 : #ifndef HAVE_STRLWR
292 : char *
293 0 : strlwr(char *s)
294 : {
295 : char *p;
296 0 : for(p=s; *p; p++ )
297 0 : *p = tolower(*p);
298 0 : return s;
299 : }
300 : #endif
|