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