/* -*- mia-c++ -*-
* Copyright (c) 2004
* Max-Planck-Institute for Human Cognitive and Brain Science
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
// $Id: miaUtils.hh 887 2006-03-01 12:22:14Z write1 $
/*! \brief Some easy cache, string, and file manipulation tools
\file miaUtils.hh
\author G. Wollny, wollny@cbs.mpg.de, 2004
\author M. Tittgemeyer, tittge@cbs.mpg.de, 2004
*/
#ifndef __MIA_TOOLS_HH
#define __MIA_TOOLS_HH 1
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
//#pragma interface
#include <list>
#include <string>
#include <sstream>
#include <vector>
#include <stdexcept>
#include <mia/core/defines.hh>
NS_MIA_BEGIN
template <class T>
class TCachedValue {
bool cached;
T value;
public:
TCachedValue() : cached(false){}
bool is_valid() const {return cached;}
void invalidate() {cached = false;}
const T& get() const {return value;}
void set(const T& v){value = v; cached=true;};
};
/**
a helper class that stores the current working directory on construction
and goes back to the it when it is destroyed
*/
class CCWDSaver {
char *cwd;
public:
CCWDSaver();
~CCWDSaver();
};
/*! a functor to search for files */
class FSearchFiles {
std::list<std::string>& result;
const std::string pattern;
public:
/** constructor of functor
\param __result takes an (empty) list to which the found files will be stored
\param __pattern holds the search pattern
*/
FSearchFiles(std::list<std::string>& __result, const std::string& __pattern);
/** search the given \a path with the stored search pattern and add the found files to result */
void operator()(const std::string& path);
};
/*!
\param str string
\param chr the character to search
erase char from the begin of str to the last
occurence of ch from and return the string
*/
std::string erase_to_last_of(std::string const& str, char chr);
/*!
\param str string
\param chr character
split string s by first occurence of char c, returning the second part.
str is set to the first part. Neither include the split character.
*/
std::string split(std::string& str, char chr);
/*!
return true if prefix is a prefix of str.
behavior is undefined if prefix is an empty string
*/
bool is_prefix(std::string const & str, std::string const & prefix);
/*!
\param str the string to tokenize
\param sep the separator_char
separate fields in a string in a list of token;
field are separated by the sep character,
sep char can be escaped by '\\' to specify a sep char in a token,
'\\' not followed by a sep is taken as is e.g. "\,\a" --> ",\a"
*/
std::vector<std::string> separate_token(std::string const & str, char sep);
/// remove trim chars from start of input string return the new string
std::string ltrim(std::string const & str, std::string const & totrim = "\t ");
/// remove trim chars from end of input string return the new string
std::string rtrim(std::string const & str, std::string const & totrim = "\t ");
/// ltrim(rtrim(str))
std::string trim(std::string const & str, std::string const & totrim = "\t ");
/*! format_percent - smart format of double percentage value
\param value - the value
\param int_width - the maximum integer integer width default to 2
\param frac_width - the fractionnary width default to 4
This formats a percentage into exactly the given width and returns it.
If the integer part is larger than the given int_width,
the returned string will be wider.
The returned string is never shorter than (fract_with + int_width + 1)
*/
std::string const
format_percent(double value, size_t int_width, size_t frac_width);
/// prefered width to format percentage
static unsigned int const percent_int_width = 2;
static unsigned int const percent_fract_width = 4;
static unsigned int const percent_width = percent_int_width + percent_fract_width + 1;
/*!
\param src input parameter
Convert From src to a T through an istringstream.
Throws invalid_argument if conversion fail.
Note that this is not as foolproof as boost's lexical_cast
*/
template <typename To, typename From>
To op_lexical_cast(From const & src)
{
std::ostringstream in;
if (!(in << src)) {
throw std::invalid_argument("mia_lexical_cast<T>()");
}
std::istringstream out(in.str());
To value;
if (!(out >> value)) {
throw std::invalid_argument("mia_lexical_cast<T>(\"" +
in.str() +"\")");
}
return value;
}
/*!
Specialization accepting hexadecimal and octal number in input.
Note that mia_lexical_cast<unsigned int>("0x23") will fail
because it calls the non specialized template.
*/
template <>
unsigned int op_lexical_cast<unsigned int>(std::string const & str);
NS_MIA_END
#endif