home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / forut062.zip / ForUtil-0.62 / lib / stringutil.c < prev    next >
C/C++ Source or Header  |  1996-08-28  |  4KB  |  201 lines

  1. #ifndef lint
  2. static char rcsId[]="$Header: /usr/local/rcs/ForUtil/lib/RCS/stringutil.c,v 1.7 1996/08/28 17:42:14 koen Exp koen $";
  3. #endif
  4. /*****
  5. * stringutil.c : some usefull string routines
  6. *
  7. * This file Version    $Revision: 1.7 $
  8. *
  9. * Creation date:    Mon Feb  4 03:54:11 GMT+0100 1980
  10. * Last modification:     $Date: 1996/08/28 17:42:14 $
  11. * By:            $Author: koen $
  12. * Current State:    $State: Exp $
  13. *
  14. * Author:        koen
  15. * (C)Copyright 1995 Ripley Software Development
  16. * All Rights Reserved
  17. *****/
  18. /*****
  19. * ChangeLog 
  20. * $Log: stringutil.c,v $
  21. * Revision 1.7  1996/08/28 17:42:14  koen
  22. * Added print_version_id routine: prints a version string on stdout.
  23. *
  24. * Revision 1.6  1996/08/07 21:18:40  koen
  25. * Corrected a potential bug in strend.
  26. *
  27. * Revision 1.5  1996/08/02 14:53:58  koen
  28. * Added the downcase, file_has_ext and strend routines.
  29. *
  30. * Revision 1.4  1996/07/30 02:02:42  koen
  31. * Reordered include files.
  32. *
  33. * Revision 1.3  1996/07/16 09:21:27  koen
  34. * cast in ErrorString if HAVE_STRERROR is defined
  35. *
  36. * Revision 1.2  1996/05/06 00:36:53  koen
  37. * Adapted for MSDOS.
  38. *
  39. * Revision 1.1  1996/04/25 02:30:29  koen
  40. * Initial Revision
  41. *
  42. *****/ 
  43. /* needed to prevent multiple variable decls under MSDOS in sysdeps.h */
  44. #define LIBRARY_OBJECT
  45.  
  46. /* include this before anything else */
  47. #ifdef HAVE_CONFIG_H
  48. #include "autoconf.h"
  49. #endif
  50.  
  51. #include <ctype.h>
  52. #include <errno.h>
  53.  
  54. #ifndef HAVE_STRERROR
  55. extern char *sys_errlist[];
  56. extern int errno;
  57. #else
  58. #include <string.h>
  59. #endif
  60.  
  61. #include "sysdeps.h"
  62.  
  63. /****
  64. * Print a version information
  65. * p_name: progran name
  66. * p_major: major version id, plain string
  67. * p_minor: minor version id, rcs-style revision string.
  68. *****/
  69. void
  70. print_version_id(char *p_name, char *p_major, char *p_minor)
  71. {
  72.     char *chPtr;
  73.     char minor[8];
  74.  
  75.     /* move to the first space */
  76.     for(chPtr = p_minor; *chPtr != ' ' && *chPtr != '\0'; chPtr++);
  77.     /* sanity check */
  78.     if(*chPtr == '\0')
  79.         chPtr = " 0.0 $";
  80.     chPtr++;    /* skip past leading space */
  81.     memset(minor, '\0', 8);
  82.     strncpy(minor, chPtr, strlen(chPtr)-2); /* remove trailing stuff */
  83.     printf("%s version %s.%s\n", p_name, p_major, minor);
  84. }
  85.  
  86. /****
  87. * Make string downcase
  88. ****/
  89. inline void
  90. downcase(char *string)
  91. {
  92.     register char *outPtr = string;
  93.     for(outPtr = string; *outPtr != '\0'; 
  94.         *(outPtr++) = tolower(*(string++)));
  95. }
  96.  
  97. /****
  98. * Make string uppercase
  99. ****/
  100. inline void
  101. upcase(char *string)
  102. {
  103.     register char *outPtr = string;
  104.     for(outPtr = string; *outPtr != '\0'; 
  105.         *(outPtr++) = toupper(*(string++)));
  106. }
  107.  
  108. /*****
  109. * See if s1 does have an extension attached to it
  110. * return 1 of it does, 0 if it does not
  111. ******/
  112. inline int
  113. file_has_ext(char *s1)
  114. {
  115.     int l1 = strlen(s1)-1;    /* don't include '\0' */
  116.     char *p1 = s1+l1;
  117.  
  118.     for(; *p1 && *p1 != SLASH && *p1 != '.' ; p1--);
  119.  
  120.     if(*p1 && *p1 == '.')
  121.         return(1);
  122.     return(0);
  123. }
  124.  
  125. /*****
  126. * see if the string s2 is at the end of string s1
  127. * returns 1 if it does, 0 if it does not
  128. *****/
  129. inline int
  130. strend(char *s1, char *s2)
  131. {
  132.     register int i,j;
  133.  
  134.     /* -1 since we don't include '\0' */
  135.     for(i = strlen(s1)-1, j = strlen(s2)-1; s1[i] == s2[j] && j >= 0; 
  136.         i--, j--);
  137.  
  138.     if(j == -1)
  139.         return(1);
  140.     return(0);
  141. }
  142.  
  143. #if !defined (HAVE_STRSTR)
  144. /* Return the starting address of string S2 in S1;
  145.    return 0 if it is not found. 
  146.    (note: take from the gnu file utilities) */
  147. inline char
  148. *strstr (char *s1, char *s2)
  149. {
  150.     register int i;
  151.     register char *p1;
  152.     register char *p2;
  153.     register char *s = s1;
  154.  
  155.     for (p2 = s2, i = 0; *s; p2 = s2, i++, s++)
  156.     {
  157.         for (p1 = s; *p1 && *p2 && *p1 == *p2; p1++, p2++)
  158.             ;
  159.         if (!*p2)
  160.             break;
  161.     }
  162.     if (!*p2)
  163.         return s1 + i;
  164.     return 0;
  165. }
  166. #endif
  167.  
  168. /*****
  169. * Remove white spaces from a string
  170. *****/
  171. void
  172. stripspaces(char *string)
  173. {
  174.         char *outPtr = string;
  175.         while (1)
  176.         {
  177.                 if (*string != ' ' && *string != '\t')
  178.                         *(outPtr++) = *(string++);
  179.                 else
  180.                         string++;
  181.                 if (*string == 0)
  182.                 {
  183.                         *outPtr = '\0';
  184.                         return;
  185.                 }
  186.         }
  187. }
  188.  
  189. /*****
  190. * Generic error string routine
  191. *****/
  192. char 
  193. *ErrorString(void)
  194. {
  195. #ifdef HAVE_STRERROR
  196.     return((char*)strerror(errno));
  197. #else
  198.     return(sys_errlist[errno]); 
  199. #endif 
  200. }
  201.