1 #ifndef BOTC_OBJWRITER_H |
|
2 #define BOTC_OBJWRITER_H |
|
3 |
|
4 #include <stdio.h> |
|
5 #include <typeinfo> |
|
6 #include <string.h> |
|
7 #include "main.h" |
|
8 #include "str.h" |
|
9 #include "databuffer.h" |
|
10 |
|
11 class ObjWriter { |
|
12 public: |
|
13 // ==================================================================== |
|
14 // MEMBERS |
|
15 |
|
16 // Pointer to the file we're writing to |
|
17 FILE* fp; |
|
18 |
|
19 // Path to the file we're writing to |
|
20 string filepath; |
|
21 |
|
22 // The main buffer - the contents of this is what we |
|
23 // write to file after parsing is complete |
|
24 DataBuffer* MainBuffer; |
|
25 |
|
26 // onenter buffer - the contents of the onenter{} block |
|
27 // is buffered here and is merged further at the end of state |
|
28 DataBuffer* OnEnterBuffer; |
|
29 |
|
30 // Mainloop buffer - the contents of the mainloop{} block |
|
31 // is buffered here and is merged further at the end of state |
|
32 DataBuffer* MainLoopBuffer; |
|
33 |
|
34 // Switch buffer - switch case data is recorded to this |
|
35 // buffer initially, instead of into main buffer. |
|
36 DataBuffer* SwitchBuffer; |
|
37 |
|
38 // How many bytes have we written to file? |
|
39 unsigned int numWrittenBytes; |
|
40 |
|
41 // How many references did we resolve in the main buffer? |
|
42 unsigned int numWrittenReferences; |
|
43 |
|
44 // ==================================================================== |
|
45 // METHODS |
|
46 ObjWriter (string path); |
|
47 void WriteString (char* s); |
|
48 void WriteString (const char* s); |
|
49 void WriteString (string s); |
|
50 void WriteBuffer (DataBuffer* buf); |
|
51 void WriteBuffers (); |
|
52 void WriteStringTable (); |
|
53 void WriteToFile (); |
|
54 DataBuffer* GetCurrentBuffer (); |
|
55 |
|
56 unsigned int AddMark (string name); |
|
57 unsigned int FindMark (string name); |
|
58 unsigned int AddReference (unsigned int mark); |
|
59 void MoveMark (unsigned int mark); |
|
60 void OffsetMark (unsigned int mark, int offset); |
|
61 void DeleteMark (unsigned int mark); |
|
62 |
|
63 template <class T> void Write (T stuff) { |
|
64 GetCurrentBuffer ()->Write (stuff); |
|
65 } |
|
66 |
|
67 // Default to word |
|
68 void Write (word stuff) { |
|
69 Write<word> (stuff); |
|
70 } |
|
71 |
|
72 void DoWrite (byte stuff) { |
|
73 Write<byte> (stuff); |
|
74 } |
|
75 |
|
76 private: |
|
77 // Write given data to file. |
|
78 template <class T> void WriteDataToFile (T stuff) { |
|
79 // One byte at a time |
|
80 union_t<T> uni; |
|
81 uni.val = stuff; |
|
82 for (unsigned int x = 0; x < sizeof (T); x++) { |
|
83 fwrite (&uni.b[x], 1, 1, fp); |
|
84 numWrittenBytes++; |
|
85 } |
|
86 } |
|
87 }; |
|
88 |
|
89 #endif // BOTC_OBJWRITER_H |
|