Skip to content

Commit

Permalink
strict stoll, MCN TOC/QTOC priority, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
superg committed Mar 18, 2023
1 parent bdb8200 commit 9039765
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 11 deletions.
34 changes: 32 additions & 2 deletions common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ std::vector<std::pair<int32_t, int32_t>> string_to_ranges(const std::string &str

std::string s;
std::getline(range_ss, s, '-');
r.first = stoi(s);
r.first = stoll_strict(s);
std::getline(range_ss, s, '-');
r.second = stoi(s) + 1;
r.second = stoll_strict(s) + 1;

ranges.push_back(r);
}
Expand Down Expand Up @@ -176,4 +176,34 @@ std::string track_extract_basename(std::string str)
return basename;
}


long long stoll_strict(const std::string &str)
{
size_t idx = 0;
long long number = std::stoll(str, &idx);

// suboptimal but at least something
if(idx != str.length())
throw std::invalid_argument("invalid stol argument");

return number;
}


bool stoll_try(long long &value, const std::string &str)
{
bool success = true;

try
{
value = stoll_strict(str);
}
catch(...)
{
success = false;
}

return success;
}

}
2 changes: 2 additions & 0 deletions common.hh
Original file line number Diff line number Diff line change
Expand Up @@ -288,5 +288,7 @@ std::string ranges_to_string(const std::vector<std::pair<int32_t, int32_t>> &ran
const std::pair<int32_t, int32_t> *inside_range(int32_t lba, const std::vector<std::pair<int32_t, int32_t>> &ranges);
std::string system_date_time(std::string fmt);
std::string track_extract_basename(std::string str);
long long stoll_strict(const std::string &str);
bool stoll_try(long long &value, const std::string &str);

}
2 changes: 1 addition & 1 deletion image_browser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ std::list<std::shared_ptr<ImageBrowser::Entry>> ImageBrowser::Entry::Entries()
auto s = identifier.find((char)iso9660::Characters::SEPARATOR2);
std::string name(s == std::string::npos ? identifier : identifier.substr(0, s));

uint32_t version(s == std::string::npos ? 1 : std::stoi(identifier.substr(s + 1)));
uint32_t version(s == std::string::npos ? 1 : stoll_strict(identifier.substr(s + 1)));

// entries.push_back(std::make_shared<Entry>(name, version, dr, _ifs));
entries.push_back(std::shared_ptr<Entry>(new Entry(_browser, name, version, dr)));
Expand Down
2 changes: 1 addition & 1 deletion options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ Options::Options(int argc, const char *argv[])
}
else if(i_value != nullptr)
{
*i_value = std::stoi(o, nullptr, 0);
*i_value = stoll_strict(o);
i_value = nullptr;
}
else
Expand Down
2 changes: 1 addition & 1 deletion tests/tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ bool test_unscramble()
if(tokens[1] == "null")
lba_ptr = nullptr;
else
*lba_ptr = std::stol(tokens[1]);
*lba_ptr = stoll_strict(tokens[1]);
bool scrambled = tokens[2] == "pass";
bool unscrambled = scrambler.Descramble(sector.data(), lba_ptr, sector.size());

Expand Down
34 changes: 28 additions & 6 deletions toc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const char TOC::_ISRC_TABLE[] =


TOC::TOC(const std::vector<uint8_t> &toc_buffer, bool full_toc)
: _qtoc(false)
{
if(full_toc)
InitFullTOC(toc_buffer);
Expand All @@ -34,6 +35,7 @@ TOC::TOC(const std::vector<uint8_t> &toc_buffer, bool full_toc)


TOC::TOC(const ChannelQ *subq, uint32_t sectors_count, int32_t lba_start)
: _qtoc(true)
{
bool track_active = false;
for(uint32_t lba_index = 0; lba_index < sectors_count; ++lba_index)
Expand Down Expand Up @@ -289,6 +291,8 @@ void TOC::UpdateMCN(const ChannelQ *subq, uint32_t sectors_count)
{
for(uint32_t i = 0; i < sizeof(Q.mode2.mcn); ++i)
mcn += fmt::format("{:02}", bcd_decode(Q.mode2.mcn[i]));

// remove trailing zero
mcn.pop_back();
}
break;
Expand Down Expand Up @@ -590,11 +594,18 @@ std::ostream &TOC::PrintCUE(std::ostream &os, const std::string &image_name, uin
{
bool multisession = sessions.size() > 1;

std::string mcn_print(mcn);
if(mcn_print.empty() && cd_text_index < cd_text.size() && !cd_text[cd_text_index].mcn_isrc.empty())
mcn_print = "0" + cd_text[cd_text_index].mcn_isrc;
std::string mcn_print(GetMCN(mcn, cd_text, cd_text_index));

// make sure to prepend 0 to 12-digit MCN, total length should always be 13 digits
{
long long mcn_value;
if(mcn_print.length() == 12 && stoll_try(mcn_value, mcn_print))
mcn_print = fmt::format("{:013}", mcn_value);
}

if(!mcn_print.empty())
os << fmt::format("CATALOG {}", mcn_print) << std::endl;

if(cd_text_index < cd_text.size())
PrintCDTextCUE(os, cd_text[cd_text_index], 0);

Expand Down Expand Up @@ -647,9 +658,7 @@ std::ostream &TOC::PrintCUE(std::ostream &os, const std::string &image_name, uin
if(cd_text_index < t.cd_text.size())
PrintCDTextCUE(os, t.cd_text[cd_text_index], 4);

std::string isrc_print(t.isrc);
if(isrc_print.empty() && cd_text_index < t.cd_text.size() && !t.cd_text[cd_text_index].mcn_isrc.empty())
isrc_print = t.cd_text[cd_text_index].mcn_isrc;
std::string isrc_print(GetMCN(t.isrc, t.cd_text, cd_text_index));
if(!isrc_print.empty())
os << fmt::format(" ISRC {}", isrc_print) << std::endl;

Expand Down Expand Up @@ -865,6 +874,19 @@ TOC::CDText *TOC::GetCDText(uint8_t index, uint8_t track_number)
}


std::string TOC::GetMCN(const std::string &qtoc_mcn, const std::vector<CDText> &toc_cd_text, uint32_t cd_text_index) const
{
std::string toc_mcn(cd_text_index < toc_cd_text.size() ? toc_cd_text[cd_text_index].mcn_isrc : "");

if(qtoc_mcn.empty())
return toc_mcn;
else if(toc_mcn.empty())
return qtoc_mcn;
else
return _qtoc ? qtoc_mcn : toc_mcn;
}


std::string TOC::TrackString(uint8_t track_number) const
{
std::string track_string;
Expand Down
3 changes: 3 additions & 0 deletions toc.hh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ struct TOC

std::vector<Session> sessions;

bool _qtoc;

// supplemental
std::string mcn;
std::vector<CDText> cd_text;
Expand Down Expand Up @@ -117,6 +119,7 @@ private:
static std::ostream &PrintCDTextCUE(std::ostream &os, const CDText &cd_text, uint32_t indent_level);
static bool IsTextPack(PackType pack_type);
CDText *GetCDText(uint8_t index, uint8_t track_number);
std::string GetMCN(const std::string &qtoc_mcn, const std::vector<CDText> &toc_cd_text, uint32_t cd_text_index) const;
uint32_t TrackNumberWidth() const;
std::string DecodeText(const char* text, bool unicode, uint8_t language_code, CharacterCode character_code) const;
std::string DescriptorText(const CD_TEXT_Descriptor &descriptor) const;
Expand Down

0 comments on commit 9039765

Please sign in to comment.