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 : xml_file.set_parse_mode(get_parse_mode()); 50 15 : Result result = xml_file.read(path); 51 15 : if (result.error()) return ESYSREPO_RESULT(result); 52 : 53 15 : xml_file.get_data()->set_format(manifest::Format::XML); 54 15 : set_data(xml_file.get_data()); 55 15 : return ESYSREPO_RESULT(ResultCode::OK); 56 15 : } 57 : else 58 : { 59 : //! \TODO implement manifest with JSON format ... if ever ... 60 0 : return ESYSREPO_RESULT(ResultCode::NOT_IMPLEMENTED); 61 : } 62 15 : } 63 : 64 0 : Result File::write(const std::string &path) 65 : { 66 0 : set_filename(path); 67 : 68 0 : std::ofstream ofs(path); 69 : 70 0 : if (!ofs.is_open()) return ESYSREPO_RESULT(ResultCode::ERROR_OPENING_FILE); 71 : 72 0 : return write(ofs); 73 0 : } 74 : 75 0 : Result File::write(std::ostream &os) 76 : { 77 0 : if (get_data() == nullptr) return ESYSREPO_RESULT(ResultCode::MANIFEST_XML_DATA_NULLPTR); 78 0 : if (get_data()->get_format() == manifest::Format::NOT_SET) 79 0 : return ESYSREPO_RESULT(ResultCode::MANIFEST_FORMAT_NOT_SET); 80 0 : if (get_data()->get_format() == manifest::Format::UNKNOWN) 81 0 : return ESYSREPO_RESULT(ResultCode::MANIFEST_FORMAT_UNKNOWN); 82 0 : if (get_data()->get_format() == manifest::Format::XML) 83 : { 84 0 : manifest::XMLFile xml_file; 85 : 86 0 : xml_file.set_data(get_data()); 87 : 88 0 : return xml_file.write(os); 89 0 : } 90 : 91 0 : if (get_data()->get_format() == manifest::Format::JSON) 92 : { 93 : //! \TODO 94 0 : return ESYSREPO_RESULT(ResultCode::NOT_IMPLEMENTED); 95 : } 96 0 : return ESYSREPO_RESULT(ResultCode::MANIFEST_FORMAT_UNKNOWN); 97 : } 98 : 99 0 : FileBase *File::get_impl() 100 : { 101 0 : return m_impl.get(); 102 : } 103 : 104 0 : const FileBase *File::get_impl() const 105 : { 106 0 : return m_impl.get(); 107 : } 108 : 109 : } // namespace esys::repo::manifest