add function to get valid input file path

This commit is contained in:
Christopher A. Mosher 2022-12-07 23:45:59 -05:00
parent 2093696a5e
commit 3c504822f0
5 changed files with 148 additions and 7 deletions

View File

@ -77,6 +77,7 @@ disk2steppermotorrotor.cpp
diskcontroller.cpp
drive.cpp
drivemotor.cpp
e2filesystem.cpp
E2wxApp.cpp
E2wxFrame.cpp
emptyslot.cpp

111
src/e2filesystem.cpp Normal file
View File

@ -0,0 +1,111 @@
/*
epple2
Copyright (C) 2022 by Christopher A. Mosher <cmosher01@gmail.com>
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 <http://www.gnu.org/licenses/>.
*/
/*
* File: e2filesystem.cpp
* Author: Christopher Alan Mosher, Shelton, Connecticut
*
* Created on December 7, 2022, 8:24 PM
*/
#include "e2filesystem.h"
#include <boost/log/trivial.hpp>
#include <system_error>
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;
}

33
src/e2filesystem.h Normal file
View File

@ -0,0 +1,33 @@
/*
epple2
Copyright (C) 2022 by Christopher A. Mosher <cmosher01@gmail.com>
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 <http://www.gnu.org/licenses/>.
*/
/*
* File: e2filesystem.cpp
* Author: Christopher Alan Mosher, Shelton, Connecticut
*
* Created on December 7, 2022, 8:24 PM
*/
#ifndef E2FILESYSTEM_H
#define E2FILESYSTEM_H
#include <filesystem>
std::filesystem::path valid_input_file(const std::filesystem::path path, const std::filesystem::path base);
#endif /* E2FILESYSTEM_H */

View File

@ -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();

View File

@ -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;
}