home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Resources / System / BoingBag1 / Contributions / Workbench / RexxArpLib3p6 / src / util.c < prev    next >
C/C++ Source or Header  |  1998-06-21  |  4KB  |  247 lines

  1. /**    Util.c
  2.   *
  3.   *    Implement some basic utility functions for rexxarplib.library.
  4.   *
  5.   *   AUTHOR/DATE:  W.G.J. Langeveld, November 1987.
  6.   *   ============
  7.   *
  8.   *    CURRENT VERSION:
  9.   *
  10.   *    This version has been converted to SAS C 6.5 format. It has been modified
  11.   *    for modern definition sequences for ANSI compilation. This no longer works
  12.   *    with OS versions prior to 2.04.
  13.   *
  14.   *    SC 6.5 #define's "index" and "rindex" to be aliased to strchr and strrchr
  15.   *    respectively. This rather destroyed the behavior of the subroutines below.
  16.   *    Hence the conditional undefs for index and rindex.
  17.   *
  18.   *   AUTHOR/DATE:  Joanne Dow, jdow@bix.com, June 1998.
  19.   *   ============
  20.   *
  21.   **/
  22. #include <functions.h>
  23. #include "ralprotos.h"
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27. #include <simpreq.h>
  28. #include <proto/rexxsyslib.h>
  29.  
  30. /**
  31.  *
  32.  *   Case insensitive compare
  33.  *
  34. **/
  35. strcmpu( char *s, char *t )
  36. {
  37.     for ( ; toupper(*s) == toupper(*t); s++, t++)
  38.     if (*s == '\0')
  39.         return(0);
  40.     
  41.     return(toupper(*s) - toupper(*t));
  42. }
  43.  
  44. strncmpu( char *s, char *t, int n)
  45. {
  46.     n--;
  47.     for ( ; (toupper(*s) == toupper(*t)) && n; s++, t++, n--)
  48.     if (*s == '\0')
  49.         return(0);
  50.     
  51.     return(toupper(*s) - toupper(*t));
  52. }
  53.  
  54. /*
  55.  *   index and rindex    == strchr and strrchr via defines in 6.5 strings.h
  56.  *        Since these two provide a NULL return on testing for '\0' we have
  57.  *        to undefine index and rindex to that these compiled versions work.
  58.  */
  59. #ifdef index
  60. #undef index
  61. #endif
  62.  
  63. char *index( const char *s, int c )
  64. {
  65.     if (c == 0)
  66.         return(NULL);
  67.     return(strchr(s, c));
  68. }
  69.  
  70. #ifdef rindex
  71. #undef rindex
  72. #endif
  73.  
  74. char *rindex( const char *s, int c )
  75. {
  76.     if (c == 0)
  77.         return(NULL);
  78.     return(strrchr(s, c));
  79. }
  80.  
  81. /**
  82.  *
  83.  *   Convert a long to an ascii string. sprintf is too much...
  84.  *
  85. **/
  86. #define BASE 10
  87.  
  88. char *ltoa( char *str, long n )
  89. {
  90.     static char nums[] = "0123456789ABCDEF";
  91.     int i, j, s = 0, c;
  92.     
  93.     if (n < 0L) 
  94.     {
  95.         s = 1;
  96.         n = - n;
  97.     }
  98.     j = -1;
  99.     if (n == 0L) 
  100.     {
  101.         j++;
  102.         str[0] = '0';
  103.     }
  104.     while (n) 
  105.     {
  106.         j++;
  107.         i = n % BASE;
  108.         str[j] = nums[i];
  109.         n /= BASE;
  110.     }
  111.     if (s) 
  112.     {
  113.         j++;
  114.         str[j] = '-';
  115.     }
  116.     str[j + 1] = '\0';
  117.     for (i = 0; i < j; i++, j--) 
  118.     {
  119.         c = str[i];
  120.         str[i] = str[j];
  121.         str[j] = c;
  122.     }
  123.     
  124.     return(str);
  125. }
  126.  
  127. /**
  128.  *
  129.  *   Find index of t in s (K&R index function, K&R page 67).
  130.  *   Modifications: returns pointer to position in s, not numeric index.
  131.  *                  This makes it compatible with ANSI strstr(), but this one is
  132.  *                  case insensitive.
  133.  *
  134. **/
  135. char *strstr( const char s[], const char t[] )
  136. {
  137.     int i,j,k;
  138.     
  139.     for (i = 0; s[i] != '\0'; i++) 
  140.     {
  141.         for (j = i, k = 0;
  142.             t[k] != '\0' && tolower(s[j]) == tolower(t[k]); j++, k++)
  143.         ;
  144.         if (t[k] == '\0')
  145.             return(&s[i]);
  146.     }
  147.     return(0L);
  148. }
  149.  
  150.  
  151. /**
  152.  *
  153.  *   Interface to CreateArgstring()
  154.  *   Note, that this can't be a macro since "a" may be an expression and the
  155.  *   order of evaluation wouldn't be guaranteed.
  156.  *
  157. **/
  158. char *CAS( char *a )
  159. {
  160.     char *CreateArgstring();
  161.     
  162.     return((char *) CreateArgstring( a, (long) strlen(a)) );
  163. }
  164.  
  165. /**
  166.  *
  167.  *   Check whether the specified number of arguments are non-null
  168.  *
  169. **/
  170. long checkargs( int n, char *args[] )
  171. {
  172.     int i;
  173.     
  174.     if (args == 0L)
  175.         return(17L);
  176.     
  177.     for (i = 0; i < n; i++) 
  178.     {
  179.         if (args[i] == 0L)
  180.             return(12L);
  181.     }
  182.     
  183.     return(0);
  184. }
  185.  
  186. /**
  187.  *
  188.  *   Function to decode a string into substrings using \ as newline.
  189.  *
  190. **/
  191. DecodeAutoText( char *s, char *t[], char *ps )
  192. {
  193.     int i;
  194.     char *ind;
  195.     
  196.     if (s == 0L)
  197.         return;
  198.     if (t == 0L)
  199.         return;
  200.     if (ps == 0L)
  201.         return;
  202.     
  203.     strcpy(ps, s);
  204.     ind = ps;
  205.     for (i = 0; i < SR_MAXPROMPTS; i++) 
  206.     {
  207.         t[i] = ind;
  208.         ind = index(ind, '\\');
  209.         if (ind == 0L)
  210.             break;
  211.         else
  212.             *ind = '\0';
  213.         ind++;
  214.     }
  215.     
  216.     return;
  217. }
  218.  
  219. /**
  220.  *
  221.  *   Make a stem variable out of parts
  222.  *
  223. **/
  224. MakeStem(char *s, char *name, int n)
  225. {
  226.     char numbuff[10];
  227.     
  228.     if (s && name) 
  229.     {
  230.         ltoa(numbuff, (long) n);
  231.         strcpy(s, name);
  232.         strcat(s, ".");
  233.         strcat(s, numbuff);
  234.     }
  235. }
  236.  
  237. MakeStemS( char *s, char *name, char *field )
  238. {
  239.     if (s && name && field) 
  240.     {
  241.         strcpy(s, name);
  242.         strcat(s, ".");
  243.         strcat(s, field);
  244.     }
  245. }
  246.  
  247.