- Treballant en respak2
This commit is contained in:
88
respak2.cpp
Normal file
88
respak2.cpp
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
void printHelp()
|
||||||
|
{
|
||||||
|
cout << "Usage: respak2 <command>" << endl;
|
||||||
|
cout << endl << "Valid commands:" << endl;
|
||||||
|
cout << "-p: Pack all files and folders within the 'data' folder into a file named 'data.jf2'." << endl;
|
||||||
|
cout << "-u: Unpack all files inside the file 'data.jf2' into the 'data' folder." << endl;
|
||||||
|
cout << "-l: List the contents of the 'data.jf2' file." << endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printErrorAndExit(string errorMsg) {
|
||||||
|
cout << errorMsg << endl;
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void printDirectory(string path, int tabs=0)
|
||||||
|
{
|
||||||
|
for (const auto & entry : fs::directory_iterator(path))
|
||||||
|
{
|
||||||
|
for(int i=0;i<tabs;++i) cout << '\t';
|
||||||
|
cout << entry.path().string() << endl;
|
||||||
|
if (entry.is_directory()) printDirectory(entry.path().string(),tabs+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void packDirectory(string path, ofstream fo, uint32_t& num_files, uint32_t& toc_index)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void doPack()
|
||||||
|
{
|
||||||
|
printDirectory("data");
|
||||||
|
|
||||||
|
uint32_t num_files = 4;
|
||||||
|
uint32_t toc_offset = 512;
|
||||||
|
|
||||||
|
ofstream fo;
|
||||||
|
fo.open("data.jf2", ios::binary);
|
||||||
|
if (!fo.is_open()) printErrorAndExit("ERROR: No s'ha pogut obrir l'arxiu 'data.jf2'.");
|
||||||
|
fo.write("PK2",4);
|
||||||
|
fo.write( (char*)&num_files, 4 );
|
||||||
|
fo.write( (char*)&toc_offset, 4 );
|
||||||
|
|
||||||
|
packDirectory("data", fo, num_files, toc_offset);
|
||||||
|
|
||||||
|
fo.seekp(4);
|
||||||
|
fo.write( (char*)&num_files, 4 );
|
||||||
|
fo.write( (char*)&toc_offset, 4 );
|
||||||
|
|
||||||
|
fo.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void doUnpack()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void doList()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
if (argc<2) printHelp();
|
||||||
|
|
||||||
|
string command = argv[1];
|
||||||
|
|
||||||
|
if ( command == "-p" )
|
||||||
|
doPack();
|
||||||
|
else if ( command == "-u" )
|
||||||
|
doUnpack();
|
||||||
|
else if ( command == "-l" )
|
||||||
|
doList();
|
||||||
|
else
|
||||||
|
printHelp();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user