-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacroexp.cpp
More file actions
383 lines (310 loc) · 8.41 KB
/
macroexp.cpp
File metadata and controls
383 lines (310 loc) · 8.41 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
/*******************************************************************
*
* DESCRIPTION: class macroExpansion
*
* AUTHOR: Daniel A. Rodriguez
*
* EMAIL: mailto://drodrigu@dc.uba.ar
*
* DATE: 25/07/1999 (v2)
*
*******************************************************************/
/** include files **/
#include <unistd.h>
#include <cstdlib>
#include "macroexp.h" // class macroExpansion
#include "strutil.h" // string utils
#include <cstring>
#define TAM_BUFFER 32000
#define MACRO_HEADER '#'
#define COMENTARIO_HEADER '%'
#define MACRO_INCLUDE "#INCLUDE"
#define MACRO_MACRO "#MACRO"
#define MACRO_BEGINMACRO "#BEGINMACRO"
#define MACRO_ENDMACRO "#ENDMACRO"
#ifdef __WINDOWS__ // Ugh!!!
#define SEPARATOR '\\'
#define SEPARATOR_STR "\\"
#define SEPARATOR_HEADER ".\\"
#else // We're in Unix !!!!!!!
#define SEPARATOR '/'
#define SEPARATOR_STR "/"
#define SEPARATOR_HEADER "./"
#endif
using namespace std;
macroExpansion *instanceMacroExpansion = NULL;
macroExpansion::macroExpansion(string file)
:fileName(file)
,newFileName("")
{
MASSERTMSG( file != "", "Can't create a MacroExpansion with an invalid filename.\n");
}
char *advanceWhileChar( char *line, char c )
{
while( *line != 0 && *line == c)
line++;
return line;
}
int isStringSimilar( char *line, long posLine, char *cadena )
{
line = advanceWhileChar( line + posLine, ' ' );
if (*line == 0)
return 0;
string sAux(line);
if (upcase(substringLenght(sAux, strlen(cadena))) == cadena)
return 1;
return 0;
}
inline int isStringSimilar( char *line, char *cadena )
{
return isStringSimilar(line, 0, cadena);
}
long advanceMacro(char *line, long posLine)
{
while ( *(line + posLine) != ')' )
posLine++;
return posLine;
}
macroExpansion::~macroExpansion()
{
if (!newFileName.empty())
unlink(newFileName.c_str());
// Hack to avoid deleting twice the same object.
instanceMacroExpansion = NULL;
}
int isInTheRootDirectory(const char *fileName, char separator)
{
register long cant = 0;
while (*fileName != 0)
{
if (*fileName == separator)
cant++;
fileName++;
}
return cant == 1;
}
string getFileName(const char *pathName)
{
// Search for the begginig of the fileName
int posi = strlen(pathName) - 1;
while (pathName[posi] != SEPARATOR)
posi--;
// Found the separator
string sAux = SEPARATOR_STR;
posi++;
while (pathName[posi] != 0)
sAux += pathName[posi++];
return sAux;
}
void includePathEnvironment(string *fileName)
{
if (getenv("TEMP") != NULL)
*fileName = getenv("TEMP") + getFileName(fileName->c_str());
else if (getenv("TMP") != NULL)
*fileName = getenv("TMP") + getFileName(fileName->c_str());
else
*fileName = SEPARATOR_HEADER + getFileName(fileName->c_str());
}
void cambiarSiEstaEnElRaiz(string *fileName)
{
if (isInTheRootDirectory(fileName->c_str(), SEPARATOR))
includePathEnvironment(fileName);
}
string macroExpansion::expand()
{
newFileName = trimSpaces(tmpnam(NULL));
cambiarSiEstaEnElRaiz(&newFileName);
FILE *fileIn, *fileOut;
char line[TAM_BUFFER], oldLine[TAM_BUFFER], auxBuffer[TAM_BUFFER];
register long posiRead, posiAux, maxPosiRead = 0;
register int huboMacro = 0;
string file;
file = trimSpaces(fileName);
fileIn = fopen( file.c_str(), "r" );
MASSERTMSG( fileIn != NULL, "Can't open the file " + fileName );
fileOut = fopen( newFileName.c_str(), "w+" );
MASSERTMSG( fileOut != NULL, "Can't open temporary file." );
while (!feof(fileIn))
{
fgets( line, TAM_BUFFER, fileIn );
maxPosiRead = strlen(line);
MASSERTMSG( maxPosiRead < TAM_BUFFER - 1, "The file '" + fileName + "' has a line too long");
if (!feof(fileIn))
{
strcpy(auxBuffer, ""); // Clear the buffer
posiRead = posiAux = 0;
huboMacro = 0;
if (line[0] == COMENTARIO_HEADER)
;
else if (isInclude(line, posiRead))
addIncludeFile( getData(line, MACRO_INCLUDE) );
else
{
while (posiRead < maxPosiRead)
{
if (line[posiRead] == COMENTARIO_HEADER)
{
posiRead = maxPosiRead;
auxBuffer[posiAux++] = '\n';
}
else if (line[posiRead] == MACRO_HEADER)
{
if (isMacro(line, posiRead))
{
// First: Makes a flush of the auxBuffer
fwrite(auxBuffer, 1, posiAux, fileOut);
posiAux = 0;
// Now: Adds the contents of the macro
putMacroExpansion( fileOut, upcase(getData(line, posiRead, MACRO_MACRO)) );
posiRead = advanceMacro(line, posiRead);
huboMacro = 1;
}
else
// Add the char to auxBuffer
auxBuffer[posiAux++] = line[posiRead];
}
else
// Add the char to auxBuffer
auxBuffer[posiAux++] = line[posiRead];
posiRead++;
}
// Flush the buffer
if (posiAux != 1 || !huboMacro)
fwrite(auxBuffer, 1, posiAux, fileOut);
}
strcpy(oldLine, line);
}
}
if (strcmp(oldLine, line) != 0)
{
strcpy(auxBuffer, ""); // Clear the buffer
posiRead = posiAux = 0;
huboMacro = 0;
if (line[0] == COMENTARIO_HEADER)
;
else if (isInclude(line, posiRead))
addIncludeFile( getData(line, MACRO_INCLUDE) );
else
{
while (posiRead != maxPosiRead)
{
if (line[posiRead] == COMENTARIO_HEADER)
{
posiRead = maxPosiRead;
auxBuffer[posiAux++] = '\n';
}
else if (line[posiRead] == MACRO_HEADER)
{
if (isMacro(line, posiRead))
{
// First: Makes a flush of the auxBuffer
fwrite(auxBuffer, 1, posiAux, fileOut);
posiAux = 0;
// Now: Adds the contents of the macro
putMacroExpansion( fileOut, upcase(getData(line, posiRead, MACRO_MACRO)) );
posiRead = advanceMacro(line, posiRead);
huboMacro = 1;
}
else
// Add the char to auxBuffer
auxBuffer[posiAux++] = line[posiRead];
}
else
// Add the char to auxBuffer
auxBuffer[posiAux++] = line[posiRead];
posiRead++;
}
// Flush the buffer
if (posiAux != 1 || !huboMacro)
fwrite(auxBuffer, 1, posiAux, fileOut);
}
strcpy(oldLine, line);
}
fclose(fileIn);
fclose(fileOut);
return newFileName;
}
char *advanceToChar( char *line, char c )
{
while( *line != 0 && *line != c)
line++;
return line;
}
int macroExpansion::isInclude( char *line )
{
return isStringSimilar( line, MACRO_INCLUDE );
}
int macroExpansion::isInclude( char *line, long posLine )
{
return isStringSimilar( line, posLine, MACRO_INCLUDE );
}
int macroExpansion::isMacro( char *line, long posLine )
{
return isStringSimilar( line, posLine, MACRO_MACRO );
}
int macroExpansion::isBeginOfMacro( char *line )
{
return isStringSimilar( line, MACRO_BEGINMACRO );
}
int macroExpansion::isEndOfMacro( char *line )
{
return isStringSimilar( line, MACRO_ENDMACRO );
}
string macroExpansion::getData( char *line, long posLine, string macroCmd)
{
line = advanceToChar( line + posLine, '(' );
MASSERTMSG( *line != 0, "Can't find the '(' in the " + macroCmd + " statement.");
string sAux("");
line++;
while( *line != 0 && *line != ')')
{
sAux += *line;
line++;
}
MASSERTMSG( *line != 0, "Can't find the ')' in the " + macroCmd + " statement.");
// Now, sAux has the data required
return sAux;
}
void macroExpansion::addIncludeFile( string file )
{
includeFiles.push_back( file );
}
void macroExpansion::putMacroExpansion( FILE *fileOut, string macroName )
{
list<string>::iterator cursor;
for (cursor = includeFiles.begin(); cursor != includeFiles.end(); cursor++)
if (putMacroExpansionInFile( fileOut, macroName, *cursor))
return; // TODO OK
MASSERTMSG( 0, "The macro '" + macroName + "' is undefined.");
}
int macroExpansion::putMacroExpansionInFile( FILE *fileOut, string macroName, string macroFile)
{
FILE *fileMacro;
char macroLine[TAM_BUFFER];
string file(trimSpaces(macroFile));
fileMacro = fopen( file.c_str(), "r");
MASSERTMSG( fileMacro != NULL, "Can't open the macro file '" + macroFile + "'.");
while (!feof(fileMacro))
{
fgets( macroLine, TAM_BUFFER, fileMacro );
if (isBeginOfMacro(macroLine) && upcase(getData(macroLine, MACRO_BEGINMACRO)) == macroName) // This is the macro what i'm looking for
return copyMacro( fileOut, fileMacro, macroLine, macroName );
}
fclose(fileMacro);
return 0; // The macro isn't in this file.
}
int macroExpansion::copyMacro( FILE *fileOut, FILE *fileIn, char *macroLine, string macroName )
{
while (!feof(fileIn))
{
fgets( macroLine, TAM_BUFFER, fileIn );
if (isEndOfMacro( macroLine ))
{
fclose(fileIn);
return 1;
}
fputs( macroLine, fileOut);
}
MASSERTMSG( 0, "Can't find the '#EndMacro' statement for the macro '" + macroName + "'.");
return 0;
}