Version: SMASH-3.4
inputfunctions.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2015,2017-2018,2020,2024
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_SMASH_INPUTFUNCTIONS_H_
11 #define SRC_INCLUDE_SMASH_INPUTFUNCTIONS_H_
12 
13 #include <iostream>
14 #include <string>
15 #include <utility>
16 
17 #include "forwarddeclarations.h"
18 #include "particletype.h"
19 
20 namespace smash {
21 
22 /// Line consists of a line number and the contents of that line
23 struct Line { /*{{{*/
24  /// Initialize line with empty string and number
25  Line() = default;
26  /// Initialize a line with line number \p n and text \p t
27  Line(int n, std::string &&t) : number(n), text(std::move(t)) {}
28  /// Line number
29  int number;
30  /// Line content.
31  std::string text;
32 }; /*}}}*/
33 
34 /**
35  * Builds a meaningful error message
36  *
37  * Takes the message and quotes the Line where the error occurs
38  *
39  * \param[in] message Error message
40  * \param[in] line Line object containing line number and line content.
41  */
42 inline std::string build_error_string(std::string message, const Line &line) {
43  return message + " (on line " + std::to_string(line.number) + ": \"" +
44  line.text + "\")";
45 }
46 
47 /**
48  * Helper function for parsing particles.txt and decaymodes.txt.
49  *
50  * This function goes through an input stream line by line and removes
51  * comments and empty lines. The remaining lines will be returned as a vector
52  * of strings and linenumber pairs (Line).
53  *
54  * \param[in] input an lvalue reference to an input stream
55  */
56 build_vector_<Line> line_parser(const std::string &input);
57 
58 /// Makes sure that nothing is left to read from this line.
59 inline void ensure_all_read(std::istream &input, const Line &line) { /*{{{*/
60  std::string tmp;
61  input >> tmp;
62  if (!input.eof()) {
64  build_error_string("While loading the Particle data:\nGarbage (" + tmp +
65  ") at the remainder of the line.",
66  line));
67  }
68 } /*}}}*/
69 
70 /**
71  * Utility function to read a complete input stream (e.g. file) into one string.
72  *
73  * \param[in] input The input stream. Since it reads until EOF und thus "uses up
74  * the whole input stream" the function takes an rvalue reference to the stream
75  * object (just pass a temporary).
76  *
77  * \note There's no slicing here: the actual istream object is a temporary that
78  * is not destroyed until read_all returns.
79  *
80  * \warning The GNU compiler reports false positive warnings here because of
81  * <a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105580">this bug</a>
82  * and therefore we temporary manually disable that diagnostic.
83  */
84 #if defined(__GNUC__) && (__GNUC__ >= 12) && (__GNUC__ < 15)
85 #pragma GCC diagnostic push
86 #pragma GCC diagnostic ignored "-Wnull-dereference"
87 #endif
88 inline std::string read_all(std::istream &&input) {
89  return {std::istreambuf_iterator<char>{input},
90  std::istreambuf_iterator<char>{}};
91 }
92 #if defined(__GNUC__) && (__GNUC__ >= 12) && (__GNUC__ < 15)
93 #pragma GCC diagnostic pop
94 #endif
95 
96 /** Check if a line in the string ends with \\r\\n. This may happen when a file
97  * was edited on Windows.
98  *
99  * \param[in] in Input string
100  * \returns True if \\r\\n was found, else false
101  */
102 inline bool has_crlf_line_ending(const std::string in) {
103  if (in.find("\r\n") != std::string::npos) {
104  return true;
105  }
106  return false;
107 }
108 
109 } // namespace smash
110 
111 #endif // SRC_INCLUDE_SMASH_INPUTFUNCTIONS_H_
constexpr int n
Neutron.
Definition: action.h:24
void ensure_all_read(std::istream &input, const Line &line)
Makes sure that nothing is left to read from this line.
build_vector_< Line > line_parser(const std::string &input)
Helper function for parsing particles.txt and decaymodes.txt.
std::string to_string(ThermodynamicQuantity quantity)
Convert a ThermodynamicQuantity enum value to its corresponding string.
Definition: stringify.cc:26
bool has_crlf_line_ending(const std::string in)
Check if a line in the string ends with \r\n.
std::string read_all(std::istream &&input)
Utility function to read a complete input stream (e.g.
std::string build_error_string(std::string message, const Line &line)
Builds a meaningful error message.
Line consists of a line number and the contents of that line.
int number
Line number.
std::string text
Line content.
Line()=default
Initialize line with empty string and number.
Line(int n, std::string &&t)
Initialize a line with line number n and text t.