| 1 | // |
| 2 | // ACME - a crossassembler for producing 6502/65c02/65816 code. |
| 3 | // Copyright (C) 1998-2006 Marco Baye |
| 4 | // Have a look at "acme.c" for further info |
| 5 | // |
| 6 | // Dynamic buffer stuff |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | #include "acme.h" |
| 12 | #include "global.h" |
| 13 | #include "dynabuf.h" |
| 14 | #include "input.h" |
| 15 | |
| 16 | |
| 17 | // Constants and macros |
| 18 | |
| 19 | // macro to grow dynabuf (CAUTION - fails if a < 1) |
| 20 | #define MAKE_LARGER_THAN(a) (2*(a)) |
| 21 | // if someone requests a dynabuf smaller than this, use this size instead |
| 22 | #define DYNABUF_MINIMUM_INITIALSIZE 128 // should be >0 (see above) |
| 23 | // initial size for global dynabuf |
| 24 | // (as it holds macros, loop bodies, etc., make it large to begin with) |
| 25 | #define GLOBALDYNABUF_INITIALSIZE 1024 // should be >0 (see above) |
| 26 | |
| 27 | |
| 28 | // Variables |
| 29 | dynabuf_t* GlobalDynaBuf; // global dynamic buffer |
| 30 | |
| 31 | |
| 32 | // Functions |
| 33 | |
| 34 | // get new buffer of given size |
| 35 | static void resize(dynabuf_t* db, size_t new_size) { |
| 36 | char* new_buf; |
| 37 | |
| 38 | new_buf = realloc(db->buffer, new_size); |
| 39 | if(new_buf == NULL) |
| 40 | Throw_serious_error(exception_no_memory_left); |
| 41 | db->reserved = new_size; |
| 42 | db->buffer = new_buf; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | // Exported functions |
| 47 | |
| 48 | // Create and init a dynamic buffer and return pointer |
| 49 | dynabuf_t* DynaBuf_create(int initial_size) { |
| 50 | dynabuf_t* db; |
| 51 | |
| 52 | if(initial_size < DYNABUF_MINIMUM_INITIALSIZE) |
| 53 | initial_size = DYNABUF_MINIMUM_INITIALSIZE; |
| 54 | if((db = malloc(sizeof(dynabuf_t)))) { |
| 55 | db->size = 0; |
| 56 | db->reserved = initial_size; |
| 57 | db->buffer = malloc(initial_size); |
| 58 | if(db->buffer) |
| 59 | return(db);// if both pointers are != NULL, no error |
| 60 | } |
| 61 | // otherwise, complain |
| 62 | fputs("Error: No memory for dynamic buffer.\n", stderr); |
| 63 | exit(EXIT_FAILURE); |
| 64 | } |
| 65 | |
| 66 | // Enlarge buffer |
| 67 | void DynaBuf_enlarge(dynabuf_t* db) { |
| 68 | resize(db, MAKE_LARGER_THAN(db->reserved)); |
| 69 | } |
| 70 | |
| 71 | // Claim enough memory to hold a copy of the current buffer contents, |
| 72 | // make that copy and return it. |
| 73 | // The copy must be released by calling free(). |
| 74 | char* DynaBuf_get_copy(dynabuf_t* db) { |
| 75 | char *copy; |
| 76 | |
| 77 | copy = safe_malloc(db->size); |
| 78 | memcpy(copy, db->buffer, db->size); |
| 79 | return(copy); |
| 80 | } |
| 81 | |
| 82 | // add char to buffer |
| 83 | void DynaBuf_append(dynabuf_t* db, char byte) { |
| 84 | DYNABUF_APPEND(db, byte); |
| 85 | } |
| 86 | |
| 87 | // Append string to buffer (without terminator) |
| 88 | void DynaBuf_add_string(dynabuf_t* db, const char* string) { |
| 89 | char byte; |
| 90 | |
| 91 | while((byte = *string++)) |
| 92 | DYNABUF_APPEND(db, byte); |
| 93 | } |
| 94 | |
| 95 | // make sure DynaBuf is large enough to take "size" more bytes |
| 96 | // return pointer to end of current contents |
| 97 | static char* ensure_free_space(dynabuf_t* db, int size) { |
| 98 | while((db->reserved - db->size) < size) |
| 99 | resize(db, MAKE_LARGER_THAN(db->reserved)); |
| 100 | return(db->buffer + db->size); |
| 101 | } |
| 102 | |
| 103 | // add string version of int to buffer (without terminator) |
| 104 | void DynaBuf_add_signed_long(dynabuf_t* db, signed long value) { |
| 105 | char *write = ensure_free_space(db, INTVAL_MAXCHARACTERS + 1); |
| 106 | |
| 107 | db->size += sprintf(write, "%ld", value); |
| 108 | } |
| 109 | |
| 110 | // add string version of float to buffer (without terminator) |
| 111 | void DynaBuf_add_double(dynabuf_t* db, double value) { |
| 112 | char *write = ensure_free_space(db, 40); // reserve 40 chars |
| 113 | |
| 114 | // write up to 30 significant characters. remaining 10 should suffice |
| 115 | // for sign, decimal point, exponent, terminator etc. |
| 116 | db->size += sprintf(write, "%.30g", value); |
| 117 | } |
| 118 | |
| 119 | // Convert buffer contents to lower case (target and source may be identical) |
| 120 | void DynaBuf_to_lower(dynabuf_t* target, dynabuf_t* source) { |
| 121 | char *read, |
| 122 | *write; |
| 123 | |
| 124 | // make sure target can take it |
| 125 | if(source->size > target->reserved) |
| 126 | resize(target, source->size); |
| 127 | // convert to lower case |
| 128 | read = source->buffer;// CAUTION - ptr may change when buf grows! |
| 129 | write = target->buffer;// CAUTION - ptr may change when buf grows! |
| 130 | while(*read) |
| 131 | *write++ = (*read++) | 32; |
| 132 | // Okay, so this method of converting to lowercase is lousy. |
| 133 | // But actually it doesn't matter, because only pre-defined |
| 134 | // keywords are converted, and all of those are plain |
| 135 | // old-fashioned 7-bit ASCII anyway. So I guess it'll do. |
| 136 | *write = '\0'; // terminate |
| 137 | } |
| 138 | |
| 139 | // Initialisation - allocate global dynamic buffer |
| 140 | void DynaBuf_init(void) { |
| 141 | GlobalDynaBuf = DynaBuf_create(GLOBALDYNABUF_INITIALSIZE); |
| 142 | } |