Debian Codegraph @

contrib/ms-sbvar/utilities_dw/arrays/dw_array.c

raw
1 /*
2 * Copyright (C) 1996-2011 Daniel Waggoner
3 *
4 * This free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * It is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * If you did not received a copy of the GNU General Public License
15 * with this software, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "dw_array.h"
19 #include "dw_error.h"
20 #include "dw_std.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdarg.h>
25
26 //================================== Macros ===================================//
27 #define dw_ElementSizeA(a) (dw_SpecsA(a)->size)
28 #define dw_GetOffsetA(a) (dw_SpecsA(a)->offset)
29 #define dw_IsSameTypeA(a1,a2) (!memcmp(dw_SpecsA(a1),dw_SpecsA(a2),sizeof(TElementSpecification)))
30 #define dw_IsPointerA(a) (dw_SpecsA(a)->flag & dw_ARRAY_POINTER)
31 #define dw_UseMemcpyA(a) (dw_SpecsA(a)->flag & dw_ARRAY_USE_MEMCPY)
32 #define dw_DeleteSpecsA(a) (dw_SpecsA(a)->flag & dw_ARRAY_DELETE_SPECS)
33 #define dw_GetDestructorA(a) (dw_SpecsA(a)->destructor)
34 #define dw_GetDefaultConstructorA(s) (dw_SpecsA(a)->default_constructor)
35 #define dw_GetPointerCopyConstructorA(a) (dw_SpecsA(a)->pointer_copy_constructor)
36 #define dw_GetStaticCopyConstructorA(a) (dw_SpecsA(a)->static_copy_constructor)
37 #define dw_GetPrintRoutineA(a) (dw_SpecsA(a)->print_routine)
38 #define dw_GetReadRoutineA(a) (dw_SpecsA(a)->read_routine)
39
40
41 /*******************************************************************************/
42 /********************** C-style multi-dimensional arrays ***********************/
43 /*******************************************************************************/
44 /*
45 Frees a C-style multi-dimensional array. The pointer a must point to a valid
46 array created via a call to dw_CreateArray() or be a null pointer.
47 */
48 void dw_FreeArray(void* a)
49 {
50 int i, size, offset;
51 void (*Destructor)(void*);
52 if (a)
53 {
54 if ((Destructor=dw_GetDestructorA(a)))
55 {
56 if (dw_IsPointerA(a))
57 for (i=dw_DimA(a)-1; i >= 0; i--)
58 Destructor(((void**)a)[i]);
59 else
60 for (i=(size=dw_ElementSizeA(a))*(dw_DimA(a)-1); i >= 0; i-=size)
61 Destructor((void*)(((char*)a) + i));
62 }
63 offset=dw_GetOffsetA(a);
64 if (dw_DeleteSpecsA(a)) dw_free(dw_SpecsA(a));
65 dw_free((void*)(((char*)a) - offset));
66 }
67 }
68
69 /*
70 Assumes:
71 specs: Pointer to a valid TElementSpecification structure.
72 dim: Positive integer
73
74 Returns:
75 A pointer to a valid array of lenth dim upon success and a null pointer upon
76 failure.
77
78 Notes:
79 The return value should be type cast to the appropriate pointer type.
80 */
81 void* dw_CreateArray(TElementSpecification *specs, int dim)
82 {
83 void *a=(void*)NULL;
84 int i;
85 if (dim <= 0)
86 dw_Error(ARG_ERR);
87 else
88 if (!(a=dw_malloc(dim*specs->size + specs->offset)))
89 dw_Error(MEM_ERR);
90 else
91 {
92 a=(void*)(((char*)a)+specs->offset);
93 dw_DimA(a)=dim;
94 dw_SpecsA(a)=specs;
95 if (specs->default_constructor)
96 for (i=(specs->size)*(dim-1); i >= 0; i-=specs->size)
97 specs->default_constructor((void*)(((char*)a) + i));
98 }
99 return a;
100 }
101
102 /*
103 Assumes:
104 specs: Pointer to a valid TElementSpecification structure.
105 depth: Positive integer
106 dim: Array of positive integers of length at least depth
107
108 Returns:
109 A pointer to a valid multidimensiona array. The dimensions of the array are
110 determined by depth and dim.
111
112 Notes:
113 The return value should be type cast to the appropriate pointer type.
114 */
115 void* dw_CreateMultidimensionalArray(TElementSpecification *specs, int depth, int *dim)
116 {
117 int i;
118 void *a;
119 if (depth == 1) return dw_CreateArray(specs,dim[0]);
120 if ((a=dw_CreateArray_array(dim[0])))
121 for (i=dim[0]-1; i >= 0; i--)
122 if (!(((void**)a)[i]=dw_CreateMultidimensionalArray(specs,depth-1,dim+1)))
123 {
124 dw_FreeArray(a);
125 return (void*)NULL;
126 }
127 return a;
128 }
129 /*******************************************************************************/
130 /*******************************************************************************/
131 /*******************************************************************************/
132
133 /*******************************************************************************/
134 /**************************** Default Constructors *****************************/
135 /*******************************************************************************/
136 void DefaultPointerConstructor(void *element)
137 {
138 *((void**)element)=(void*)NULL;
139 }
140 /*******************************************************************************/
141 /*******************************************************************************/
142 /*******************************************************************************/
143
144 /*******************************************************************************/
145 /******************************* Print Functions *******************************/
146 /*******************************************************************************/
147 int dw_PrintArray(FILE *f, void *a, char *format)
148 {
149 int i, size;
150 int (*PrintRoutine)(FILE*, void*, char*);
151 if (f && a)
152 if ((PrintRoutine=dw_GetPrintRoutineA(a)))
153 {
154 if (dw_IsPointerA(a))
155 for (i=0; i < dw_DimA(a); i++)
156 { if (!PrintRoutine(f,((void**)a)[i],format)) return 0; }
157 else
158 for (size=dw_ElementSizeA(a), i=0; i < dw_DimA(a); i++)
159 { if (!PrintRoutine(f,(void*)(((char*)a) + i*size),format)) return 0; }
160 fprintf(f,"\n");
161 return 1;
162 }
163 return 0;
164 }
165
166 static int dw_PrintInt(FILE* f, void* element, char *format)
167 {
168 return (fprintf(f,format ? format : "%d ",*((int*)element)) < 0) ? 0 : 1;
169 }
170
171 static int dw_PrintDouble(FILE* f, void* element, char *format)
172 {
173 return (fprintf(f,format ? format : "%lf ",*((double*)element)) < 0) ? 0 : 1;
174 }
175
176 static int dw_PrintFloat(FILE* f, void* element, char *format)
177 {
178 return (fprintf(f,format ? format : "%f ",*((float*)element)) < 0) ? 0 : 1;
179 }
180
181 static int dw_PrintChar(FILE* f, void* element, char *format)
182 {
183 return (fprintf(f,format ? format : "%c ",*((char*)element)) < 0) ? 0 : 1;
184 }
185
186 static int dw_PrintString(FILE* f, void* element, char *format)
187 {
188 return (fprintf(f,format ? format : "%s\t",(char*)element) < 0) ? 0 : 1;
189 }
190 /*******************************************************************************/
191 /*******************************************************************************/
192 /*******************************************************************************/
193
194 /*******************************************************************************/
195 /******************************* Read Functions ********************************/
196 /*******************************************************************************/
197 int dw_ReadArray(FILE *f, void *a)
198 {
199 int i, size;
200 int (*ReadRoutine)(FILE*, void*);
201 if (f && a)
202 if ((ReadRoutine=dw_GetReadRoutineA(a)))
203 {
204 if (dw_IsPointerA(a))
205 for (i=0; i < dw_DimA(a); i++)
206 { if (!ReadRoutine(f,((void**)a)[i])) return 0; }
207 else
208 for (size=dw_ElementSizeA(a), i=0; i < dw_DimA(a); i++)
209 { if (!ReadRoutine(f,(void*)(((char*)a) + i*size))) return 0; }
210 return 1;
211 }
212 return 0;
213 }
214
215 static int dw_ReadInt(FILE* f, void* element)
216 {
217 return (fscanf(f," %d ",(int*)element) != 1) ? 0 : 1;
218 }
219
220 static int dw_ReadDouble(FILE* f, void* element)
221 {
222 return (fscanf(f," %lf ",(double*)element) != 1) ? 0 : 1;
223 }
224
225 static int dw_ReadFloat(FILE* f, void* element)
226 {
227 return (fscanf(f," %f ",(float*)element) != 1) ? 0 : 1;
228 }
229
230 static int dw_ReadChar(FILE* f, void* element)
231 {
232 return (fscanf(f," %c ",(char*)element) != 1) ? 0 : 1;
233 }
234 /*******************************************************************************/
235 /*******************************************************************************/
236 /*******************************************************************************/
237
238 /*******************************************************************************/
239 /****************************** Copy Constructors ******************************/
240 /*******************************************************************************/
241 /*
242 Assumes
243 */
244 static int FullCopyAttempt(void **d, void *s, void* (*copy)(void*, void*), void (*destructor)(void*))
245 {
246 if (s)
247 if (*d)
248 {
249 if (!copy(*d,s))
250 {
251 if (destructor) destructor(*d);
252 if (!(*d=copy((void*)NULL,s))) return 0;
253 }
254 }
255 else
256 {
257 if (!(*d=copy((void*)NULL,s))) return 0;
258 }
259 else
260 if (*d)
261 {
262 if (destructor) destructor(*d);
263 *d=(void*)NULL;
264 }
265 return 1;
266 }
267
268 /*
269 Assumes:
270 d: A valid array or null pointer
271 s: A valid array
272
273 Returns:
274 Upon success returns a copy of the array s. If d is null, then the array is
275 created. Upon failure, a null pointer is returned.
276
277 Notes:
278 If d is
279 */
280 void* dw_CopyArray(void* d, void* s)
281 {
282 int i, size;
283 void* original_d=d;
284
285 if (!s) return (void*)NULL;
286
287 if (s == d) return d;
288
289 if (!d)
290 { if (!(d=dw_CreateArray(dw_SpecsA(s),dw_DimA(s)))) return (void*)NULL; }
291 else
292 { if ((dw_DimA(s) != dw_DimA(d)) || !dw_IsSameTypeA(d,s)) return (void*)NULL; }
293
294 if (dw_UseMemcpyA(s))
295 {
296 memcpy(d,s,dw_DimA(s)*dw_ElementSizeA(s));
297 }
298 else if (dw_GetPointerCopyConstructorA(s))
299 {
300 for (i=dw_DimA(s)-1; i >= 0; i--)
301 if (!FullCopyAttempt(((void**)d)+i,((void**)s)[i],dw_GetPointerCopyConstructorA(s),dw_GetDestructorA(d)))
302 {
303 if (!original_d) dw_FreeArray(d);
304 return (void*)NULL;
305 }
306 }
307 else if (dw_GetStaticCopyConstructorA(s))
308 {
309 for (i=(size=dw_ElementSizeA(s))*(dw_DimA(s)-1); i >= 0; i-=size)
310 if (!dw_GetStaticCopyConstructorA(s)((void*)(((char*)d) + i),(void*)(((char*)s) + i)))
311 {
312 if (!original_d) dw_FreeArray(d);
313 return (void*)NULL;
314 }
315 }
316 else
317 {
318 if (!original_d) dw_FreeArray(d);
319 return (void*)NULL;
320 }
321
322 return d;
323 }
324
325 /*
326 Assumes
327 Both d and s are valid pointers and both *d and *s are either null or a
328 null terminated string. If *d is a null terminated string, then it must
329 have been created via a call to dw_malloc(), dw_calloc() or dw_realloc().
330
331 Returns
332 Returns one upon success and zero upon failure.
333
334 Results
335 If is *s is null, then *d is freed if it is non-null and is then set to
336 null. If *s is null terminated string, then *d is reallocated if more
337 memory is required and then *s is copied into *d.
338
339 Notes
340 It is critical that this function be called only if the destination string
341 was dynamically created via a call to dw_malloc(), dw_calloc() or dw_realloc(). If
342 this is not the case, then servere memory problems can result.
343 */
344 static int dw_CopyString(void *d, void *s)
345 {
346 char* dest;
347 if (*((char**)s))
348 if ((dest=(char*)dw_realloc(*((char**)d),strlen(*((char**)s))+1)))
349 strcpy(*((char**)d)=dest,*((char**)s));
350 else
351 return 0;
352 else
353 if (*((char**)d))
354 {
355 dw_free(*((char**)d));
356 *((char**)d)=(char*)NULL;
357 }
358 return 1;
359 }
360 /*******************************************************************************/
361 /*******************************************************************************/
362 /*******************************************************************************/
363
364 /*******************************************************************************/
365 /********* Multidimensional Arrays Create Via Variable Argument Lists **********/
366 /*******************************************************************************/
367 /*
368 Assumes:
369 specs: Pointer to a valid TElementSpecification structure.
370 depth: Positive integer
371
372 Returns:
373 A pointer to a valid multidimensiona array. The dimensions of the array are
374 determined by depth the variable list of arguments.
375
376 Notes:
377 The return value should be type cast to the appropriate pointer type. The
378 variable list of arguments must be at least of length depth and consist of
379 positive integers.
380 */
381 void* dw_CreateMultidimensionalArrayList(TElementSpecification *specs, int depth, ...)
382 {
383 va_list ap;
384 int i, *dim;
385 void *a=(void*)NULL;
386 if ((dim=(int*)dw_malloc(depth*sizeof(int))))
387 {
388 va_start(ap,depth);
389 for (i=0; i < depth; i++) dim[i]=va_arg(ap,int);
390 va_end(ap);
391 a=dw_CreateMultidimensionalArray(specs,depth,dim);
392 dw_free(dim);
393 }
394 return a;
395 }
396
397 void* dw_CreateMultidimensionalArrayList_string(int depth, ...)
398 {
399 va_list ap;
400 int i, *dim;
401 void *a=(void*)NULL;
402 if ((dim=(int*)dw_malloc(depth*sizeof(int))))
403 {
404 va_start(ap,depth);
405 for (i=0; i < depth; i++) dim[i]=va_arg(ap,int);
406 va_end(ap);
407 a=dw_CreateMultidimensionalArray_string(depth,dim);
408 dw_free(dim);
409 }
410 return a;
411 }
412
413 void* dw_CreateMultidimensionalArrayList_int(int depth, ...)
414 {
415 va_list ap;
416 int i, *dim;
417 void *a=(void*)NULL;
418 if ((dim=(int*)dw_malloc(depth*sizeof(int))))
419 {
420 va_start(ap,depth);
421 for (i=0; i < depth; i++) dim[i]=va_arg(ap,int);
422 va_end(ap);
423 a=dw_CreateMultidimensionalArray_int(depth,dim);
424 dw_free(dim);
425 }
426 return a;
427 }
428
429 void* dw_CreateMultidimensionalArrayList_double(int depth, ...)
430 {
431 va_list ap;
432 int i, *dim;
433 void *a=(void*)NULL;
434 if ((dim=(int*)dw_malloc(depth*sizeof(int))))
435 {
436 va_start(ap,depth);
437 for (i=0; i < depth; i++) dim[i]=va_arg(ap,int);
438 va_end(ap);
439 a=dw_CreateMultidimensionalArray_double(depth,dim);
440 dw_free(dim);
441 }
442 return a;
443 }
444
445 void* dw_CreateMultidimensionalArrayList_float(int depth, ...)
446 {
447 va_list ap;
448 int i, *dim;
449 void *a=(void*)NULL;
450 if ((dim=(int*)dw_malloc(depth*sizeof(int))))
451 {
452 va_start(ap,depth);
453 for (i=0; i < depth; i++) dim[i]=va_arg(ap,int);
454 va_end(ap);
455 a=dw_CreateMultidimensionalArray_float(depth,dim);
456 dw_free(dim);
457 }
458 return a;
459 }
460
461 void* dw_CreateMultidimensionalArrayList_char(int depth, ...)
462 {
463 va_list ap;
464 int i, *dim;
465 void *a=(void*)NULL;
466 if ((dim=(int*)dw_malloc(depth*sizeof(int))))
467 {
468 va_start(ap,depth);
469 for (i=0; i < depth; i++) dim[i]=va_arg(ap,int);
470 va_end(ap);
471 a=dw_CreateMultidimensionalArray_char(depth,dim);
472 dw_free(dim);
473 }
474 return a;
475 }
476 /*******************************************************************************/
477 /*******************************************************************************/
478 /*******************************************************************************/
479
480 /*******************************************************************************/
481 /****************************** Initialize Arrays ******************************/
482 /*******************************************************************************/
483 int dw_InitializeArray(void *a, void *x)
484 {
485 int i, size;
486 if (a)
487 {
488 if (dw_IsArrayA(a))
489 {
490 for (i=dw_DimA(a)-1; i >= 0; i--)
491 if (!dw_InitializeArray(((void**)a)[i],x)) return 0;
492 }
493 else if (dw_UseMemcpyA(a))
494 {
495 for (size=dw_ElementSizeA(a), i=size*(dw_DimA(a)-1); i >= 0; i-=size)
496 memcpy((void*)(((char*)a) + i),x,size);
497 }
498 else if (dw_GetPointerCopyConstructorA(a))
499 {
500 for (i=dw_DimA(a)-1; i >= 0; i--)
501 if (!FullCopyAttempt(((void**)a)+i,x,dw_GetPointerCopyConstructorA(a),dw_GetDestructorA(a))) return 0;
502 }
503 else if (dw_GetStaticCopyConstructorA(a))
504 {
505 for (i=(size=dw_ElementSizeA(a))*(dw_DimA(a)-1); i >= 0; i-=size)
506 if (!dw_GetStaticCopyConstructorA(a)((void*)(((char*)a)+i),x)) return 0;
507 }
508 else
509 return 0;
510 return 1;
511 }
512 return 0;
513 }
514
515 int dw_InitializeArray_int(void *a, int x) { return dw_InitializeArray(a,&x); }
516
517 int dw_InitializeArray_double(void *a, double x) { return dw_InitializeArray(a,&x); }
518
519 int dw_InitializeArray_float(void *a, float x) { return dw_InitializeArray(a,&x); }
520
521 int dw_InitializeArray_char(void *a, char x) { return dw_InitializeArray(a,&x); }
522 /*******************************************************************************/
523 /*******************************************************************************/
524 /*******************************************************************************/
525
526
527 /*******************************************************************************/
528 /**************************** TElementSpecification ****************************/
529 /*******************************************************************************/
530 TElementSpecification* CreateArraySpecification_pointer(void (*destructor)(void *))
531 {
532 TElementSpecification *specs;
533 if ((specs=(TElementSpecification*)dw_malloc(sizeof(TElementSpecification))))
534 {
535 specs->flag=dw_ARRAY_POINTER | dw_ARRAY_DELETE_SPECS;
536 specs->size=sizeof(void*);
537 specs->offset=sizeof(void*)*((sizeof(int)+sizeof(TElementSpecification*)+sizeof(void*)-1)/sizeof(void*)),
538 specs->destructor=destructor;
539 specs->default_constructor=DefaultPointerConstructor;
540 specs->pointer_copy_constructor=NULL;
541 specs->static_copy_constructor=NULL;
542 specs->print_routine=NULL;
543 specs->read_routine=NULL;
544 }
545 return specs;
546 }
547
548 TElementSpecification dw_IntSpecs =
549 {
550 dw_ARRAY_USE_MEMCPY,
551 sizeof(int),
552 sizeof(int)*((sizeof(int)+sizeof(TElementSpecification*)+sizeof(int)-1)/sizeof(int)),
553 NULL,
554 NULL,
555 NULL,
556 NULL,
557 dw_PrintInt,
558 dw_ReadInt
559 };
560
561 TElementSpecification dw_DoubleSpecs =
562 {
563 dw_ARRAY_USE_MEMCPY,
564 sizeof(double),
565 sizeof(double)*((sizeof(int)+sizeof(TElementSpecification*)+sizeof(double)-1)/sizeof(double)),
566 NULL,
567 NULL,
568 NULL,
569 NULL,
570 dw_PrintDouble,
571 dw_ReadDouble
572 };
573
574 TElementSpecification dw_FloatSpecs =
575 {
576 dw_ARRAY_USE_MEMCPY,
577 sizeof(float),
578 sizeof(float)*((sizeof(int)+sizeof(TElementSpecification*)+sizeof(float)-1)/sizeof(float)),
579 NULL,
580 NULL,
581 NULL,
582 NULL,
583 dw_PrintFloat,
584 dw_ReadFloat
585 };
586
587 TElementSpecification dw_CharSpecs =
588 {
589 dw_ARRAY_USE_MEMCPY,
590 sizeof(char),
591 sizeof(char)*((sizeof(int)+sizeof(TElementSpecification*)+sizeof(char)-1)/sizeof(char)),
592 NULL,
593 NULL,
594 NULL,
595 NULL,
596 dw_PrintChar,
597 dw_ReadChar
598 };
599
600 TElementSpecification dw_StringSpecs =
601 {
602 dw_ARRAY_POINTER,
603 sizeof(char*),
604 sizeof(char*)*((sizeof(int)+sizeof(TElementSpecification*)+sizeof(char*)-1)/sizeof(char*)),
605 dw_free,
606 DefaultPointerConstructor,
607 NULL,
608 dw_CopyString,
609 dw_PrintString,
610 NULL
611 };
612
613 TElementSpecification dw_ArraySpecs =
614 {
615 dw_ARRAY_POINTER | dw_ARRAY_ARRAY,
616 sizeof(void*),
617 sizeof(void*)*((sizeof(int)+sizeof(TElementSpecification*)+sizeof(void*)-1)/sizeof(void*)),
618 dw_FreeArray,
619 DefaultPointerConstructor,
620 dw_CopyArray,
621 NULL,
622 dw_PrintArray,
623 dw_ReadArray
624 };
625 /*******************************************************************************/
626 /*******************************************************************************/
627 /*******************************************************************************/