default to gb_Deliver_CLEARONDELIVER == as multiuserenv are rare now
[LibreOffice.git] / dmake / dmstring.c
blob53068a9deba03097415bc353588483855e3eb9ad
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)
13 -- AUTHOR
14 -- Dennis Vadura, dvadura@dmake.wticorp.com
16 -- WWW
17 -- http://dmake.wticorp.com/
19 -- COPYRIGHT
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.
26 -- LOG
27 -- Use cvs log to obtain detailed change logs.
30 #include "extern.h"
32 PUBLIC char *
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 */
42 char *src;
43 char *data;
44 int n;
45 int fr;
47 char *t;
48 int l;
49 int flag = FALSE;
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();
62 strcpy( t, src );
63 if (n) strncat( t, data, n );
64 t[ l-1 ] = '\0';
66 if( !flag && fr ) FREE( src );
68 DB_PRINT( "str", ("Result [%s]", t) );
69 DB_RETURN( t );
75 PUBLIC char *
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 */
81 char *src;
82 char *data;
83 int fr;
85 char *t;
86 int l;
87 int sflag;
88 int dflag;
90 DB_ENTER( "DmStrAdd" );
92 sflag = dflag = fr;
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;
99 if( *src ) l++;
101 if( (t = MALLOC( l, char )) == NIL(char) ) No_ram();
103 strcpy( t, src );
105 if( *data )
107 if( *src ) strcat( t, " " );
108 strcat( t, data );
111 if( sflag ) FREE( src );
112 if( dflag ) FREE( data );
114 DB_PRINT( "str", ("Result [%s]", t) );
115 DB_RETURN( t );
120 PUBLIC char *
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. */
126 char *src1;
127 char *src2;
129 src2 = DmStrAdd( src1, src2, FALSE );
130 if( src1 != NIL(char) ) FREE( src1 );
131 return( src2 );
135 PUBLIC char *
136 DmStrDup( str )/*
137 ================= Duplicate the contents of a string, by using malloc */
138 char *str;
140 char *t;
142 if( str == NIL(char) ) return( NIL(char) );
144 if( (t = MALLOC( strlen( str )+1, char )) == NIL(char) ) No_ram();
145 strcpy( t, str );
147 return( t );
152 PUBLIC char *
153 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;
160 char *t;
161 size_t size;
162 size_t alloced;
163 char *tmp;
164 char *dest;
165 int seen_equal = 0;
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++)
172 if(*tmp == '"')
173 alloced++;
175 if((t = MALLOC(alloced, char)) == NIL(char)) No_ram();
177 for(tmp = str, dest = t; *tmp; tmp++, dest++) {
178 if(*tmp == '=' && !seen_equal) {
179 seen_equal = 1;
180 *dest++ = *tmp;
181 *dest = '"';
182 continue;
184 if(*tmp == '"')
185 *dest++ = '\\';
186 *dest = *tmp;
189 if(!seen_equal)
190 Fatal("DmStrDup2 invoked without argument of form x=y\n");
192 *dest++ = '"';
193 *dest = 0;
195 return t;
200 PUBLIC char *
201 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. */
207 char *s1;
208 char *s2;
210 register char *t;
212 if( s1 == NIL(char) || s2 == NIL(char) ) return( "" );
214 for( t=s1; *t && (strchr( s2, *t ) == NIL(char)); t++ );
215 return( t );
221 PUBLIC char *
222 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. "" ) */
228 char *s1;
229 char *s2;
231 register char *t;
233 if( s1 == NIL(char) || s2 == NIL(char) ) return( "" );
235 for( t=s1; *t && (strchr( s2, *t ) != NIL(char)); t++ );
236 return( t );
242 PUBLIC char *
243 DmStrStr( s1, s2 )/*
244 ==================== find first occurrence in s1 of s2 */
245 char *s1;
246 char *s2;
248 register char *s;
249 register char *p;
250 register char *r;
252 if( s1 != NIL(char) && s2 != NIL(char) )
253 for( s=s1; *s; s++ )
254 if( *s == *s2 )
256 for( r=s+1, p = s2+1; *p && (*r == *p); r++, p++ );
257 if( !*p ) return( s );
260 return( NIL(char) );
265 PUBLIC char *
266 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. */
272 char *s;
273 char *e;
275 char save;
276 int len = e-s;
278 if( len < 0 || len > strlen(s) )
279 Fatal( "Internal Error: SubStr fails consistency test" );
281 save = *e;
282 *e = '\0';
283 s = DmStrDup( s );
284 *e = save;
286 return( s );
290 /* Provide "missing" string function. */
291 #ifndef HAVE_STRLWR
292 char *
293 strlwr(char *s)
295 char *p;
296 for(p=s; *p; p++ )
297 *p = tolower(*p);
298 return s;
300 #endif