4 * Created on: Nov 14, 2010
21 #include "W1Configure.hh"
26 template <class NumberDataType>
27 bool string_to_number(NumberDataType& result,
28 const std::string& string_param,
29 std::ios_base& (*format)(std::ios_base&))
31 std::istringstream iss(string_param);
32 return !(iss >> format >> result).fail();
41 void W1Util::printout_date(struct tm *date_time) {
42 cout << "date: " << date_time->tm_year << " " << date_time->tm_mon << " " << date_time->tm_mday << " " << date_time->tm_hour << " " << date_time->tm_min << " " << date_time->tm_sec << endl;
45 void W1Util::printout_data(Data *data) {
48 printout_date(data->date_time);
49 for (ii = 0; ii < data->data_list.size(); ii++) {
50 log_debug(" data[%d] = %f\n", ii, data->data_list.at(ii));
55 char *W1Util::parse_directory_path(const char *file_path) {
62 if (file_path != NULL) {
64 b_count = strlen(file_path);
65 for (p = &((char *)file_path)[b_count]; p != (char *)file_path; p--) {
69 b_count = (b_count - f_size) + 1;
70 ret_val = strndup(file_path, b_count);
71 log_debug("dir: %s\n", ret_val);
79 bool W1Util::mkdirs(char *path) {
86 // go through each directory one by and and create if not exist
87 for (p = path; *p; p++) {
92 // test whether directory exist and is writable
93 if (access(path, F_OK)) {
94 log_debug("trying to create directory: %s\n", path);
95 err_flg = mkdir(path, S_IRWXU);
104 if (ret_val == true) {
105 // test also the existense of whole directory
106 if (access(path, F_OK)) {
107 log_debug("trying to create directory: %s\n", path);
108 err_flg = mkdir(path, S_IRWXU);
117 log_error("Could not create NULL directory\n");
122 std::ofstream *W1Util::open_for_writing(const char *f_path) {
131 if (f_path != NULL) {
132 b_count = strlen(f_path);
133 if ((f_path[b_count - 1] != '/') &&
134 (f_path[b_count - 1] != '\\')) {
135 ret_val = new ofstream();
136 ret_val->open(f_path, ios::app);
137 if (ret_val->is_open() == false) {
138 d_path = parse_directory_path(f_path);
139 if (d_path != NULL) {
140 b_flg = mkdirs(d_path);
144 ret_val->open(f_path, ios::app);
149 log_error("Could not open file, invalid file name. (%s)\n", f_path);
153 log_error("Could not open file, invalid file name. (= NULL)\n");
158 string W1Util::concat_paths(string path_start, string path_end) {
159 return concat_paths(path_start.c_str(), path_end.c_str());
162 string W1Util::concat_paths(const char *path_start, const char *path_end) {
168 if (path_start != NULL) {
169 ret_val = path_start;
170 if (path_end != NULL) {
172 b_count = ret_val.length();
173 pos = ret_val.find_last_of("/");
174 if (pos == (b_count -1)) {
175 ret_val.append(end_str);
178 pos = end_str.find_first_of("/");
180 ret_val.append(end_str);
184 ret_val.append(end_str);
190 if (path_end != NULL) {
197 bool W1Util::is_subdirectory(const char *path, dirent *direntry) {
199 struct stat stat_info;
203 if (direntry != NULL) {
204 if ((strcmp(direntry->d_name, ".") == 0) ||
205 (strcmp(direntry->d_name, "..") == 0)) {
209 fname = concat_paths(path, direntry->d_name);
210 stat(fname.c_str(), &stat_info);
211 ret_val = S_ISDIR(stat_info.st_mode);
212 //log_debug("stat for: %s: %d\n", fname.c_str(), ret_val);
218 bool W1Util::is_datafile(const char *path, dirent *direntry) {
220 struct stat stat_info;
226 if (direntry != NULL) {
227 name = direntry->d_name;
228 pos = name.find(DATAFILE_SUFFIX);
230 fname = concat_paths(path, direntry->d_name);
231 stat(fname.c_str(), &stat_info);
232 ret_val = S_ISREG(stat_info.st_mode);
239 * get sub-directories sorted in alphabetical order.
241 vector<string> W1Util::get_subdirectories(const string& path) {
245 vector<string> ret_val;
247 log_debug("scanning path: %s\n", path.c_str());
249 if (path.empty() == false) {
250 dir = opendir(path.c_str());
254 direntry = readdir(dir);
255 if (direntry != NULL) {
256 bool_flg = is_subdirectory(path.c_str(), direntry);
257 if (bool_flg == true) {
258 ret_val.push_back(string(direntry->d_name));
259 //log_debug("added dir: %s\n", direntry->d_name);
267 sort(ret_val.begin(), ret_val.end());
274 * get sub-directories sorted in alphabetical order.
276 vector<string> W1Util::get_data_files(const string& path) {
279 vector<string> ret_val;
282 if (path.empty() == false) {
283 dir = opendir(path.c_str());
287 direntry = readdir(dir);
288 if (direntry != NULL) {
289 if (is_datafile(path.c_str(), direntry) == true) {
290 ret_val.push_back(string(direntry->d_name));
298 sort(ret_val.begin(), ret_val.end());
304 struct tm *W1Util::parse_date_str(string date_str) {
305 stringstream ss(date_str);
306 struct tm *ret_val = NULL;
308 //ss << "2007-07-19 17:18:01";
317 ret_val = (struct tm*)malloc(sizeof(struct tm));
318 ss >>ret_val->tm_year >>c >>ret_val->tm_mon >>c >>ret_val->tm_mday >>ret_val->tm_hour >>c >>ret_val->tm_min >>c >>ret_val->tm_sec;
320 //printout_date(ret_val);
324 Data *W1Util::parse_data_line(const string& dataline) {
325 stringstream ss(dataline);
332 ret_val = new Data();
334 while(getline(ss, item, '|')) {
337 ret_val->date_time = parse_date_str(item);
340 // skip the device type for now.Maybe better to store only once under the year dir...
343 suc_flg = string_to_number<double>(dbl, item, dec);
345 ret_val->data_list.push_back(dbl);
353 Data *W1Util::load_first_data_row(const string& datafile_path) {
359 in.open(datafile_path.c_str());
360 if (in.eof() == false) {
362 ret_val = parse_data_line(line);
367 Data *W1Util::load_last_data_row(const string& datafile_path) {
374 in.open(datafile_path.c_str());
375 while (in.eof() == false) {
377 if (line.empty() == false) {
381 if (prev_line.empty() == false) {
382 ret_val = parse_data_line(prev_line);