Use py_resource module
[python/dscho.git] / Modules / structmodule.c
blob649f3307107e2cbcaf23f2df052e77a98f6df18e
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* struct module -- pack values into and (out of) strings */
27 #include "allobjects.h"
28 #include "modsupport.h"
30 static object *StructError;
33 /* Define various structs to figure out the alignments of types */
35 #ifdef __MWERKS__
37 ** XXXX We have a problem here. There are no unique alignment rules
38 ** on the PowerPC mac.
40 #ifdef __powerc
41 #pragma options align=mac68k
42 #endif
43 #endif /* __MWERKS__ */
45 typedef struct { char c; short x; } s_short;
46 typedef struct { char c; int x; } s_int;
47 typedef struct { char c; long x; } s_long;
48 typedef struct { char c; float x; } s_float;
49 typedef struct { char c; double x; } s_double;
51 #define SHORT_ALIGN (sizeof(s_short) - sizeof(short))
52 #define INT_ALIGN (sizeof(s_int) - sizeof(int))
53 #define LONG_ALIGN (sizeof(s_long) - sizeof(long))
54 #define FLOAT_ALIGN (sizeof(s_float) - sizeof(float))
55 #define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double))
57 #ifdef __powerc
58 #pragma options align=reset
59 #endif
62 /* Align a size according to a format code */
64 static int
65 align(size, c)
66 int size;
67 int c;
69 int a;
71 switch (c) {
72 case 'h': a = SHORT_ALIGN; break;
73 case 'i': a = INT_ALIGN; break;
74 case 'l': a = LONG_ALIGN; break;
75 case 'f': a = FLOAT_ALIGN; break;
76 case 'd': a = DOUBLE_ALIGN; break;
77 default: return size;
79 return (size + a - 1) / a * a;
83 /* calculate the size of a format string */
85 static int
86 calcsize(fmt)
87 char *fmt;
89 char *s;
90 char c;
91 int size, num, itemsize, x;
93 s = fmt;
94 size = 0;
95 while ((c = *s++) != '\0') {
96 if ('0' <= c && c <= '9') {
97 num = c - '0';
98 while ('0' <= (c = *s++) && c <= '9') {
99 x = num*10 + (c - '0');
100 if (x/10 != num) {
101 err_setstr(StructError,
102 "int overflow in fmt");
103 return -1;
105 num = x;
107 if (c == '\0')
108 break;
110 else
111 num = 1;
113 size = align(size, c);
115 switch (c) {
117 case 'x': /* pad byte */
118 case 'b': /* byte-sized int */
119 case 'c': /* char */
120 itemsize = 1;
121 break;
123 case 'h': /* short ("half-size)" int */
124 itemsize = sizeof(short);
125 break;
127 case 'i': /* natural-size int */
128 itemsize = sizeof(int);
129 break;
131 case 'l': /* long int */
132 itemsize = sizeof(long);
133 break;
135 case 'f': /* float */
136 itemsize = sizeof(float);
137 break;
139 case 'd': /* double */
140 itemsize = sizeof(double);
141 break;
143 default:
144 err_setstr(StructError, "bad char in fmt");
145 return -1;
149 x = num * itemsize;
150 size += x;
151 if (x/itemsize != num || size < 0) {
152 err_setstr(StructError, "total struct size too long");
153 return -1;
158 return size;
162 /* pack(fmt, v1, v2, ...) --> string */
164 static object *
165 struct_calcsize(self, args)
166 object *self; /* Not used */
167 object *args;
169 char *fmt;
170 int size;
172 if (!getargs(args, "s", &fmt))
173 return NULL;
174 size = calcsize(fmt);
175 if (size < 0)
176 return NULL;
177 return newintobject((long)size);
181 /* pack(fmt, v1, v2, ...) --> string */
183 static object *
184 struct_pack(self, args)
185 object *self; /* Not used */
186 object *args;
188 object *format, *result, *v;
189 char *fmt;
190 int size, num;
191 int i, n;
192 char *s, *res, *restart;
193 char c;
194 long ival;
195 double fval;
197 if (args == NULL || !is_tupleobject(args) ||
198 (n = gettuplesize(args)) < 1) {
199 err_badarg();
200 return NULL;
202 format = gettupleitem(args, 0);
203 if (!getargs(format, "s", &fmt))
204 return NULL;
205 size = calcsize(fmt);
206 if (size < 0)
207 return NULL;
208 result = newsizedstringobject((char *)NULL, size);
209 if (result == NULL)
210 return NULL;
212 s = fmt;
213 i = 1;
214 res = restart = getstringvalue(result);
216 while ((c = *s++) != '\0') {
217 if ('0' <= c && c <= '9') {
218 num = c - '0';
219 while ('0' <= (c = *s++) && c <= '9')
220 num = num*10 + (c - '0');
221 if (c == '\0')
222 break;
224 else
225 num = 1;
227 res = restart + align((int)(res-restart), c);
229 while (--num >= 0) {
230 switch (c) {
232 case 'x': /* pad byte */
233 *res++ = '\0';
234 break;
236 case 'l':
237 case 'i':
238 case 'h':
239 case 'b':
240 if (i >= n) {
241 err_setstr(StructError,
242 "insufficient arguments to pack");
243 goto fail;
245 v = gettupleitem(args, i++);
246 if (!is_intobject(v)) {
247 err_setstr(StructError,
248 "bad argument type to pack");
249 goto fail;
251 ival = getintvalue(v);
252 switch (c) {
253 case 'b':
254 *res++ = ival;
255 break;
256 case 'h':
257 *(short*)res = ival;
258 res += sizeof(short);
259 break;
260 case 'i':
261 *(int*)res = ival;
262 res += sizeof(int);
263 break;
264 case 'l':
265 *(long*)res = ival;
266 res += sizeof(long);
267 break;
269 break;
271 case 'c':
272 if (i >= n) {
273 err_setstr(StructError,
274 "insufficient arguments to pack");
275 goto fail;
277 v = gettupleitem(args, i++);
278 if (!is_stringobject(v) ||
279 getstringsize(v) != 1) {
280 err_setstr(StructError,
281 "bad argument type to pack");
282 goto fail;
284 *res++ = getstringvalue(v)[0];
285 break;
287 case 'd':
288 case 'f':
289 if (i >= n) {
290 err_setstr(StructError,
291 "insufficient arguments to pack");
292 goto fail;
294 v = gettupleitem(args, i++);
295 if (!is_floatobject(v)) {
296 err_setstr(StructError,
297 "bad argument type to pack");
298 goto fail;
300 fval = getfloatvalue(v);
301 switch (c) {
302 case 'f':
303 *(float*)res = (float)fval;
304 res += sizeof(float);
305 break;
306 case 'd':
307 memcpy(res, (char*)&fval, sizeof fval);
308 res += sizeof(double);
309 break;
311 break;
313 default:
314 err_setstr(StructError, "bad char in fmt");
315 goto fail;
321 if (i < n) {
322 err_setstr(StructError, "too many arguments for pack fmt");
323 goto fail;
326 return result;
328 fail:
329 DECREF(result);
330 return NULL;
334 /* Helper to convert a list to a tuple */
336 static object *
337 totuple(list)
338 object *list;
340 int len = getlistsize(list);
341 object *tuple = newtupleobject(len);
342 if (tuple != NULL) {
343 int i;
344 for (i = 0; i < len; i++) {
345 object *v = getlistitem(list, i);
346 INCREF(v);
347 settupleitem(tuple, i, v);
350 DECREF(list);
351 return tuple;
355 /* unpack(fmt, string) --> (v1, v2, ...) */
357 static object *
358 struct_unpack(self, args)
359 object *self; /* Not used */
360 object *args;
362 char *str, *start, *fmt, *s;
363 char c;
364 int len, size, num, x;
365 object *res, *v;
367 if (!getargs(args, "(ss#)", &fmt, &start, &len))
368 return NULL;
369 size = calcsize(fmt);
370 if (size != len) {
371 err_setstr(StructError, "unpack str size does not match fmt");
372 return NULL;
374 res = newlistobject(0);
375 if (res == NULL)
376 return NULL;
377 str = start;
378 s = fmt;
379 while ((c = *s++) != '\0') {
380 if ('0' <= c && c <= '9') {
381 num = c - '0';
382 while ('0' <= (c = *s++) && c <= '9')
383 num = num*10 + (c - '0');
384 if (c == '\0')
385 break;
387 else
388 num = 1;
390 str = start + align((int)(str-start), c);
392 while (--num >= 0) {
393 switch (c) {
395 case 'x':
396 str++;
397 continue;
399 case 'b':
400 x = *str++;
401 if (x >= 128)
402 x -= 256;
403 v = newintobject((long)x);
404 break;
406 case 'c':
407 v = newsizedstringobject(str, 1);
408 str++;
409 break;
411 case 'h':
412 v = newintobject((long)*(short*)str);
413 str += sizeof(short);
414 break;
416 case 'i':
417 v = newintobject((long)*(int*)str);
418 str += sizeof(int);
419 break;
421 case 'l':
422 v = newintobject(*(long*)str);
423 str += sizeof(long);
424 break;
426 case 'f':
427 v = newfloatobject((double)*(float*)str);
428 str += sizeof(float);
429 break;
431 case 'd':
433 double d;
434 memcpy((char *)&d, str, sizeof d);
435 v = newfloatobject(d);
436 str += sizeof(double);
437 break;
440 default:
441 err_setstr(StructError, "bad char in fmt");
442 goto fail;
445 if (v == NULL || addlistitem(res, v) < 0)
446 goto fail;
447 DECREF(v);
451 return totuple(res);
453 fail:
454 DECREF(res);
455 return NULL;
459 /* List of functions */
461 static struct methodlist struct_methods[] = {
462 {"calcsize", struct_calcsize},
463 {"pack", struct_pack, 1/*varargs*/},
464 {"unpack", struct_unpack},
465 {NULL, NULL} /* sentinel */
469 /* Module initialization */
471 void
472 initstruct()
474 object *m, *d;
476 /* Create the module and add the functions */
477 m = initmodule("struct", struct_methods);
479 /* Add some symbolic constants to the module */
480 d = getmoduledict(m);
481 StructError = newstringobject("struct.error");
482 dictinsert(d, "error", StructError);
484 /* Check for errors */
485 if (err_occurred())
486 fatal("can't initialize module struct");