ooRexx  4.2.0-source
About: ooRexx (Open Object Rexx) is a free implementation of Object Rexx. Object Rexx is an enhancement of the classic Rexx interpreter; a full-featured programming language with a human-oriented syntax.
  Fossies Dox: ooRexx-4.2.0-source.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

StringClassBit.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 StringClassBit.cpp */
40 /* */
41 /* REXX string BITxxx methods */
42 /* */
43 /******************************************************************************/
44 
45 #include <ctype.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <math.h>
49 #include "RexxCore.h"
50 #include "StringClass.hpp"
51 
52 /******************************************************************************/
53 /* Arguments: String to bitand with self */
54 /* pad character to use */
55 /* */
56 /* Returned: New string object */
57 /******************************************************************************/
59  RexxString *pad)
60 {
61  char PadChar; /* pad character */
62  const char *String1; /* string 1 pointer */
63  const char *PadString; /* padded string part */
64  const char *String2; /* string 2 pointer */
65  size_t String1Len; /* string 1 length */
66  size_t String2Len; /* string 2 length */
67  size_t MinLength; /* length of shorter string */
68  size_t PadLength; /* length to pad */
69  size_t MaxLength; /* longest length */
70  RexxString *Retval; /* return value */
71  const char *Source; /* source string pointer */
72  char *Target; /* target string pointer */
73 
74  /* get string we will be doing bit */
75  /* stuff to... */
76  string2 = optionalStringArgument(string2, OREF_NULLSTRING, ARG_ONE);
77  String2Len = string2->getLength(); /* get the string length */
78  String2 = string2->getStringData(); /* get the string data pointer */
79  /* get the pad character */
80  PadChar = optionalPadArgument(pad, (char)0xff, ARG_TWO);
81 
82  String1 = this->getStringData(); /* point to the first string */
83  String1Len = this->getLength(); /* get the length */
84  if (String1Len <= String2Len)
85  { /* string 1 shorter or equal? */
86  MinLength = String1Len; /* string 1 is the shorter */
87  MaxLength = String2Len; /* string 2 is the longer */
88  PadString = String2; /* padding is done on string2 */
89  Source = String1; /* operate from string 1 */
90  }
91  else
92  {
93  MinLength = String2Len; /* string 2 is the shorter */
94  MaxLength = String1Len; /* string 1 is the longer */
95  PadString = String1; /* padding is done on string1 */
96  Source = String2; /* operate from string 2 */
97  }
98  PadLength = MaxLength - MinLength; /* get the padding length */
99  /* Duplicate Longer */
100  Retval = raw_string(MaxLength);
101  Target = Retval->getWritableData(); /* point to the tArget */
102  memcpy(Target, PadString, MaxLength);/* now copy in the longer one */
103 
104  while (MinLength--)
105  { /* while shorter has data */
106  /* and in each character */
107  *Target = *Target & *Source++;
108  Target++; /* step the target */
109  }
110 
111  while (PadLength--)
112  { /* while pad needed */
113  /* and in a pad character */
114  *Target = *Target & PadChar;
115  Target++; /* step the target */
116  }
117  return Retval; /* return result string */
118 }
119 
120 /* the BITOR function */
121 /******************************************************************************/
122 /* Arguments: String to bitor with self */
123 /* pad character to use */
124 /* */
125 /* Returned: New string object */
126 /******************************************************************************/
128  RexxString *pad)
129 {
130  char PadChar; /* pad character */
131  const char *String1; /* string 1 pointer */
132  const char *PadString; /* padded string part */
133  const char *String2; /* string 2 pointer */
134  size_t String1Len; /* string 1 length */
135  size_t String2Len; /* string 2 length */
136  size_t MinLength; /* length of shorter string */
137  size_t PadLength; /* length to pad */
138  size_t MaxLength; /* longest length */
139  RexxString *Retval; /* return value */
140  const char *Source; /* source string pointer */
141  char *Target; /* tArget string pointer */
142 
143  /* get string we will be doing bit */
144  /* stuff to... */
145  string2 = optionalStringArgument(string2, OREF_NULLSTRING, ARG_ONE);
146  String2Len = string2->getLength(); /* get the string length */
147  String2 = string2->getStringData(); /* get the string data pointer */
148  /* get the pad character */
149  PadChar = optionalPadArgument(pad, 0x00, ARG_TWO);
150 
151  String1 = this->getStringData(); /* point to the first string */
152  String1Len = this->getLength(); /* get the length */
153  if (String1Len <= String2Len)
154  { /* string 1 shorter or equal? */
155  MinLength = String1Len; /* string 1 is the shorter */
156  MaxLength = String2Len; /* string 2 is the longer */
157  PadString = String2; /* padding is done on string2 */
158  Source = String1; /* operate from string 1 */
159  }
160  else
161  {
162  MinLength = String2Len; /* string 2 is the shorter */
163  MaxLength = String1Len; /* string 1 is the longer */
164  PadString = String1; /* padding is done on string1 */
165  Source = String2; /* operate from string 2 */
166  }
167  PadLength = MaxLength - MinLength; /* get the padding length */
168  /* Duplicate Longer */
169  Retval = raw_string(MaxLength);
170  Target = Retval->getWritableData(); /* point to the tArget */
171  memcpy(Target, PadString, MaxLength);/* now copy in the longer one */
172 
173  while (MinLength--)
174  { /* while shorter has data */
175  /* and in each character */
176  *Target = *Target | *Source++;
177  Target++; /* step the target */
178  }
179 
180  while (PadLength--)
181  { /* while pad needed */
182  /* and in a pad character */
183  *Target = *Target | PadChar;
184  Target++; /* step the target */
185  }
186  return Retval; /* return result string */
187 }
188 
189 /* the BITXOR function */
190 /******************************************************************************/
191 /* Arguments: String to bitxor with self */
192 /* pad character to use */
193 /* */
194 /* Returned: New string object */
195 /******************************************************************************/
197  RexxString *pad)
198 {
199  char PadChar; /* pad character */
200  const char *String1; /* string 1 pointer */
201  const char *PadString; /* padded string part */
202  const char *String2; /* string 2 pointer */
203  size_t String1Len; /* string 1 length */
204  size_t String2Len; /* string 2 length */
205  size_t MinLength; /* length of shorter string */
206  size_t PadLength; /* length to pad */
207  size_t MaxLength; /* longest length */
208  RexxString *Retval; /* return value */
209  const char *Source; /* source string pointer */
210  char *Target; /* tArget string pointer */
211 
212  /* get string we will be doing bit */
213  /* stuff to... */
214  string2 = optionalStringArgument(string2, OREF_NULLSTRING, ARG_ONE);
215  String2Len = string2->getLength(); /* get the string length */
216  String2 = string2->getStringData(); /* get the string data pointer */
217  /* get the pad character */
218  PadChar = optionalPadArgument(pad, 0x00, ARG_TWO);
219 
220  String1 = this->getStringData(); /* point to the first string */
221  String1Len = this->getLength(); /* get the length */
222  if (String1Len <= String2Len)
223  { /* string 1 shorter or equal? */
224  MinLength = String1Len; /* string 1 is the shorter */
225  MaxLength = String2Len; /* string 2 is the longer */
226  PadString = String2; /* padding is done on string2 */
227  Source = String1; /* operate from string 1 */
228  }
229  else
230  {
231  MinLength = String2Len; /* string 2 is the shorter */
232  MaxLength = String1Len; /* string 1 is the longer */
233  PadString = String1; /* padding is done on string1 */
234  Source = String2; /* operate from string 2 */
235  }
236  PadLength = MaxLength - MinLength; /* get the padding length */
237  /* Duplicate Longer */
238  Retval = raw_string(MaxLength);
239  Target = Retval->getWritableData(); /* point to the tArget */
240  memcpy(Target, PadString, MaxLength);/* now copy in the longer one */
241 
242  while (MinLength--)
243  { /* while shorter has data */
244  /* and in each character */
245  *Target = *Target ^ *Source++;
246  Target++; /* step the target */
247  }
248 
249  while (PadLength--)
250  { /* while pad needed */
251  /* and in a pad character */
252  *Target = *Target ^ PadChar;
253  Target++; /* step the target */
254  }
255  return Retval; /* return result string */
256 }
257 
RexxString::getLength
size_t getLength()
Definition: StringClass.hpp:330
ARG_TWO
const int ARG_TWO
Definition: RexxCore.h:81
RexxString::getWritableData
char * getWritableData()
Definition: StringClass.hpp:334
RexxString::getStringData
const char * getStringData()
Definition: StringClass.hpp:333
RexxString::bitXor
RexxString * bitXor(RexxString *, RexxString *)
Definition: StringClassBit.cpp:196
raw_string
RexxString * raw_string(stringsize_t l)
Definition: StringClass.hpp:529
ARG_ONE
const int ARG_ONE
Definition: RexxCore.h:80
optionalStringArgument
RexxString * optionalStringArgument(RexxObject *o, RexxString *d, size_t p)
Definition: RexxCore.h:321
StringClass.hpp
optionalPadArgument
char optionalPadArgument(RexxObject *o, char d, size_t p)
Definition: RexxCore.h:351
RexxString::bitAnd
RexxString * bitAnd(RexxString *, RexxString *)
Definition: StringClassBit.cpp:58
RexxString::bitOr
RexxString * bitOr(RexxString *, RexxString *)
Definition: StringClassBit.cpp:127
RexxCore.h
RexxString
Definition: StringClass.hpp:119