| 1 | // ACME - a crossassembler for producing 6502/65c02/65816/65ce02 code. |
| 2 | // Copyright (C) 1998-2024 Marco Baye |
| 3 | // Have a look at "acme.c" for further info |
| 4 | // |
| 5 | // Dynamic buffer stuff |
| 6 | #ifndef dynabuf_H |
| 7 | #define dynabuf_H |
| 8 | |
| 9 | |
| 10 | #include "config.h" |
| 11 | #include <stdlib.h> // for size_t |
| 12 | |
| 13 | |
| 14 | // macros |
| 15 | #define DYNABUF_APPEND(db, byte) \ |
| 16 | do { \ |
| 17 | if (db->size == db->reserved) \ |
| 18 | dynabuf_enlarge(db); \ |
| 19 | db->buffer[(db->size)++] = byte;\ |
| 20 | } while (0) |
| 21 | // the next one is dangerous - the buffer location can change when a character |
| 22 | // is appended. So after calling this, don't change the buffer as long as you |
| 23 | // use the address. |
| 24 | #define GLOBALDYNABUF_CURRENT (GlobalDynaBuf->buffer) |
| 25 | |
| 26 | |
| 27 | // dynamic buffer structure |
| 28 | struct dynabuf { |
| 29 | char *buffer; // pointer to buffer |
| 30 | size_t size; // size of buffer's used portion |
| 31 | size_t reserved; // total size of buffer |
| 32 | }; |
| 33 | // new way of declaration/definition: |
| 34 | // the small struct above is static, only the buffer itself gets malloc'd (on |
| 35 | // first "clear"). |
| 36 | #define STRUCT_DYNABUF_REF(name, size) struct dynabuf name[1] = {{NULL, 0, size}} |
| 37 | // the "[1]" makes sure the name refers to the address and not the struct |
| 38 | // itself, so existing code where the name referred to a pointer does not need |
| 39 | // to be changed. |
| 40 | |
| 41 | |
| 42 | // variables |
| 43 | extern struct dynabuf GlobalDynaBuf[1]; // global dynamic buffer |
| 44 | // TODO - get rid of this, or move to global.c |
| 45 | |
| 46 | |
| 47 | // (ensure buffer is ready to use, then) clear dynamic buffer |
| 48 | extern void dynabuf_clear(struct dynabuf *db); |
| 49 | |
| 50 | // this gets called by "APPEND" macro whenever buffer is too small |
| 51 | extern void dynabuf_enlarge(struct dynabuf *db); |
| 52 | |
| 53 | // return malloc'd copy of buffer contents |
| 54 | extern char *dynabuf_get_copy(struct dynabuf *db); |
| 55 | |
| 56 | // add string to buffer (terminator is added, but not included in "size"!) |
| 57 | extern void dynabuf_add_string(struct dynabuf *db, const char *str); |
| 58 | |
| 59 | // add byte sequence to buffer |
| 60 | extern void dynabuf_add_bytes(struct dynabuf *db, const char *src, size_t size); |
| 61 | |
| 62 | // add long integer as decimal number to buffer |
| 63 | extern void dynabuf_add_signed_long(struct dynabuf *db, signed long number); |
| 64 | |
| 65 | // convert buffer contents to lower case |
| 66 | extern void dynabuf_to_lower(struct dynabuf *target, struct dynabuf *source); |
| 67 | |
| 68 | // append char to buffer |
| 69 | extern void dynabuf_append(struct dynabuf *db, char); |
| 70 | |
| 71 | |
| 72 | #endif |