LevelS C++ support library  3.82
string_utils.h
Go to the documentation of this file.
1 /*
2  * This file is part of libcxxsupport.
3  *
4  * libcxxsupport is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * libcxxsupport is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with libcxxsupport; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
21  * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
22  * (DLR).
23  */
24 
25 /*! \file string_utils.h
26  * Various functions for manipulating strings.
27  *
28  * Copyright (C) 2002-2017 Max-Planck-Society
29  * \author Martin Reinecke
30  */
31 
32 #ifndef PLANCK_STRING_UTILS_H
33 #define PLANCK_STRING_UTILS_H
34 
35 #include <vector>
36 #include <map>
37 #include "datatypes.h"
38 
39 /*! \defgroup stringutilsgroup String handling helper functions */
40 /*! \{ */
41 
42 /*! Returns the string \a orig without leading and trailing whitespace. */
43 std::string trim (const std::string &orig);
44 
45 /*! Returns a string containing the text representation of \a x.
46  Care is taken that no information is lost in the conversion. */
47 template<typename T> std::string dataToString(const T &x);
48 template<> std::string dataToString (const bool &x);
49 template<> std::string dataToString (const std::string &x);
50 template<> std::string dataToString (const float &x);
51 template<> std::string dataToString (const double &x);
52 template<> std::string dataToString (const long double &x);
53 
54 /*! Returns a string containing the text representation of \a x, padded
55  with leading zeroes to \a width characters. */
56 std::string intToString(int64 x, tsize width);
57 
58 /*! Reads a value of a given datatype from a string */
59 template<typename T> void stringToData (const std::string &x, T &value);
60 template<> void stringToData (const std::string &x, std::string &value);
61 template<> void stringToData (const std::string &x, bool &value);
62 
63 /*! Reads a value of a given datatype from a string */
64 template<typename T> inline T stringToData (const std::string &x)
65  { T result; stringToData(x,result); return result; }
66 
67 /*! Parses the file \a filename and returns the key/value pairs in \a dict. */
68 void parse_file (const std::string &filename,
69  std::map<std::string,std::string> &dict);
70 
71 void parse_cmdline_classic (int argc, const char **argv,
72  const std::vector<std::string> &leading_args,
73  std::map<std::string,std::string> &dict);
74 
75 void parse_cmdline_classic (int argc, const char **argv,
76  std::map<std::string,std::string> &dict);
77 
78 void parse_cmdline_equalsign (int argc, const char **argv,
79  const std::vector<std::string> &leading_args,
80  std::map<std::string,std::string> &dict);
81 
82 void parse_cmdline_equalsign (int argc, const char **argv,
83  std::map<std::string,std::string> &dict);
84 
85 /*! Case-insensitive string comparison
86  Returns \a true, if \a a and \a b differ only in capitalisation,
87  else \a false. */
88 bool equal_nocase (const std::string &a, const std::string &b);
89 
90 /*! Returns lowercase version of \a input. */
91 std::string tolower(const std::string &input);
92 
93 /*! Tries to split \a inp into a white-space separated list of values of
94  type \a T, and appends them to \a list. */
95 template<typename T> void split (const std::string &inp, std::vector<T> &list);
96 
97 /*! Tries to split \a inp into a white-space separated list of values of
98  type \a T, and appends them to \a list. */
99 template<typename T> inline std::vector<T> split (const std::string &inp)
100  { std::vector<T> res; split(inp,res); return res; }
101 
102 /*! Breaks the string \a inp into tokens separated by \a delim, and returns them
103  in \a list. */
104 void tokenize (const std::string &inp, char delim,
105  std::vector<std::string> &list);
106 
107 /*! Breaks the string \a inp into tokens separated by \a delim, and returns them
108  as a vector<string>. */
109 inline std::vector<std::string> tokenize (const std::string &inp, char delim)
110  {
111  std::vector<std::string> res;
112  tokenize(inp,delim,res);
113  return res;
114  }
115 
116 /*! Reads all white-space separated strings from \a filename, and returns
117  them in \a words. */
118 void parse_words_from_file (const std::string &filename,
119  std::vector<std::string> &words);
120 
121 /*! \} */
122 
123 #endif
void tokenize(const std::string &inp, char delim, std::vector< std::string > &list)
std::string intToString(int64 x, tsize width)
Definition: string_utils.cc:93
void parse_words_from_file(const std::string &filename, std::vector< std::string > &words)
std::size_t tsize
Definition: datatypes.h:116
bool equal_nocase(const std::string &a, const std::string &b)
std::string trim(const std::string &orig)
void parse_file(const std::string &filename, std::map< std::string, std::string > &dict)
void stringToData(const std::string &x, T &value)
std::string dataToString(const T &x)
Definition: string_utils.cc:52
void split(const std::string &inp, std::vector< T > &list)
std::string tolower(const std::string &input)

Generated on Thu Jul 28 2022 17:32:06 for LevelS C++ support library