1 /* ScummVM Tools 2 * 3 * ScummVM Tools is the legal property of its developers, whose 4 * names are too numerous to list here. Please refer to the 5 * COPYRIGHT file distributed with this source distribution. 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation; either version 2 10 * of the License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 20 */ 21 22 #ifndef UTIL_H 23 #define UTIL_H 24 25 #include "common/scummsys.h" 26 27 /* An excercise in bloated macro writing ;-)*/ 28 29 /*Declare a flexarray type*/ 30 #define FLEXARRAY(type, extra) struct{\ 31 type* data;\ 32 int used, size, itemsize;\ 33 extra\ 34 } 35 36 /* [DJ] Declare a flexarray type without extra fields - Visual C++ complains 37 ** when the FLEXARRAY macro is used and extra parameter is not specified 38 */ 39 #define FLEXARRAY_NOEXTRA(type) struct{\ 40 type* data;\ 41 int used, size, itemsize;\ 42 } 43 44 /*Initialize a flexarray*/ 45 #define FLEXARRAY_INIT(type, array) do {(array).itemsize=sizeof(type); (array).size=0; (array).used=0;} while(0) 46 47 /*Prints an error message and returns (from the function using the macro)*/ 48 #define FLEXARRAY_PANIC(message, errorcode) do{\ 49 fprintf message;\ 50 errorcode;\ 51 } while(0) 52 53 /*Use if the state of the array is incnsistent*/ 54 #define FLEXARRAY_PANIC_BADSTATE(array, errorcode) FLEXARRAY_PANIC((stderr, "PANIC: flexarray "#array" is in an inconsistent state (%d,%d).\n", (array).used, (array).size), errorcode) 55 56 /*Use if memory allocation fails*/ 57 #define FLEXARRAY_PANIC_MEMORY(array, bytes, errorcode) FLEXARRAY_PANIC((stderr, "PANIC: Unable to allocate %d bytes for flexarray"#array"\n", bytes), errorcode) 58 59 /*Allocates an initial array*/ 60 #define FLEXARRAY_ALLOCATE_INITIAL(type, array, errorcode) do{\ 61 (array).data= (type*)sci_malloc(4*(array).itemsize);\ 62 if((array).data==0) FLEXARRAY_PANIC_MEMORY(array, 4*(array).itemsize, errorcode);\ 63 (array).size=4;\ 64 (array).used=0;\ 65 } while(0) 66 67 /*Doubles the size of the allocated area*/ 68 #define FLEXARRAY_RESIZE(type, array, errorcode) do{\ 69 int size=(array).size*2*(array).itemsize;\ 70 (array).data= (type*)sci_realloc((array).data, size);\ 71 if((array).data==0) FLEXARRAY_PANIC_MEMORY(array, size, errorcode);\ 72 (array).size*=2;\ 73 } while(0) 74 75 /*Appends /value/ at the end of the array, resizing as necessary*/ 76 #define FLEXARRAY_APPEND(type, array, value, errorcode) do\ 77 {\ 78 if((array).used>=(array).size)\ 79 {\ 80 if((array).size==0) FLEXARRAY_ALLOCATE_INITIAL(type, array, errorcode);\ 81 else {\ 82 if((array).used==(array).size) FLEXARRAY_RESIZE(type, array, errorcode);\ 83 else FLEXARRAY_PANIC_BADSTATE(array, errorcode);\ 84 }\ 85 }\ 86 (array).data[(array).used++]=(value);\ 87 }while(0) 88 89 /*Adds space for a value at the end of the array, resizing as necessary, but 90 *does not initialize the value*/ 91 #define FLEXARRAY_ADD_SPACE(type, array, items, errorcode) do{\ 92 if((array).used+items>(array).size)\ 93 {\ 94 if((array).size==0) FLEXARRAY_ALLOCATE_INITIAL(type, array, errorcode);\ 95 else if((array).used==(array).size) FLEXARRAY_RESIZE(type, array, errorcode);\ 96 else FLEXARRAY_PANIC_BADSTATE(array, errorcode);\ 97 }\ 98 (array).used++;\ 99 } while(0) 100 101 #endif 102