1 /* RCS $Id: dmstring.c,v 1.2 2007-01-18 09:29:40 vg Exp $
4 -- String handling code
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)
14 -- Dennis Vadura, dvadura@dmake.wticorp.com
17 -- http://dmake.wticorp.com/
20 -- Copyright (c) 1996,1997 by WTI Corp. All rights reserved.
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.
27 -- Use cvs log to obtain detailed change logs.
33 DmStrJoin( src
, data
, n
, fr
)/*
34 ===============================
35 Join data to src according to value of n.
37 n = -1 - return strcat( src, data )
38 n >= 0 - return strncat( src, data, n )
40 FREE original src if fr == TRUE, else leave it alone */
51 DB_ENTER( "DmStrJoin" );
53 if( src
== NIL(char) ) { src
= ""; flag
= TRUE
; }
54 if( data
== NIL(char) ) data
= "";
55 DB_PRINT( "str", ("Joining [%s] [%s] %d", src
, data
, n
) );
57 if( n
== -1 ) n
= strlen( data
);
59 l
= strlen( src
) + n
+ 1;
60 if( (t
= MALLOC( l
, char )) == NIL(char) ) No_ram();
63 if (n
) strncat( t
, data
, n
);
66 if( !flag
&& fr
) FREE( src
);
68 DB_PRINT( "str", ("Result [%s]", t
) );
76 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 */
90 DB_ENTER( "DmStrAdd" );
94 if( src
== NIL(char) ) { src
= ""; sflag
= FALSE
; }
95 if( data
== NIL(char) ) { data
= ""; dflag
= FALSE
; }
96 DB_PRINT( "str", ("Adding [%s] [%s] %d", src
, data
, fr
) );
98 l
= strlen(src
) + strlen(data
) + 1;
101 if( (t
= MALLOC( l
, char )) == NIL(char) ) No_ram();
107 if( *src
) strcat( t
, " " );
111 if( sflag
) FREE( src
);
112 if( dflag
) FREE( data
);
114 DB_PRINT( "str", ("Result [%s]", t
) );
121 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. */
129 src2
= DmStrAdd( src1
, src2
, FALSE
);
130 if( src1
!= NIL(char) ) FREE( src1
);
137 ================= Duplicate the contents of a string, by using malloc */
142 if( str
== NIL(char) ) return( NIL(char) );
144 if( (t
= MALLOC( strlen( str
)+1, char )) == NIL(char) ) No_ram();
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. */
167 if(str
== NIL(char)) return(NIL(char));
168 size
= strlen(str
) + 1;
169 alloced
= size
+ 2; /* for two quotes */
171 for(tmp
= str
; *tmp
; tmp
++)
175 if((t
= MALLOC(alloced
, char)) == NIL(char)) No_ram();
177 for(tmp
= str
, dest
= t
; *tmp
; tmp
++, dest
++) {
178 if(*tmp
== '=' && !seen_equal
) {
190 Fatal("DmStrDup2 invoked without argument of form x=y\n");
201 DmStrPbrk( s1
, s2
)/*
203 find first occurence 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. */
212 if( s1
== NIL(char) || s2
== NIL(char) ) return( "" );
214 for( t
=s1
; *t
&& (strchr( s2
, *t
) == NIL(char)); t
++ );
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
233 if( s1
== NIL(char) || s2
== NIL(char) ) return( "" );
235 for( t
=s1
; *t
&& (strchr( s2
, *t
) != NIL(char)); t
++ );
244 ==================== find first occurrence in s1 of s2 */
252 if( s1
!= NIL(char) && s2
!= NIL(char) )
256 for( r
=s
+1, p
= s2
+1; *p
&& (*r
== *p
); r
++, p
++ );
257 if( !*p
) return( s
);
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. */
278 if( len
< 0 || len
> strlen(s
) )
279 Fatal( "Internal Error: SubStr fails consistency test" );
290 /* Provide "missing" string function. */