LifeV
StringUtility.hpp
Go to the documentation of this file.
1 //@HEADER
2 /*
3 *******************************************************************************
4 
5 Copyright (C) 2004, 2005, 2007 EPFL, Politecnico di Milano, INRIA
6 Copyright (C) 2010 EPFL, Politecnico di Milano, Emory University
7 
8 This file is part of LifeV.
9 
10 LifeV is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation, either version 3 of the License, or
13 (at your option) any later version.
14 
15 LifeV is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with LifeV. If not, see <http://www.gnu.org/licenses/>.
22 
23 *******************************************************************************
24 */
25 //@HEADER
26 /*!
27  @file
28  @brief String utilities
29 
30  @date 13-12-2010
31  @author
32 
33  @maintainer Radu Popescu <radu.popescu@epfl.ch>
34 */
35 
36 #ifndef STRING_UTILITY_H
37 #define STRING_UTILITY_H
38 
39 #include <cstdio>
40 #include <cstdlib>
41 #include <cstring>
42 #include <iosfwd>
43 #include <sstream>
44 
45 
46 #include <boost/algorithm/string.hpp>
47 #include <boost/lexical_cast.hpp>
48 
49 
50 #include <lifev/core/LifeV.hpp>
51 
52 namespace LifeV
53 {
54 /*! \file util_string.h
55 \brief Special structures for handling mesh faces and sides
56 \version 0.0 Experimental 5/2/00. Luca Formaggia
57 Some utilities for handling ascii files
58 */
59 
60 /*! It gets a the next line from std::istream
61 */
62 std::istream& eatLine ( std::istream& s );
63 //!skip lines starting with '!%#;$'
64 std::istream& eatComments ( std::istream& s );
65 //! gets next uncommented line
66 std::istream& nextGoodLine ( std::istream& s, std::string& line );
67 /*!
68  always return a std::string with len characters
69  - if the s has more than len characters : keep only the first len
70  - if the s has less than len characters : complete with c until len
71 */
72 std::string& setStringLength ( std::string& s, unsigned int len, char c );
73 
74 
75 //! extends atoi to STL std::strings (from Stroustrup)
76 int atoi ( const std::string& s );
77 
78 std::string operator+ ( const std::string& str, const int i );
79 std::string operator+ ( const std::string& str, const long int i );
80 std::string operator+ ( const std::string& str, const unsigned int i );
81 
82 template <typename EntryType>
83 void parseList ( const std::string& slist, std::list<EntryType>& list )
84 {
85  std::string stringList = slist;
86  if ( slist == "" )
87  {
88  return;
89  }
90 
91  int commaPos = 0;
92 
93  while ( commaPos != (int) std::string::npos )
94  {
95  commaPos = stringList.find ( "," );
96 
97  std::stringstream stream;
98  stream << stringList.substr ( 0, commaPos ).c_str();
99 
100  EntryType var;
101  stream >> var;
102  list.push_back ( var );
103 
104  stringList = stringList.substr ( commaPos + 1 );
105  }
106 }
107 
108 // @author Cristiano Malossi
109 // Convert a std::string to a number ( Int, bool, Real, ... )
110 inline Real string2number ( const std::string& s )
111 {
112  std::stringstream out;
113  out << s;
114 
115  Real n;
116  out >> n;
117 
118  return n;
119 
120  // Temporary disabled
121  //return std::to_string( s );
122 }
123 
124 // @author Cristiano Malossi
125 // Convert a number ( Int, bool, Real, ... ) to a std::string
126 template <typename NumberType>
127 inline std::string number2string ( const NumberType& n )
128 {
129  return boost::lexical_cast< std::string > ( n );
130  //return std::to_string ( n );
131 }
132 
133 // @author Cristiano Malossi
134 // Convert an Enum to a std::string using a map as a library for conversion
135 template < typename EnumeratorType >
136 inline std::string enum2String ( const EnumeratorType& Enum,
137  const std::map < std::string,
138  EnumeratorType > & Map )
139 {
140  for ( typename std::map<std::string, EnumeratorType>::const_iterator j = Map.begin(); j != Map.end() ; ++j )
141  if ( j->second == Enum )
142  {
143  return j->first;
144  }
145 
146  return "NO_TYPE_FOUND";
147 }
148 
149 // @author Cristiano Malossi
150 // Convert a string made by NumberTypes separated by commas, to a vector of numbers
151 template< typename NumberType >
152 void string2numbersVector ( const std::string& string,
153  std::vector< NumberType >& numberVector )
154 {
155  //Split the string
156  std::vector< std::string > stringVector;
157  boost::split ( stringVector, string, boost::is_any_of ( "," ) );
158 
159  //Convert to the right type
160  for ( UInt i ( 0 ); i < static_cast<UInt> ( stringVector.size() ); ++i )
161  {
162  numberVector.push_back ( static_cast<NumberType> ( std::atoi ( stringVector[i].c_str() ) ) );
163  }
164 }
165 
166 } // Namespace LifeV
167 
168 #endif // STRING_UTILITY_H
std::istream & eatLine(std::istream &s)
std::string operator+(const std::string &str, const long i)
void updateInverseJacobian(const UInt &iQuadPt)
std::istream & eatComments(std::istream &s)
skip lines starting with &#39;!%#;$&#39;
double Real
Generic real data.
Definition: LifeV.hpp:175