home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / C / Samples / CSAPE32.ARJ / SOURCE / OWLSCR / STRTOOLS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-22  |  3.3 KB  |  181 lines

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