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; }
293 bool convert(
const std::string& s, T& t);
306 void operator()(
const std::string& element, std::vector<T>& vec)
309 vec.push_back(std::numeric_limits<T>::quiet_NaN());
311 vec.push_back(theplu::yat::utility::convert<T>(element));
322 struct VectorPusher<std::string>
327 void operator()(
const std::string& element, std::vector<std::string>& vec)
329 vec.push_back(element);
347 void mkdir(
const std::string& dir, mode_t mode=0777);
356 void mkdir_p(
const std::string& dir, mode_t mode=0777);
365 void remove(
const std::string& fn);
375 void rename(
const std::string& from,
const std::string to);
383 void replace(std::string& full_str, std::string old_str, std::string new_str);
387 template<
typename InputIterator,
typename OutputIterator>
389 OutputIterator result)
392 while (first!=last) {
393 if (std::isnan(*first)) {
410 std::ostringstream ss;
421 throw runtime_error(std::string(
"yat::utility::convert(\"")+s+
428 bool is(
const std::string& s)
436 void load(std::istream&
is, std::vector<std::vector<T> >& matrix,
437 char sep,
char line_sep,
bool ignore_empty,
440 size_t nof_columns=0;
442 while(getline(is, line, line_sep)){
443 if (line.empty() && ignore_empty)
445 matrix.push_back(std::vector<T>());
446 std::vector<T>& v=matrix.back();
447 v.reserve(nof_columns);
448 std::stringstream ss(line);
451 detail::VectorPusher<T> pusher;
452 if(sep!=
'\0' && !line.empty() && line[line.size()-1]==sep)
455 if (rectangle && nof_columns && v.size()!=nof_columns) {
456 std::ostringstream s;
457 s <<
"load stream error: "
458 <<
"line " << matrix.size() <<
" has " << v.size()
459 <<
" columns; expected " << nof_columns <<
" columns.";
462 nof_columns =
std::max(nof_columns, v.size());
466 is.clear(std::ios::goodbit);
470 void load(std::istream&
is, std::vector<T>& vec,
char sep)
472 detail::VectorPusher<T> pusher;
479 ok=getline(is, element, sep);
482 pusher(element, vec);
489 bool convert(
const std::string& s, T& result)
491 if (!std::numeric_limits<T>::is_signed) {
493 std::string::const_iterator iter = s.begin();
494 while (iter!=s.end() && std::isspace(*iter))
498 if (iter==s.end() || *iter==
'-')
501 std::istringstream ss(s);
505 result = std::numeric_limits<T>::quiet_NaN();
509 result = std::numeric_limits<T>::infinity();
514 YAT_ASSERT(std::numeric_limits<T>::is_signed);
515 result = -std::numeric_limits<T>::infinity();