[go: up one dir, main page]

Menu

[r789]: / mia2 / mia / core / utils.hh  Maximize  Restore  History

Download this file

179 lines (149 with data), 5.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* -*- 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
#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 EXPORT_CORE CCWDSaver {
char *cwd;
public:
CCWDSaver();
~CCWDSaver();
};
#ifndef WIN32
/*! 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);
};
#endif
/*!
\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