src/object_writer.h

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

mercurial