-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCGFile.h
229 lines (200 loc) · 6.81 KB
/
CGFile.h
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
#ifndef _CGFILE_H_
#define _CGFILE_H_
#include <stdio.h>
#include <assert.h>
#include <iostream>
#include <DS/DSBasicTypes.h>
#include "CGBasicTypes.h"
#define POS_STACK_SIZE 2000
class CGPosStack
{
public:
CGPosStack(void)
{
stack = new CGPos[POS_STACK_SIZE];
assert(stack);
stack_pointer = -1;
}
~CGPosStack(void)
{
delete[] stack;
}
DSResult Push(CGPos elem)
{
if (stack_pointer == POS_STACK_SIZE - 2)
{
assert(DS_FALSE);
return DS_ERROR;
}
stack_pointer++;
stack[stack_pointer] = elem;
return DS_OK;
}
CGPos Pop(void) /* Hole & Entferne oberstes Element */
{
assert(stack_pointer >= 0);
stack_pointer--;
return stack[stack_pointer + 1];
}
CGPos Top(void) /* Hole oberstes Element */
{
assert(stack_pointer >= 0);
return stack[stack_pointer];
}
DSBoolean IsEmpty(void)
{
if (stack_pointer == -1)
return DS_TRUE;
else
return DS_FALSE;
}
DSResult Empty(void)
{
while (!IsEmpty())
{
Pop();
}
return DS_OK;
}
private:
CGPos *stack;
int stack_pointer;
};
/******************************************************************************
* Klasse CGFile
* Zweck: Pretty-Printer zur formatierten Ausgabe von uebergebenen Strings
* sowie Sonderfunktionen wie Einfuegen von Kommentar usw.
******************************************************************************/
class CGFile
{
public:
CGFile(class DSString * = NULL,
class DSString * = NULL,
DSBoolean = DS_FALSE);
CGFile(const char *, const char *, DSBoolean = DS_FALSE);
~CGFile(void);
DSResult CGFileOpen(void);
DSResult CGFileClose(void);
DSResult CGInsertString(const char *, CGPos = 0, CGNewLine = CG_NO_EOL);
DSResult CGInsertString(class DSString *, CGPos = 0, CGNewLine = CG_NO_EOL);
DSResult CGInsertString(class DSString &, CGPos = 0, CGNewLine = CG_NO_EOL);
DSResult CGInsertCardinal(DSCardinal, CGPos = 0, CGNewLine = CG_NO_EOL);
CGPos CGPosition(void);
class DSString *CGFileName(void);
class DSString *CGFilePath(void);
DSResult CGInsertStringComment(const char *, const char *, CGPos = 0);
DSResult CGInsertStringComment(class DSString *, class DSString *, CGPos = 0);
DSResult CGInsertStringComment(class DSString &, class DSString &, CGPos = 0);
DSResult CGInsertHeadline(CGPos = 0);
DSResult CGInsertBaseline(CGPos = 0);
DSResult CGInsertCopyright(void);
DSResult CGInsertHead(const char *, CGPos = 0);
DSResult CGInsertHead(class DSString *, CGPos = 0);
DSResult CGInsertHead(class DSString &, CGPos = 0);
private:
class DSString *CGDivide(class DSString *, CGPos, CGNewLine);
DSResult CGUpdateStack(class DSString *);
DSResult CGEmptyStack(CGPosStack &);
FILE *file_pointer;
class DSString *file_name;
class DSString *file_path;
DSBoolean file_open;
CGPos actual_line_position;
CGPosStack stack;
CGPosStack outstream_stack;
DSBoolean is_in_text;
DSBoolean no_line_break;
DSBoolean pretty_printing;
DSBoolean is_in_comment;
DSBoolean is_in_comment_line;
};
/**************************************************************************
* Makro zum Vereinfachen der Codegenerierung. Erwartet dass
* an der Verwendungsstelle die Variable 'result' vom Typ CGBoolean
* definiert ist.
* 1. Parameter: Codegenerierungsfile vom Type 'CGFile'
* 2. Parameter: String der generiert werden soll
* 3. Parameter: Einrueckungstiefe
* 4. Parameter: Anhaengen von EOL (ja/nein)
**************************************************************************/
#define InsertString(m_file, m_string, m_depth, m_eol)\
if ((result = (m_file).CGInsertString((m_string),\
(m_depth), (m_eol))) != DS_OK)\
{\
std::cerr << "Error while writing on file "\
<< (m_file).CGFilePath()\
<< (m_file).CGFileName()\
<< "." << std::endl;\
return result;\
}
#define InsertIncludeSCL(m_file, m_string, m_depth, m_eol)\
InsertString(m_file, "#include <" CG_PATH_SCL_DIR m_string ">", (m_depth), (m_eol));
#define InsertIncludePEV(m_file, m_string, m_depth, m_eol)\
InsertString(m_file, "#include <" CG_PATH_PEV_DIR m_string ">", (m_depth), (m_eol));
#define InsertIncludeTL(m_file, m_string, m_depth, m_eol)\
InsertString(m_file, "#include <" CG_PATH_TL_DIR m_string ">", (m_depth), (m_eol));
#define InsertCardinal(m_file, m_card, m_depth, m_eol)\
if ((result = (m_file).CGInsertCardinal((m_card),\
(m_depth), (m_eol))) != DS_OK)\
{\
std::cerr << "Error while writing on file "\
<< (m_file).CGFilePath()\
<< (m_file).CGFileName()\
<< "." << std::endl;\
return result;\
}
#define InsertStringComment(m_file, m_string, m_comment, m_depth)\
if ((result = (m_file).CGInsertStringComment((m_string),\
(m_comment),\
(m_depth))) != DS_OK)\
{\
std::cerr << "Error while writing on file "\
<< (m_file).CGFilePath()\
<< (m_file).CGFileName()\
<< "." << std::endl;\
return result;\
}
#define InsertHead(m_file, m_string, m_depth)\
if ((result = (m_file).CGInsertHead((m_string),\
(m_depth))) != DS_OK)\
{\
std::cerr << "Error while writing on file "\
<< (m_file).CGFilePath()\
<< (m_file).CGFileName()\
<< "." << std::endl;\
return result;\
}
#define InsertLine(m_file)\
if ((result = (m_file).CGInsertString("", 0, CG_WITH_EOL)) != DS_OK)\
{\
std::cerr << "Error while writing on file "\
<< (m_file).CGFilePath()\
<< (m_file).CGFileName()\
<< "." << std::endl;\
return result;\
}
#define InsertCopyright(m_file)\
if ((result = (m_file).CGInsertCopyright()) != DS_OK)\
{\
std::cerr << "Error while writing on file "\
<< (m_file).CGFilePath()\
<< (m_file).CGFileName()\
<< "." << std::endl;\
return result;\
}
/*************************************************************/
/* Hilfsmakro MemAssert: Ueberprueft ob allokierter Speicher */
/* erhalten wurde. Falls nicht, wird */
/* die Fehlermeldung CG_OUT_OF_MEMORY */
/* zurueckgegeben. */
/*************************************************************/
#define MemAssert(pointer)\
{\
if (!pointer)\
{\
std::cerr << "Memory allocation fault in file " << __FILE__ << " at line "\
<< __LINE__ << "! Bailing out.";\
return DS_OUT_OF_MEMORY;\
}\
}
#endif