1 #ifndef _theplu_yat_utility_utility_
2 #define _theplu_yat_utility_utility_
35 #include "Exception.h"
36 #include "yat_assert.h"
38 #include <gsl/gsl_math.h>
67 std::string
basename(
const std::string& fn);
79 template<
typename InputIterator,
typename OutputIterator>
81 OutputIterator result);
93 void chdir(
const std::string& dir);
103 void chmod(
const std::string& filename, mode_t mode);
121 T
convert(
const std::string& s);
129 void copy_file(
const std::string& source,
const std::string& target);
141 std::string
dirname(
const std::string& fn);
153 bool fnmatch(
const std::string& pattern,
const std::string& str,
162 bool is(
const std::string& s);
170 bool is_double(
const std::string&) YAT_DEPRECATE;
180 bool is_equal(std::
string s, std::
string other);
188 bool is_float(const std::
string&) YAT_DEPRECATE;
196 bool is_int(const std::
string&) YAT_DEPRECATE;
201 bool is_nan(const std::
string& s);
239 void load(std::istream&
is, std::vector<std::vector<T> >& vec,
char sep='\0',
240 char line_sep='\n',
bool ignore_empty=false,
bool rectangle=true);
264 void load(std::istream& is, std::vector<T>& vec,
char sep='\0');
274 T
log2(T x) {
return std::log(x)/M_LN2; }
289 bool convert(
const std::string& s, T& t);
302 void operator()(
const std::string& element, std::vector<T>& vec)
305 vec.push_back(std::numeric_limits<T>::quiet_NaN());
307 vec.push_back(theplu::yat::utility::convert<T>(element));
323 void operator()(
const std::string& element, std::vector<std::string>& vec)
325 vec.push_back(element);
339 void mkdir(
const std::string& dir, mode_t mode=0777);
346 void mkdir_p(
const std::string& dir, mode_t mode=0777);
353 void remove(
const std::string& fn);
363 void rename(
const std::string& from,
const std::string to);
369 void replace(std::string& full_str, std::string old_str, std::string new_str);
373 template<
typename InputIterator,
typename OutputIterator>
375 OutputIterator result)
378 while (first!=last) {
379 if (std::isnan(*first)) {
396 std::ostringstream ss;
407 throw runtime_error(std::string(
"yat::utility::convert(\"")+s+
414 bool is(
const std::string& s)
422 void load(std::istream&
is, std::vector<std::vector<T> >& matrix,
423 char sep,
char line_sep,
bool ignore_empty,
426 size_t nof_columns=0;
428 while(getline(is, line, line_sep)){
429 if (line.empty() && ignore_empty)
431 matrix.push_back(std::vector<T>());
432 std::vector<T>& v=matrix.back();
433 v.reserve(nof_columns);
434 std::stringstream ss(line);
438 if(sep!=
'\0' && !line.empty() && line[line.size()-1]==sep)
441 if (rectangle && nof_columns && v.size()!=nof_columns) {
442 std::ostringstream s;
443 s <<
"load stream error: "
444 <<
"line " << matrix.size() <<
" has " << v.size()
445 <<
" columns; expected " << nof_columns <<
" columns.";
448 nof_columns =
std::max(nof_columns, v.size());
452 is.clear(std::ios::goodbit);
456 void load(std::istream&
is, std::vector<T>& vec,
char sep)
465 ok=getline(is, element, sep);
468 pusher(element, vec);
474 bool convert(
const std::string& s, T& result)
476 if (!std::numeric_limits<T>::is_signed) {
478 std::string::const_iterator iter = s.begin();
479 while (iter!=s.end() && std::isspace(*iter))
483 if (iter==s.end() || *iter==
'-')
486 std::istringstream ss(s);
490 result = std::numeric_limits<T>::quiet_NaN();
494 result = std::numeric_limits<T>::infinity();
499 YAT_ASSERT(std::numeric_limits<T>::is_signed);
500 result = -std::numeric_limits<T>::infinity();