#include "StarDirectives.hpp" #include "StarImage.hpp" #include "StarImageProcessing.hpp" #include "StarXXHash.hpp" #include "StarLogging.hpp" namespace Star { Directives::Entry::Entry(ImageOperation&& newOperation, size_t strBegin, size_t strLength) { operation = move(newOperation); begin = strBegin; length = strLength; } Directives::Entry::Entry(ImageOperation const& newOperation, size_t strBegin, size_t strLength) { operation = newOperation; begin = strBegin; length = strLength; } Directives::Entry::Entry(Entry const& other) { operation = other.operation; begin = other.begin; length = other.length; } StringView Directives::Entry::string(Shared const& parent) const { StringView result = parent.string; result = result.utf8().substr(begin, length); return result; } bool Directives::Shared::empty() const { return entries.empty(); } Directives::Shared::Shared(List&& givenEntries, String&& givenString, StringView givenPrefix) { entries = move(givenEntries); string = move(givenString); prefix = givenPrefix; hash = XXH3_64bits(string.utf8Ptr(), string.utf8Size()); } Directives::Directives() {} Directives::Directives(String const& directives) { parse(String(directives)); } Directives::Directives(String&& directives) { parse(move(directives)); } Directives::Directives(const char* directives) { parse(directives); } void Directives::parse(String&& directives) { if (directives.empty()) return; List newList; StringView view(directives); StringView prefix = ""; view.forEachSplitView("?", [&](StringView split, size_t beg, size_t end) { if (!split.empty()) { try { ImageOperation operation = imageOperationFromString(split); newList.emplace_back(move(operation), beg, end); } catch (StarException const& e) { if (beg == 0) prefix = split; else newList.emplace_back(ErrorImageOperation{ std::current_exception() }, beg, end); } } }); if (newList.empty() && !prefix.empty()) return; shared = std::make_shared(move(newList), move(directives), prefix); } String Directives::string() const { if (!shared) return ""; else return shared->string; } StringView Directives::prefix() const { if (!shared) return ""; else return shared->prefix; } String const* Directives::stringPtr() const { if (!shared) return nullptr; else return &shared->string; } String Directives::buildString() const { if (shared) { String built = shared->prefix; for (auto& entry : shared->entries) { built += "?"; built += entry.string(*shared); } return built; } return String(); } String& Directives::addToString(String& out) const { if (!empty()) out += shared->string; return out; } size_t Directives::hash() const { return shared ? shared->hash : 0; } size_t Directives::size() const { return shared ? shared->entries.size() : 0; } inline bool Directives::empty() const { return !shared || shared->empty(); } inline Directives::operator bool() const { return !empty(); } DataStream& operator>>(DataStream& ds, Directives& directives) { String string; ds.read(string); directives.parse(move(string)); return ds; } DataStream& operator<<(DataStream& ds, Directives const& directives) { if (directives) ds.write(directives.shared->string); else ds.write(String()); return ds; } DirectivesGroup::DirectivesGroup() : m_count(0) {} DirectivesGroup::DirectivesGroup(String const& directives) : m_count(0) { if (directives.empty()) return; Directives parsed(directives); if (parsed) { m_directives.emplace_back(move(parsed)); m_count = m_directives.back().size(); } } DirectivesGroup::DirectivesGroup(String&& directives) : m_count(0) { if (directives.empty()) { directives.clear(); return; } Directives parsed(move(directives)); if (parsed) { m_directives.emplace_back(move(parsed)); m_count = m_directives.back().size(); } } inline bool DirectivesGroup::empty() const { return m_count == 0; } inline DirectivesGroup::operator bool() const { return empty(); } bool DirectivesGroup::compare(DirectivesGroup const& other) const { if (m_count != other.m_count) return false; if (empty()) return true; return hash() == other.hash(); } void DirectivesGroup::append(Directives const& directives) { m_directives.emplace_back(directives); m_count += m_directives.back().size(); } void DirectivesGroup::clear() { m_directives.clear(); m_count = 0; } DirectivesGroup& DirectivesGroup::operator+=(Directives const& other) { append(other); return *this; } inline String DirectivesGroup::toString() const { String string; addToString(string); return string; } void DirectivesGroup::addToString(String& string) const { for (auto& directives : m_directives) if (directives.shared) string += directives.shared->string; } void DirectivesGroup::forEach(DirectivesCallback callback) const { for (auto& directives : m_directives) { if (directives.shared) { for (auto& entry : directives.shared->entries) callback(entry, directives); } } } bool DirectivesGroup::forEachAbortable(AbortableDirectivesCallback callback) const { for (auto& directives : m_directives) { if (directives.shared) { for (auto& entry : directives.shared->entries) { if (!callback(entry, directives)) return false; } } } return true; } inline Image DirectivesGroup::applyNewImage(Image const& image) const { Image result = image; applyExistingImage(result); return result; } void DirectivesGroup::applyExistingImage(Image& image) const { forEach([&](auto const& entry, Directives const& directives) { if (auto error = entry.operation.ptr()) std::rethrow_exception(error->exception); else processImageOperation(entry.operation, image); }); } inline size_t DirectivesGroup::hash() const { XXHash3 hasher; for (auto& directives : m_directives) { size_t hash = directives.hash(); hasher.push((const char*)&hash, sizeof(hash)); } return hasher.digest(); } const List& DirectivesGroup::list() const { return m_directives; } bool operator==(DirectivesGroup const& a, DirectivesGroup const& b) { return a.compare(b); } bool operator!=(DirectivesGroup const& a, DirectivesGroup const& b) { return !a.compare(b); } DataStream& operator>>(DataStream& ds, DirectivesGroup& directivesGroup) { String string; ds.read(string); directivesGroup = DirectivesGroup(move(string)); return ds; } DataStream& operator<<(DataStream& ds, DirectivesGroup const& directivesGroup) { ds.write(directivesGroup.toString()); return ds; } size_t hash::operator()(DirectivesGroup const& s) const { return s.hash(); } }