1 /* |
|
2 Copyright 2012-2014 Santeri Piippo |
|
3 All rights reserved. |
|
4 |
|
5 Redistribution and use in source and binary forms, with or without |
|
6 modification, are permitted provided that the following conditions |
|
7 are met: |
|
8 |
|
9 1. Redistributions of source code must retain the above copyright |
|
10 notice, this list of conditions and the following disclaimer. |
|
11 2. Redistributions in binary form must reproduce the above copyright |
|
12 notice, this list of conditions and the following disclaimer in the |
|
13 documentation and/or other materials provided with the distribution. |
|
14 3. The name of the author may not be used to endorse or promote products |
|
15 derived from this software without specific prior written permission. |
|
16 |
|
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
|
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
|
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
|
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
|
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
|
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #ifndef BOTC_OBJWRITER_H |
|
30 #define BOTC_OBJWRITER_H |
|
31 |
|
32 #include <stdio.h> |
|
33 #include <typeinfo> |
|
34 #include <string.h> |
|
35 #include "main.h" |
|
36 #include "str.h" |
|
37 #include "data_buffer.h" |
|
38 |
|
39 class object_writer |
|
40 { |
|
41 public: |
|
42 // ==================================================================== |
|
43 // MEMBERS |
|
44 |
|
45 // Pointer to the file we're writing to |
|
46 FILE* fp; |
|
47 |
|
48 // The main buffer - the contents of this is what we |
|
49 // write to file after parsing is complete |
|
50 data_buffer* MainBuffer; |
|
51 |
|
52 // onenter buffer - the contents of the onenter{} block |
|
53 // is buffered here and is merged further at the end of state |
|
54 data_buffer* OnEnterBuffer; |
|
55 |
|
56 // Mainloop buffer - the contents of the mainloop{} block |
|
57 // is buffered here and is merged further at the end of state |
|
58 data_buffer* MainLoopBuffer; |
|
59 |
|
60 // Switch buffer - switch case data is recorded to this |
|
61 // buffer initially, instead of into main buffer. |
|
62 data_buffer* SwitchBuffer; |
|
63 |
|
64 // How many bytes have we written to file? |
|
65 int numWrittenBytes; |
|
66 |
|
67 // How many references did we resolve in the main buffer? |
|
68 int numWrittenReferences; |
|
69 |
|
70 // ==================================================================== |
|
71 // METHODS |
|
72 object_writer(); |
|
73 void write_string (string s); |
|
74 void write_buffer (data_buffer* buf); |
|
75 void write_member_buffers(); |
|
76 void write_string_table(); |
|
77 void write_to_file (string filepath); |
|
78 data_buffer* get_current_buffer(); |
|
79 |
|
80 int add_mark (string name); |
|
81 int find_byte_mark (string name); |
|
82 int add_reference (int mark); |
|
83 void move_mark (int mark); |
|
84 void offset_mark (int mark, int offset); |
|
85 void delete_mark (int mark); |
|
86 |
|
87 template <class T> void write (T stuff) |
|
88 { |
|
89 get_current_buffer()->write (stuff); |
|
90 } |
|
91 |
|
92 private: |
|
93 // Write given data to file. |
|
94 template <class T> void write_data_to_file (T stuff) |
|
95 { |
|
96 // One byte at a time |
|
97 generic_union<T> uni; |
|
98 uni.as_t = stuff; |
|
99 |
|
100 for (int x = 0; x < sizeof (T); x++) |
|
101 { |
|
102 fwrite (&uni.as_bytes[x], 1, 1, fp); |
|
103 numWrittenBytes++; |
|
104 } |
|
105 } |
|
106 }; |
|
107 |
|
108 #endif // BOTC_OBJWRITER_H |
|