52 |
52 |
53 class ObjWriter { |
53 class ObjWriter { |
54 public: |
54 public: |
55 // ==================================================================== |
55 // ==================================================================== |
56 // MEMBERS |
56 // MEMBERS |
|
57 |
|
58 // Pointer to the file we're writing to |
57 FILE* fp; |
59 FILE* fp; |
|
60 |
|
61 // Path to the file we're writing to |
58 str filepath; |
62 str filepath; |
|
63 |
|
64 // The main buffer - the contents of this is what we |
|
65 // write to file after parsing is complete |
59 DataBuffer* MainBuffer; |
66 DataBuffer* MainBuffer; |
|
67 |
|
68 // onenter buffer - the contents of the onenter{} block |
|
69 // is buffered here and is merged further at the end of state |
60 DataBuffer* OnEnterBuffer; |
70 DataBuffer* OnEnterBuffer; |
|
71 |
|
72 // Mainloop buffer - the contents of the mainloop{} block |
|
73 // is buffered here and is merged further at the end of state |
61 DataBuffer* MainLoopBuffer; |
74 DataBuffer* MainLoopBuffer; |
|
75 |
|
76 // Switch buffer - switch case data is recorded to this |
|
77 // buffer initially, instead of into main buffer. |
62 DataBuffer* SwitchBuffer; |
78 DataBuffer* SwitchBuffer; |
|
79 |
|
80 // How many bytes have we written to file? |
63 unsigned int numWrittenBytes; |
81 unsigned int numWrittenBytes; |
|
82 |
|
83 // How many references did we resolve in the main buffer? |
64 unsigned int numWrittenReferences; |
84 unsigned int numWrittenReferences; |
65 |
85 |
66 // ==================================================================== |
86 // ==================================================================== |
67 // METHODS |
87 // METHODS |
68 ObjWriter (str path); |
88 ObjWriter (str path); |
81 void MoveMark (unsigned int mark); |
101 void MoveMark (unsigned int mark); |
82 void OffsetMark (unsigned int mark, int offset); |
102 void OffsetMark (unsigned int mark, int offset); |
83 void DeleteMark (unsigned int mark); |
103 void DeleteMark (unsigned int mark); |
84 |
104 |
85 template <class T> void Write (T stuff) { |
105 template <class T> void Write (T stuff) { |
86 DataBuffer* buffer = GetCurrentBuffer (); |
106 GetCurrentBuffer ()->Write<T> (stuff); |
87 buffer->Write<T> (stuff); |
|
88 return; |
|
89 } |
107 } |
90 |
108 |
91 // Default to word |
109 // Default to word |
92 void Write (word stuff) { |
110 void Write (word stuff) { |
93 Write<word> (stuff); |
111 Write<word> (stuff); |
94 } |
112 } |
95 |
113 |
|
114 private: |
|
115 // Write given data to file. |
96 template <class T> void WriteDataToFile (T stuff) { |
116 template <class T> void WriteDataToFile (T stuff) { |
97 // One byte at a time |
117 // One byte at a time |
|
118 union_t<T> uni; |
|
119 uni.val = stuff; |
98 for (unsigned int x = 0; x < sizeof (T); x++) { |
120 for (unsigned int x = 0; x < sizeof (T); x++) { |
99 union_t<T> uni; |
|
100 uni.val = stuff; |
|
101 fwrite (&uni.b[x], 1, 1, fp); |
121 fwrite (&uni.b[x], 1, 1, fp); |
102 numWrittenBytes++; |
122 numWrittenBytes++; |
103 } |
123 } |
104 } |
124 } |
105 }; |
125 }; |