This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
string_stack.cpp
251 lines (172 loc) · 4.56 KB
/
string_stack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "string_stack.hpp"
#include <stdio.h>
#include <string.h>
#ifdef _WIN32
extern int getline(char** lineptr, size_t* n, FILE* stream);
#endif
extern FILE* program;
// resets the object to it's original state
void StrStack::clear(){
for (size_t i = 0; i < stackDepth; i++)
free(*(buffer--));
// set buffer to it's original size
stackHead = buffer = (char**) realloc(buffer, 256);
stackDepth = 0;
}
// doubles the size of the buffer
void StrStack::grow(){
// don't copy the contents of an empty buffer (shouldn't get called in this case...)
if (stackDepth == 0)
stackHead = buffer = (char**) realloc(buffer, ((1 << ++sizeFactor) * 256) * sizeof(char*));
else {
char** oldHead = stackHead;
// make a new buffer twice as big as the old one
char** buffer2 = stackHead = (char**) malloc(((1 << ++sizeFactor) * 256) * sizeof(char*));
// copy all the strings into their new locations
for (ssize_t i = 0; i < buffer - oldHead; i++)
*(buffer2 + i) = *(oldHead + i);
buffer2 += buffer - oldHead ;
// delete the old buffer
free(oldHead);
// replace buffer with buffer2
buffer = buffer2;
}
}
// pushes a line to the top of the stack
void StrStack::push(const char* str){
if (!str)
return;
*buffer = (char*) malloc(strlen(str) + 2);
// write the string to the buffer
strcpy(*buffer, str);
// go to next empty space
buffer++;
// if the size needs to be doubled after adding a new element
if (stackDepth++ == (1u <<sizeFactor) * 256)
grow();
}
// deletes the string at the top of the stack.
void StrStack::pop(){
if (stackDepth != 0) {
// deallocate the line
// & continue to point to the top
free(*(--buffer));
// decrease stack depth
stackDepth--;
}
}
// modify the top element
void StrStack::changeTop(const char* str){
if (stackDepth) {
char*& topStr = *(buffer - 1);
// change the size to fit the new string
topStr = (char*) realloc(topStr, strlen(str) + 1);
strcpy(topStr, str);
}
}
void StrStack::toString(char** dest, size_t* space){
// reallocate memory to fit data
*space = totalLength();
free(*dest);
*dest = (char*) malloc(*space);
//*dest = (char*) realloc((void*) *dest, *space);
//memset(*dest, '-', *space);
// begin copying in data
char** buff = stackHead; // start from bottom of stack
char* cpyto = *dest;
// copy each string into *dest
do {
char* line = *buff;
size_t len = strlen(line); // len of str + \n
strcpy(cpyto, line);
*(cpyto + len + 1) = '\n';
cpyto += len;
} while (++buff < buffer);
}
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
extern FILE* program;
extern unsigned int line;
namespace macro {
static bool endOfStk(char*& str, int16_t& depth) {
// NULL string (safety first)
if (!str)
return false;
bool quoted = false;
// base indentation
if (depth == 0)
return true;
if (!*str)
return false;
if (depth == 1 && *str == '}')
return true;
if (*str == '{')
depth++;
else if (*str == '}')
depth--;
else if (*str == '\"')
quoted = true;
// the line is commented out
else if (*str == '#')
while (*(str + 1) && *(str + 1) != '\n')
str++;
while (depth && *(++str))
if (!quoted && *str == '{')
depth++;
else if (!quoted && *str == '}')
depth--;
else if (!quoted && *str == '#')
while (*(str + 1) && *(str + 1) != '\n') {
str++;
}
else if (*str == '\"') {
if (!(quoted && *(str - 1) == '\\'))
quoted = !quoted;
} else if (*str == '\n')
quoted = false;
return !depth;
}
// note, str is modified and this is used in processLine()
StrStack* getMacro(char*& str, FILE* codeFeed, char*& codeLine){
codeLine = str;
int16_t depth = 1; // shouldn't be >65000 levels of indentation...
StrStack* ret = new StrStack();
bool isEnd = endOfStk(str, depth);
if (!isEnd) {
ret->push(codeLine);
//ret->push("\n");
} else {
*str = '\0';
str++;
ret->push(codeLine);
codeLine = NULL;
return ret;
}
codeLine = (char*) malloc(255);
size_t lineLen = 255;
while (!isEnd) {
if (getline(&codeLine, &lineLen, codeFeed) == -1) {
//free(codeLine);
delete ret;
return NULL; // this signals an error from process_line.hpp
}
str = codeLine;
if (codeFeed == program)
line++; // we added a line to our file
isEnd = endOfStk(str, depth);
if (!isEnd) {
ret->push(codeLine);
//ret->push("\n");
} else {
*str = '\0';
ret->push(codeLine);
//ret->push("\n");
*str = ' ';
//free(codeLine);
return ret;
}
}
//free(codeLine);
delete ret;
return NULL;
}
}