Line data Source code
1 : /*! 2 : * \file esys/repo/manifest/file_manifest.cpp 3 : * \brief 4 : * 5 : * \cond 6 : * __legal_b__ 7 : * 8 : * Copyright (c) 2021 Michel Gillet 9 : * Distributed under the MIT License. 10 : * (See accompanying file LICENSE.txt or 11 : * copy at https://opensource.org/licenses/MIT) 12 : * 13 : * __legal_e__ 14 : * \endcond 15 : * 16 : */ 17 : 18 : #include "esys/repo/esysrepo_prec.h" 19 : #include "esys/repo/manifest/file.h" 20 : #include "esys/repo/manifest/xmlfile.h" 21 : 22 : #include <fstream> 23 : 24 : namespace esys::repo::manifest 25 : { 26 : 27 15 : File::File() = default; 28 : 29 15 : File::~File() = default; 30 : 31 15 : Result File::read(const std::string &path) 32 : { 33 15 : std::ifstream ifs; 34 : 35 15 : set_filename(path); 36 : 37 15 : ifs.open(path); 38 15 : if (!ifs.is_open()) return ESYSREPO_RESULT(ResultCode::ERROR_OPENING_FILE, path); 39 : 40 15 : std::string line; 41 15 : std::getline(ifs, line); 42 15 : ifs.close(); 43 : 44 15 : if (line.find("<?xml") != std::string::npos) 45 : { 46 15 : manifest::XMLFile xml_file; 47 : 48 15 : xml_file.set_data(get_data()); 49 15 : Result result = xml_file.read(path); 50 15 : if (result.error()) return ESYSREPO_RESULT(result); 51 : 52 15 : xml_file.get_data()->set_format(manifest::Format::XML); 53 15 : set_data(xml_file.get_data()); 54 15 : return ESYSREPO_RESULT(ResultCode::OK); 55 15 : } 56 : else 57 : { 58 : //! \TODO implement manifest with JSON format ... if ever ... 59 0 : return ESYSREPO_RESULT(ResultCode::NOT_IMPLEMENTED); 60 : } 61 15 : } 62 : 63 0 : Result File::write(const std::string &path) 64 : { 65 0 : set_filename(path); 66 : 67 0 : std::ofstream ofs(path); 68 : 69 0 : if (!ofs.is_open()) return ESYSREPO_RESULT(ResultCode::ERROR_OPENING_FILE); 70 : 71 0 : return write(ofs); 72 0 : } 73 : 74 0 : Result File::write(std::ostream &os) 75 : { 76 0 : if (get_data() == nullptr) return ESYSREPO_RESULT(ResultCode::MANIFEST_XML_DATA_NULLPTR); 77 0 : if (get_data()->get_format() == manifest::Format::NOT_SET) 78 0 : return ESYSREPO_RESULT(ResultCode::MANIFEST_FORMAT_NOT_SET); 79 0 : if (get_data()->get_format() == manifest::Format::UNKNOWN) 80 0 : return ESYSREPO_RESULT(ResultCode::MANIFEST_FORMAT_UNKNOWN); 81 0 : if (get_data()->get_format() == manifest::Format::XML) 82 : { 83 0 : manifest::XMLFile xml_file; 84 : 85 0 : xml_file.set_data(get_data()); 86 : 87 0 : return xml_file.write(os); 88 0 : } 89 : 90 0 : if (get_data()->get_format() == manifest::Format::JSON) 91 : { 92 : //! \TODO 93 0 : return ESYSREPO_RESULT(ResultCode::NOT_IMPLEMENTED); 94 : } 95 0 : return ESYSREPO_RESULT(ResultCode::MANIFEST_FORMAT_UNKNOWN); 96 : } 97 : 98 0 : FileBase *File::get_impl() 99 : { 100 0 : return m_impl.get(); 101 : } 102 : 103 0 : const FileBase *File::get_impl() const 104 : { 105 0 : return m_impl.get(); 106 : } 107 : 108 : } // namespace esys::repo::manifest