MFEM  v4.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, Lawrence Livermore National Security, LLC. Produced at
2 // the Lawrence Livermore National Laboratory. LLNL-CODE-443211. All Rights
3 // reserved. See file COPYRIGHT for details.
4 //
5 // This file is part of the MFEM library. For more information and source code
6 // availability see http://mfem.org.
7 //
8 // MFEM is free software; you can redistribute it and/or modify it under the
9 // terms of the GNU Lesser General Public License (as published by the Free
10 // Software Foundation) version 2.1 dated February 1999.
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 inline void skip_comment_lines(std::istream &is, const char comment_char)
28 {
29  while (1)
30  {
31  is >> std::ws;
32  if (is.peek() != comment_char)
33  {
34  break;
35  }
36  is.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
37  }
38 }
39 
40 // Check for, and remove, a trailing '\r'.
41 inline void filter_dos(std::string &line)
42 {
43  if (!line.empty() && *line.rbegin() == '\r')
44  {
45  line.resize(line.size()-1);
46  }
47 }
48 
49 // Convert an integer to a string
50 inline std::string to_string(int i)
51 {
52  std::stringstream ss;
53  ss << i;
54 
55  // trim leading spaces
56  std::string out_str = ss.str();
57  out_str = out_str.substr(out_str.find_first_not_of(" \t"));
58  return out_str;
59 }
60 
61 // Convert an integer to a 0-padded string with the given number of 'digits'
62 inline std::string to_padded_string(int i, int digits)
63 {
64  std::ostringstream oss;
65  oss << std::setw(digits) << std::setfill('0') << i;
66  return oss.str();
67 }
68 
69 // Convert a string to an int
70 inline int to_int(const std::string& str)
71 {
72  int i;
73  std::stringstream(str) >> i;
74  return i;
75 }
76 
77 }
78 
79 #endif
std::string to_string(int i)
Definition: text.hpp:50
void skip_comment_lines(std::istream &is, const char comment_char)
Definition: text.hpp:27
std::string to_padded_string(int i, int digits)
Definition: text.hpp:62
int to_int(const std::string &str)
Definition: text.hpp:70
void filter_dos(std::string &line)
Definition: text.hpp:41