Line data Source code
1 : /* Copyright (C) 2025 Robert Schiwon 2 : * 3 : * This file is part of libjdx. 4 : * 5 : * libjdx is free software: you can redistribute it and/or modify it under the 6 : * terms of the GNU Lesser General Public License as published by the Free 7 : * Software Foundation, either version 3 of the License, or (at your option) any 8 : * later version. 9 : * 10 : * libjdx is distributed in the hope that it will be useful, but WITHOUT ANY 11 : * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 12 : * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 13 : * details. 14 : * 15 : * You should have received a copy of the GNU Lesser General Public License 16 : * along with libjdx. If not, see <https://www.gnu.org/licenses/>. 17 : */ 18 : #include "jdx/BrukerSpecificParameters.hpp" 19 : #include "jdx/ParseException.hpp" 20 : #include "util/LdrUtils.hpp" 21 : 22 23 : libjdx::jdx::BrukerSpecificParameters::BrukerSpecificParameters( 23 33 : io::TextReader& reader, std::optional<std::string>& nextLine) 24 : { 25 23 : parse(reader, nextLine); 26 18 : } 27 : 28 23 : void libjdx::jdx::BrukerSpecificParameters::parse( 29 : io::TextReader& reader, std::optional<std::string>& nextLine) 30 : { 31 23 : if (!nextLine.has_value() 32 23 : || !util::isBrukerSpecificSectionStart(nextLine.value())) 33 : { 34 : throw ParseException("Illegal start of Bruker specific section: " 35 1 : + nextLine.value_or("")); 36 : } 37 22 : if (reader.eof()) 38 : { 39 : throw ParseException( 40 : "Illegal body of Bruker specific section. No content in: " 41 1 : + nextLine.value_or("")); 42 : } 43 : 44 21 : m_name = util::stripLineComment(nextLine.value(), false, true) 45 21 : .second.value_or(""); 46 21 : nextLine = reader.readLine(); 47 21 : if (!isDashedLine(nextLine)) 48 : { 49 : throw ParseException( 50 : "Illegal body of Bruker specific section. No dash line after: " 51 1 : + m_name); 52 : } 53 20 : nextLine = reader.eof() ? std::optional<std::string>{} : reader.readLine(); 54 : 55 20 : parseLdrs(reader, nextLine); 56 18 : } 57 : 58 31 : bool libjdx::jdx::BrukerSpecificParameters::isDashedLine( 59 : std::optional<std::string>& nextLine) 60 : { 61 31 : if (!nextLine.has_value()) 62 : { 63 1 : return false; 64 : } 65 30 : auto [content, comment] 66 60 : = util::stripLineComment(nextLine.value(), true, true); 67 30 : if (content.empty() && comment.has_value()) 68 : { 69 29 : return comment.value().rfind("-----", 0) == 0; 70 : } 71 1 : return false; 72 : } 73 : 74 85 : void libjdx::jdx::BrukerSpecificParameters::parseLdrs( 75 : io::TextReader& reader, std::optional<std::string>& nextLine) 76 : { 77 85 : while (nextLine.has_value() 78 84 : && !util::isBrukerSpecificSectionStart(nextLine.value()) 79 169 : && !util::isBrukerSpecificSectionEnd(nextLine.value())) 80 : { 81 : // TODO: skip other leading comments? 82 130 : auto [label, value] = util::parseLdrStart(nextLine.value()); 83 65 : nextLine = parseStringValue(value, reader); 84 65 : m_content.emplace_back(label, value); 85 : } 86 : 87 20 : if (!nextLine.has_value() 88 30 : || (!util::isBrukerSpecificSectionStart(nextLine.value()) 89 10 : && !util::isBrukerSpecificSectionEnd(nextLine.value()))) 90 : { 91 : throw ParseException("Unexpected end of Bruker specific section:" 92 1 : + std::string{s_sectionEndText}); 93 : } 94 : 95 19 : if (util::isBrukerSpecificSectionEnd(nextLine.value())) 96 : { 97 : // skip dashes after section end marker 98 : nextLine 99 10 : = reader.eof() ? std::optional<std::string>{} : reader.readLine(); 100 10 : if (!isDashedLine(nextLine)) 101 : { 102 : throw ParseException("Illegal end of Bruker specific " 103 : "section. No dash line after: " 104 1 : + std::string{s_sectionEndText}); 105 : } 106 : nextLine 107 9 : = reader.eof() ? std::optional<std::string>{} : reader.readLine(); 108 : } 109 18 : } 110 : 111 16 : std::string libjdx::jdx::BrukerSpecificParameters::getName() const 112 : { 113 16 : return m_name; 114 : } 115 : 116 : std::vector<libjdx::jdx::StringLdr> 117 8 : libjdx::jdx::BrukerSpecificParameters::getContent() const 118 : { 119 8 : return m_content; 120 : }