X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=libctsupport%2Fstrfuncs.cpp;fp=libctsupport%2Fstrfuncs.cpp;h=8033df4fb38b96a4de822293289e311889b1f317;hp=0000000000000000000000000000000000000000;hb=99dd1d6ed10db1f669a5fe6af71225a50fc0ddfb;hpb=2c61ff85796550481227f2fbec53506a6b5bd365 diff --git a/libctsupport/strfuncs.cpp b/libctsupport/strfuncs.cpp new file mode 100644 index 0000000..8033df4 --- /dev/null +++ b/libctsupport/strfuncs.cpp @@ -0,0 +1,168 @@ +/***************************************************************************** +** This is part of the CTSim program +** Copyright (C) 1983-2000 Kevin Rosenberg +** +** $Id: strfuncs.cpp,v 1.1 2000/06/19 02:58:08 kevin Exp $ +** +** This program is free software; you can redistribute it and/or modify +** it under the terms of the GNU General Public License (version 2) as +** published by the Free Software Foundation. +** +** This program is distributed in the hope that it will be useful, +** but WITHOUT ANY WARRANTY; without even the implied warranty of +** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +** GNU General Public License for more details. +** +** You should have received a copy of the GNU General Public License +** along with this program; if not, write to the Free Software +** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +******************************************************************************/ + +#include +#include "kstddef.h" + + +/* NAME + * str_skip_head Skip leading characters of string + * + * SYNOPSIS + * shortened = str_skip_head (str, charlist) + * OUT shortened Start of shortened string + * IN char *str String to have beginning skipped + * IN char *charlist List of characters to skip over + * + * NOTES + * This routine returns the position in a string (str) of the + * first character that is not in an specified string of characters + * (charlist). + */ + + +const char* +str_skip_head (const char* str, const char* const charlist) +{ + const char* p = str; + + while (*p && (strchr (charlist, *p) != NULL)) + p++; + + return (p); +} + +char* +str_skip_head (char* str, char* charlist) +{ + char* p = str; + + while (*p && (strchr (charlist, *p) != NULL)) + p++; + + return (p); +} + + +/* NAME + * str_lower Convert a string to lower case + * + * SYNOPSIS + * str = str_lower (str) + * char *str String to be converted + */ + +char * +str_lower (char *s) +{ + char *p = s; + + while (*p) { /* while (*p != EOS) */ + *p = tolower(*p); + ++p; + } + + return (s); +} + +/* NAME + * str_rm_tail Remove characters from end of string + * + * SYNOPSIS + * str = str_rm_tail (str, charlist) + * char *str String to have end removed +k * char *charlist List of characters to remove from string + * + */ + + +char * +str_rm_tail (char *str, const char* const charlist) +{ + int i; + + for (i = strlen(str) - 1; i >= 0; i--) + if (strchr (charlist, str[i]) != NULL) + str[i] = EOS; + else + break; /* found non-specified char, all done */ + + return (str); +} + +/* NAME + * str_wrm_tail Remove white space from end of string + * + * SYNOPSIS + * str = str_wrm_tail (str) + * char *str String to have white space removed + * + */ + +char * +str_wrm_tail (char *str) +{ + return (str_rm_tail(str, "\b\t\n\r")); +} + +/* NAME + * str_upper Convert a string to upper case + * + * SYNOPSIS + * str = str_upper (str) + * char *str String to be converted + */ + +char * +str_upper (char *s) +{ + char *p = s; + + while (*p) { /* while (*s != EOS) */ + *p = toupper(*p); + p++; + } + + return (s); +} + + +#ifdef TEST +int +main (void) +{ + string str, clist; + char *skip; + + printf ("Test program for str_skip_head\n"); + printf ("\n"); + printf ("Enter string that will have its head skipped -- "); + gets (str); + printf ("Enter list of characters to be skipped -- "); + gets (clist); + printf ("\n"); + + skip = str_skip_head (str, clist); + + printf ("Shortened string = '%s'\n", skip); +} +#endif + +