home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / pcomm-2.0.2 / part02 / strings.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-13  |  4.1 KB  |  239 lines

  1. /*
  2.  * Miscellaneous string routines.
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include "config.h"
  7.  
  8. /*
  9.  * Do a fancy string copy.  If NULL, return null.  If pointer to NULL, then
  10.  * return the special "null_ptr" variable.  If a normal copy, allocate
  11.  * memory first.
  12.  */
  13.  
  14. char *
  15. str_dup(str)
  16. char *str;
  17. {
  18.     extern char *null_ptr;
  19.     char *ret, *malloc(), *strcpy();
  20.     void error_win();
  21.  
  22.     if (str == NULL)
  23.         return(NULL);
  24.                     /* if pointer to null */
  25.     if (*str == '\0')
  26.         return(null_ptr);
  27.  
  28.     if (!(ret = malloc((unsigned int) strlen(str)+1)))
  29.         error_win(1, "Out of memory", "");
  30.  
  31.     strcpy(ret, str);
  32.     return(ret);
  33. }
  34.  
  35. /*
  36.  * Perform the free(2) function, but check for NULL and the special
  37.  * "null_ptr" variable first.
  38.  */
  39.  
  40. void
  41. free_ptr(str)
  42. char *str;
  43. {
  44.     extern char *null_ptr;
  45.     void free();
  46.  
  47.     if (str != NULL && str != null_ptr)
  48.         free(str);
  49.     return;
  50. }
  51.  
  52. /*
  53.  * Replace a string.  Follows the same convention as str_dup(), except
  54.  * that realloc() is used instead of malloc().  Returns a pointer to
  55.  * the new string (which may have moved).
  56.  */
  57.  
  58. char *
  59. str_rep(s1, s2)
  60. char *s1, *s2;
  61. {
  62.     extern char *null_ptr;
  63.     void free_ptr(), error_win();
  64.     char *s, *malloc(), *realloc(), *strcpy();
  65.  
  66.                     /* copy null pointer ? */
  67.     if (s2 == NULL) {
  68.         free_ptr(s1);
  69.         return(NULL);
  70.     }
  71.     if (s2 == '\0') {
  72.         free_ptr(s1);
  73.         return(null_ptr);
  74.     }
  75.                     /* use realloc()? */
  76.     if (s1 == NULL || s1 == null_ptr) {
  77.         if (!(s = malloc((unsigned int) strlen(s2)+1)))
  78.             error_win(1, "Out of memory", "");
  79.     }
  80.     else {
  81.         if (!(s = realloc(s1, (unsigned int) strlen(s2)+1)))
  82.             error_win(1, "Out of memory", "");
  83.     }
  84.  
  85.     strcpy(s, s2);
  86.     return(s);
  87. }
  88.  
  89. /*
  90.  * This routine is similar to strtok(3).  But this version handles missing
  91.  * tokens by returning a pointer to null.  Also it takes a single separator
  92.  * character as an argument.  Returns a NULL on end of string or error.
  93.  */
  94.  
  95. char *
  96. str_tok(str, c)
  97. char *str, c;
  98. {
  99.     char *strchr();
  100.     static char *ptr, *sep;
  101.                     /* start at beginning */
  102.     if (str != NULL)
  103.         ptr = str;
  104.     else
  105.         ptr = sep;
  106.                     /* at the end? */
  107.     if (*ptr == '\0')
  108.         return(NULL);
  109.                     /* no separator? */
  110.     if (!(sep = strchr(ptr, c)))
  111.         return(NULL);
  112.                     /* zap the sep, move past it */
  113.     *sep = '\0';
  114.     sep++;
  115.  
  116.     return(ptr);
  117. }
  118.  
  119. #ifndef HAVE_STRSTR
  120. /*
  121.  * Return a pointer to the first occurrence of string str2 in str1.
  122.  * Returns a NULL if str2 is not in str1.
  123.  */
  124.  
  125. char *
  126. strstr(str1, str2)
  127. char *str1, *str2;
  128. {
  129.     int len;
  130.  
  131.     len = strlen(str2);
  132.     while (*str1) {
  133.         if (*str2 == *str1) {
  134.             if (!strncmp(str2, str1, len))
  135.                 return(str1);
  136.         }
  137.         str1++;
  138.     }
  139.     return(NULL);
  140. }
  141. #endif /* HAVE_STRSTR */
  142.  
  143. #ifdef BSD
  144. /*
  145.  * Returns the length of the initial segment of string which consists
  146.  * entirely of characters from charset.
  147.  */
  148.  
  149. int
  150. strspn(string, charset)
  151. char    *string;
  152. register char    *charset;
  153. {
  154.     register char *p, *q;
  155.  
  156.     for(q=string; *q != '\0'; ++q) {
  157.         for(p=charset; *p != '\0' && *p != *q; ++p)
  158.             ;
  159.         if(*p == '\0')
  160.             break;
  161.     }
  162.     return(q-string);
  163. }
  164.  
  165. /*
  166.  * Strtok considers string to consist of a sequence of zero or more
  167.  * text tokens separated by spans of one or more characters from sepset.
  168.  */
  169.  
  170. char *
  171. strtok(string, sepset)
  172. char    *string, *sepset;
  173. {
  174.     register char    *p, *q, *r;
  175.     static char    *savept;
  176.     char *strpbrk();
  177.  
  178.     /*first or subsequent call*/
  179.     p = (string == NULL)? savept: string;
  180.  
  181.     if(p == 0)        /* return if no tokens remaining */
  182.         return(NULL);
  183.  
  184.     q = p + strspn(p, sepset);    /* skip leading separators */
  185.  
  186.     if(*q == '\0')        /* return if no tokens remaining */
  187.         return(NULL);
  188.  
  189.     if((r = strpbrk(q, sepset)) == NULL)    /* move past token */
  190.         savept = 0;    /* indicate this is last token */
  191.     else {
  192.         *r = '\0';
  193.         savept = ++r;
  194.     }
  195.     return(q);
  196. }
  197.  
  198. /*
  199.  * Return ptr to first occurrence of any character from `brkset'
  200.  * in the character string `string'; NULL if none exists.
  201.  */
  202.  
  203. char *
  204. strpbrk(string, brkset)
  205. register char *string, *brkset;
  206. {
  207.     register char *p;
  208.  
  209.     if(!string || !brkset)
  210.         return(0);
  211.     do {
  212.         for(p=brkset; *p != '\0' && *p != *string; ++p)
  213.             ;
  214.         if(*p != '\0')
  215.             return(string);
  216.     }
  217.     while(*string++);
  218.     return(0);
  219. }
  220.  
  221. /*
  222.  * Copies the character c, n times to string s
  223.  */
  224.  
  225. char *
  226. memset(s, c, n)
  227. char *s, c;
  228. int n;
  229. {
  230.     char *s1 = s;
  231.  
  232.     while (n > 0) {
  233.         --n;
  234.         *s++ = c;
  235.     }
  236.     return(s1);
  237. }
  238. #endif /* BSD */
  239.