Skip to content

Commit

Permalink
missing DVD profiles added, string functions separated
Browse files Browse the repository at this point in the history
  • Loading branch information
superg committed Dec 8, 2023
1 parent aa703bc commit 5e2549f
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 254 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ jobs:
- name: Customize Software (macOS)
if: ${{ matrix.os == 'macos-12' }}
# ignore brew link warning, revert after https://github.com/actions/setup-python/issues/577 is fixed
run: |
brew update
brew install llvm@17
brew install llvm@17 || true
brew install ninja
brew install cmake
Expand Down
1 change: 1 addition & 0 deletions cd/cd_dump.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import utils.file_io;
import utils.logger;
import utils.misc;
import utils.signal;
import utils.strings;



Expand Down
1 change: 1 addition & 0 deletions cd/toc.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import crc.crc16_gsm;
import scsi.mmc;
import utils.endian;
import utils.misc;
import utils.strings;



Expand Down
1 change: 1 addition & 0 deletions drive.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import utils.endian;
import utils.file_io;
import utils.logger;
import utils.misc;
import utils.strings;



Expand Down
5 changes: 4 additions & 1 deletion dump.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import utils.endian;
import utils.file_io;
import utils.logger;
import utils.misc;
import utils.strings;



Expand Down Expand Up @@ -263,7 +264,9 @@ export bool profile_is_dvd(GET_CONFIGURATION_FeatureCode_ProfileList profile)
|| profile == GET_CONFIGURATION_FeatureCode_ProfileList::DVD_R_DL
|| profile == GET_CONFIGURATION_FeatureCode_ProfileList::DVD_R_DL_LJR
|| profile == GET_CONFIGURATION_FeatureCode_ProfileList::DVD_PLUS_RW
|| profile == GET_CONFIGURATION_FeatureCode_ProfileList::DVD_PLUS_R;
|| profile == GET_CONFIGURATION_FeatureCode_ProfileList::DVD_PLUS_R
|| profile == GET_CONFIGURATION_FeatureCode_ProfileList::DVD_PLUS_RW_DL
|| profile == GET_CONFIGURATION_FeatureCode_ProfileList::DVD_PLUS_R_DL;
}


Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ target_sources(tests
"${CMAKE_SOURCE_DIR}/crc/crc32.ixx"
"${CMAKE_SOURCE_DIR}/utils/file_io.ixx"
"${CMAKE_SOURCE_DIR}/utils/misc.ixx"
"${CMAKE_SOURCE_DIR}/utils/strings.ixx"
)

add_test(NAME tests COMMAND tests WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}")
5 changes: 3 additions & 2 deletions tests/tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import crc.crc16_gsm;
import crc.crc32;
import utils.file_io;
import utils.misc;
import utils.strings;



Expand Down Expand Up @@ -166,7 +167,7 @@ bool test_bcd()

std::cout << std::endl;
}

for(size_t i = 0; i < cases.size(); ++i)
{
std::cout << std::format("bcd_encode: {:3} -> {:02X}... ", cases[i].second, cases[i].first) << std::flush;
Expand All @@ -181,7 +182,7 @@ bool test_bcd()

std::cout << std::endl;
}

return success;
}

Expand Down
252 changes: 11 additions & 241 deletions utils/misc.ixx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,15 @@ module;
#include <cassert>
#include <cmath>
#include <cstdint>
#include <filesystem>
#include <format>
#include <fstream>
#include <functional>
#include <iomanip>
#include <list>
#include <map>
#include <optional>
#include <set>
#include <sstream>
#include <string>
#include "throw_line.hh"

export module utils.misc;

import cd.cd;



namespace gpsxre
Expand Down Expand Up @@ -282,6 +274,17 @@ T digits_count(T value)
}


export template<unsigned int bits, class T>
T sign_extend(T value)
{
// clear extra bits
T v = value & (1 << bits) - 1;

T m = (T)1 << bits - 1;
return (v ^ m) - m;
}


