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 
52 // Pointer to the function GetConcurrencyInfos declared in RexxActivation.hpp
54 
56 {
57  concurrencyCollector = collector;
58 }
59 
60 
61 void Utilities::GetConcurrencyInfos(struct ConcurrencyInfos &concurrencyInfos)
62 {
63  if (concurrencyCollector != NULL) concurrencyCollector(concurrencyInfos);
64  else
65  {
66  memset(&concurrencyInfos, '\0', sizeof(concurrencyInfos));
67  concurrencyInfos.lock = '?'; // It's a way to know that the structure was not filled
68  }
69 }
70 
71 
73  const char *string, /* search string */
74  const char *set, /* reference set */
75  size_t length ) /* size of string */
76 /*********************************************************************/
77 /* Function: Find first occurence of set member in memory */
78 /*********************************************************************/
79 {
80  while (length-- > 0)
81  { /* search through string */
82 
83  if (strchr(set, *string))
84  { /* find a match in ref set? */
85  return string;
86  }
87  string++; /* step the pointer */
88  }
89  return NULL; /* return matched position */
90 }
91 
92 
93 /**
94  * Portable implementation of an ascii-z caseless string compare.
95  *
96  * @param opt1 First string argument
97  * @param opt2 Second string argument.
98  *
99  * @return The compare result. Returns 0, negative, or positive depending
100  * one the ordering compare result.
101  */
102 int Utilities::strCaselessCompare(const char *op1, const char *op2)
103 {
104  while (tolower(*op1) == tolower(*op2))
105  {
106  if (*op1 == 0)
107  {
108  return 0;
109  }
110  op1++;
111  op2++;
112  }
113 
114  return(tolower(*op1) - tolower(*op2));
115 }
116 
117 /**
118  * Portable implementation of a caseless memory compare.
119  *
120  * @param opt1 First memory location to compare.
121  * @param opt2 Second memory location.
122  * @param len Length to compare.
123  *
124  * @return The compare result. Returns 0, negative, or positive depending
125  * one the ordering compare result.
126  */
127 int Utilities::memicmp(const void *mem1, const void *mem2, size_t len)
128 {
129  const char *op1 = (const char *)mem1;
130  const char *op2 = (const char *)mem2;
131  while(len != 0)
132  {
133  if (tolower(*op1) != tolower(*op2))
134  {
135  return tolower(*op1) - tolower(*op2);
136 
137  }
138  op1++;
139  op2++;
140  len--;
141  }
142  return 0;
143 }
144 
145 /**
146  * Portable implementation of an ascii-z string to uppercase (in place).
147  *
148  * @param str String argument
149  *
150  * @return The address of the str unput argument.
151  */
152 void Utilities::strupper(char *str)
153 {
154  while (*str)
155  {
156  *str = toupper(*str);
157  str++;
158  }
159 
160  return;
161 }
162 
163 
164 /**
165  * Portable implementation of an ascii-z string to uppercase (in place).
166  *
167  * @param str String argument
168  *
169  * @return The address of the str unput argument.
170  */
171 void Utilities::strlower(char *str)
172 {
173  while (*str)
174  {
175  *str = tolower(*str);
176  str++;
177  }
178 
179  return;
180 }
181 
182 
183 /**
184  * Bounded strchr() function.
185  *
186  * @param data The data pointer.
187  * @param n The maximum length to scan.
188  * @param ch The character of interest.
189  *
190  * @return The pointer to the located character, or NULL if it isn't found.
191  */
192 const char *Utilities::strnchr(const char *data, size_t n, char ch)
193 {
194  const char *endPtr = data + n;
195  while (data < endPtr && *data != '\0')
196  {
197  if (*data == ch)
198  {
199  return data;
200  }
201  data++;
202  }
203  return NULL;
204 }
static ConcurrencyInfosCollector concurrencyCollector
Definition: Utilities.cpp:53
void(* ConcurrencyInfosCollector)(struct ConcurrencyInfos &concurrencyInfos)
Definition: Utilities.hpp:72
static void SetConcurrencyInfosCollector(ConcurrencyInfosCollector)
Definition: Utilities.cpp:55
static const char * locateCharacter(const char *s, const char *set, size_t l)
Definition: Utilities.cpp:72
static void GetConcurrencyInfos(struct ConcurrencyInfos &concurrencyInfos)
Definition: Utilities.cpp:61
static const char * strnchr(const char *, size_t n, char ch)
Definition: Utilities.cpp:192
static void strupper(char *str)
Definition: Utilities.cpp:152
static void strlower(char *str)
Definition: Utilities.cpp:171
static int strCaselessCompare(const char *opt1, const char *opt2)
Definition: Utilities.cpp:102
static int memicmp(const void *opt1, const void *opt2, size_t len)
Definition: Utilities.cpp:127