objwriter.cxx

changeset 32
d11a034aabfd
parent 26
54eaea6dc27c
child 33
fd35f6cb5f28
equal deleted inserted replaced
31:ad027ea58097 32:d11a034aabfd
49 #include "bots.h" 49 #include "bots.h"
50 50
51 extern bool g_GotMainLoop; 51 extern bool g_GotMainLoop;
52 52
53 ObjWriter::ObjWriter (str path) { 53 ObjWriter::ObjWriter (str path) {
54 MainBuffer = new DataBuffer;
54 MainLoopBuffer = new DataBuffer; 55 MainLoopBuffer = new DataBuffer;
55 OnEnterBuffer = new DataBuffer; 56 OnEnterBuffer = new DataBuffer;
56
57 numWrittenBytes = 0; 57 numWrittenBytes = 0;
58 fp = fopen (path, "w"); 58 filepath = path;
59 CHECK_FILE (fp, path, "writing");
60 } 59 }
61 60
62 ObjWriter::~ObjWriter () { 61 ObjWriter::~ObjWriter () {
63 fclose (fp); 62 delete MainBuffer;
64 delete MainLoopBuffer; 63 delete MainLoopBuffer;
65 delete OnEnterBuffer; 64 delete OnEnterBuffer;
66 } 65 }
67 66
68 void ObjWriter::WriteString (char* s) { 67 void ObjWriter::WriteString (char* s) {
87 } 86 }
88 87
89 // Write the onenter and mainloop buffers, IN THAT ORDER 88 // Write the onenter and mainloop buffers, IN THAT ORDER
90 for (int i = 0; i < 2; i++) { 89 for (int i = 0; i < 2; i++) {
91 DataBuffer* buf = (!i) ? OnEnterBuffer : MainLoopBuffer; 90 DataBuffer* buf = (!i) ? OnEnterBuffer : MainLoopBuffer;
92 for (unsigned int x = 0; x < buf->writepos; x++) { 91 for (unsigned int x = 0; x < buf->writesize; x++) {
93 unsigned char c = *(buf->buffer+x); 92 unsigned char c = *(buf->buffer+x);
94 Write<unsigned char> (c); 93 Write<unsigned char> (c);
95 } 94 }
96 95
97 // Clear the buffer afterwards for potential next state 96 // Clear the buffer afterwards for potential next state
100 } 99 }
101 100
102 // Next state definitely has no mainloop yet 101 // Next state definitely has no mainloop yet
103 g_GotMainLoop = false; 102 g_GotMainLoop = false;
104 } 103 }
104
105 // Write main buffer to file
106 void ObjWriter::WriteToFile () {
107 fp = fopen (filepath, "w");
108 CHECK_FILE (fp, filepath, "writing");
109
110 if (sizeof (unsigned char) != 1)
111 error ("size of unsigned char isn't 1, but %d!\n", sizeof (unsigned char));
112
113 for (unsigned int x = 0; x < MainBuffer->writesize; x++) {
114 unsigned char c = *(MainBuffer->buffer+x);
115 fwrite (&c, 1, 1, fp);
116 numWrittenBytes ++;
117 }
118
119 printf ("-- %u byte%s written to %s\n", numWrittenBytes, PLURAL (numWrittenBytes), filepath.chars());
120 fclose (fp);
121 }

mercurial