diff --git a/libxisf.cpp b/libxisf.cpp index ef8636a..5ce4bc8 100644 --- a/libxisf.cpp +++ b/libxisf.cpp @@ -17,21 +17,21 @@ ************************************************************************/ #include "libxisf.h" -#include -#include -#include -#include -#include -#include +#include "streambuffer.h" #include +#include +#include +#include +#include #include #include #include +#include +#include #include #ifdef HAVE_ZSTD #include #endif -#include "streambuffer.h" namespace LibXISF { @@ -701,6 +701,7 @@ public: void open(const ByteArray &data); /** Open image from istream. This method takes ownership of *io pointer */ void open(std::istream *io); + void open(const std::filesystem::path &path); /** Close opended file release all data. */ void close(); /** Return number of images inside file */ @@ -754,6 +755,14 @@ void XISFReaderPrivate::open(std::istream *io) readXISFHeader(); } +void XISFReaderPrivate::open(const std::filesystem::path &path) +{ + close(); + _io = std::make_unique(path, std::ios_base::in | std::ios_base::binary); + readSignature(); + readXISFHeader(); +} + void XISFReaderPrivate::close() { _io.reset(); @@ -1032,6 +1041,7 @@ public: void save(const String &name); void save(ByteArray &data); void save(std::ostream &io); + void save(const std::filesystem::path &path); 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); @@ -1084,6 +1094,16 @@ void XISFWriterPrivate::save(std::ostream &io) } } +void XISFWriterPrivate::save(const std::filesystem::path &path) +{ + std::ofstream fw(path, std::ios_base::out | std::ios_base::binary); + + if(fw.fail()) + throw Error("Failed to open file"); + + save(fw); +} + void XISFWriterPrivate::writeImage(const Image &image) { _images.push_back(image); @@ -1304,6 +1324,11 @@ void XISFReader::open(std::istream *io) p->open(io); } +void XISFReader::open(const std::filesystem::path &path) +{ + p->open(path); +} + void XISFReader::close() { p->close(); @@ -1349,6 +1374,11 @@ void XISFWriter::save(std::ostream &io) p->save(io); } +void XISFWriter::save(const std::filesystem::path &path) +{ + p->save(path); +} + void XISFWriter::writeImage(const Image &image) { p->writeImage(image); @@ -1361,12 +1391,14 @@ public: void open(const ByteArray &data); /** Open image from istream. This method takes ownership of *io pointer */ void open(std::istream *io); + void open(const std::filesystem::path &path); /** Close opended file release all data. */ void close(); void save(const String &name); void save(ByteArray &data); void save(std::ostream &io); + void save(const std::filesystem::path &path); void addFITSKeyword(uint32_t image, const FITSKeyword &keyword); void updateFITSKeyword(uint32_t image, const FITSKeyword &keyword, bool add); @@ -1408,6 +1440,13 @@ void XISFModifyPrivate::open(std::istream *io) readXISFHeader(); } +void XISFModifyPrivate::open(const std::filesystem::path &path) +{ + close(); + _io = std::make_unique(path, std::ios_base::in | std::ios_base::binary); + readXISFHeader(); +} + void XISFModifyPrivate::close() { _io.reset(); @@ -1487,6 +1526,16 @@ void XISFModifyPrivate::save(std::ostream &io) } } +void XISFModifyPrivate::save(const std::filesystem::path &path) +{ + std::ofstream fw(path, std::ios_base::out | std::ios_base::binary); + + if(fw.fail()) + throw Error("Failed to open file"); + + save(fw); +} + void XISFModifyPrivate::addFITSKeyword(uint32_t image, const FITSKeyword &keyword) { if(!_root) @@ -1657,6 +1706,11 @@ void XISFModify::open(std::istream *io) p->open(io); } +void XISFModify::open(const std::filesystem::path &path) +{ + p->open(path); +} + void XISFModify::close() { p->close(); @@ -1677,6 +1731,11 @@ void XISFModify::save(std::ostream &io) p->save(io); } +void XISFModify::save(const std::filesystem::path &path) +{ + p->save(path); +} + void XISFModify::addFITSKeyword(uint32_t image, const FITSKeyword &keyword) { p->addFITSKeyword(image, keyword); diff --git a/libxisf.h b/libxisf.h index 2e8981d..b277e00 100644 --- a/libxisf.h +++ b/libxisf.h @@ -20,15 +20,15 @@ #define LIBXISF_H #include "libXISF_global.h" -#include -#include -#include -#include -#include -#include #include -#include +#include #include +#include +#include +#include +#include +#include +#include namespace LibXISF { @@ -370,6 +370,7 @@ public: void open(const ByteArray &data); /** Open image from istream. This method takes ownership of *io pointer */ void open(std::istream *io); + void open(const std::filesystem::path &path); /** Close opended file release all data. */ void close(); /** Return number of images inside file */ @@ -396,6 +397,7 @@ public: void save(const String &name); void save(ByteArray &data); void save(std::ostream &io); + void save(const std::filesystem::path &path); void writeImage(const Image &image); private: XISFWriterPrivate *p; @@ -419,10 +421,12 @@ public: void open(const String &name); void open(const ByteArray &data); void open(std::istream *io); + void open(const std::filesystem::path &path); void close(); void save(const String &name); void save(ByteArray &data); void save(std::ostream &io); + void save(const std::filesystem::path &path); /** * @brief addFITSKeyword append new keyword to image diff --git a/test/main.cpp b/test/main.cpp index d125b56..a94077e 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -102,7 +102,7 @@ int main(int argc, char **argv) else { LibXISF::XISFReader reader; - reader.open(argv[1]); + reader.open(LibXISF::String(argv[1])); TEST(reader.imagesCount() != 1, "No image"); const LibXISF::Image &image = reader.getImage(0);