commit 078663b80402ef2957a283d0b7b0175107c29be6
parent 5e779020edd02c6953329cd0d38051335782bf92
Author: cfillion <cfillion@users.noreply.github.com>
Date: Mon, 4 Jan 2016 11:37:41 -0500
refactor Remote::fromFile
Diffstat:
4 files changed, 40 insertions(+), 22 deletions(-)
diff --git a/src/reapack.cpp b/src/reapack.cpp
@@ -112,15 +112,18 @@ void ReaPack::importRemote()
if(!GetUserFileNameForRead(&path[0], title, "ReaPackRemote"))
return;
- Remote remote;
+ Remote::ReadCode code;
+ const Remote remote = Remote::fromFile(path, &code);
- switch(Remote::fromFile(path, &remote)) {
+ switch(code) {
case Remote::ReadFailure:
ShowMessageBox(strerror(errno), title, 0);
return;
case Remote::InvalidName:
+ ShowMessageBox("Invalid .ReaPackRemote file (invalid name)!", title, 0);
+ return;
case Remote::InvalidUrl:
- ShowMessageBox("Invalid .ReaPackRemote file!", title, 0);
+ ShowMessageBox("Invalid .ReaPackRemote file (invalid url)!", title, 0);
return;
default:
break;
diff --git a/src/remote.cpp b/src/remote.cpp
@@ -43,12 +43,16 @@ static bool ValidateUrl(const string &url)
return !url.empty();
}
-auto Remote::fromFile(const string &path, Remote *remote) -> ReadCode
+Remote Remote::fromFile(const string &path, ReadCode *code)
{
ifstream file(make_autostring(path));
- if(!file)
- return ReadFailure;
+ if(!file) {
+ if(code)
+ *code = ReadFailure;
+
+ return {};
+ }
string name;
getline(file, name);
@@ -58,16 +62,23 @@ auto Remote::fromFile(const string &path, Remote *remote) -> ReadCode
file.close();
+ if(!ValidateName(name)) {
+ if(code)
+ *code = InvalidName;
- if(!ValidateName(name))
- return InvalidName;
- else if(!ValidateUrl(url))
- return InvalidUrl;
+ return {};
+ }
+ else if(!ValidateUrl(url)) {
+ if(code)
+ *code = InvalidUrl;
+
+ return {};
+ }
- remote->setName(name);
- remote->setUrl(url);
+ if(code)
+ *code = Success;
- return Success;
+ return {name, url};
}
Remote Remote::fromString(const string &data, ReadCode *code)
diff --git a/src/remote.hpp b/src/remote.hpp
@@ -32,7 +32,7 @@ public:
InvalidUrl,
};
- static ReadCode fromFile(const std::string &path, Remote *remote);
+ static Remote fromFile(const std::string &path, ReadCode *code = nullptr);
static Remote fromString(const std::string &data, ReadCode *code = nullptr);
Remote();
diff --git a/test/remote.cpp b/test/remote.cpp
@@ -151,13 +151,15 @@ TEST_CASE("remote from file", M) {
Remote remote;
SECTION("not found") {
- const auto code = Remote::fromFile(RPATH "404.ReaPackRemote", &remote);
+ Remote::ReadCode code;
+ const Remote remote = Remote::fromFile(RPATH "404.ReaPackRemote", &code);
REQUIRE(code == Remote::ReadFailure);
REQUIRE_FALSE(remote.isValid());
}
SECTION("valid") {
- const auto code = Remote::fromFile(RPATH "test.ReaPackRemote", &remote);
+ Remote::ReadCode code;
+ const Remote remote = Remote::fromFile(RPATH "test.ReaPackRemote", &code);
REQUIRE(code == Remote::Success);
REQUIRE(remote.isValid());
REQUIRE(remote.name() == "name");
@@ -165,24 +167,26 @@ TEST_CASE("remote from file", M) {
}
SECTION("invalid name") {
- const auto code = Remote::fromFile(
- RPATH "invalid_name.ReaPackRemote", &remote);
+ Remote::ReadCode code;
+ const Remote remote = Remote::fromFile(
+ RPATH "invalid_name.ReaPackRemote", &code);
REQUIRE(code == Remote::InvalidName);
REQUIRE_FALSE(remote.isValid());
}
SECTION("invalid url") {
- const auto code = Remote::fromFile(
- RPATH "missing_url.ReaPackRemote", &remote);
+ Remote::ReadCode code;
+ const Remote remote = Remote::fromFile(
+ RPATH "missing_url.ReaPackRemote", &code);
REQUIRE(code == Remote::InvalidUrl);
REQUIRE_FALSE(remote.isValid());
}
SECTION("unicode name") {
- const auto code = Remote::fromFile(
- RPATH "Новая папка.ReaPackRemote", &remote);
+ Remote::ReadCode code;
+ Remote::fromFile(RPATH "Новая папка.ReaPackRemote", &code);
REQUIRE(code == Remote::Success);
}