/* ************************************************************* RWhois Software Copyright (c) 1994 Scott Williamson and Mark Kosters Copyright (c) 1996-1998 Network Solutions, Inc. See the file LICENSE for conditions of use and distribution. **************************************************************** */ #include "rwhois.h" #include "strutils.h" /* strutil.c: a collection of string manipulation tools. */ /* History: originally located in misc.c */ /* stripchar: Strips out all occurences of a specific character (in place) */ char * stripchar(char *str, char ch) { int i; int j; int len; if (!str) { return NULL; } len = strlen(str); for (i = 0, j = 0; i < len; i++) { if (str[i] != ch) { str[j++] = str[i]; } } str[j] = '\0'; return(str); } /* strip_trailing: strips any characters matching ch off of the end of a string. */ char * strip_trailing(char *str, char ch) { int i; int len; if (NOT_STR_EXISTS(str)) { return NULL; } len = strlen(str); for (i = len - 1; str[i] == ch && i >= 0; i--) { str[i] = '\0'; } return(str); } /* strip_leading: strips any characters matching ch off the front of a string. */ char * strip_leading(char *str, char ch) { char *p1; char *p2; if (!str) { return NULL; } p1 = p2 = str; while (*p1 == ch) { p1++; } /* copy to front only if necessary */ if (p1 != p2) { while (*p2) { *p2++ = *p1++; } } return(str); } char * strip_control(char *str) { int i; int j; int len; if (!str) { return NULL; } len = strlen(str); for (i = 0, j = 0; i < len; i++) { if (!iscntrl((int) str[i]) || str[i] == '\t') { str[j++] = str[i]; } } str[j] = '\0'; return(str); } /* rtrim: strips whitespace off of the end of a string. */ char * rtrim(char *str) { int i; if (!str) { return NULL; } for (i = strlen(str) - 1; i >= 0 && isspace((int) str[i]); i--) { str[i] = '\0'; } return(str); } /* ltrim: strips the whitespace off the the front of a string */ char * ltrim(char *str) { char *p1; char *p2; if (!str) { return NULL; } p1 = p2 = str; while (isspace((int) *p1)) { p1++; } /* copy to front only if necessary */ if (p1 != p2) { while (*p1) { *p2++ = *p1++; } *p2 = '\0'; } return(str); } /* trim: trim whitespace off of both ends of a string (in place) */ char * trim(char *str) { if (!str) { return NULL; } rtrim(str); ltrim(str); return(str); } /* reverses the string */ char * strrev(char *str) { char *head; char *tail; char c; if (!str) { return NULL; } for (head = str, tail = str + strlen(str) - 1; head < tail; head++, tail--) { c = *head; *head = *tail; *tail = c; } return(str); } char * skip_whitespace(char *str) { char *p = str; if (!str) { return NULL; } while (isspace((int) *p)) p++; return(p); } int count_char(char *str, char c) { char *s; int count; if (!str) { return -1; } for (count = 0, s = str; *s; s++) { if (*s == c) count++; } return(count); } int count_spaces(char *str) { char *s; int count; if (!str) { return -1; } for (count = 0, s = str; *s; s++) { if (isspace((int) *s)) count++; } return(count); } /* strSTR: does a case-insensitve sub-string search */ char * strSTR(char *str1, char *str2) /* by Jeff Odum 09/91 */ { int i; int j; int x; /* return immediately if NULL data */ if (NOT_STR_EXISTS(str1) || NOT_STR_EXISTS(str2)) { return NULL; } for (x = 0; str1[x]; x++) { if (toupper(str1[x]) == toupper(str2[0])) { /* Matched first letter */ for (i=x, j=0; (str1[i] && str2[j]) && (toupper(str1[i]) == toupper(str2[j])); i++, j++ ); if (!str2[j]) { /* Matched whole string */ return (&str1[x]); } } } return NULL; } /* strupr: Upcases a string. */ char * strupr(char *a) { char *b; if (!a) { return NULL; } for (b = a; *a; a++) { *a = toupper(*a); } return (b); } char * compact_whitespace(char *str) { int i; int j; int len; int flag; if (!str) { return NULL; } len = strlen(str); for (i = 0, j = 0, flag = FALSE; i < len; i++) { if (isspace((int) str[i])) { if (!flag) { str[j] = ' '; j++; flag = TRUE; } } else { str[j] = str[i]; j++; flag = FALSE; } } str[j] = '\0'; return(str); } /* check if the string has any whitespaces (space, tab ..) */ int is_no_whitespace_str(char *str) { if (!str) return FALSE; for ( ; *str; str++ ) { if (isspace((int) *str)) { return FALSE; } } return TRUE; } /* check if the given character string is just made of digits. */ int is_number_str(char *str) { if (!str) return FALSE; for ( ; *str; str++ ) { if ( !isdigit((int) *str) ) { return FALSE; } } return TRUE; }