Changed fs::meta_value::to_string() to not be static and not require meta_type (#9510)

No need to pass in the meta_type when using std::visit() on the std::variant
This commit is contained in:
npwoods 2022-04-03 21:38:57 -04:00 committed by GitHub
parent 07a357463b
commit 945cb29e74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 17 deletions

View File

@ -32,20 +32,34 @@ const char *meta_data::entry_name(meta_name name)
return ""; return "";
} }
std::string meta_value::to_string(meta_type type, const meta_value &m) std::string meta_value::to_string() const
{ {
switch(type) { std::string result;
case meta_type::string: return m.as_string();
case meta_type::number: return util::string_format("0x%x", m.as_number()); std::visit([this, &result](auto &&arg)
case meta_type::flag: return m.as_flag() ? "t" : "f"; {
case meta_type::date: { using T = std::decay_t<decltype(arg)>;
auto dt = m.as_date(); if constexpr (std::is_same_v<T, std::string>)
return util::string_format("%04d-%02d-%02d %02d:%02d:%02d", {
dt.year, dt.month, dt.day_of_month, result = as_string();
dt.hour, dt.minute, dt.second); }
} else if constexpr (std::is_same_v<T, uint64_t>)
} {
return std::string(""); result = util::string_format("0x%x", as_number());
}
else if constexpr (std::is_same_v<T, bool>)
{
result = as_flag() ? "t" : "f";
}
else if constexpr (std::is_same_v<T, util::arbitrary_datetime>)
{
auto dt = as_date();
result = util::string_format("%04d-%02d-%02d %02d:%02d:%02d",
dt.year, dt.month, dt.day_of_month,
dt.hour, dt.minute, dt.second);
}
}, value);
return result;
} }
} // namespace fs } // namespace fs

View File

@ -46,7 +46,7 @@ enum class meta_type {
class meta_value { class meta_value {
public: public:
static std::string to_string(meta_type type, const meta_value &m); std::string to_string() const;
static meta_value from_string(meta_type type, std::string value); static meta_value from_string(meta_type type, std::string value);
meta_value() { value = false; } meta_value() { value = false; }

View File

@ -283,7 +283,7 @@ static void dir_scan(u32 depth, fs::filesystem_t::dir_t dir, std::vector<std::ve
if(!meta.has(m.m_name)) if(!meta.has(m.m_name))
continue; continue;
size_t slot = nmap.find(m.m_name)->second; size_t slot = nmap.find(m.m_name)->second;
std::string val = fs::meta_value::to_string(m.m_type, meta.get(m.m_name)); std::string val = meta.get(m.m_name).to_string();
if(slot == 0) if(slot == 0)
val = head + "dir " + val; val = head + "dir " + val;
entries[id][slot] = val; entries[id][slot] = val;
@ -301,7 +301,7 @@ static void dir_scan(u32 depth, fs::filesystem_t::dir_t dir, std::vector<std::ve
if(!meta.has(m.m_name)) if(!meta.has(m.m_name))
continue; continue;
size_t slot = nmap.find(m.m_name)->second; size_t slot = nmap.find(m.m_name)->second;
std::string val = fs::meta_value::to_string(m.m_type, meta.get(m.m_name)); std::string val = meta.get(m.m_name).to_string();
if(slot == 0) if(slot == 0)
val = head + (c.m_type == fs::dir_entry_type::system_file ? "sys " : "file ") + val; val = head + (c.m_type == fs::dir_entry_type::system_file ? "sys " : "file ") + val;
entries[id][slot] = val; entries[id][slot] = val;
@ -323,7 +323,7 @@ static int generic_dir(image_handler &ih)
if(!vmeta.empty()) { if(!vmeta.empty()) {
std::string vinf = "Volume:"; std::string vinf = "Volume:";
for(const auto &e : vmetad) for(const auto &e : vmetad)
vinf += util::string_format(" %s=%s", fs::meta_data::entry_name(e.m_name), fs::meta_value::to_string(e.m_type, vmeta.get(e.m_name))); vinf += util::string_format(" %s=%s", fs::meta_data::entry_name(e.m_name), vmeta.get(e.m_name).to_string());
printf("%s\n\n", vinf.c_str()); printf("%s\n\n", vinf.c_str());
} }