Utilities.cpp
Go to the documentation of this file.
1 /*----------------------------------------------------------------------------*/
2 /* */
3 /* Copyright (c) 1995, 2004 IBM Corporation. All rights reserved. */
4 /* Copyright (c) 2005-2009 Rexx Language Association. All rights reserved. */
5 /* */
6 /* This program and the accompanying materials are made available under */
7 /* the terms of the Common Public License v1.0 which accompanies this */
8 /* distribution. A copy is also available at the following address: */
9 /* http://www.oorexx.org/license.html */
10 /* */
11 /* Redistribution and use in source and binary forms, with or */
12 /* without modification, are permitted provided that the following */
13 /* conditions are met: */
14 /* */
15 /* Redistributions of source code must retain the above copyright */
16 /* notice, this list of conditions and the following disclaimer. */
17 /* Redistributions in binary form must reproduce the above copyright */
18 /* notice, this list of conditions and the following disclaimer in */
19 /* the documentation and/or other materials provided with the distribution. */
20 /* */
21 /* Neither the name of Rexx Language Association nor the names */
22 /* of its contributors may be used to endorse or promote products */
23 /* derived from this software without specific prior written permission. */
24 /* */
25 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS */
26 /* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT */
27 /* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS */
28 /* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
29 /* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
30 /* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
31 /* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */
32 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY */
33 /* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
34 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
35 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
36 /* */
37 /*----------------------------------------------------------------------------*/
38 /****************************************************************************/
39 /* REXX Kernel */
40 /* */
41 /* Utility Functions */
42 /* */
43 /****************************************************************************/
44 
45 #include <stdarg.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <ctype.h>
49 #include <sys/types.h>
50 #include "Utilities.hpp"
51 
53  const char *string, /* search string */
54  const char *set, /* reference set */
55  sizeB_t length ) /* size of string */
56 /*********************************************************************/
57 /* Function: Find first occurence of set member in memory */
58 /*********************************************************************/
59 {
60  while (length-- > 0)
61  { /* search through string */
62 
63  if (strchr(set, *string))
64  { /* find a match in ref set? */
65  return string;
66  }
67  string++; /* step the pointer */
68  }
69  return NULL; /* return matched position */
70 }
71 
72 
73 /**
74  * Portable implementation of an ascii-z caseless string compare.
75  *
76  * @param opt1 First string argument
77  * @param opt2 Second string argument.
78  *
79  * @return The compare result. Returns 0, negative, or positive depending
80  * one the ordering compare result.
81  */
82 int Utilities::strCaselessCompare(const char *op1, const char *op2)
83 {
84  while (tolower(*op1) == tolower(*op2))
85  {
86  if (*op1 == 0)
87  {
88  return 0;
89  }
90  op1++;
91  op2++;
92  }
93 
94  return(tolower(*op1) - tolower(*op2));
95 }
96 
97 /**
98  * Portable implementation of a caseless memory compare.
99  *
100  * @param opt1 First memory location to compare.
101  * @param opt2 Second memory location.
102  * @param len Length to compare.
103  *
104  * @return The compare result. Returns 0, negative, or positive depending
105  * one the ordering compare result.
106  */
107 int Utilities::memicmp(const void *mem1, const void *mem2, size_t len)
108 {
109  const char *op1 = (const char *)mem1;
110  const char *op2 = (const char *)mem2;
111  while(len != 0)
112  {
113  if (tolower(*op1) != tolower(*op2))
114  {
115  return tolower(*op1) - tolower(*op2);
116 
117  }
118  op1++;
119  op2++;
120  len--;
121  }
122  return 0;
123 }
124 
125 /**
126  * Portable implementation of an ascii-z string to uppercase (in place).
127  *
128  * @param str String argument
129  *
130  * @return The address of the str unput argument.
131  */
132 void Utilities::strupper(char *str)
133 {
134  while (*str)
135  {
136  *str = toupper(*str);
137  str++;
138  }
139 
140  return;
141 }
142 
143 
144 /**
145  * Portable implementation of an ascii-z string to uppercase (in place).
146  *
147  * @param str String argument
148  *
149  * @return The address of the str unput argument.
150  */
151 void Utilities::strlower(char *str)
152 {
153  while (*str)
154  {
155  *str = tolower(*str);
156  str++;
157  }
158 
159  return;
160 }
161 
162 
163 /**
164  * Bounded strchr() function.
165  *
166  * @param data The data pointer.
167  * @param n The maximum length to scan.
168  * @param ch The character of interest.
169  *
170  * @return The pointer to the located character, or NULL if it isn't found.
171  */
172 const char *Utilities::strnchr(const char *data, sizeB_t n, char ch)
173 {
174  const char *endPtr = data + n;
175  while (data < endPtr && *data != '\0')
176  {
177  if (*data == ch)
178  {
179  return data;
180  }
181  data++;
182  }
183  return NULL;
184 }
static const char * strnchr(const char *, sizeB_t n, char ch)
Definition: Utilities.cpp:172
static void strupper(char *str)
Definition: Utilities.cpp:132
static void strlower(char *str)
Definition: Utilities.cpp:151
static int strCaselessCompare(const char *opt1, const char *opt2)
Definition: Utilities.cpp:82
static const char * locateCharacter(const char *s, const char *set, sizeB_t l)
Definition: Utilities.cpp:52
static int memicmp(const void *opt1, const void *opt2, size_t len)
Definition: Utilities.cpp:107
stringsizeB_t sizeB_t
Definition: rexx.h:248