home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / infoserv / gopher / Unix / gopher-gateways / techinfo / techinpher / v1.0 / util.c.Z / util.c
Encoding:
C/C++ Source or Header  |  1993-01-26  |  1.9 KB  |  95 lines

  1. #include <string.h>
  2. char *domalloc(unsigned long bytes);
  3. void parsefields(char *str, char **fields, int *numfields, char delim, int max);
  4.  
  5. unsigned long    _allocated = 0;
  6.  
  7. /* parsefields() changes delim and end of line into '\0' ,
  8.    putting the args into array of char pointers */
  9.  
  10. void
  11. parsefields (char *str, char **fields, int *numfields, char delim, int max)
  12. {
  13.   char *cp;
  14.   
  15.   cp = index (str, '\r');
  16.   if (!cp) cp = index (str, '\n');    
  17.   if (cp) *cp = 0;
  18.  
  19.   if (strlen(str) < 1) {
  20.     *numfields = 0;
  21.     return;
  22.   }
  23.   
  24.   fields[0] = str;
  25.   for (*numfields = 1; *numfields < max ; ) {
  26.       fields[*numfields] = index (fields[(*numfields)-1], delim);
  27.       if (fields[*numfields]) {
  28.     *(fields[*numfields]) = 0;      /* wipe out delim */
  29.     fields[*numfields] += 1;        /* move one char past delim */
  30.     (*numfields)++;
  31.       }
  32.       else 
  33.     break;
  34.     }
  35. }
  36.  
  37.  
  38. char *
  39. domalloc(unsigned long    bytes)
  40. {
  41.   char *cp;
  42.   
  43.   if (bytes == 0) 
  44.     bytes = 1;
  45.  
  46.   cp = (char *) malloc(bytes);
  47.   
  48.   _allocated += bytes;
  49.   if (!cp)
  50.     printf("Malloc failed to get %lu bytes.\n", bytes);
  51.   return cp;
  52. }
  53.  
  54.  
  55. /**************************************************************************/
  56. /* Count # of occurences of char c in string str */
  57. int
  58. strcnt(char *str, char c)
  59. {
  60.   register int    cnt = 0;
  61.   
  62.   while (*str)
  63.     if (*str++ == c)
  64.       cnt++;
  65.   return cnt;
  66. }
  67.  
  68. /**************************************************************************/
  69. /* tolower a string, returning the length of the string */
  70. int strlower(char *str)
  71. {
  72.   int    cnt = 0;
  73.   
  74.   while (*str) {
  75.     if (isupper(*str))
  76.       *str = tolower(*str);
  77.     str++, cnt++;
  78.   }
  79.   return cnt;
  80. }
  81.  
  82.  
  83. substi_char (char *istr, char *ostr, char ch_frm, char ch_to)
  84. /* Convert ch_frm into ch_to where ever it occurs in istr, write
  85.    results in ostr */
  86. {
  87.   char *cp;
  88.   
  89.   strcpy (ostr, istr);  /* copy istr to ostr */
  90.   for (cp = ostr; *cp; cp++) {
  91.     if (*cp == ch_frm)
  92.       *cp = ch_to;
  93.   }
  94. }
  95.