| Bsoft 2.0.5 Bernard's software package

string_util.cpp
Go to the documentation of this file.
1
9 #include "string_util.h"
10 #include "utilities.h"
11
12 // Declaration of global variables
13 extern int verbose; // Level of output to the screen
14
15
16 string command_line(int argc, char** argv)
17 {
18 string cl(argv[0]);
19
20 for ( long i=1; i<argc; ++i ) {
21 cl += " ";
22 cl += argv[i];
23 }
24
25 return cl;
26 }
27
28 string base(string& filename) {
29 return filename.substr(0, filename.rfind("."));
30 }
31
32 string extension(string filename)
33 {
34 string ext(filename.substr(filename.rfind(".")+1));
35
36 transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
37
38 return ext;
39 }
40
41 string replace_extension(string& filename, string ext)
42 {
43 string newname = filename.substr(0, filename.rfind(".")+1);
44
45 return newname + ext;
46 }
47
48 string insert(string& filename, string ins)
49 {
50 string newname = filename.substr(0, filename.rfind("."));
51 string ext = filename.substr(filename.rfind(".")+1);
52
53 newname += ins + ext;
54
55 return newname;
56 }
57
69 string parameter_file_path(string& filename)
70 {
71 string path;
72
73 if ( getenv("BPARAM") ) {
74 path = getenv("BPARAM");
75 if ( path[-1] != ']' && path[-1] != '/')
76 path += "/";
77 } else
78 path = "/usr/local/bsoft/parameters/";
79 path += filename;
80
81 if ( verbose & VERB_DEBUG )
82 cout << "DEBUG parameter_file_path: " << path << endl;
83
84 return path;
85 }
86
92 vector<string> split(const string &s)
93 {
94 istringstream ss(s);
95 vector<string> sv((istream_iterator<string>(ss)),
96 istream_iterator<string>());
97 return sv;
98 }
99
106 vector<string> split(const string &s, char delim)
107 {
108 stringstream ss(s);
109 string item;
110 vector<string> tokens;
111
112 while (getline(ss, item, delim)) {
113 tokens.push_back(item);
114 }
115
116 return tokens;
117 }
118
119 vector<long> parse_integer_vector(string vecstr)
120 {
121 vector<string> sv(split(vecstr, ','));
122
123 vector<long> vec;
124
125 for ( auto it = sv.begin(); it != sv.end(); ++it )
126 vec.push_back(stol(*it));
127
128 return vec;
129 }
130
131 vector<double> parse_real_vector(string vecstr)
132 {
133 vector<string> sv(split(vecstr, ','));
134
135 vector<double> vec;
136
137 for ( auto it = sv.begin(); it != sv.end(); ++it )
138 vec.push_back(stod(*it));
139
140 return vec;
141 }
142
143 string local_time()
144 {
145 chrono::system_clock::time_point t = chrono::system_clock::now();
146
147 time_t tt = chrono::system_clock::to_time_t(t);
148
149 string st = ctime(&tt);
150
151 st.erase(st.length()-1);
152
153 // cout << "Time: " << st << "." << endl;
154
155 return st;
156 }
157
158
base
string base(string &filename)
Definition: string_util.cpp:28
verbose
int verbose
command_line
string command_line(int argc, char **argv)
Definition: string_util.cpp:16
parse_real_vector
vector< double > parse_real_vector(string vecstr)
Definition: string_util.cpp:131
insert
string insert(string &filename, string ins)
Definition: string_util.cpp:48
utilities.h
Header file for general utilities.
replace_extension
string replace_extension(string &filename, string ext)
Definition: string_util.cpp:41
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
parameter_file_path
string parameter_file_path(string &filename)
Finds the parameter file path.
Definition: string_util.cpp:69
string_util.h
parse_integer_vector
vector< long > parse_integer_vector(string vecstr)
Definition: string_util.cpp:119
extension
string extension(string filename)
Definition: string_util.cpp:32
local_time
string local_time()
Definition: string_util.cpp:143
split
vector< string > split(const string &s)
Splits a string on whitespace.
Definition: string_util.cpp:92