r10877: Automated commit for Debian build of ctsim upstream-version-4.4.3
[ctsim.git] / libctsupport / strfuncs.cpp
1 /*****************************************************************************
2 **  This is part of the CTSim program
3 **  Copyright (c) 1983-2001 Kevin Rosenberg
4 **
5 **  $Id$
6 **
7 **  This program is free software; you can redistribute it and/or modify
8 **  it under the terms of the GNU General Public License (version 2) as
9 **  published by the Free Software Foundation.
10 **
11 **  This program is distributed in the hope that it will be useful,
12 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 **  GNU General Public License for more details.
15 **
16 **  You should have received a copy of the GNU General Public License
17 **  along with this program; if not, write to the Free Software
18 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 ******************************************************************************/
20
21 #include <ctype.h>
22 #include "ctsupport.h"
23
24
25 /* NAME
26  *      str_skip_head                   Skip leading characters of string
27  *
28  * SYNOPSIS
29  *      shortened = str_skip_head (str, charlist)
30  *  OUT shortened                       Start of shortened string
31  *  IN  char *str                       String to have beginning skipped
32  *  IN  char *charlist                  List of characters to skip over
33  *
34  * NOTES
35  *          This routine returns the position in a string (str) of the
36  *      first character that is not in an specified string of characters
37  *      (charlist).
38  */
39
40
41 char*
42 str_skip_head (const char* str, const char* const charlist) 
43 {
44   const char* p = str;
45
46   while (*p && (strchr (charlist, *p) != NULL))
47     p++;
48
49   return (const_cast<char*>(p));
50 }
51
52 char*
53 str_skip_head (const char* str, char* charlist) 
54 {
55   const char* p = str;
56
57   while (*p && (strchr (charlist, *p) != NULL))
58     p++;
59
60   return (const_cast<char*>(p));
61 }
62
63
64 /* NAME
65  *      str_lower                       Convert a string to lower case
66  *
67  * SYNOPSIS
68  *      str  = str_lower (str)
69  *      char *str                       String to be converted
70  */
71
72 char *
73 str_lower (char *s)
74 {
75     char *p = s;
76     
77     while (*p) {                        /* while (*p != EOS) */
78         *p = tolower(*p);
79         ++p;
80     }
81
82     return (s);
83 }
84
85 /* NAME
86  *      str_rm_tail                     Remove characters from end of string
87  *
88  * SYNOPSIS
89  *      str = str_rm_tail (str, charlist)
90  *      char *str                       String to have end removed
91 k *     char *charlist                  List of characters to remove from string
92  *
93  */
94
95
96 char *
97 str_rm_tail (char *str, const char* const charlist)
98 {
99   int i;
100   
101   for (i = strlen(str) - 1; i >= 0; i--)
102     if (strchr (charlist, str[i]) != NULL)
103       str[i] = EOS;
104     else
105       break;            /* found non-specified char, all done */
106
107   return (str);
108 }
109
110 /* NAME
111  *      str_wrm_tail                    Remove white space from end of string
112  *
113  * SYNOPSIS
114  *      str = str_wrm_tail (str)
115  *      char *str                       String to have white space removed
116  *
117  */
118
119 char *
120 str_wrm_tail (char *str)
121 {
122   return (str_rm_tail(str, "\b\t\n\r"));
123 }
124
125 /* NAME
126  *      str_upper                       Convert a string to upper case
127  *
128  * SYNOPSIS
129  *      str  = str_upper (str)
130  *      char *str                       String to be converted
131  */
132
133 char *
134 str_upper (char *s)
135 {
136   char *p = s;
137
138   while (*p) {                  /* while (*s != EOS) */
139     *p = toupper(*p);
140     p++;
141   }
142
143   return (s);
144 }
145
146
147 #ifdef TEST
148 int 
149 main (void)
150 {
151   string str, clist;
152   char *skip;
153
154   printf ("Test program for str_skip_head\n");
155   printf ("\n");
156   printf ("Enter string that will have its head skipped -- ");
157   gets (str);
158   printf ("Enter list of characters to be skipped -- ");
159   gets (clist);
160   printf ("\n");
161   
162   skip = str_skip_head (str, clist);
163   
164   printf ("Shortened string = '%s'\n", skip);
165 }
166 #endif
167
168