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