src/objwriter.h

changeset 72
03e4d9db3fd9
parent 71
11f23fabf8a6
equal deleted inserted replaced
71:11f23fabf8a6 72:03e4d9db3fd9
1 /* 1 #ifndef BOTC_OBJWRITER_H
2 * botc source code 2 #define BOTC_OBJWRITER_H
3 * Copyright (C) 2012 Santeri `Dusk` Piippo
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. Neither the name of the developer nor the names of its contributors may
15 * be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 * 4. Redistributions in any form must be accompanied by information on how to
18 * obtain complete source code for the software and any accompanying
19 * software that uses the software. The source code must either be included
20 * in the distribution or be available for no more than the cost of
21 * distribution plus a nominal fee, and must be freely redistributable
22 * under reasonable conditions. For an executable file, complete source
23 * code means the source code for all modules it contains. It does not
24 * include source code for modules or files that typically accompany the
25 * major components of the operating system on which the executable file
26 * runs.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
32 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 #ifndef __OBJWRITER_H__
42 #define __OBJWRITER_H__
43 3
44 #include <stdio.h> 4 #include <stdio.h>
45 #include <typeinfo> 5 #include <typeinfo>
46 #include <string.h> 6 #include <string.h>
47 #include "common.h" 7 #include "main.h"
48 #include "str.h" 8 #include "str.h"
49 #include "databuffer.h" 9 #include "databuffer.h"
50 10
51 class ObjWriter { 11 class ObjWriter {
52 public: 12 public:
55 15
56 // Pointer to the file we're writing to 16 // Pointer to the file we're writing to
57 FILE* fp; 17 FILE* fp;
58 18
59 // Path to the file we're writing to 19 // Path to the file we're writing to
60 str filepath; 20 string filepath;
61 21
62 // The main buffer - the contents of this is what we 22 // The main buffer - the contents of this is what we
63 // write to file after parsing is complete 23 // write to file after parsing is complete
64 DataBuffer* MainBuffer; 24 DataBuffer* MainBuffer;
65 25
81 // How many references did we resolve in the main buffer? 41 // How many references did we resolve in the main buffer?
82 unsigned int numWrittenReferences; 42 unsigned int numWrittenReferences;
83 43
84 // ==================================================================== 44 // ====================================================================
85 // METHODS 45 // METHODS
86 ObjWriter (str path); 46 ObjWriter (string path);
87 void WriteString (char* s); 47 void WriteString (char* s);
88 void WriteString (const char* s); 48 void WriteString (const char* s);
89 void WriteString (str s); 49 void WriteString (string s);
90 void WriteBuffer (DataBuffer* buf); 50 void WriteBuffer (DataBuffer* buf);
91 void WriteBuffers (); 51 void WriteBuffers ();
92 void WriteStringTable (); 52 void WriteStringTable ();
93 void WriteToFile (); 53 void WriteToFile ();
94 DataBuffer* GetCurrentBuffer (); 54 DataBuffer* GetCurrentBuffer ();
95 55
96 unsigned int AddMark (str name); 56 unsigned int AddMark (string name);
97 unsigned int FindMark (str name); 57 unsigned int FindMark (string name);
98 unsigned int AddReference (unsigned int mark); 58 unsigned int AddReference (unsigned int mark);
99 void MoveMark (unsigned int mark); 59 void MoveMark (unsigned int mark);
100 void OffsetMark (unsigned int mark, int offset); 60 void OffsetMark (unsigned int mark, int offset);
101 void DeleteMark (unsigned int mark); 61 void DeleteMark (unsigned int mark);
102 template <class T> void DoWrite (const char* func, T stuff) { 62
103 GetCurrentBuffer ()->DoWrite (func, stuff); 63 template <class T> void Write (T stuff) {
64 GetCurrentBuffer ()->Write (stuff);
104 } 65 }
105 66
106 // Default to word 67 // Default to word
107 void DoWrite (const char* func, word stuff) { 68 void Write (word stuff) {
108 DoWrite<word> (func, stuff); 69 Write<word> (stuff);
109 } 70 }
110 71
111 void DoWrite (const char* func, byte stuff) { 72 void DoWrite (byte stuff) {
112 DoWrite<byte> (func, stuff); 73 Write<byte> (stuff);
113 } 74 }
114 75
115 private: 76 private:
116 // Write given data to file. 77 // Write given data to file.
117 template <class T> void WriteDataToFile (T stuff) { 78 template <class T> void WriteDataToFile (T stuff) {
123 numWrittenBytes++; 84 numWrittenBytes++;
124 } 85 }
125 } 86 }
126 }; 87 };
127 88
128 #endif // __OBJWRITER_H__ 89 #endif // BOTC_OBJWRITER_H

mercurial