Use custom class instead of std::map

This commit is contained in:
topjohnwu 2021-03-02 23:16:10 -08:00
parent 027ec70262
commit 117ae71025
4 changed files with 33 additions and 28 deletions

View File

@ -607,8 +607,8 @@ void decompress(char *infile, const char *outfile) {
}
void compress(const char *method, const char *infile, const char *outfile) {
auto it = name2fmt.find(method);
if (it == name2fmt.end())
format_t fmt = name2fmt[method];
if (fmt == UNKNOWN)
LOGE("Unknown compression method: [%s]\n", method);
bool in_std = infile == "-"sv;
@ -624,7 +624,7 @@ void compress(const char *method, const char *infile, const char *outfile) {
/* If user does not provide outfile and infile is not
* STDIN, output to <infile>.[ext] */
string tmp(infile);
tmp += fmt2ext[it->second];
tmp += fmt2ext[fmt];
out_fp = xfopen(tmp.data(), "we");
fprintf(stderr, "Compressing to [%s]\n", tmp.data());
rm_in = true;
@ -633,7 +633,7 @@ void compress(const char *method, const char *infile, const char *outfile) {
out_fp = outfile == "-"sv ? stdout : xfopen(outfile, "we");
}
auto strm = get_encoder(it->second, make_unique<fp_stream>(out_fp));
auto strm = get_encoder(fmt, make_unique<fp_stream>(out_fp));
char buf[4096];
size_t len;

View File

@ -1,26 +1,9 @@
#include <string.h>
#include "format.hpp"
std::map<std::string_view, format_t> name2fmt;
Name2Fmt name2fmt;
Fmt2Name fmt2name;
Fmt2Ext fmt2ext;
class FormatInit {
public:
FormatInit() {
name2fmt["gzip"] = GZIP;
name2fmt["xz"] = XZ;
name2fmt["lzma"] = LZMA;
name2fmt["bzip2"] = BZIP2;
name2fmt["lz4"] = LZ4;
name2fmt["lz4_legacy"] = LZ4_LEGACY;
name2fmt["lz4_lg"] = LZ4_LG;
}
};
static FormatInit init;
#define CHECKED_MATCH(s) (len >= (sizeof(s) - 1) && BUFFER_MATCH(buf, s))
format_t check_fmt(const void *buf, size_t len) {
@ -103,3 +86,17 @@ const char *Fmt2Ext::operator[](format_t fmt) {
return "";
}
}
#define CHECK(s, f) else if (name == s) return f;
format_t Name2Fmt::operator[](std::string_view name) {
if (0) {}
CHECK("gzip", GZIP)
CHECK("xz", XZ)
CHECK("lzma", LZMA)
CHECK("bzip2", BZIP2)
CHECK("lz4", LZ4)
CHECK("lz4_legacy", LZ4_LEGACY)
CHECK("lz4_lg", LZ4_LG)
else return UNKNOWN;
}

View File

@ -1,6 +1,5 @@
#pragma once
#include <map>
#include <string_view>
typedef enum {
@ -69,8 +68,13 @@ public:
const char *operator[](format_t fmt);
};
class Name2Fmt {
public:
format_t operator[](std::string_view name);
};
format_t check_fmt(const void *buf, size_t len);
extern std::map<std::string_view, format_t> name2fmt;
extern Name2Fmt name2fmt;
extern Fmt2Name fmt2name;
extern Fmt2Ext fmt2ext;

View File

@ -12,6 +12,12 @@
using namespace std;
static void print_methods() {
for (int fmt = GZIP; fmt < LZOP; ++fmt) {
fprintf(stderr, "%s ", fmt2name[(format_t) fmt]);
}
}
static void usage(char *arg0) {
fprintf(stderr,
R"EOF(MagiskBoot - Boot Image Modification Tool
@ -96,8 +102,7 @@ Supported actions:
<infile>/[outfile] can be '-' to be STDIN/STDOUT
Supported methods: )EOF", arg0);
for (auto &it : name2fmt)
fprintf(stderr, "%s ", it.first.data());
print_methods();
fprintf(stderr, R"EOF(
@ -106,8 +111,7 @@ Supported actions:
<infile>/[outfile] can be '-' to be STDIN/STDOUT
Supported methods: )EOF");
for (auto &it : name2fmt)
fprintf(stderr, "%s ", it.first.data());
print_methods();
fprintf(stderr, "\n\n");
exit(1);