File: strfuncs.cpp

package info (click to toggle)
ctsim 6.0.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,868 kB
  • sloc: cpp: 26,967; sh: 7,782; ansic: 1,256; perl: 296; makefile: 148
file content (166 lines) | stat: -rw-r--r-- 3,823 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*****************************************************************************
**  This is part of the CTSim program
**  Copyright (c) 1983-2009 Kevin Rosenberg
**
**  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 <ctype.h>
#include "ctsupport.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).
 */


char*
str_skip_head (const char* str, const char* const charlist)
{
  const char* p = str;

  while (*p && (strchr (charlist, *p) != NULL))
    p++;

  return (const_cast<char*>(p));
}

char*
str_skip_head (const char* str, char* charlist)
{
  const char* p = str;

  while (*p && (strchr (charlist, *p) != NULL))
    p++;

  return (const_cast<char*>(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