From c6581e11220cfce71aef1c77a9797b9bf8838c31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Du=C5=A1an=20Poizl?= Date: Sun, 3 Aug 2025 20:40:56 +0200 Subject: [PATCH] Add method to modify XISF Property --- libxisf.cpp | 29 ++++++++++++++++++++++++++++- libxisf.h | 6 ++++++ variant.cpp | 2 -- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/libxisf.cpp b/libxisf.cpp index 93c23c1..03ce14f 100644 --- a/libxisf.cpp +++ b/libxisf.cpp @@ -1034,11 +1034,11 @@ public: void save(std::ostream &io); void writeImage(const Image &image); static void writeFITSKeyword(pugi::xml_node &node, const FITSKeyword &keyword); + static void writePropertyElement(pugi::xml_node &node, const Property &property); private: void writeHeader(); void writeImageElement(pugi::xml_node &node, const Image &image); void writeDataBlockAttributes(pugi::xml_node &image_node, const DataBlock &dataBlock); - void writePropertyElement(pugi::xml_node &node, const Property &property); void writeMetadata(pugi::xml_node &node); void updateImageAttachmentPos(pugi::xml_node &root, size_t offset); ByteArray _xisfHeader; @@ -1371,6 +1371,7 @@ public: void addFITSKeyword(uint32_t image, const FITSKeyword &keyword); void updateFITSKeyword(uint32_t image, const FITSKeyword &keyword, bool add); void removeFITSKeyword(uint32_t image, const String &name); + void updateProperty(uint32_t image, const Property &property); private: void readXISFHeader(); void parseAttachmentPos(pugi::xml_node &root); @@ -1551,6 +1552,27 @@ void XISFModifyPrivate::removeFITSKeyword(uint32_t image, const String &name) imageNode.remove_child(keywordNode); } +void XISFModifyPrivate::updateProperty(uint32_t image, const Property &property) +{ + if(!_root) + throw Error("No input file opened"); + + pugi::xpath_node_set images = _root.select_nodes("//Image"); + + if(image >= images.size()) + throw Error("Out of bounds"); + + pugi::xpath_variable_set variables; + variables.set("id", property.id.c_str()); + pugi::xml_node imageNode = images[image].node(); + pugi::xml_node propertyNode = imageNode.select_node("Property[@id=string($id)]", &variables).node(); + + if(propertyNode) + imageNode.remove_child(propertyNode); + + XISFWriterPrivate::writePropertyElement(imageNode, property); +} + void XISFModifyPrivate::readXISFHeader() { char signature[8]; @@ -1669,6 +1691,11 @@ void XISFModify::removeFITSKeyword(uint32_t image, const String &name) p->removeFITSKeyword(image, name); } +void XISFModify::updateProperty(uint32_t image, const Property &property) +{ + p->updateProperty(image, property); +} + #define STRING_ENUM(map, map2, c, e) { map.insert({#e, c::e}); map2.insert({c::e, #e}); } struct Init diff --git a/libxisf.h b/libxisf.h index 0442694..2e8981d 100644 --- a/libxisf.h +++ b/libxisf.h @@ -443,6 +443,12 @@ public: * @param name of keyword that will be removed */ void removeFITSKeyword(uint32_t image, const String &name); + /** + * @brief updateProperty add new or update existing XISF Property + * @param image index of image to update + * @param property new value of property + */ + void updateProperty(uint32_t image, const Property &property); private: XISFModifyPrivate *p; }; diff --git a/variant.cpp b/variant.cpp index bb8ba75..c9366ce 100644 --- a/variant.cpp +++ b/variant.cpp @@ -17,7 +17,6 @@ ************************************************************************/ #include -#include #include #include #include @@ -403,7 +402,6 @@ Variant variantFromString(Variant::Type type, const String &str) Variant::Type Variant::type() const { - int idx = _value.index(); return (Variant::Type)_value.index(); }