home *** CD-ROM | disk | FTP | other *** search
- /*
- strtools.c 11/18/86
-
- % various string functions.
-
- OWL 1.2
- Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- 4/30/88 jmd Removed length restrictions (strright and strcenter).
- 7/27/88 jmd Moved to oakland lib from C-scape.
- 12/27/88 jmd Added str_compact
-
- 11/29/89 jmd added casts for DG
- 3/28/90 jmd ansi-fied
- 6/13/90 jdc replaced strclip's backwards string math with integer math
- 10/22/90 jmd fixed return value of strcompact
- */
-
-
- #include "oakhead.h"
- #include "strdecl.h"
- #include <ctype.h>
- /* -------------------------------------------------------------------------- */
-
- char *strleft(char *s, int len)
- /*
- Removes leading spaces and pads a string with spaces on the right
- until it's len characters long.
- String must have enough space allocated for it.
- */
- {
- strpreclip(s);
- strpad(s, len);
- return(s);
- }
-
- char *strright(char *s, int len)
- /*
- Removes trailing spaces and pads a string with spaces on the left
- until it's len characters long.
- String must have enough space allocated for it.
- */
- {
- int spaces, slen;
-
- strclip(s);
- slen = strlen(s);
- spaces = len - slen;
- if (spaces <= 0) {
- return(s);
- }
-
- memmove((VOID *) (s + spaces), (VOID *) s, slen + 1);
- memset((VOID *) s, ' ', spaces);
- return(s);
- }
-
- char *strcenter(char *s, int len)
- /*
- Adjusts the string's trailing and leading spaces s until it's len
- characters long with the text centered.
- String must have enough space allocated for it.
- */
- {
- int spaces, slen;
-
- strclip(s);
- strpreclip(s);
-
- slen = strlen(s);
- spaces = len - slen;
- if (spaces <= 0) {
- return(s);
- }
-
- /* adjust leading spaces */
- if (spaces / 2 > 0) {
- memmove((VOID *) (s + spaces/2), (VOID *) s, slen + 1);
- memset((VOID *) s, ' ', spaces/2);
- }
-
- /* adjust trailing spaces */
- strpad(s, len);
- return(s);
- }
-
- char *strclip(char *s)
- /*
- Removes the trailing spaces from a string.
- */
- {
- int index;
-
- for (index = strlen(s) - 1; s[index] == ' ' && index >= 0; index--) {
- s[index] = '\0';
- }
-
- return(s);
- }
-
- char *strpreclip(char *s)
- /*
- Removes the leading spaces from a string.
- */
- {
- char *p;
-
- for (p = s; *p == ' ' && *p != '\0'; p++)
- ;
-
- strcpy(s, p);
- return(s);
- }
-
- char *strpad(char *s, int len)
- /*
- Pad a string with spaces to length len.
- String must have enough space allocated for it.
- */
- {
- int sl;
-
- if ((sl = strlen(s)) < len) {
- memset((VOID *) (s + sl), ' ', len - sl);
- }
-
- s[len] = '\0';
- return(s);
- }
-
- char *strfill(char *s, char chr, int count)
- /*
- Fills the string with count chr's aand NULL terminates it.
- String must have enough space allocated for it.
- */
- {
- memset((VOID *) s, chr, count);
- s[count] = '\0';
- return(s);
- }
-
- char invert_char(char c)
- /*
- Swaps low and high nibbles of a char.
- */
- {
- char x = 0;
-
- x = c << 4;
- c >>= 4;
- c &= 0x0f; /* make sure there are no high bits */
- x |= c;
-
- return(x);
- }
-
-
- char *strcompact(char *string)
- /*
- Removes all spaces, newlines, and tabs from a string.
- returns 'string'
- */
- {
- char *p, *s;
-
- for (p = s = string; ; p++) {
- if (*p != ' ' && *p != '\n' && *p != '\r' && *p != '\t') {
- *s++ = *p;
- }
- if (*p == '\0') {
- break;
- }
- }
-
- return(string);
- }
-
-