Debian Codegraph @

src/dynabuf.c

raw symbols SCIP
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 #include "dynabuf.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "global.h"
11 #include "input.h"
12
13
14 // Constants and macros
15
16 // macro to grow dynabuf (CAUTION - fails if a < 1)
17 #define MAKE_LARGER_THAN(a) (2 * (a))
18 // if someone requests a dynabuf smaller than this, use this size instead
19 #define DYNABUF_MINIMUM_INITIALSIZE 128 // should be >0 (see above)
20 // initial size for global dynabuf
21 // (as it holds macros, loop bodies, etc., make it large to begin with)
22 #define GLOBALDYNABUF_INITIALSIZE 1024 // should be >0 (see above)
23 // TODO - get rid of this, or move to global.c
24
25
26 // Variables
27 STRUCT_DYNABUF_REF(GlobalDynaBuf, GLOBALDYNABUF_INITIALSIZE); // global dynamic buffer
28 // TODO - get rid of this, or move to global.c
29
30
31 // Functions
32
33 // get new buffer of given size
34 static void resize(struct dynabuf *db, size_t new_size)
35 {
36 char *new_buf;
37
38 //printf("Growing dynabuf to size %d.\n", new_size);
39 new_buf = realloc(db->buffer, new_size);
40 if (new_buf == NULL)
41 throw_serious_error(exception_no_memory_left);
42 db->reserved = new_size;
43 db->buffer = new_buf;
44 }
45 // get buffer mem and fill in struct
46 static void initstruct(struct dynabuf *db, size_t initial_size)
47 {
48 //printf("dynabuf-init: %d.\n", initial_size);
49 if (initial_size < DYNABUF_MINIMUM_INITIALSIZE)
50 initial_size = DYNABUF_MINIMUM_INITIALSIZE;
51 db->size = 0;
52 db->reserved = initial_size;
53 db->buffer = malloc(initial_size);
54 if (db->buffer == NULL) {
55 // scream and die because there is not enough memory
56 fputs("Error: No memory for dynamic buffer.\n", stderr);
57 exit(EXIT_FAILURE);
58 }
59 }
60
61
62 // Exported functions
63
64 // (ensure buffer is ready to use, then) clear dynamic buffer
65 void dynabuf_clear(struct dynabuf *db)
66 {
67 if (db->buffer == NULL)
68 initstruct(db, db->reserved); // get initial buffer
69 db->size = 0; // clear buffer
70 }
71
72 // this gets called by "APPEND" macro whenever buffer is too small
73 void dynabuf_enlarge(struct dynabuf *db)
74 {
75 resize(db, MAKE_LARGER_THAN(db->reserved));
76 }
77
78 // Claim enough memory to hold a copy of the current buffer contents,
79 // make that copy and return it.
80 // The copy must be released by calling free().
81 char *dynabuf_get_copy(struct dynabuf *db)
82 {
83 char *copy;
84
85 copy = safe_malloc(db->size);
86 memcpy(copy, db->buffer, db->size);
87 return copy;
88 }
89
90 // add char to buffer
91 void dynabuf_append(struct dynabuf *db, char byte)
92 {
93 DYNABUF_APPEND(db, byte);
94 }
95
96 // add string to buffer (terminator is added, but not included in "size"!)
97 void dynabuf_add_string(struct dynabuf *db, const char *string)
98 {
99 char byte;
100
101 do {
102 byte = *string++;
103 DYNABUF_APPEND(db, byte);
104 } while (byte);
105 db->size--; // do not consider terminator to be part of content
106 }
107
108 // make sure DynaBuf is large enough to take "size" more bytes
109 // return pointer to end of current contents
110 static char *ensure_free_space(struct dynabuf *db, int size)
111 {
112 while ((db->reserved - db->size) < size)
113 resize(db, MAKE_LARGER_THAN(db->reserved));
114 return db->buffer + db->size;
115 }
116
117 // append byte sequence to buffer
118 void dynabuf_add_bytes(struct dynabuf *db, const char *src, size_t size)
119 {
120 char *target;
121
122 target = ensure_free_space(db, size);
123 memcpy(target, src, size);
124 db->size += size;
125 }
126
127 // add long integer as decimal number to buffer
128 #define NUMBUFSIZE 30 // 21 would be large enough(tm) even for 64bit systems
129 void dynabuf_add_signed_long(struct dynabuf *db, signed long number)
130 {
131 char *target;
132 int added;
133
134 target = ensure_free_space(db, NUMBUFSIZE);
135 #if _BSD_SOURCE || _XOPEN_SOURCE >= 500 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
136 added = snprintf(target, NUMBUFSIZE, "%ld", number);
137 #else
138 added = sprintf(target, "%ld", number);
139 #endif
140 db->size += added;
141 }
142
143 // convert buffer contents to lower case (target and source may be identical)
144 void dynabuf_to_lower(struct dynabuf *target, struct dynabuf *source)
145 {
146 char *read,
147 *write,
148 byte;
149
150 // if target has not been initialised yet, do it now
151 // (do not clear it unconditionally, because it may equal source!)
152 if (target->buffer == NULL)
153 initstruct(target, target->reserved); // get initial buffer
154
155 // make sure target can take it
156 if (source->size > target->reserved)
157 resize(target, source->size);
158 // convert to lower case
159 read = source->buffer; // CAUTION - ptr may change when buf grows!
160 write = target->buffer; // CAUTION - ptr may change when buf grows!
161 while ((byte = *read++)) {
162 // we want to keep underscore, so this check restricts:
163 if (byte <= 'Z')
164 byte |= 32;
165 *write++ = byte;
166 }
167 // Okay, so this method of converting to lowercase is lousy.
168 // But actually it doesn't matter, because only pre-defined
169 // keywords are converted, and all of those are plain
170 // old-fashioned 7-bit ASCII anyway. So I guess it'll do.
171 // FIXME - use BYTE_ macro from global.h
172 *write = '\0'; // terminate
173 }