43 |
43 |
44 #include <stdio.h> |
44 #include <stdio.h> |
45 #include <typeinfo> |
45 #include <typeinfo> |
46 #include <string.h> |
46 #include <string.h> |
47 #include "common.h" |
47 #include "common.h" |
|
48 #include "databuffer.h" |
48 #include "str.h" |
49 #include "str.h" |
49 |
50 |
50 extern int g_CurMode; |
51 extern int g_CurMode; |
51 |
|
52 // ============================================================================ |
|
53 // DataBuffer: A dynamic data buffer. |
|
54 class DataBuffer { |
|
55 public: |
|
56 // The actual buffer |
|
57 unsigned char* buffer; |
|
58 |
|
59 // How big is the buffer? |
|
60 unsigned int bufsize; |
|
61 |
|
62 // Position in the buffer |
|
63 unsigned int writepos; |
|
64 unsigned int readpos; |
|
65 |
|
66 // METHODS |
|
67 DataBuffer (unsigned int size=128) { |
|
68 writepos = 0; |
|
69 readpos = 0; |
|
70 |
|
71 buffer = new unsigned char[size]; |
|
72 bufsize = size; |
|
73 } |
|
74 |
|
75 ~DataBuffer () { |
|
76 delete buffer; |
|
77 } |
|
78 |
|
79 template<class T> void Write(T stuff) { |
|
80 if (sizeof (char) != 1) { |
|
81 error ("DataBuffer: sizeof(char) must be 1!\n"); |
|
82 } |
|
83 |
|
84 // Out of space, must resize |
|
85 if (writepos + sizeof(T) >= bufsize) { |
|
86 // First, store the old buffer temporarily |
|
87 char* copy = new char[bufsize]; |
|
88 printf ("Resizing buffer: copy buffer to safety. %u bytes to copy\n", bufsize); |
|
89 memcpy (copy, buffer, bufsize); |
|
90 |
|
91 // Remake the buffer with the new size. |
|
92 // Have a bit of leeway so we don't have to |
|
93 // resize immediately again. |
|
94 size_t newsize = bufsize + sizeof (T) + 128; |
|
95 delete buffer; |
|
96 buffer = new unsigned char[newsize]; |
|
97 bufsize = newsize; |
|
98 |
|
99 // Now, copy the new stuff over. |
|
100 memcpy (buffer, copy, bufsize); |
|
101 |
|
102 // Nuke the copy now as it's no longer needed |
|
103 delete copy; |
|
104 } |
|
105 |
|
106 // Write the new stuff one byte at a time |
|
107 for (unsigned int x = 0; x < sizeof (T); x++) { |
|
108 buffer[writepos] = CharByte<T> (stuff, x); |
|
109 writepos++; |
|
110 } |
|
111 } |
|
112 |
|
113 template<class T> T Read() { |
|
114 T result = buffer[readpos]; |
|
115 readpos += sizeof (T); |
|
116 return result; |
|
117 } |
|
118 |
|
119 private: |
|
120 template <class T> unsigned char CharByte (T a, unsigned int b) { |
|
121 if (b >= sizeof (T)) |
|
122 error ("CharByte: tried to get byte %u out of a %u-byte %s\n", |
|
123 b, sizeof (T), typeid(T).name()); |
|
124 |
|
125 unsigned long p1 = pow<unsigned long> (256, b); |
|
126 unsigned long p2 = pow<unsigned long> (256, b+1); |
|
127 unsigned long r = (a % p2) / p1; |
|
128 |
|
129 if (r > 256) |
|
130 error ("result %lu too big!", r); |
|
131 |
|
132 unsigned char ur = static_cast<unsigned char> (r); |
|
133 return ur; |
|
134 } |
|
135 }; |
|
136 |
52 |
137 class ObjWriter { |
53 class ObjWriter { |
138 public: |
54 public: |
139 // ==================================================================== |
55 // ==================================================================== |
140 // MEMBERS |
56 // MEMBERS |
141 FILE* fp; |
57 FILE* fp; |
|
58 str filepath; |
|
59 DataBuffer* MainBuffer; |
142 DataBuffer* OnEnterBuffer; |
60 DataBuffer* OnEnterBuffer; |
143 DataBuffer* MainLoopBuffer; |
61 DataBuffer* MainLoopBuffer; |
144 unsigned int numWrittenBytes; |
62 unsigned int numWrittenBytes; |
145 |
63 |
146 // ==================================================================== |
64 // ==================================================================== |
149 ~ObjWriter (); |
67 ~ObjWriter (); |
150 void WriteString (char* s); |
68 void WriteString (char* s); |
151 void WriteString (const char* s); |
69 void WriteString (const char* s); |
152 void WriteString (str s); |
70 void WriteString (str s); |
153 void WriteBuffers (); |
71 void WriteBuffers (); |
|
72 void WriteToFile (); |
154 |
73 |
155 template <class T> void Write (T stuff) { |
74 template <class T> void Write (T stuff) { |
156 // Mainloop and onenter are written into a separate buffer. |
75 DataBuffer* buffer = (g_CurMode == MODE_MAINLOOP) ? MainLoopBuffer : |
157 if (g_CurMode == MODE_MAINLOOP || g_CurMode == MODE_ONENTER) { |
76 (g_CurMode == MODE_ONENTER) ? OnEnterBuffer : |
158 DataBuffer* buffer = (g_CurMode == MODE_MAINLOOP) ? MainLoopBuffer : OnEnterBuffer; |
77 MainBuffer; |
159 buffer->Write<T> (stuff); |
78 buffer->Write<T> (stuff); |
160 return; |
79 return; |
161 } |
|
162 |
|
163 fwrite (&stuff, sizeof (T), 1, fp); |
|
164 numWrittenBytes += sizeof (T); |
|
165 } |
80 } |
|
81 |
166 // Cannot use default arguments in function templates.. |
82 // Cannot use default arguments in function templates.. |
167 void Write (long stuff) {Write<long> (stuff);} |
83 void Write (long stuff) {Write<long> (stuff);} |
168 }; |
84 }; |
169 |
85 |
170 #endif // __OBJWRITER_H__ |
86 #endif // __OBJWRITER_H__ |