Skip to content

Commit

Permalink
sources reorg: std::popcount, str.ends_with() refactoring, image comm…
Browse files Browse the repository at this point in the history
…on code -> dump.ixx
  • Loading branch information
superg committed Dec 7, 2023
1 parent c6b3a48 commit cee4e26
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 75 deletions.
3 changes: 2 additions & 1 deletion cd/cd_dump.ixx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module;
#include <algorithm>
#include <bit>
#include <cstdint>
#include <filesystem>
#include <format>
Expand Down Expand Up @@ -363,7 +364,7 @@ uint32_t state_from_c2(std::vector<State> &state, const uint8_t *c2_data)
if(c2_quad)
{
state[i] = State::ERROR_C2;
c2_count += bits_count(c2_quad);
c2_count += std::popcount(c2_quad);
}
}

Expand Down
54 changes: 54 additions & 0 deletions dump.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module;
#include <filesystem>
#include <format>
#include <fstream>
#include <list>
#include <map>
#include <memory>
#include <sstream>
Expand Down Expand Up @@ -332,4 +333,57 @@ export int32_t track_offset_by_sync(int32_t lba_start, int32_t lba_end, std::fst
return write_offset;
}


export std::list<std::pair<std::string, bool>> cue_get_entries(const std::filesystem::path &cue_path)
{
std::list<std::pair<std::string, bool>> entries;

std::fstream fs(cue_path, std::fstream::in);
if(!fs.is_open())
throw_line("unable to open file ({})", cue_path.filename().string());

std::pair<std::string, bool> entry;
std::string line;
while(std::getline(fs, line))
{
auto tokens(tokenize(line, " \t", "\"\""));
if(tokens.size() == 3)
{
if(tokens[0] == "FILE")
entry.first = tokens[1];
else if(tokens[0] == "TRACK" && !entry.first.empty())
{
entry.second = tokens[2] != "AUDIO";
entries.push_back(entry);
entry.first.clear();
}
}
}

return entries;
}


//FIXME: just do regexp
export std::string track_extract_basename(std::string str)
{
std::string basename = str;

// strip extension
{
auto pos = basename.find_last_of('.');
if(pos != std::string::npos)
basename = std::string(basename, 0, pos);
}

// strip (Track X)
{
auto pos = str.find(" (Track ");
if(pos != std::string::npos)
basename = std::string(basename, 0, pos);
}

return basename;
}

}
4 changes: 2 additions & 2 deletions dvd/dvd_key.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ std::map<std::string, std::pair<uint32_t, uint32_t>> extract_vob_list(SectorRead
if(e->isDirectory())
continue;

if(ends_with(e->name(), ".VOB"))
if(e->name().ends_with(".VOB"))
titles[e->name()] = std::pair(e->sectorsOffset(), e->sectorsOffset() + e->sectorsSize());
}

Expand Down Expand Up @@ -132,7 +132,7 @@ export void dvd_key(Context &ctx, const Options &options)
{
Disc_READ_Reader reader(*ctx.sptd);
auto vobs = extract_vob_list(&reader);

bool cppm = false;

CSS css(*ctx.sptd);
Expand Down
76 changes: 4 additions & 72 deletions utils/misc.ixx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module;
#include <algorithm>
#include <bit>
#include <cassert>
#include <cmath>
#include <cstdint>
Expand Down Expand Up @@ -94,13 +95,9 @@ void clean_write(T *dst, size_t dst_offset, size_t size, T data)


export template<typename T>
bool is_zeroed(const T *data, uint64_t count)
bool is_zeroed(const T *data, size_t size)
{
for(uint64_t i = 0; i < count; ++i)
if(data[i])
return false;

return true;
return std::all_of(data, data + size, [](T v){ return v == 0; });
}


Expand Down Expand Up @@ -191,25 +188,13 @@ void bit_copy(T *dst, size_t dst_offset, const T *src, size_t src_offset, size_t
}


export template<typename T>
uint32_t bits_count(T value)
{
uint32_t count = 0;

for(; value; ++count)
value &= value - 1;

return count;
}


export template<typename T>
uint64_t bit_diff(const T *data1, const T *data2, uint64_t count)
{
uint64_t diff = 0;

for(uint64_t i = 0; i < count; ++i)
diff += bits_count(data1[i] ^ data2[i]);
diff += std::popcount(data1[i] ^ data2[i]);

return diff;
}
Expand Down Expand Up @@ -563,29 +548,6 @@ export std::string system_date_time(std::string fmt)
}


//FIXME: just do regexp
export std::string track_extract_basename(std::string str)
{
std::string basename = str;

// strip extension
{
auto pos = basename.find_last_of('.');
if(pos != std::string::npos)
basename = std::string(basename, 0, pos);
}

// strip (Track X)
{
auto pos = str.find(" (Track ");
if(pos != std::string::npos)
basename = std::string(basename, 0, pos);
}

return basename;
}


export template<unsigned int bits, class T>
T sign_extend(T value)
{
Expand All @@ -609,34 +571,4 @@ export bool number_is_month(uint32_t month)
return month >= 1 && month <= 12;
}


export std::list<std::pair<std::string, bool>> cue_get_entries(const std::filesystem::path &cue_path)
{
std::list<std::pair<std::string, bool>> entries;

std::fstream fs(cue_path, std::fstream::in);
if(!fs.is_open())
throw_line("unable to open file ({})", cue_path.filename().string());

std::pair<std::string, bool> entry;
std::string line;
while(std::getline(fs, line))
{
auto tokens(tokenize(line, " \t", "\"\""));
if(tokens.size() == 3)
{
if(tokens[0] == "FILE")
entry.first = tokens[1];
else if(tokens[0] == "TRACK" && !entry.first.empty())
{
entry.second = tokens[2] != "AUDIO";
entries.push_back(entry);
entry.first.clear();
}
}
}

return entries;
}

}

0 comments on commit cee4e26

Please sign in to comment.