Give walk command more structured output
This commit is contained in:
+32
-42
@@ -11,9 +11,12 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <map>
|
||||||
#include <ox/std/strops.hpp>
|
#include <ox/std/strops.hpp>
|
||||||
#include <ox/fs/filesystem.hpp>
|
#include <ox/fs/filesystem.hpp>
|
||||||
|
|
||||||
|
#include "toollib.hpp"
|
||||||
|
|
||||||
// suppress warnings about using fopen
|
// suppress warnings about using fopen
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma warning(disable:4996)
|
#pragma warning(disable:4996)
|
||||||
@@ -33,27 +36,6 @@ const static auto usage = "usage:\n"
|
|||||||
"\toxfs walk <FS file>\n"
|
"\toxfs walk <FS file>\n"
|
||||||
"\toxfs version\n";
|
"\toxfs version\n";
|
||||||
|
|
||||||
uint8_t *loadFileBuff(FILE *file, ::size_t *sizeOut = nullptr) {
|
|
||||||
if (file) {
|
|
||||||
fseek(file, 0, SEEK_END);
|
|
||||||
const auto size = ftell(file);
|
|
||||||
rewind(file);
|
|
||||||
auto buff = new uint8_t[size];
|
|
||||||
auto itemsRead = fread(buff, size, 1, file);
|
|
||||||
fclose(file);
|
|
||||||
if (sizeOut) {
|
|
||||||
*sizeOut = itemsRead ? size : 0;
|
|
||||||
}
|
|
||||||
return buff;
|
|
||||||
} else {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t *loadFileBuff(const char *path, ::size_t *sizeOut = nullptr) {
|
|
||||||
return loadFileBuff(fopen(path, "rb"), sizeOut);
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t bytes(const char *str) {
|
size_t bytes(const char *str) {
|
||||||
auto size = ::ox_strlen(str);
|
auto size = ::ox_strlen(str);
|
||||||
const auto lastChar = str[size-1];
|
const auto lastChar = str[size-1];
|
||||||
@@ -371,35 +353,43 @@ int walk(int argc, char **args) {
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int help(int, char**) {
|
||||||
|
cout << usage << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int version(int, char**) {
|
||||||
|
cout << "oxfstool version " << oxfstoolVersion << endl;
|
||||||
|
cout << "oxfs format version " << FileStore16::VERSION << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **args) {
|
int main(int argc, char **args) {
|
||||||
auto err = 0;
|
auto err = 0;
|
||||||
|
map<string, int(*)(int, char**)> cmdMap = {
|
||||||
|
{ "format", format },
|
||||||
|
{ "read", read },
|
||||||
|
{ "write", [](int argc, char **args) { return write(argc, args, false); } },
|
||||||
|
{ "write-expand", [](int argc, char **args) { return write(argc, args, true); } },
|
||||||
|
{ "compact", compact },
|
||||||
|
{ "rm", remove },
|
||||||
|
{ "walk", walk },
|
||||||
|
{ "help", help },
|
||||||
|
{ "version", version },
|
||||||
|
};
|
||||||
|
|
||||||
if (argc > 1) {
|
if (argc > 1) {
|
||||||
auto cmd = args[1];
|
auto cmd = args[1];
|
||||||
if (ox_strcmp(cmd, "format") == 0) {
|
auto f = cmdMap[cmd];
|
||||||
err = format(argc, args);
|
if (f) {
|
||||||
} else if (ox_strcmp(cmd, "read") == 0) {
|
err = f(argc, args);
|
||||||
err = read(argc, args);
|
|
||||||
} else if (ox_strcmp(cmd, "write") == 0) {
|
|
||||||
err = write(argc, args, false);
|
|
||||||
} else if (ox_strcmp(cmd, "write-expand") == 0) {
|
|
||||||
err = write(argc, args, true);
|
|
||||||
} else if (ox_strcmp(cmd, "compact") == 0) {
|
|
||||||
err = compact(argc, args);
|
|
||||||
} else if (ox_strcmp(cmd, "rm") == 0) {
|
|
||||||
err = remove(argc, args);
|
|
||||||
} else if (ox_strcmp(cmd, "walk") == 0) {
|
|
||||||
err = walk(argc, args);
|
|
||||||
} else if (ox_strcmp(cmd, "help") == 0) {
|
|
||||||
printf("%s\n", usage);
|
|
||||||
} else if (ox_strcmp(cmd, "version") == 0) {
|
|
||||||
printf("oxfstool version %s\n", oxfstoolVersion);
|
|
||||||
printf("oxfs format version %d\n", FileStore16::VERSION);
|
|
||||||
} else {
|
} else {
|
||||||
printf("Command '%s' not recognized.\n", cmd);
|
cout << "Command '" << cmd << "' not recognized." << endl;
|
||||||
err = 1;
|
err = 1;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
printf("%s\n", usage);
|
help(argc, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user