1 /* 2 tre-stack.h: Stack definitions 3 4 Copyright (c) 2001-2006 Ville Laurikari <vl@iki.fi> 5 6 This library is free software; you can redistribute it and/or 7 modify it under the terms of the GNU Lesser General Public 8 License as published by the Free Software Foundation; either 9 version 2.1 of the License, or (at your option) any later version. 10 11 This library 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 GNU 14 Lesser General Public License for more details. 15 16 You should have received a copy of the GNU Lesser General Public 17 License along with this library; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 19 20 */ 21 22 23 #ifndef TRE_STACK_H 24 #define TRE_STACK_H 1 25 26 #include "regex.h" 27 28 typedef struct tre_stack_rec tre_stack_t; 29 30 /* Creates a new stack object. `size' is initial size in bytes, `max_size' 31 is maximum size, and `increment' specifies how much more space will be 32 allocated with realloc() if all space gets used up. Returns the stack 33 object or NULL if out of memory. */ 34 tre_stack_t * 35 tre_stack_new(int size, int max_size, int increment); 36 37 /* Frees the stack object. */ 38 void 39 tre_stack_destroy(tre_stack_t *s); 40 41 /* Returns the current number of objects in the stack. */ 42 int 43 tre_stack_num_objects(tre_stack_t *s); 44 45 /* Each tre_stack_push_*(tre_stack_t *s, <type> value) function pushes 46 `value' on top of stack `s'. Returns REG_ESPACE if out of memory. 47 This tries to realloc() more space before failing if maximum size 48 has not yet been reached. Returns REG_OK if successful. */ 49 #define declare_pushf(typetag, type) \ 50 reg_errcode_t tre_stack_push_ ## typetag(tre_stack_t *s, type value) 51 52 declare_pushf(voidptr, void *); 53 declare_pushf(int, int); 54 55 /* Each tre_stack_pop_*(tre_stack_t *s) function pops the topmost 56 element off of stack `s' and returns it. The stack must not be 57 empty. */ 58 #define declare_popf(typetag, type) \ 59 type tre_stack_pop_ ## typetag(tre_stack_t *s) 60 61 declare_popf(voidptr, void *); 62 declare_popf(int, int); 63 64 /* Just to save some typing. */ 65 #define STACK_PUSH(s, typetag, value) \ 66 do \ 67 { \ 68 status = tre_stack_push_ ## typetag(s, value); \ 69 } \ 70 while (0) 71 72 #define STACK_PUSHX(s, typetag, value) \ 73 { \ 74 status = tre_stack_push_ ## typetag(s, value); \ 75 if (status != REG_OK) \ 76 break; \ 77 } 78 79 #define STACK_PUSHR(s, typetag, value) \ 80 { \ 81 reg_errcode_t _status; \ 82 _status = tre_stack_push_ ## typetag(s, value); \ 83 if (_status != REG_OK) \ 84 return _status; \ 85 } 86 87 #endif /* TRE_STACK_H */ 88 89 /* EOF */ 90