-
Notifications
You must be signed in to change notification settings - Fork 0
/
Output.hpp
executable file
·103 lines (93 loc) · 2.75 KB
/
Output.hpp
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
// Output.hpp
//
#if !defined(OUTPUT_HPP)
# define OUTPUT_HPP
# if !defined(BORLAND)
# if !defined(WIN32)
# define GOOD_COMPILER
# endif
# endif
#define CALL_ERROR Output::Err() << "ERROR\n\n"
#define ERR_WHERE ScriptState::getLocation() << std::endl << std::endl
# include <string>
# include <ostream>
# include <stdio.h>
using std::ostream;
using std::string;
////////////////////////////////////////////////////////////////////////////////
// Some script parsing functions
////////////////////////////////////////////////////////////////////////////////
class StackNode {
private:
public:
StackNode(): str(""), line(0), next(NULL) {};
StackNode(string curData, unsigned int curLine) :
str(curData), line(curLine), next(NULL) {};
inline ~StackNode() { if (next) delete next; };
inline StackNode * add(string newStr, unsigned long newLine) {
if (str != "") {
// Assign new StackNode to next and return it
return (next = new StackNode(newStr, newLine));
}
str = newStr; line = newLine;
return this;
}
string str;
unsigned int line;
StackNode *next;
};
class ScriptState {
public:
inline ScriptState(string name):
m_curLine(0), m_scriptName(name), m_parentState(ms_curScriptState),
m_StackTop(NULL) {
ms_curScriptState = this;
m_StackTop = new StackNode;
};
inline ~ScriptState() {
ms_curScriptState = m_parentState;
if (m_StackTop)
delete m_StackTop;
};
inline static unsigned int getCurLine() {
return ms_curScriptState->m_curLine;
};
inline static string getLocation() {
if (ms_curScriptState) return ms_curScriptState->desc();
return "";
};
inline static StackNode * getStackTop() {return ms_curScriptState->m_StackTop;}
inline static void setCurLine(unsigned int curLine) {
ms_curScriptState->m_curLine = curLine;
};
private:
inline string desc() const {
string toReturn = "";
if (m_curLine > 0) {
char buf[20];
sprintf(buf, "%d", m_curLine);
toReturn = " in " + m_scriptName + " at line number " + string(buf);
}
if (m_parentState)
toReturn += "\n\t" + m_parentState->desc();
return toReturn;
}
unsigned int m_curLine;
const string m_scriptName;
ScriptState * const m_parentState;
StackNode * m_StackTop;
static ScriptState * ms_curScriptState;
};
class Output {
private:
static ostream *StdOut;
static ostream *StdErr;
static string CurrentUser;
public:
static inline void setStreams(ostream &out, ostream &err) {
StdOut = &out; StdErr = &err;
}
static inline ostream& Out() { return *StdOut; }
static inline ostream& Err() { return *StdErr; }
};
#endif // OUTPUT_HPP