Skip to content

Commit 815e363

Browse files
committed
Added C++ JPAK Class
1 parent 7a25c3c commit 815e363

5 files changed

Lines changed: 158 additions & 10 deletions

File tree

cpp/.cproject

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,24 @@
2828
<tool id="cdt.managedbuild.tool.gnu.cross.c.compiler.1835160734" name="Cross GCC Compiler" superClass="cdt.managedbuild.tool.gnu.cross.c.compiler">
2929
<option defaultValue="gnu.c.optimization.level.none" id="gnu.c.compiler.option.optimization.level.1044706102" name="Optimization Level" superClass="gnu.c.compiler.option.optimization.level" valueType="enumerated"/>
3030
<option id="gnu.c.compiler.option.debugging.level.1073835195" name="Debug Level" superClass="gnu.c.compiler.option.debugging.level" value="gnu.c.debugging.level.max" valueType="enumerated"/>
31-
<option id="gnu.c.compiler.option.misc.pic.431893111" superClass="gnu.c.compiler.option.misc.pic" value="true" valueType="boolean"/>
31+
<option id="gnu.c.compiler.option.misc.pic.431893111" name="Position Independent Code (-fPIC)" superClass="gnu.c.compiler.option.misc.pic" value="true" valueType="boolean"/>
3232
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.263984192" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
3333
</tool>
3434
<tool id="cdt.managedbuild.tool.gnu.cross.cpp.compiler.83800803" name="Cross G++ Compiler" superClass="cdt.managedbuild.tool.gnu.cross.cpp.compiler">
3535
<option id="gnu.cpp.compiler.option.optimization.level.1248909425" name="Optimization Level" superClass="gnu.cpp.compiler.option.optimization.level" value="gnu.cpp.compiler.optimization.level.none" valueType="enumerated"/>
3636
<option id="gnu.cpp.compiler.option.debugging.level.657901764" name="Debug Level" superClass="gnu.cpp.compiler.option.debugging.level" value="gnu.cpp.compiler.debugging.level.max" valueType="enumerated"/>
37-
<option id="gnu.cpp.compiler.option.other.pic.765880724" superClass="gnu.cpp.compiler.option.other.pic" value="true" valueType="boolean"/>
37+
<option id="gnu.cpp.compiler.option.other.pic.765880724" name="Position Independent Code (-fPIC)" superClass="gnu.cpp.compiler.option.other.pic" value="true" valueType="boolean"/>
38+
<option id="gnu.cpp.compiler.option.other.other.2018074473" name="Other flags" superClass="gnu.cpp.compiler.option.other.other" value="-c -fmessage-length=0 -std=c++11" valueType="string"/>
3839
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.1757886321" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
3940
</tool>
4041
<tool id="cdt.managedbuild.tool.gnu.cross.c.linker.248135629" name="Cross GCC Linker" superClass="cdt.managedbuild.tool.gnu.cross.c.linker">
4142
<option defaultValue="true" id="gnu.c.link.option.shared.1111004342" name="Shared (-shared)" superClass="gnu.c.link.option.shared" valueType="boolean"/>
4243
</tool>
4344
<tool id="cdt.managedbuild.tool.gnu.cross.cpp.linker.660083193" name="Cross G++ Linker" superClass="cdt.managedbuild.tool.gnu.cross.cpp.linker">
4445
<option defaultValue="true" id="gnu.cpp.link.option.shared.2089636387" name="Shared (-shared)" superClass="gnu.cpp.link.option.shared" valueType="boolean"/>
46+
<option id="gnu.cpp.link.option.libs.801443669" superClass="gnu.cpp.link.option.libs" valueType="libs">
47+
<listOptionValue builtIn="false" srcPrefixMapping="" srcRootPath="" value="jsoncpp"/>
48+
</option>
4549
<inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.1093083765" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input">
4650
<additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
4751
<additionalInput kind="additionalinput" paths="$(LIBS)"/>

cpp/.project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<projectDescription>
3-
<name>libjpak</name>
3+
<name>jpaktool</name>
44
<comment></comment>
55
<projects>
66
</projects>

cpp/JPAK.cpp

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@
77

88
#include "JPAK.h"
99

