home *** CD-ROM | disk | FTP | other *** search
- /*
- strtools.c 11/18/86
-
- % various string functions.
-
- OWL 1.1
- 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
- */
-
-
- #include "oakhead.h"
- #include "strdecl.h"
- #include <ctype.h>
- /* -------------------------------------------------------------------------- */
-
- char *strleft(s, len)
- 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(s, len)
- 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(s + spaces, s, slen + 1);
- memset(s, ' ', spaces);
- return(s);
- }
-
- char *strcenter(s, len)
- 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(s + spaces/2, s, slen + 1);
- memset(s, ' ', spaces/2);
- }
-
- /* adjust trailing spaces */
- strpad(s, len);
- return(s);
- }
-
- char *strclip(s)
- char *s;
- /*
- Removes the trailing spaces from a string.
- */
- {
- char *p;
-
- for (p = s + strlen(s) - 1; *p == ' ' && p >= s; p--) {
- *p = '\0';
- }
-
- return(s);
- }
-
- char *strpreclip(s)
- 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(s, len)
- 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(s + sl, ' ', len - sl);
-
- s[len] = '\0';
- return(s);
- }
-
- char *strfill(s, chr, count)
- 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(s, chr, count);
- s[count] = '\0';
- return(s);
- }
-
- char invert_char(c)
- 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(s)
- char *s;
- /*
- Removes all spaces, newlines, and tabs from a string.
- */
- {
- char *p;
-
- for (p = s; ; p++) {
- if (*p != ' ' && *p != '\n' && *p != '\r' && *p != '\t') {
- *s++ = *p;
- }
- if (*p == '\0') {
- break;
- }
- }
-
- return(s);
- }
-
-
-
-
-