MFEM v4.10.0
Finite element discretization library
Loading...
Searching...
No Matches
text.hpp
Go to the documentation of this file.
1// Copyright (c) 2010-2026, Lawrence Livermore National Security, LLC. Produced
2// at the Lawrence Livermore National Laboratory. All Rights reserved. See files
3// LICENSE and NOTICE for details. LLNL-CODE-806117.
4//
5// This file is part of the MFEM library. For more information and source code
6// availability visit https://mfem.org.
7//
8// MFEM is free software; you can redistribute it and/or modify it under the
9// terms of the BSD-3 license. We welcome feedback and contributions, see file
10// CONTRIBUTING.md for details.
11
12#ifndef MFEM_TEXT
13#define MFEM_TEXT
14
15#include "../config/config.hpp"
16#include <istream>
17#include <iomanip>
18#include <sstream>
19#include <string>
20#include <limits>
21#include <algorithm>
22
23namespace mfem
24{
25
26// Utilities for text parsing
27
28using std::to_string;
29
30/// Check if the stream starts with @a comment_char. If so skip it.
31inline void skip_comment_lines(std::istream &is, const char comment_char)
32{
33 while (1)
34 {
35 is >> std::ws;
36 if (is.peek() != comment_char)
37 {
38 break;
39 }
40 is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
41 }
42}
43
44/// Check for, and remove, a trailing '\\r' from and std::string.
45inline void filter_dos(std::string &line)
46{
47 if (!line.empty() && *line.rbegin() == '\r')
48 {
49 line.resize(line.size()-1);
50 }
51}
52
53/** @brief Read a string formatted using std::quoted. Return nonzero on error.
54
55 The stream @a in must begin with @a delim. After clearing @a result and
56 extracting the opening @a delim, characters are extracted from @a in and
57 processed as follows:
58 - if the character is @a delim, return 0;
59 - if the character is different from @a escape, it is appended to @a result;
60 - if the character is @a escape, the next character from @a in is extracted
61 and if it is one of @a delim or @a escape, it is appended to @a result;
62 otherwise, both @a escape and the character after it are appended to
63 @a result; note that the latter case is not possible if the input was
64 formatted with std::quoted with the same @a delim and @a escape
65 characters.
66
67 If the stream @a in does not begin with @a delim, error code 1 is returned.
68 If reading the stream fails, error code 2 is returned. On success, zero is
69 returned and the closing @a delim character is the last character extracted
70 from @a in. */
71inline int parse_quoted_string(std::string &result, std::istream &in,
72 char delim = '"', char escape = '\\')
73{
74 using tt = std::string::traits_type; // std::char_traits<char>
75 auto equal = [](tt::int_type c1, tt::char_type c2) -> bool
76 {
77 return tt::eq_int_type(c1, tt::to_int_type(c2));
78 };
79 result.clear();
80 if (!equal(in.peek(), delim)) { return 1; }
81 in.get(); // extract delim
82 for (auto c = in.get(); !equal(c, delim); c = in.get())
83 {
84 if (equal(c, escape))
85 {
86 c = in.get();
87 if (!equal(c, escape) && !equal(c, delim)) { result += escape; }
88 }
89 if (!in) { return 2; }
90 result += tt::to_char_type(c);
91 }
92 return 0;
93}
94
95/// Convert an integer to a 0-padded string with the given number of @a digits
96inline std::string to_padded_string(int i, int digits)
97{
98 std::ostringstream oss;
99 oss << std::setw(digits) << std::setfill('0') << i;
100 return oss.str();
101}
102
103/// Convert a string to an int
104inline int to_int(const std::string& str)
105{
106 int i;
107 std::stringstream(str) >> i;
108 return i;
109}
110
111}
112
113#endif
std::string to_padded_string(int i, int digits)
Convert an integer to a 0-padded string with the given number of digits.
Definition text.hpp:96
int parse_quoted_string(std::string &result, std::istream &in, char delim='"', char escape = '\\')
Read a string formatted using std::quoted. Return nonzero on error.
Definition text.hpp:71
void filter_dos(std::string &line)
Check for, and remove, a trailing '\r' from and std::string.
Definition text.hpp:45
int to_int(const std::string &str)
Convert a string to an int.
Definition text.hpp:104
void skip_comment_lines(std::istream &is, const char comment_char)
Check if the stream starts with comment_char. If so skip it.
Definition text.hpp:31