clean seafile api

This commit is contained in:
shell committed 2026-07-21 22:00:38 +08:00
1 parent 133424f5f8
commit 3e4694ceb5
14 files changed
+476 -290

No files matched your search

+115
View File
@@ -0,0 +1,115 @@
//
// Created by Shell on 2026/7/21.
//
#include <sstream>
#include <vector>
#include <stdexcept>
#include <seafile/util/version.hpp>
namespace seafile::util {
Version::Version(const std::string &version) {
parse(version);
}
int Version::major() const noexcept {
return major_;
}
std::optional<int> Version::minor() const noexcept {
return minor_;
}
std::optional<int> Version::patch() const noexcept {
return patch_;
}
std::string Version::toString() const {
std::string result = std::to_string(major_);
if (minor_) {
result += "." + std::to_string(*minor_);
}
if (patch_) {
result += "." + std::to_string(*patch_);
}
return result;
}
bool Version::operator==(const Version &other) const {
return major_ == other.major_
&& minor().value_or(0) == other.minor().value_or(0)
&& patch().value_or(0) == other.patch().value_or(0);
}
bool Version::operator<(const Version &other) const {
if (major_ != other.major_) {
return major_ < other.major_;
}
if (minor().value_or(0) != other.minor().value_or(0)) {
return minor().value_or(0) < other.minor().value_or(0);
}
return patch().value_or(0) < other.patch().value_or(0);
}
void Version::parse(const std::string &version) {
std::stringstream ss(version);
std::string part;
std::vector<int> parts;
while (std::getline(ss, part, '.')) {
if (part.empty()) {
throw std::invalid_argument(
"Invalid version: " + version
);
}
try {
parts.push_back(std::stoi(part));
} catch (...) {
throw std::invalid_argument(
"Invalid version number: " + part
);
}
}
if (parts.empty()) {
throw std::invalid_argument(
"Version cannot be empty"
);
}
if (parts.size() > 3) {
throw std::invalid_argument(
"Version supports at most 3 components"
);
}
major_ = parts[0];
if (parts.size() >= 2) {
minor_ = parts[1];
}
if (parts.size() >= 3) {
patch_ = parts[2];
}
}
}