home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c185 / 2.ddi / OWLSRC.EXE / CSCAPE / SOURCE / STRTOOLS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-06  |  3.1 KB  |  192 lines

  1. /*
  2.     strtools.c      11/18/86
  3.  
  4.     % various string functions.
  5.  
  6.     OWL 1.1
  7.     Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
  8.     ALL RIGHTS RESERVED.
  9.  
  10.     Revision History:
  11.     -----------------
  12.      4/30/88 jmd    Removed length restrictions (strright and strcenter).
  13.      7/27/88 jmd    Moved to oakland lib from C-scape.
  14.     12/27/88 jmd    Added str_compact
  15. */
  16.  
  17.  
  18. #include "oakhead.h"
  19. #include "strdecl.h"
  20. #include <ctype.h>
  21. /* -------------------------------------------------------------------------- */
  22.  
  23. char *strleft(s, len)
  24.     char *s;           
  25.     int  len;
  26. /* 
  27.     Removes leading spaces and pads a string with spaces on the right 
  28.     until it's len characters long.
  29.     String must have enough space allocated for it.        
  30. */
  31. {
  32.     strpreclip(s);
  33.     strpad(s, len);
  34.     return(s);
  35. }
  36.  
  37. char *strright(s, len)
  38.     char *s;
  39.     int  len;
  40. /* 
  41.     Removes trailing spaces and pads a string with spaces on the left 
  42.     until it's len characters long.
  43.     String must have enough space allocated for it.        
  44. */
  45. {
  46.     int  spaces, slen;
  47.  
  48.     strclip(s);
  49.     slen = strlen(s);
  50.     spaces = len - slen;
  51.     if (spaces <= 0) {
  52.         return(s);
  53.     }
  54.  
  55.     memmove(s + spaces, s, slen + 1);
  56.     memset(s, ' ', spaces);
  57.     return(s);
  58. }
  59.  
  60. char *strcenter(s, len)
  61.     char *s;
  62.     int  len;
  63. /* 
  64.     Adjusts the string's trailing and leading spaces s until it's len 
  65.     characters long with the text centered.
  66.     String must have enough space allocated for it.        
  67. */
  68. {
  69.     int spaces, slen;
  70.     
  71.     strclip(s);
  72.     strpreclip(s);
  73.  
  74.     slen = strlen(s);
  75.     spaces = len - slen;
  76.     if (spaces <= 0) {
  77.         return(s);
  78.     }
  79.  
  80.     /* adjust leading spaces */
  81.     if (spaces / 2 > 0) {
  82.         memmove(s + spaces/2, s, slen + 1);
  83.         memset(s, ' ', spaces/2);
  84.     }
  85.  
  86.     /* adjust trailing spaces */
  87.     strpad(s, len);    
  88.     return(s);
  89. }
  90.  
  91. char *strclip(s)        
  92.     char *s;
  93. /* 
  94.     Removes the trailing spaces from a string.
  95. */
  96. {
  97.     char  *p;
  98.  
  99.     for (p = s + strlen(s) - 1; *p == ' ' && p >= s; p--) {
  100.         *p = '\0';
  101.     }
  102.  
  103.     return(s);
  104. }
  105.  
  106. char *strpreclip(s)        
  107.     char *s;
  108. /* 
  109.     Removes the leading spaces from a string.
  110. */
  111. {
  112.     char *p;
  113.  
  114.     for (p = s; *p == ' ' && *p != '\0'; p++) 
  115.         ;
  116.  
  117.     strcpy(s, p);
  118.     return(s);
  119. }
  120.  
  121. char *strpad(s, len)    
  122.     char *s;
  123.     int  len;
  124. /*  
  125.     Pad a string with spaces to length len.
  126.     String must have enough space allocated for it.
  127. */
  128. {
  129.     int sl;
  130.     
  131.     if ((sl = strlen(s)) < len)
  132.         memset(s + sl, ' ', len - sl);
  133.  
  134.     s[len] = '\0';
  135.     return(s);
  136. }
  137.  
  138. char *strfill(s, chr, count)        
  139.     char *s;
  140.     char chr;
  141.     int  count;
  142. /* 
  143.     Fills the string with count chr's aand NULL terminates it.
  144.     String must have enough space allocated for it.
  145. */
  146. {
  147.     memset(s, chr, count);
  148.     s[count] = '\0';
  149.     return(s);
  150. }
  151.  
  152. char invert_char(c)
  153.     char c;
  154. /*
  155.     Swaps low and high nibbles of a char.
  156. */
  157. {
  158.     char x = 0;
  159.  
  160.     x = c << 4;
  161.     c >>= 4;
  162.     c &= 0x0f;        /* make sure there are no high bits */
  163.     x |= c;
  164.  
  165.     return(x);    
  166. }
  167.  
  168.  
  169. char *strcompact(s)
  170.     char *s;
  171. /*
  172.     Removes all spaces, newlines, and tabs from a string.
  173. */
  174. {
  175.     char *p;
  176.  
  177.     for (p = s; ; p++) {
  178.         if (*p != ' ' && *p != '\n' && *p != '\r' && *p != '\t') {
  179.             *s++ = *p;
  180.         }
  181.         if (*p == '\0') {
  182.             break;
  183.         }
  184.     }
  185.  
  186.     return(s);
  187. }
  188.  
  189.  
  190.  
  191.  
  192.