MFEM  v4.2.0
Finite element discretization library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
text.hpp
Go to the documentation of this file.
1 // Copyright (c) 2010-2020, 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 <istream>
16 #include <iomanip>
17 #include <sstream>
18 #include <string>
19 #include <limits>
20 #include <algorithm>
21 
22 namespace mfem
23 {
24 
25 // Utilities for text parsing
26 
27 /// Check if the stream starts with @a comment_char. If so skip it.
28 inline void skip_comment_lines(std::istream &is, const char comment_char)
29 {
30  while (1)
31  {
32  is >> std::ws;
33  if (is.peek() != comment_char)
34  {
35  break;
36  }
37  is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
38  }
39 }
40 
41 /// Check for, and remove, a trailing '\\r' from and std::string.
42 inline void filter_dos(std::string &line)
43 {
44  if (!line.empty() && *line.rbegin() == '\r')
45  {
46  line.resize(line.size()-1);
47  }
48 }
49 
50 /// Convert an integer to an std::string.
51 inline std::string to_string(int i)
52 {
53  std::stringstream ss;
54  ss << i;
55 
56  // trim leading spaces
57  std::string out_str = ss.str();
58  out_str = out_str.substr(out_str.find_first_not_of(" \t"));
59  return out_str;
60 }
61 
62 /// Convert an integer to a 0-padded string with the given number of @a digits
63 inline std::string to_padded_string(int i, int digits)
64 {
65  std::ostringstream oss;
66  oss << std::setw(digits) << std::setfill('0') << i;
67  return oss.str();
68 }
69 
70 /// Convert a string to an int
71 inline int to_int(const std::string& str)
72 {
73  int i;
74  std::stringstream(str) >> i;
75  return i;
76 }
77 
78 }
79 
80 #endif
std::string to_string(int i)
Convert an integer to an std::string.
Definition: text.hpp:51
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:28
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:63
int to_int(const std::string &str)
Convert a string to an int.
Definition: text.hpp:71
void filter_dos(std::string &line)
Check for, and remove, a trailing &#39;\r&#39; from and std::string.
Definition: text.hpp:42