Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify function and avoid strtok #2793

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 10 additions & 30 deletions app/exiv2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1097,52 +1097,32 @@ int Params::getopt(int argc, char* const Argv[]) {
// local implementations
namespace {
bool parseTime(const std::string& ts, int64_t& time) {
std::string hstr;
std::string mstr;
std::string sstr;
auto cts = new char[ts.length() + 1];
strcpy(cts, ts.c_str());
auto tmp = ::strtok(cts, ":");
if (tmp)
hstr = tmp;
tmp = ::strtok(nullptr, ":");
if (tmp)
mstr = tmp;
tmp = ::strtok(nullptr, ":");
if (tmp)
sstr = tmp;
delete[] cts;

std::istringstream sts(ts);
int sign = 1;
int64_t hh = 0;
int64_t mm = 0;
int64_t ss = 0;

// [-]HH part
if (!Util::strtol(hstr.c_str(), hh))
if (!(sts >> hh))
return false;
if (hh < 0) {
sign = -1;
hh *= -1;
}
// check for the -0 special case
if (hh == 0 && Exiv2::Internal::contains(hstr, '-'))
if (hh == 0 && Exiv2::Internal::contains(ts, '-'))
sign = -1;
// MM part, if there is one
if (!mstr.empty()) {
if (!Util::strtol(mstr.c_str(), mm))
return false;
if (mm > 59)
return false;
if (mm < 0)
if (sts.peek() == ':') {
sts.ignore();
if (!(sts >> mm) || mm > 59 || mm < 0)
return false;
}
// SS part, if there is one
if (!sstr.empty()) {
if (!Util::strtol(sstr.c_str(), ss))
return false;
if (ss > 59)
return false;
if (ss < 0)
if (sts.peek() == ':') {
sts.ignore();
if (!(sts >> ss) || ss > 59 || ss < 0)
return false;
}

Expand Down