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/BrukerRelaxSection.hpp" 19 : #include "jdx/ParseException.hpp" 20 : #include "util/LdrUtils.hpp" 21 : #include "util/StringUtils.hpp" 22 : 23 27 : libjdx::jdx::BrukerRelaxSection::BrukerRelaxSection(const std::string& label, 24 : const std::string& value, io::TextReader& reader, 25 33 : std::optional<std::string>& nextLine) 26 : { 27 28 : validate(label, value, nextLine); 28 26 : parse(reader, nextLine); 29 24 : } 30 : 31 27 : void libjdx::jdx::BrukerRelaxSection::validate(const std::string& label, 32 : std::string value, std::optional<std::string>& nextLine) 33 : { 34 27 : util::trim(value); 35 27 : if (label != s_label || !value.empty()) 36 : { 37 2 : throw ParseException("Illegal start of Bruker " + std::string{s_label} 38 4 : + " section: " + std::string{"##"} + label + ": " 39 3 : + value); 40 : } 41 26 : } 42 : 43 26 : void libjdx::jdx::BrukerRelaxSection::parse( 44 : io::TextReader& reader, std::optional<std::string>& nextLine) 45 : { 46 26 : if (reader.eof()) 47 : { 48 : throw ParseException( 49 1 : "Premature end of Bruker " + std::string{s_label} + " section."); 50 : } 51 25 : nextLine = reader.readLine(); 52 25 : if (util::isBrukerSpecificSectionStart(nextLine.value())) 53 : { 54 : // RELAX LDR immediately followed by 55 : // $$ Bruker specific parameters 56 : // => skip further parsing 57 8 : return; 58 : } 59 34 : auto [label, fileName] = util::parseLdrStart(nextLine.value()); 60 17 : if (label.rfind(s_labelFileNameStart, 0) != 0) 61 : { 62 2 : throw ParseException("Illegal start of Bruker " + std::string{s_label} 63 4 : + " section. " + std::string{s_label} 64 2 : + " line not followed by " 65 3 : + std::string{s_labelFileNameStart} + "...."); 66 : } 67 16 : util::trim(fileName); 68 16 : m_name = fileName; 69 : 70 105 : while (!reader.eof()) 71 : { 72 105 : nextLine = reader.readLine(); 73 210 : if (!nextLine.has_value() || util::isLdrStart(nextLine.value()) 74 210 : || util::isBrukerSpecificSectionStart(nextLine.value())) 75 : { 76 16 : break; 77 : } 78 : 79 178 : auto line = nextLine.value(); 80 89 : if (line.rfind("$$ ", 0) == 0) 81 : { 82 72 : line = line.substr(3); 83 : } 84 17 : else if (line.rfind("$$", 0) == 0) 85 : { 86 1 : line = line.substr(2); 87 : } 88 89 : m_content.append(line); 89 89 : m_content.append("\n"); 90 : } 91 : } 92 : 93 38 : std::string libjdx::jdx::BrukerRelaxSection::getName() const 94 : { 95 38 : return m_name; 96 : } 97 : 98 12 : std::string libjdx::jdx::BrukerRelaxSection::getContent() const 99 : { 100 12 : return m_content; 101 : }