forked from GokuMK/TSRE5
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFileBuffer.cpp
168 lines (147 loc) · 4.04 KB
/
FileBuffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* This file is part of TSRE5.
*
* TSRE5 - train sim game engine and MSTS/OR Editors.
* Copyright (C) 2016 Piotr Gadecki <[email protected]>
*
* Licensed under GNU General Public License 3.0 or later.
*
* See LICENSE.md or https://www.gnu.org/licenses/gpl.html
*/
#include "FileBuffer.h"
#include "ReadFile.h"
#include <QFile>
#include <QDebug>
#include <string>
#include <algorithm>
FileBuffer::FileBuffer() {
this->off = 0;
}
FileBuffer::FileBuffer(unsigned char * data, int nLength) {
this->data = data;
this->length = nLength;
this->off = 0;
}
FileBuffer::FileBuffer(const FileBuffer* orig) {
length = orig->length;
data = new unsigned char[length];
std::copy ( orig->data, orig->data+length, data );
}
FileBuffer::~FileBuffer() {
delete[] this->data;
}
int FileBuffer::getInt() {
this->off += 4;
return *((int*) & this->data[this->off - 4]);
}
int FileBuffer::getToken(){
this->off += 4;
return (*((int*) & this->data[this->off - 4]) - this->tokenOffset);
}
void FileBuffer::setTokenOffset(int val){
this->tokenOffset = val;
}
unsigned int FileBuffer::getUint() {
this->off += 4;
return *((unsigned int*) & this->data[this->off - 4]);
}
unsigned short int FileBuffer::getShort() {
this->off += 2;
//return this->data[this->off - 2]*256 + 0;
return *((unsigned short int*) & this->data[this->off - 2]);
}
void FileBuffer::skipBOM(){
if(this->getShort() == 65279)
return;
off -= 2;
return;
}
bool FileBuffer::isBOM(){
if(this->getShort() == 65279){
off -= 2;
return true;
}
off -= 2;
return false;
}
short int FileBuffer::getSignedShort() {
this->off += 2;
//return this->data[this->off - 2]*256 + 0;
return *((short int*) & this->data[this->off - 2]);
}
float FileBuffer::getFloat() {
this->off += 4;
return *(float*) & this->data[this->off - 4];
}
unsigned char FileBuffer::get() {
return this->data[this->off++];
}
QString* FileBuffer::getString(int start, int end) {
QString* s = new QString();
for (int i = start; i < end; i += 2) {
if (data[i] == 13) continue;
*s += QChar(data[i], data[i + 1]);
}
return s;
}
void FileBuffer::findToken(int id) {
int s;
while (length > off) {
s = (int) getInt();
if (s == id)
return;
s = getInt();
off += s;
}
return;
}
void FileBuffer::toUtf16(){
if(isBOM()) return;
qDebug() << "converting to UTF16";
//
unsigned char * newData = new unsigned char[length * 2];
for(int i = 0; i < length; i++){
newData[i*2] = data[i];
newData[i*2+1] = 0;
}
length = length * 2;
delete[] data;
data = newData;
}
bool FileBuffer::insertFile(QString incPath, QString alternativePath, QString* loaded){
int i;
QString sh;
incPath.replace("\\","/");
incPath.replace("//","/");
alternativePath.replace("\\","/");
alternativePath.replace("//","/");
QFile file(incPath);
if (!file.open(QIODevice::ReadOnly)){
if(alternativePath.length() > 0){
incPath = alternativePath;
file.setFileName(incPath);
if (!file.open(QIODevice::ReadOnly)){
qDebug() << incPath << "not exist";
return false;
}
} else {
qDebug() << incPath << "not exist";
return false;
}
}
qDebug() << incPath;
if(loaded != NULL){
*loaded = incPath;
}
FileBuffer* incData = ReadFile::readRAW(&file);
incData->toUtf16();
int remaining = length-off;
unsigned char * newData = new unsigned char[incData->length + remaining ];
memcpy(newData, incData->data, incData->length);
memcpy(newData+incData->length, data+off, remaining);
delete[] data;
data = newData;
length = incData->length + remaining;
off = 0;
skipBOM();
return true;
}