MFEM  v4.5.2
Finite element discretization library
text.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2023, 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 
23 namespace mfem
24 {
25 
26 // Utilities for text parsing
27 
28 using std::to_string;
29 
30 /// Check if the stream starts with @a comment_char. If so skip it.
31 inline 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.
45 inline 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 /// Convert an integer to a 0-padded string with the given number of @a digits
54 inline std::string to_padded_string(int i, int digits)
55 {
56  std::ostringstream oss;
57  oss << std::setw(digits) << std::setfill('0') << i;
58  return oss.str();
59 }
60 
61 /// Convert a string to an int
62 inline int to_int(const std::string& str)
63 {
64  int i;
65  std::stringstream(str) >> i;
66  return i;
67 }
68 
69 }
70 
71 #endif
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
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:54
int to_int(const std::string &str)
Convert a string to an int.
Definition: text.hpp:62
void filter_dos(std::string &line)
Check for, and remove, a trailing &#39;\r&#39; from and std::string.
Definition: text.hpp:45