-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.cpp
111 lines (93 loc) · 2.36 KB
/
common.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
#include "common.h"
#include <iostream>
std::string getString(int pos, FILE* f)
{
int t = ftell(f);
fseek(f, pos, SEEK_SET);
std::string ret;
char c;
while((c = fgetc(f)) != '\0')
ret.append(1, c);
fseek(f, t, SEEK_SET);
return ret;
}
void readStringtable(int pos, FILE* f, std::vector<std::string>& dest)
{
long oldPos = ftell(f);
fseek(f, pos, SEEK_SET);
u16 count; fread(&count, 2, 1, f); toWORD(count);
fseek(f, 2, SEEK_CUR); //skip pad bytes
for(int i = 0; i < count; ++i)
{
u16 unknown, stringOffset;
fread(&unknown, 2, 1, f); toWORD(unknown);
fread(&stringOffset, 2, 1, f); toWORD(stringOffset);
std::string s = getString(pos + stringOffset, f);
dest.push_back(s);
}
fseek(f, oldPos, SEEK_SET);
}
void writeStringtable(std::ostream& out, FILE* f, int offset)
{
int p = ftell(f);
fseek(f, offset, SEEK_SET);
u16 count; fread(&count, 2, 1, f); toWORD(count);
fseek(f, 2, SEEK_CUR); //skip pad bytes
out << "String table (" << count << " entries)" << std::endl;
for(int i = 0; i < count; ++i)
{
u16 unknown, stringOffset;
fread(&unknown, 2, 1, f); toWORD(unknown);
fread(&stringOffset, 2, 1, f); toWORD(stringOffset);
std::string s = getString(offset + stringOffset,
f);
out << " 0x" << std::hex << unknown << " - " << s << std::endl;
}
fseek(f, p, SEEK_SET);
}
void splitPath(const std::string& filename, std::string& folder,
std::string& basename)
{
std::string::size_type a = filename.rfind('\\');
std::string::size_type b = filename.rfind('/');
std::string::size_type c;
if(a == std::string::npos)
c = b;
else if(b == std::string::npos)
c = a;
else
c = std::min(a, b);
if(c != std::string::npos)
{
folder = filename.substr(0, c + 1);
basename = filename.substr(c + 1);
}
else
{
folder = "";
basename = filename;
}
}
void splitName(const std::string& basename, std::string& name,
std::string& extension)
{
std::string::size_type a = basename.rfind('.');
if(a != std::string::npos)
{
name = basename.substr(0, a);
extension = basename.substr(a + 1);
}
else
{
name = basename;
extension = "";
}
}
bool doesFileExist(const std::string& fileName)
{
FILE* f = fopen(fileName.c_str(), "rb");
bool ret = f != NULL;
if(f != NULL)
fclose(f);
return ret;
}