1 /*
2 SuperCollider real time audio synthesis system
3 Copyright (c) 2002 James McCartney. All rights reserved.
4 http://www.audiosynth.com
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 /*
21
22 Primitives for Char.
23
24 */
25
26 #include <ctype.h>
27 #include "PyrPrimitive.h"
28 #include "VMGlobals.h"
29
30
31 int prToLower(struct VMGlobals* g, int numArgsPushed);
prToLower(struct VMGlobals * g,int numArgsPushed)32 int prToLower(struct VMGlobals* g, int numArgsPushed) {
33 PyrSlot* a;
34
35 a = g->sp;
36
37 SetRawChar(a, tolower(slotRawChar(a)));
38
39 return errNone;
40 }
41
42 int prToUpper(struct VMGlobals* g, int numArgsPushed);
prToUpper(struct VMGlobals * g,int numArgsPushed)43 int prToUpper(struct VMGlobals* g, int numArgsPushed) {
44 PyrSlot* a;
45
46 a = g->sp;
47
48 SetRawChar(a, toupper(slotRawChar(a)));
49
50 return errNone;
51 }
52
prIsLower(struct VMGlobals * g,int numArgsPushed)53 int prIsLower(struct VMGlobals* g, int numArgsPushed) {
54 PyrSlot* a = g->sp;
55
56 if (islower(slotRawChar(a))) {
57 SetTrue(a);
58 } else {
59 SetFalse(a);
60 }
61
62 return errNone;
63 }
64
prIsUpper(struct VMGlobals * g,int numArgsPushed)65 int prIsUpper(struct VMGlobals* g, int numArgsPushed) {
66 PyrSlot* a = g->sp;
67
68 if (isupper(slotRawChar(a))) {
69 SetTrue(a);
70 } else {
71 SetFalse(a);
72 }
73
74 return errNone;
75 }
76
77 int prIsAlpha(struct VMGlobals* g, int numArgsPushed);
prIsAlpha(struct VMGlobals * g,int numArgsPushed)78 int prIsAlpha(struct VMGlobals* g, int numArgsPushed) {
79 PyrSlot* a;
80
81 a = g->sp;
82
83 if (isalpha(slotRawChar(a))) {
84 SetTrue(a);
85 } else {
86 SetFalse(a);
87 }
88
89 return errNone;
90 }
91
92 int prIsAlphaNum(struct VMGlobals* g, int numArgsPushed);
prIsAlphaNum(struct VMGlobals * g,int numArgsPushed)93 int prIsAlphaNum(struct VMGlobals* g, int numArgsPushed) {
94 PyrSlot* a;
95
96 a = g->sp;
97
98 if (isalnum(slotRawChar(a))) {
99 SetTrue(a);
100 } else {
101 SetFalse(a);
102 }
103
104 return errNone;
105 }
106
107 int prIsControl(struct VMGlobals* g, int numArgsPushed);
prIsControl(struct VMGlobals * g,int numArgsPushed)108 int prIsControl(struct VMGlobals* g, int numArgsPushed) {
109 PyrSlot* a;
110
111 a = g->sp;
112
113 if (iscntrl(slotRawChar(a))) {
114 SetTrue(a);
115 } else {
116 SetFalse(a);
117 }
118
119 return errNone;
120 }
121
122 int prIsDigit(struct VMGlobals* g, int numArgsPushed);
prIsDigit(struct VMGlobals * g,int numArgsPushed)123 int prIsDigit(struct VMGlobals* g, int numArgsPushed) {
124 PyrSlot* a;
125
126 a = g->sp;
127
128 if (isdigit(slotRawChar(a))) {
129 SetTrue(a);
130 } else {
131 SetFalse(a);
132 }
133
134 return errNone;
135 }
136
137 int prIsPrint(struct VMGlobals* g, int numArgsPushed);
prIsPrint(struct VMGlobals * g,int numArgsPushed)138 int prIsPrint(struct VMGlobals* g, int numArgsPushed) {
139 PyrSlot* a;
140
141 a = g->sp;
142
143 if (isprint(slotRawChar(a))) {
144 SetTrue(a);
145 } else {
146 SetFalse(a);
147 }
148
149 return errNone;
150 }
151
152 int prIsPunct(struct VMGlobals* g, int numArgsPushed);
prIsPunct(struct VMGlobals * g,int numArgsPushed)153 int prIsPunct(struct VMGlobals* g, int numArgsPushed) {
154 PyrSlot* a;
155
156 a = g->sp;
157
158 if (ispunct(slotRawChar(a))) {
159 SetTrue(a);
160 } else {
161 SetFalse(a);
162 }
163
164 return errNone;
165 }
166
167 int prIsSpace(struct VMGlobals* g, int numArgsPushed);
prIsSpace(struct VMGlobals * g,int numArgsPushed)168 int prIsSpace(struct VMGlobals* g, int numArgsPushed) {
169 PyrSlot* a;
170
171 a = g->sp;
172
173 if (isspace(slotRawChar(a))) {
174 SetTrue(a);
175 } else {
176 SetFalse(a);
177 }
178
179 return errNone;
180 }
181
182 int prAsciiValue(struct VMGlobals* g, int numArgsPushed);
prAsciiValue(struct VMGlobals * g,int numArgsPushed)183 int prAsciiValue(struct VMGlobals* g, int numArgsPushed) {
184 PyrSlot* a;
185
186 a = g->sp;
187
188 SetTagRaw(a, tagInt);
189
190 return errNone;
191 }
192
193 int prDigitValue(struct VMGlobals* g, int numArgsPushed);
prDigitValue(struct VMGlobals * g,int numArgsPushed)194 int prDigitValue(struct VMGlobals* g, int numArgsPushed) {
195 PyrSlot* a;
196 char c;
197
198 a = g->sp;
199
200 c = slotRawChar(a);
201 if (c >= '0' && c <= '9') {
202 SetInt(a, c - '0');
203 } else if (c >= 'a' && c <= 'z') {
204 SetInt(a, c - 'a' + 10);
205 } else if (c >= 'A' && c <= 'Z') {
206 SetInt(a, c - 'A' + 10);
207 } else {
208 return errFailed;
209 }
210
211 return errNone;
212 }
213
214
215 int prAsAscii(struct VMGlobals* g, int numArgsPushed);
prAsAscii(struct VMGlobals * g,int numArgsPushed)216 int prAsAscii(struct VMGlobals* g, int numArgsPushed) {
217 PyrSlot* a;
218
219 a = g->sp;
220 SetChar(a, slotRawInt(a) & 255);
221
222 return errNone;
223 }
224
225 int prAsDigit(struct VMGlobals* g, int numArgsPushed);
prAsDigit(struct VMGlobals * g,int numArgsPushed)226 int prAsDigit(struct VMGlobals* g, int numArgsPushed) {
227 PyrSlot* a;
228 int c;
229
230 a = g->sp;
231
232 c = slotRawInt(a);
233 if (c >= 0 && c <= 9) {
234 SetChar(a, slotRawInt(a) + '0');
235 } else if (c >= 10 && c <= 35) {
236 SetChar(a, slotRawInt(a) + 'A' - 10);
237 } else {
238 return errFailed;
239 }
240
241 return errNone;
242 }
243
244 void initCharPrimitives();
initCharPrimitives()245 void initCharPrimitives() {
246 int base, index = 0;
247
248 base = nextPrimitiveIndex();
249
250 definePrimitive(base, index++, "_AsciiValue", prAsciiValue, 1, 0);
251 definePrimitive(base, index++, "_DigitValue", prDigitValue, 1, 0);
252 definePrimitive(base, index++, "_AsAscii", prAsAscii, 1, 0);
253 definePrimitive(base, index++, "_AsDigit", prAsDigit, 1, 0);
254 definePrimitive(base, index++, "_ToLower", prToLower, 1, 0);
255 definePrimitive(base, index++, "_ToUpper", prToUpper, 1, 0);
256 definePrimitive(base, index++, "_IsLower", prIsLower, 1, 0);
257 definePrimitive(base, index++, "_IsUpper", prIsUpper, 1, 0);
258 definePrimitive(base, index++, "_IsAlpha", prIsAlpha, 1, 0);
259 definePrimitive(base, index++, "_IsAlphaNum", prIsAlphaNum, 1, 0);
260 definePrimitive(base, index++, "_IsPrint", prIsPrint, 1, 0);
261 definePrimitive(base, index++, "_IsPunct", prIsPunct, 1, 0);
262 definePrimitive(base, index++, "_IsControl", prIsControl, 1, 0);
263 definePrimitive(base, index++, "_IsSpace", prIsSpace, 1, 0);
264 definePrimitive(base, index++, "_IsDecDigit", prIsDigit, 1, 0);
265 }
266
267
268 #if _SC_PLUGINS_
269
270
271 # include "SCPlugin.h"
272
273 // export the function that SC will call to load the plug in.
274 # pragma export on
275 extern "C" {
276 SCPlugIn* loadPlugIn(void);
277 }
278 # pragma export off
279
280
281 // define plug in object
282 class APlugIn : public SCPlugIn {
283 public:
284 APlugIn();
285 virtual ~APlugIn();
286
287 virtual void AboutToCompile();
288 };
289
APlugIn()290 APlugIn::APlugIn() {
291 // constructor for plug in
292 }
293
~APlugIn()294 APlugIn::~APlugIn() {
295 // destructor for plug in
296 }
297
AboutToCompile()298 void APlugIn::AboutToCompile() {
299 // this is called each time the class library is compiled.
300 initCharPrimitives();
301 }
302
303 // This function is called when the plug in is loaded into SC.
304 // It returns an instance of APlugIn.
loadPlugIn()305 SCPlugIn* loadPlugIn() { return new APlugIn(); }
306
307 #endif
308