source: trunk/wutils.h @ 26

Last change on this file since 26 was 7, checked in by willem, 11 years ago

willem

File size: 3.0 KB
Line 
1
2/*
3   Copyright 2013 Willem Vermin, SURFsara
4
5   Licensed under the Apache License, Version 2.0 (the "License");
6   you may not use this file except in compliance with the License.
7   You may obtain a copy of the License at
8
9       http://www.apache.org/licenses/LICENSE-2.0
10
11   Unless required by applicable law or agreed to in writing, software
12   distributed under the License is distributed on an "AS IS" BASIS,
13   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   See the License for the specific language governing permissions and
15   limitations under the License.
16 */
17#ifndef WUTILS_H
18#define WUTILS_H
19#include <vector>
20#include <string>
21#include <sstream>
22#include <iomanip>
23#include <sys/types.h>
24#include <list>
25#include <dirent.h>
26std::vector <std::string> split(std::vector <std::string> &v, const std::string s, const char sep);
27char* str_to_charp(const std::string s);
28
29template <typename T>
30  std::string NumberToString ( T Number )
31  {
32    std::ostringstream ss;
33    ss << Number;
34    return ss.str();
35  }
36template <typename T>
37  std::string NumberToString ( T Number,int w)
38  {
39    std::ostringstream ss;
40    ss <<std::setfill('0') << std::setw(w) << Number;
41    return ss.str();
42  }
43template <typename T>
44  T StringToNumber ( const std::string &Text )
45  {
46    std::istringstream ss(Text);
47    T result;
48    return ss >> result ? result : 0;
49  }
50
51std::string strtohex(const std::string s);
52std::string hextostr(const std::string s);
53const static std::string ZBEGIN_ = "ZZZ";
54const static std::string ZEND_   = "YY";
55
56std::string zstrtohex(const std::string s);
57std::string zhextostr(const std::string s);
58
59std::string gen_random(const int len);
60
61int get_file_contents(std::string &contents,const std::string &filename);
62
63inline std::string trim_right(
64  const std::string& s,
65  const std::string& delimiters = " \f\n\r\t\v" )
66{
67  size_t p = s.find_last_not_of(delimiters);
68  if (p == s.npos)
69    return "";
70  return s.substr( 0, p + 1 );
71}
72
73inline std::string trim_left(
74  const std::string& s,
75  const std::string& delimiters = " \f\n\r\t\v" )
76{
77  size_t p = s.find_first_not_of(delimiters);
78  if (p == s.npos)
79    return "";
80  return s.substr(p);
81}
82
83inline std::string trim(
84  const std::string& s,
85  const std::string& delimiters = " \f\n\r\t\v" )
86{
87  return trim_left( trim_right( s, delimiters ), delimiters );
88}
89
90int mkdir_p(const std::string &s,mode_t m);
91
92std::string envtostr(const std::string &s);
93
94int get_dir_list(std::list <std::string> &v,
95                 const std::string       &d);
96
97size_t count_lines(std::ifstream &f);
98
99class progress_bar
100{
101  private:
102    double timer;
103    double pfrac;
104    size_t pnum;
105    bool firsttime;
106    double walltime;
107    double wallc();
108    std::string pshow;
109    size_t bar_length;
110
111  public:
112    progress_bar();
113    void clear();
114    void set_timer(float t);
115    std::string show(size_t num, double frac,bool &changed);
116    std::string show(size_t num, double frac);
117    std::string show(size_t num, bool &changed);
118    std::string show(size_t num);
119
120};
121#endif
Note: See TracBrowser for help on using the repository browser.