Sun, 26 Jan 2014 23:18:48 +0200
- merged object writer into parser
73
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | /* |
85
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
2 | Copyright 2012-2014 Santeri Piippo |
73
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
3 | All rights reserved. |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
4 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
5 | Redistribution and use in source and binary forms, with or without |
85
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
6 | modification, are permitted provided that the following conditions |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
7 | are met: |
73
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | |
85
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
9 | 1. Redistributions of source code must retain the above copyright |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
10 | notice, this list of conditions and the following disclaimer. |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
11 | 2. Redistributions in binary form must reproduce the above copyright |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
12 | notice, this list of conditions and the following disclaimer in the |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
13 | documentation and/or other materials provided with the distribution. |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
14 | 3. The name of the author may not be used to endorse or promote products |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
15 | derived from this software without specific prior written permission. |
73
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | |
85
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
17 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
18 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
19 | OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
20 | IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
21 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
22 | NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
264a61e9eba0
- use the Skulltag 97c2 license which is slightly differently worded
Teemu Piippo <crimsondusk64@gmail.com>
parents:
75
diff
changeset
|
26 | THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
73
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
27 | */ |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
28 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
29 | #ifndef BOTC_DATABUFFER_H |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
30 | #define BOTC_DATABUFFER_H |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
31 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
32 | #include <stdio.h> |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
33 | #include <string.h> |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
34 | #include "main.h" |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
35 | #include "stringtable.h" |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
36 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
37 | #define MAX_MARKS 512 |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
38 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
39 | // ============================================================================ |
86
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
40 | // data_buffer: A dynamic data buffer. |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
41 | // |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
42 | // Notes: |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
43 | // |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
44 | // - A mark is a "pointer" to a particular position in the bytecode. The actual |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
45 | // permanent position cannot be predicted in any way or form, thus these things |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
46 | // are used to "mark" a position like that for future use. |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
47 | // |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
48 | // - A reference is another "mark" that references a mark. When the bytecode |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
49 | // is written to file, they are changed into their marks' current |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
50 | // positions. Marks themselves are never written to files, only refs are |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
51 | // |
73
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | class data_buffer |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | { |
86
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
54 | PROPERTY (private, byte*, buffer, NO_OPS, STOCK_WRITE) |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
55 | PROPERTY (private, int, allocated_size, NUM_OPS, STOCK_WRITE) |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
56 | PROPERTY (private, byte*, writepos, NO_OPS, STOCK_WRITE) |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
57 | PROPERTY (private, list<byte_mark*>, marks, LIST_OPS, STOCK_WRITE) |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
58 | PROPERTY (private, list<mark_reference*>, refs, LIST_OPS, STOCK_WRITE) |
73
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | |
86
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
60 | public: |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
61 | data_buffer (int size = 128); |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
62 | ~data_buffer(); |
73
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
63 | |
86
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
64 | // ==================================================================== |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
65 | // Merge another data buffer into this one. |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
66 | // Note: @other is destroyed in the process! |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
67 | void merge_and_destroy (data_buffer* other); |
73
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
68 | |
86
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
69 | // Clones this databuffer to a new one and returns it. |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
70 | data_buffer* clone (); |
73
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
71 | |
86
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
72 | byte_mark* add_mark (string name); |
73
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
73 | |
86
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
74 | mark_reference* add_reference (byte_mark* mark, bool write_placeholder = true); |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
75 | void check_space (int bytes); |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
76 | void delete_mark (int marknum); |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
77 | void adjust_mark(byte_mark* mark); |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
78 | void offset_mark (byte_mark* mark, int offset); |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
79 | byte_mark* find_mark_by_name (const string& target); |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
80 | void dump(); |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
81 | void write_float (float a); |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
82 | void write_string_index (const string& a); |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
83 | void write_string (const string& a); |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
84 | void write_byte (int8_t data); |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
85 | void write_word (int16_t data); |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
86 | void write_dword (int32_t data); |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
87 | void copy_buffer (const data_buffer* buf); |
73
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
88 | |
86
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
89 | inline int get_write_size() const |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
90 | { |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
91 | return m_writepos - get_buffer(); |
43fe4be38a58
- merged object writer into parser
Teemu Piippo <crimsondusk64@gmail.com>
parents:
85
diff
changeset
|
92 | } |
73
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
93 | }; |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
94 | |
1ee9b312dc18
- *hot stuff coming through*
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
95 | #endif // BOTC_DATABUFFER_H |