export template<typename T>
bool batch_process_range(const std::pair<T, T> &range, T batch_size, const std::function<bool(T, T)> &func)
{
Expand All @@ -306,228 +309,6 @@ bool batch_process_range(const std::pair<T, T> &range, T batch_size, const std::
}


export std::string normalize_string(const std::string &s)
{
std::string ns;

std::istringstream iss(s);
for(std::string token; std::getline(iss, token, ' '); )
{
if(!token.empty())
ns += token + ' ';
}
if(!ns.empty())
ns.pop_back();

return ns;
}


export std::vector<std::string> tokenize(const std::string &str, const char *delimiters, const char *quotes)
{
std::vector<std::string> tokens;

std::set<char> delimiter;
for(auto d = delimiters; *d != '\0'; ++d)
delimiter.insert(*d);

bool in = false;
std::string::const_iterator s;
for(auto it = str.begin(); it < str.end(); ++it)
{
if(in)
{
// quoted
if(quotes != nullptr && *s == quotes[0])
{
if(*it == quotes[1])
{
++s;
tokens.emplace_back(s, it);
in = false;
}
}
// unquoted
else
{
if(delimiter.find(*it) != delimiter.end())
{
tokens.emplace_back(s, it);
in = false;
}
}
}
else
{
if(delimiter.find(*it) == delimiter.end())
{
s = it;
in = true;
}
}
}

// remaining entry
if(in)
tokens.emplace_back(s, str.end());

return tokens;
}


export std::optional<uint64_t> str_to_uint64(std::string::const_iterator str_begin, std::string::const_iterator str_end)
{
uint64_t value = 0;

bool valid = false;
for(auto it = str_begin; it != str_end; ++it)
{
if(std::isdigit(*it))
{
value = (value * 10) + (*it - '0');
valid = true;
}
else
{
valid = false;
break;
}
}

return valid ? std::make_optional(value) : std::nullopt;
}


export std::optional<uint64_t> str_to_uint64(const std::string &str)
{
return str_to_uint64(str.cbegin(), str.cend());
}


export std::optional<int64_t> str_to_int64(std::string::const_iterator str_begin, std::string::const_iterator str_end)
{
// empty string
auto it = str_begin;
if(it == str_end)
return std::nullopt;

// preserve sign
int64_t negative = 1;
if(*it == '+')
++it;
else if(*it == '-')
{
negative = -1;
++it;
}

if(auto value = str_to_uint64(it, str_end))
return std::make_optional(negative * *value);

return std::nullopt;
}


export std::optional<uint64_t> str_to_int64(const std::string &str)
{
return str_to_int64(str.cbegin(), str.cend());
}


export int64_t str_to_int(const std::string &str)
{
auto value = str_to_int64(str);
if(!value)
throw_line("string is not an integer number ({})", str);

return *value;
}


export std::optional<double> str_to_double(std::string::const_iterator str_begin, std::string::const_iterator str_end)
{
// empty string
auto it = str_begin;
if(it == str_end)
return std::nullopt;

// preserve sign
double negative = 1;
if(*it == '+')
++it;
else if(*it == '-')
{
negative = -1;
++it;
}

auto dot = std::find(it, str_end, '.');

if(auto whole = str_to_uint64(it, dot))
{
if(dot == str_end)
return std::make_optional(negative * *whole);
else
{
if(auto decimal = str_to_uint64(dot + 1, str_end))
{
auto fraction = *decimal / pow(10, digits_count(*decimal));

return std::make_optional(negative * (*whole + fraction));
}
}
}

return std::nullopt;
}

export double str_to_double(const std::string &str)
{
auto value = str_to_double(str.cbegin(), str.cend());
if(!value)
throw_line("string is not a double number ({})", str);

return *value;
}


export std::vector<std::pair<int32_t, int32_t>> string_to_ranges(const std::string &str)
{
std::vector<std::pair<int32_t, int32_t>> ranges;

std::istringstream iss(str);
for(std::string range; std::getline(iss, range, ':'); )
{
std::istringstream range_ss(range);
std::string s;

std::getline(range_ss, s, '-');
uint32_t lba_start = str_to_int(s);

std::getline(range_ss, s, '-');
uint32_t lba_end = str_to_int(s) + 1;

ranges.emplace_back(lba_start, lba_end);
}

return ranges;
}


export std::string ranges_to_string(const std::vector<std::pair<int32_t, int32_t>> &ranges)
{
std::string str;

for(auto const &r : ranges)
str += std::format("{}-{}:", r.first, r.second - 1);

if(!str.empty())
str.pop_back();

return str;
}


export template<typename T>
const std::pair<T, T> *inside_range(T value, const std::vector<std::pair<T, T>> &ranges)
{
Expand All @@ -548,17 +329,6 @@ export std::string system_date_time(std::string fmt)
}


export template<unsigned int bits, class T>
T sign_extend(T value)
{
// clear extra bits
T v = value & (1 << bits) - 1;

T m = (T)1 << bits - 1;
return (v ^ m) - m;
}


export bool number_is_year(uint32_t year)
{
// reasonable unixtime range
Expand Down
Loading

0 comments on commit 5e2549f

Please sign in to comment.