string_utils.h

Go to the documentation of this file.
00001 /*
00002  *  This file is part of libcxxsupport.
00003  *
00004  *  libcxxsupport is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  libcxxsupport is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with libcxxsupport; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  */
00018 
00019 /*
00020  *  libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
00021  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00022  *  (DLR).
00023  */
00024 
00025 /*! \file string_utils.h
00026  *  Various functions for manipulating strings.
00027  *
00028  *  Copyright (C) 2002-2012 Max-Planck-Society
00029  *  \author Martin Reinecke
00030  */
00031 
00032 #ifndef PLANCK_STRING_UTILS_H
00033 #define PLANCK_STRING_UTILS_H
00034 
00035 #include <vector>
00036 #include <map>
00037 #include "datatypes.h"
00038 
00039 /*! \defgroup stringutilsgroup String handling helper functions */
00040 /*! \{ */
00041 
00042 /*! Returns the string \a orig without leading and trailing whitespace. */
00043 std::string trim (const std::string &orig);
00044 
00045 /*! Returns a string containing the text representation of \a x.
00046     Care is taken that no information is lost in the conversion. */
00047 template<typename T> std::string dataToString(const T &x);
00048 template<> std::string dataToString (const bool &x);
00049 template<> std::string dataToString (const std::string &x);
00050 template<> std::string dataToString (const float &x);
00051 template<> std::string dataToString (const double &x);
00052 template<> std::string dataToString (const long double &x);
00053 
00054 /*! Returns a string containing the text representation of \a x, padded
00055     with leading zeroes to \a width characters. */
00056 std::string intToString(int64 x, tsize width);
00057 
00058 /*! Reads a value of a given datatype from a string */
00059 template<typename T> void stringToData (const std::string &x, T &value);
00060 template<> void stringToData (const std::string &x, std::string &value);
00061 template<> void stringToData (const std::string &x, bool &value);
00062 
00063 /*! Reads a value of a given datatype from a string */
00064 template<typename T> inline T stringToData (const std::string &x)
00065   { T result; stringToData(x,result); return result; }
00066 
00067 /*! Parses the file \a filename and returns the key/value pairs in \a dict. */
00068 void parse_file (const std::string &filename,
00069   std::map<std::string,std::string> &dict);
00070 
00071 void parse_cmdline_classic (int argc, const char **argv,
00072   const std::vector<std::string> &leading_args,
00073   std::map<std::string,std::string> &dict);
00074 
00075 void parse_cmdline_classic (int argc, const char **argv,
00076   std::map<std::string,std::string> &dict);
00077 
00078 void parse_cmdline_equalsign (int argc, const char **argv,
00079   const std::vector<std::string> &leading_args,
00080   std::map<std::string,std::string> &dict);
00081 
00082 void parse_cmdline_equalsign (int argc, const char **argv,
00083   std::map<std::string,std::string> &dict);
00084 
00085 /*! Case-insensitive string comparison
00086     Returns \a true, if \a a and \a b differ only in capitalisation,
00087     else \a false. */
00088 bool equal_nocase (const std::string &a, const std::string &b);
00089 
00090 /*! Returns lowercase version of \a input. */
00091 std::string tolower(const std::string &input);
00092 
00093 /*! Tries to split \a inp into a white-space separated list of values of
00094     type \a T, and appends them to \a list. */
00095 template<typename T> void split (const std::string &inp, std::vector<T> &list);
00096 
00097 /*! Breaks the string \a inp into tokens separated by \a delim, and returns them
00098     in \a list. */
00099 void tokenize (const std::string &inp, char delim,
00100   std::vector<std::string> &list);
00101 
00102 /*! Reads all white-space separated strings from \a filename, and returns
00103     them in \a words. */
00104 void parse_words_from_file (const std::string &filename,
00105   std::vector<std::string> &words);
00106 
00107 /*! \} */
00108 
00109 #endif

Generated on Thu Oct 8 14:48:51 2015 for LevelS C++ support library