Skip to content

Commit

Permalink
Fix ASC file lookup in working directory + error handling for lookup_…
Browse files Browse the repository at this point in the history
…file_insensitive
  • Loading branch information
piernov committed May 27, 2022
1 parent 29e1183 commit 64d1db2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
30 changes: 24 additions & 6 deletions src/openboardview/FileFormats/ASCFile.cpp
Expand Up @@ -145,18 +145,36 @@ void ASCFile::update_counts() {
num_nails = nails.size();
}

bool ASCFile::load_and_parse(const filesystem::path &path, const std::string &filename, void (ASCFile::*parser)(char *&, char *&, char *&, char *&, line_iterator_t&)) {
auto filepath = lookup_file_insensitive(path, filename, error_msg);
if (filepath.empty() || !error_msg.empty()) {
return false;
}
return read_asc(filepath, parser);
}

/*
* buf unused for now, read all files even if one of the supported *.asc was
* passed
*/
ASCFile::ASCFile(std::vector<char> &buf, const filesystem::path &filepath) {
char *saved_locale;
saved_locale = setlocale(LC_NUMERIC, "C"); // Use '.' as delimiter for strtod
std::error_code ec;
auto directory = filesystem::weakly_canonical(filepath, ec);
if (ec) {
error_msg = ec.message();
return;
}
directory = directory.parent_path();

char *saved_locale = setlocale(LC_NUMERIC, "C"); // Use '.' as delimiter for strtod

valid = true;
if (!read_asc(lookup_file_insensitive(filepath.parent_path(), "format.asc"), &ASCFile::parse_format)) valid = false;
if (!read_asc(lookup_file_insensitive(filepath.parent_path(), "pins.asc"), &ASCFile::parse_pin)) valid = false;
if (!read_asc(lookup_file_insensitive(filepath.parent_path(), "nails.asc"), &ASCFile::parse_nail)) valid = false;
if (!load_and_parse(directory, "format.asc", &ASCFile::parse_format)
|| !load_and_parse(directory, "pins.asc", &ASCFile::parse_pin)
|| !load_and_parse(directory, "nails.asc", &ASCFile::parse_nail)) {
valid = false;
} else {
valid = true;
}

update_counts();
setlocale(LC_NUMERIC, saved_locale); // Restore locale
Expand Down
1 change: 1 addition & 0 deletions src/openboardview/FileFormats/ASCFile.h
Expand Up @@ -19,6 +19,7 @@ class ASCFile : public BRDFile {
void parse_pin(char *&p, char *&s, char *&arena, char *&arena_end, line_iterator_t &line_it);
void parse_nail(char *&p, char *&s, char *&arena, char *&arena_end, line_iterator_t &line_it);
bool read_asc(const filesystem::path &filepath, void (ASCFile::*parser)(char *&, char *&, char *&, char *&, line_iterator_t&));
bool load_and_parse(const filesystem::path &path, const std::string &filename, void (ASCFile::*parser)(char *&, char *&, char *&, char *&, line_iterator_t&));
void update_counts();

protected:
Expand Down
11 changes: 10 additions & 1 deletion src/openboardview/utils.cpp
Expand Up @@ -68,7 +68,16 @@ bool compare_string_insensitive(const std::string &str1, const std::string &str2
}

// Case insensitive lookup of a filename at the given path
filesystem::path lookup_file_insensitive(const filesystem::path &path, const std::string &filename) {
filesystem::path lookup_file_insensitive(const filesystem::path &path, const std::string &filename, std::string &error_msg) {
std::error_code ec;
filesystem::directory_iterator di{path, ec};

if (ec) {
error_msg = "Error looking up '" + filename + "' in '" + path.string().c_str() + "': " + ec.message();
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Error looking up '%s' in '%s': %d - %s", filename.c_str(), path.string().c_str(), ec.value(), ec.message().c_str());
return {};
}

for(auto& p: filesystem::directory_iterator(path)) {
if (compare_string_insensitive(p.path().filename().string(), filename)) {
return p.path();
Expand Down
2 changes: 1 addition & 1 deletion src/openboardview/utils.h
Expand Up @@ -30,7 +30,7 @@ bool find_str_in_buf(const std::string str, const std::vector<char> &buf);
bool compare_string_insensitive(const std::string &str1, const std::string &str2);

// Case insensitive lookup of a filename at the given path
filesystem::path lookup_file_insensitive(const filesystem::path &path, const std::string &filename);
filesystem::path lookup_file_insensitive(const filesystem::path &path, const std::string &filename, std::string &error_msg);

// Split a string in a vector, delimiter is a space (stringstream iterator)
std::vector<std::string> split_string(const std::string str);
Expand Down

0 comments on commit 64d1db2

Please sign in to comment.