home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume3 / pcmail / part06 / str.c < prev   
Encoding:
C/C++ Source or Header  |  1989-02-03  |  3.9 KB  |  158 lines

  1. /*++
  2. /* NAME
  3. /*    strcons,istrcmp,strvec,vecstr 3
  4. /* SUMMARY
  5. /*    string utility routines
  6. /* PROJECT
  7. /*    pc-mail
  8. /* PACKAGE
  9. /*    general stuff
  10. /* SYNOPSIS
  11. /*    char *strcons(format,args)
  12. /*    char *format;
  13. /*
  14. /*    int istrcmp(s1,s2)
  15. /*    char *s1,s2;
  16. /*
  17. /*    char **strvec(string,separ)
  18. /*    char *string;
  19. /*    char *separ;
  20. /*
  21. /*    char *vecstr(vector,separ)
  22. /*    char **vector;
  23. /*    char *separ;
  24. /* DESCRIPTION
  25. /*    strcons() produces a formatted string, using printf()-like
  26. /*    arguments. Basically it is an sprintf() that returns a 
  27. /*    pointer to the result.
  28. /*
  29. /*    istrcmp() is a case-insensitive versions of the strcmp() functions.
  30. /*
  31. /*    strvec() breaks a null-terminated string using the separators given 
  32. /*    in separ, and returns a null-terminated vector of pointers to the 
  33. /*    resulting substrings. Memory for the vector and substrings are 
  34. /*    allocated in dynamic memory. The original string is not modified.
  35. /*
  36. /*    vecstr() takes a null-terminated vector of string pointers
  37. /*    and builds a string from the strings pointed to by the vector
  38. /*    argument, separated by the string in the separ argument.
  39. /*    Memory for the result is allocated in dynamic memory.
  40. /* FUNCTIONS AND MACROS
  41. /*    strtok(), malloc(), memcpy(), sprintf()
  42. /* DIAGNOSTICS
  43. /*    strvec(), vecstr() return a null pointer if there was not enough memory
  44. /*    avaliable to hold the result.
  45. /* BUGS
  46. /*    strcons() does not do smart garbage collection; it just uses
  47. /*    a circular buffer. The present implementation is not portable 
  48. /*    to machines that pass arguments via registers.
  49. /*
  50. /*    strvec() cannot handle strings with more than BUFSIZ words.
  51. /*    strvec() uses strtok(), which may have side effects.
  52. /* AUTHOR(S)
  53. /*    W.Z. Venema
  54. /*    Eindhoven University of Technology
  55. /*    Department of Mathematics and Computer Science
  56. /*    Den Dolech 2, P.O. Box 513, 5600 MB Eindhoven, The Netherlands
  57. /* CREATION DATE
  58. /*    Tue Apr  5 20:59:29 MET 1988
  59. /* LAST MODIFICATION
  60. /*    Wed Apr  6 00:23:08 MET 1988
  61. /* VERSION/RELEASE
  62. /*    1.1
  63. /*--*/
  64.  
  65. #include <ctype.h>
  66.  
  67. #include "defs.h"
  68.  
  69. #define    NBUF    4
  70.  
  71. /* strcons - quick-and-dirty string constructor */
  72.  
  73. /* VARARGS1 */
  74.  
  75. char *strcons(fmt,a1,a2,a3,a4)
  76. char *fmt;
  77. long a1,a2,a3,a4;
  78. {
  79.     static char strbuf[NBUF][BUFSIZ];
  80.     static int where = 0;
  81.     register char *cp;
  82.  
  83.     sprintf(cp = strbuf[where = (where+1)%NBUF],fmt,a1,a2,a3,a4);
  84.     return(cp);
  85. }
  86.  
  87. /* istrcmp - case-insensitive string comparison */
  88.  
  89. #define    LOW(c)    (isascii(c)&&isupper(c)?tolower(c):(c))
  90.  
  91. int istrcmp(s1,s2)
  92. register char *s1,*s2;
  93. {
  94.     while (*s1 && (LOW(*s1) == LOW(*s2)))
  95.     s1++,s2++;
  96.     return(LOW(*s1)-LOW(*s2));
  97. }
  98.  
  99. /* strvec - make vector of substring pointers */
  100.  
  101. char **strvec(str,sep)
  102. char *str;
  103. char *sep;
  104. {
  105.     char *tmp[BUFSIZ];            /* scratch substring pointer storage */
  106.     register char **cpp = tmp;
  107.     char *sp;                /* ptr to private copy of original */
  108.     register int bytec;
  109.  
  110.     /* make a copy of the original string */
  111.  
  112.     if ((sp = malloc(strlen(str)+1)) == 0)
  113.     return(0);
  114.     strcpy(sp,str);
  115.  
  116.     /* chop our copy at sequences of one or more separators */
  117.  
  118.     for (*cpp = strtok(sp,sep); *cpp; *++cpp = strtok((char *)0,sep))
  119.     /* void */ ;
  120.  
  121.     /* now construct the vector of pointers to the substrings */
  122.  
  123.     if ((cpp = (char **) malloc(bytec = (cpp-tmp+1)*sizeof(*cpp))) == 0)
  124.     return(0);
  125.     return((char **)memcpy((char *)cpp,(char *)tmp,bytec));
  126. }
  127.  
  128. /* vecstr - null-terminated vector of string pointers to one flat string */
  129.  
  130. public char *vecstr(vec,sep)
  131. char **vec;
  132. char *sep;
  133. {
  134.     register char **cpp;
  135.     register int len = 0;        /* length of final string */
  136.     register char *cp;
  137.     register int flen = strlen(sep);    /* filler between substrings */
  138.  
  139.     /* find out how big the resulting string will be */
  140.  
  141.     for (cpp = vec; *cpp; cpp++)
  142.     len += strlen(*cpp)+flen;
  143.  
  144.     /* allocate and initialize the result string */
  145.  
  146.     if ((cp = malloc(len+1)) == 0)
  147.     return(0);
  148.     *cp = '\0';
  149.  
  150.     /* fill the resulting string */
  151.  
  152.     for (cpp = vec; *cpp; cpp++) {
  153.     strcat(cp,*cpp);
  154.     strcat(cp,sep);
  155.     }
  156.     return(cp);
  157. }
  158.