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> |
---|
26 | std::vector <std::string> split(std::vector <std::string> &v, const std::string s, const char sep); |
---|
27 | char* str_to_charp(const std::string s); |
---|
28 | |
---|
29 | template <typename T> |
---|
30 | std::string NumberToString ( T Number ) |
---|
31 | { |
---|
32 | std::ostringstream ss; |
---|
33 | ss << Number; |
---|
34 | return ss.str(); |
---|
35 | } |
---|
36 | template <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 | } |
---|
43 | template <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 | |
---|
51 | std::string strtohex(const std::string s); |
---|
52 | std::string hextostr(const std::string s); |
---|
53 | const static std::string ZBEGIN_ = "ZZZ"; |
---|
54 | const static std::string ZEND_ = "YY"; |
---|
55 | |
---|
56 | std::string zstrtohex(const std::string s); |
---|
57 | std::string zhextostr(const std::string s); |
---|
58 | |
---|
59 | std::string gen_random(const int len); |
---|
60 | |
---|
61 | int get_file_contents(std::string &contents,const std::string &filename); |
---|
62 | |
---|
63 | inline 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 | |
---|
73 | inline 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 | |
---|
83 | inline 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 | |
---|
90 | int mkdir_p(const std::string &s,mode_t m); |
---|
91 | |
---|
92 | std::string envtostr(const std::string &s); |
---|
93 | |
---|
94 | int get_dir_list(std::list <std::string> &v, |
---|
95 | const std::string &d); |
---|
96 | |
---|
97 | size_t count_lines(std::ifstream &f); |
---|
98 | |
---|
99 | class 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 |
---|