10+
/* Extensions to help */
11+
/* I got this from http://stackoverflow.com/a/236803 */
12+
vector<string> &split(const string &s, char delim, vector<string> &elems) {
13+
std::stringstream ss(s);
14+
std::string item;
15+
while (std::getline(ss, item, delim)) {
16+
elems.push_back(item);
17+
}
18+
return elems;
19+
}
20+
21+
22+
vector<string> split(const string &s, char delim) {
23+
vector<string> elems;
24+
split(s, delim, elems);
25+
return elems;
26+
}
27+
28+
/* JPAK Stuff */
1029
JPAK::JPAK() {
1130
ready = false;
1231
}
@@ -17,7 +36,10 @@ JPAK::~JPAK() {
1736
root.clear();
1837
}
1938
bool JPAK::ProcessTable(string &table) {
20-
return reader.parse(table.c_str(), root);
39+
bool ok = reader.parse(table.c_str(), root);
40+
if(!ok)
41+
cout << "Failed to parse table: " << reader.getFormattedErrorMessages() << endl;
42+
return ok;
2143
}
2244
bool JPAK::LoadFromFile(string &filename) {
2345
char magic[5];
@@ -30,15 +52,16 @@ bool JPAK::LoadFromFile(string &filename) {
3052
jpakfile.seekg(0,ios_base::end);
3153
fsize = jpakfile.tellg();
3254
jpakfile.seekg(-4,ios_base::end);
33-
jpakfile >> tableoffset;
55+
jpakfile.read((char *)&tableoffset, 4);
3456
jpakfile.seekg(tableoffset, ios_base::beg);
35-
char table[fsize-tableoffset-4];
57+
char table[fsize-tableoffset-3];
3658
jpakfile.read(table, fsize-tableoffset-4);
59+
table[fsize-tableoffset-4] = 0x00;
3760
string table_s = table;
3861
jpakfile.seekg(0,ios_base::beg);
3962
if(ProcessTable(table_s)) {
4063
ready = true;
41-
return false;
64+
return true;
4265
}else{
4366
cout << "Cannot parse JPAK Table!" << endl;
4467
return false;
@@ -53,6 +76,56 @@ bool JPAK::LoadFromFile(string &filename) {
5376
}
5477
}
5578

56-
void JPAK::GetFile(string &path, char *buffer) {
79+
void JPAK::DecompressFile(char *in, char** out) {
80+
// TODO: Zlib decompressor
81+
}
82+
83+
void JPAK::PrintTree(Json::Value &outroot) {
84+
Json::Value files = outroot["files"];
85+
Json::Value dirs = outroot["directories"];
86+
cout << "Files: " << endl;
87+
for(auto file: files) {
88+
cout << " - " << file["name"] << endl;
89+
}
5790

91+
cout << "Dirs: " << endl;
92+
for(auto dir: dirs) {
93+
cout << " - " << dir["name"] << endl;
94+
PrintTree(dir);
95+
}
96+
}
97+
98+
void JPAK::PrintTree() {
99+
PrintTree(root);
100+
}
101+
102+
Json::Value JPAK::FindFileEntry(vector<string> &path, Json::Value &root) {
103+
if(path.size() == 1) {
104+
return root["files"].get(path[0], JPAK_NOT_FOUND_DATA);
105+
}else{
106+
string nextdir = path[0];
107+
path.erase(path.begin());
108+
if(root["directories"].get(nextdir, JPAK_NOT_FOUND_DATA) != "JPAK_NOT_FOUND_DATA")
109+
return FindFileEntry(path, root["directories"][nextdir]);
110+
else
111+
return Json::Value(JPAK_NOT_FOUND_DATA);
112+
}
113+
}
114+
bool JPAK::GetFile(string &path, char **buffer, int *size) {
115+
vector<string> paths = split(path, '/');
116+
// Here is relative to root, so the first will be blank if starts with "/"
117+
if(paths[0].size() == 0)
118+
paths.erase(paths.begin());
119+
120+
Json::Value fileentry = FindFileEntry(paths, root);
121+
if(fileentry != JPAK_NOT_FOUND_DATA) {
122+
jpakfile.seekg(fileentry["offset"].asInt(), ios_base::beg);
123+
*buffer = new char[fileentry["size"].asInt()];
124+
jpakfile.read(*buffer, fileentry["size"].asInt());
125+
*size = fileentry["size"].asInt();
126+
return true;
127+
}else{
128+
cout << "File not found: " << path << endl;
129+
return false;
130+
}
58131
}

cpp/JPAK.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <fstream>
1717
#include <string>
1818

19+
#define JPAK_NOT_FOUND_DATA "NOT_FOUND_ON_JPAK" // Constant that is returned as Json::Value when not found
20+
1921
using namespace std;
2022
class JPAK {
2123
private:
@@ -29,7 +31,13 @@ class JPAK {
2931
virtual ~JPAK();
3032

3133
bool LoadFromFile(string &);
32-
void GetFile(string &, char *);
34+
bool GetFile(string &, char **, int *);
35+
bool GetFile(const char *path, char **buf, int *size) { string t = path; return GetFile(t,buf,size); };
36+
void DecompressFile(char *, char**);
37+
void PrintTree(Json::Value &);
38+
void PrintTree();
39+
40+
Json::Value FindFileEntry(vector<string> &, Json::Value &);
3341
};
3442

3543
#endif /* JPAK_H_ */

cpp/test.json

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,64 @@
1-
{"files": {"MUSIC.MP3": {"path": "/MUSIC.MP3", "size": 3799296, "name": "MUSIC.MP3", "offset": 50092}, "MUSIC.OGG": {"path": "/MUSIC.OGG", "size": 2125034, "name": "MUSIC.OGG", "offset": 3849388}, "TITLE.JPG": {"path": "/TITLE.JPG", "size": 50079, "name": "TITLE.JPG", "offset": 13}}, "name": "nx20/13A0", "directories": {"CHARTS": {"files": {}, "path": "/CHARTS", "directories": {"DOUBLE": {"files": {"17.NX": {"path": "/CHARTS/DOUBLE/17.NX", "size": 46052, "name": "17.NX", "offset": 6005374}}, "path": "/CHARTS/DOUBLE", "directories": {}, "name": "DOUBLE", "numfiles": 1}, "SINGLE": {"files": {"17.NX": {"path": "/CHARTS/SINGLE/17.NX", "size": 30952, "name": "17.NX", "offset": 5974422}}, "path": "/CHARTS/SINGLE", "directories": {}, "name": "SINGLE", "numfiles": 1}}, "name": "CHARTS", "numfiles": 0}}, "path": "/", "numfiles": 3, "size": 0}
1+
{
2+
"files": {
3+
"MUSIC.MP3": {
4+
"path": "/MUSIC.MP3",
5+
"size": 3799296,
6+
"name": "MUSIC.MP3",
7+
"offset": 50092
8+
},
9+
"MUSIC.OGG": {
10+
"path": "/MUSIC.OGG",
11+
"size": 2125034,
12+
"name": "MUSIC.OGG",
13+
"offset": 3849388
14+
},
15+
"TITLE.JPG": {
16+
"path": "/TITLE.JPG",
17+
"size": 50079,
18+
"name": "TITLE.JPG",
19+
"offset": 13
20+
}
21+
},
22+
"name": "nx20/13A0",
23+
"directories": {
24+
"CHARTS": {
25+
"files": {},
26+
"path": "/CHARTS",
27+
"directories": {
28+
"DOUBLE": {
29+
"files": {
30+
"17.NX": {
31+
"path": "/CHARTS/DOUBLE/17.NX",
32+
"size": 46052,
33+
"name": "17.NX",
34+
"offset": 6005374
35+
}
36+
},
37+
"path": "/CHARTS/DOUBLE",
38+
"directories": {},
39+
"name": "DOUBLE",
40+
"numfiles": 1
41+
},
42+
"SINGLE": {
43+
"files": {
44+
"17.NX": {
45+
"path": "/CHARTS/SINGLE/17.NX",
46+
"size": 30952,
47+
"name": "17.NX",
48+
"offset": 5974422
49+
}
50+
},
51+
"path": "/CHARTS/SINGLE",
52+
"directories": {},
53+
"name": "SINGLE",
54+
"numfiles": 1
55+
}
56+
},
57+
"name": "CHARTS",
58+
"numfiles": 0
59+
}
60+
},
61+
"path": "/",
62+
"numfiles": 3,
63+
"size": 0
64+
}

0 commit comments

Comments
 (0)