diff --git a/CMakeLists.txt b/CMakeLists.txt index eb9cdc5..b587f6e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,6 +77,7 @@ disk2steppermotorrotor.cpp diskcontroller.cpp drive.cpp drivemotor.cpp +e2filesystem.cpp E2wxApp.cpp E2wxFrame.cpp emptyslot.cpp diff --git a/src/e2filesystem.cpp b/src/e2filesystem.cpp new file mode 100644 index 0000000..b470641 --- /dev/null +++ b/src/e2filesystem.cpp @@ -0,0 +1,111 @@ +/* + epple2 + Copyright (C) 2022 by Christopher A. Mosher + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY, without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/* + * File: e2filesystem.cpp + * Author: Christopher Alan Mosher, Shelton, Connecticut + * + * Created on December 7, 2022, 8:24 PM + */ + +#include "e2filesystem.h" + +#include + +#include + +static void log_ec(const std::error_code& ec) { + BOOST_LOG_TRIVIAL(error) << "error: " << ec.value() << " " << ec.message(); +} + +std::filesystem::path valid_input_file(const std::filesystem::path path, const std::filesystem::path base) { + std::filesystem::path p(path); + + std::error_code ec; + + + + BOOST_LOG_TRIVIAL(info) << "will check for file specified as: " << p.c_str(); + if (p.is_relative()) { + BOOST_LOG_TRIVIAL(info) << "will check for file at relative path: " << p.c_str(); + + std::filesystem::path b(base); + if (b.empty()) { + BOOST_LOG_TRIVIAL(info) << "no base path found; will use current path..."; + b = std::filesystem::current_path(ec); + if (ec) { + log_ec(ec); + b.clear(); + } + } + + BOOST_LOG_TRIVIAL(info) << "...relative to base path: " << b.c_str(); + p = b / p; + } + + + + p = p.lexically_normal(); + BOOST_LOG_TRIVIAL(info) << "checking for file at absolute path: " << p.c_str(); + bool exists = std::filesystem::exists(p, ec); + if (ec) { + log_ec(ec); + exists = false; + } + + if (!exists) { + BOOST_LOG_TRIVIAL(error) << "can't find file: " << p.c_str(); + p = std::filesystem::weakly_canonical(p, ec); + if (ec) { + log_ec(ec); + BOOST_LOG_TRIVIAL(error) << "can't determine canonical path."; + } else { + BOOST_LOG_TRIVIAL(info) << "...canonical file path was: " << p.c_str(); + } + p.clear(); + return p; + } + + + + BOOST_LOG_TRIVIAL(info) << "found file: " << p.c_str(); + + std::filesystem::path cp = std::filesystem::canonical(p, ec); + if (ec) { + log_ec(ec); + BOOST_LOG_TRIVIAL(error) << "can't determine canonical path, continuing anyway..."; + } else { + BOOST_LOG_TRIVIAL(info) << "...canonical file path was: " << cp; + } + + + + bool regular = std::filesystem::is_regular_file(p, ec); + if (ec) { + log_ec(ec); + regular = false; + } + + if (!regular) { + BOOST_LOG_TRIVIAL(error) << "not a regular file"; + p.clear(); + return p; + } + + return p; +} diff --git a/src/e2filesystem.h b/src/e2filesystem.h new file mode 100644 index 0000000..f5b38c7 --- /dev/null +++ b/src/e2filesystem.h @@ -0,0 +1,33 @@ +/* + epple2 + Copyright (C) 2022 by Christopher A. Mosher + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY, without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +/* + * File: e2filesystem.cpp + * Author: Christopher Alan Mosher, Shelton, Connecticut + * + * Created on December 7, 2022, 8:24 PM + */ + +#ifndef E2FILESYSTEM_H +#define E2FILESYSTEM_H + +#include + +std::filesystem::path valid_input_file(const std::filesystem::path path, const std::filesystem::path base); + +#endif /* E2FILESYSTEM_H */ diff --git a/src/wozfile.cpp b/src/wozfile.cpp index 692db38..15c59a8 100644 --- a/src/wozfile.cpp +++ b/src/wozfile.cpp @@ -129,10 +129,10 @@ bool WozFile::load(const std::string& filePath) { unload(); } - std::uint32_t woz2; + std::uint32_t woz2(0); in->read((char*)&woz2, sizeof(woz2)); if (woz2 != 0x325A4F57u) { - printf("WOZ2 magic bytes missing. Found: %8x", woz2); + printf("WOZ2 magic bytes missing. Found: %8x\n", woz2); delete in; return false; } @@ -321,6 +321,7 @@ void WozFile::checkForWriteProtection() { return; } + // TODO: fix; if the file doesn't exist this creates an empty file std::ofstream outf(filePath.c_str(),std::ios::binary|std::ios::app); this->writable = outf.is_open(); outf.close(); diff --git a/src/wozfile.h b/src/wozfile.h index 915ff67..631e3d8 100644 --- a/src/wozfile.h +++ b/src/wozfile.h @@ -45,7 +45,6 @@ static const std::uint8_t C_QTRACK(160); * rather, that information is known by the stepper motor and arm. */ class WozFile { - std::string fileName; std::string filePath; bool loaded; bool modified; @@ -112,10 +111,6 @@ public: bool load(const std::string& filePath); - std::string getFileName() const { - return this->fileName; - } - bool isLoaded() const { return this->loaded; }