home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dmake40.zip / dmstring.c < prev    next >
C/C++ Source or Header  |  1994-10-23  |  7KB  |  297 lines

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