home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 211.lha / Spiff / strings.c < prev    next >
C/C++ Source or Header  |  1996-02-14  |  3KB  |  163 lines

  1. /*                        Copyright (c) 1988 Bellcore
  2. **                            All Rights Reserved
  3. **       Permission is granted to copy or use this program, EXCEPT that it
  4. **       may not be sold for profit, the copyright notice must be reproduced
  5. **       on copies, and credit should be given to Bellcore where it is due.
  6. **       BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  7. */
  8.  
  9.  
  10. #ifndef lint
  11. static char rcsid[]= "$Header: strings.c,v 1.1 88/09/15 11:33:54 daniel Rel $";
  12. #endif
  13.  
  14. #include <ctype.h>
  15. #include "misc.h"
  16. #include "strings.h"
  17.  
  18. /*
  19. **    routines for handling strings.
  20. **        several routines manipulate  "words"
  21. **        a "word" is a string not containing whitespace
  22. */
  23.  
  24. /*
  25. **    copy a single word. similar to  strcpy
  26. */
  27. void
  28. S_wordcpy(to,from)
  29. char *to, *from;
  30. {
  31.     while ((*from != '\0') && isprint(*from) && (!isspace(*from)))
  32.     {
  33.         *to++ = *from++;
  34.     }
  35.     *to = '\0';
  36.     return;
  37. }
  38.  
  39. /*
  40. **    find the next whitespace character.  The address of the pointer
  41. **        is passed and the pointer itself is changed.
  42. */
  43. void
  44. S_skipword(theptr)
  45. char **theptr;
  46. {
  47.     while((**theptr != '\0') && isprint(**theptr) && (!isspace(**theptr)))
  48.     {
  49.         (*theptr)++;    /* increment the pointer, NOT the pointer
  50.                     to the pointer */
  51.     }
  52.     return;
  53. }
  54.  
  55. /*
  56. **    find the next non-whitespace character.  The address of the pointer
  57. **        is passed and the pointer itself is changed.
  58. */
  59. void
  60. S_skipspace(theptr)
  61. char **theptr;
  62. {
  63.     while((**theptr != '\0') && isspace(**theptr))
  64.     {
  65.         (*theptr)++;    /* increment the pointer, NOT the pointer
  66.                     to the pointer */
  67.     }
  68.     return;
  69. }
  70.  
  71. /*
  72. **    move the pointer to the beginning of the next word
  73. */
  74. void
  75. S_nextword(theptr)
  76. char **theptr;
  77. {
  78.     S_skipword(theptr);
  79.     S_skipspace(theptr);
  80.     return;
  81. }
  82.  
  83. /*
  84. **    see if the first string is a prefix of the second
  85. **        returns 0 if yes
  86. **        non zero if now
  87. **        sigh -- the way strcmp does
  88. */
  89. int
  90. S_wordcmp(s1,s2)
  91. char *s1,*s2;
  92. {
  93.     return(strncmp(s1,s2,strlen(s2)));
  94. }
  95.  
  96. /*
  97. **    chop off any trailing zeros on a string
  98. **        but leave one zero if there are only zeros
  99. */
  100. void
  101. S_trimzeros(str)
  102. char *str;
  103. {
  104.     /*
  105.     **    end starts out pointing at the null terminator
  106.     */
  107.     char *end = str + strlen(str);
  108.  
  109.     /*
  110.     **    if there is more than one character left in the string
  111.     */
  112.     while(end > (str+1))
  113.     {
  114.         --end;
  115.         if ('0' == *end)
  116.         {
  117.             *end = '\0';
  118.         }
  119.         else
  120.         {
  121.             return;
  122.         }
  123.     }
  124.     return;
  125. }
  126.  
  127. /*
  128. **    save a copy of the string
  129. */
  130. void
  131. S_savestr(to,from)
  132. char **to,*from;
  133. {
  134.     S_allocstr(to,strlen(from));
  135.     (void) strcpy(*to,from);
  136.     return;
  137. }
  138.  
  139. /*
  140. **    save cnt characters of the string
  141. */
  142. void
  143. S_savenstr(to,from,cnt)
  144. char **to,*from;
  145. {
  146.     S_allocstr(to,cnt);
  147.     (void) strncpy(*to,from,cnt);
  148.     *((*to)+cnt) = '\0';
  149.     return;
  150. }
  151.  
  152. /*
  153. **    allocate space for a string,  add 1 to size to
  154. **        make sure that there is room for the terminating null character
  155. */
  156. void
  157. S_allocstr(to,size)
  158. char **to;
  159. int size;
  160. {
  161.     *to = Z_ALLOC(size+1,char);
  162. }
  163.