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