using namespace std;
using namespace w1;
+using namespace plp;
template <class NumberDataType>
bool string_to_number(NumberDataType& result,
W1Util::~W1Util() {
}
-void W1Util::printout_date(struct tm *date_time) {
- 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;
-}
-
-void W1Util::printout_data(Data *data) {
- int ii;
- if (data != NULL) {
- printout_date(data->date_time);
- for (ii = 0; ii < data->data_list.size(); ii++) {
- log_debug(" data[%d] = %f\n", ii, data->data_list.at(ii));
- }
- }
-}
-
char *W1Util::parse_directory_path(const char *file_path) {
char *p;
size_t b_count;
return ret_val;
}
-bool W1Util::mkdirs(char *path) {
+bool W1Util::mkdirs(const char *path) {
bool ret_val;
char *p;
int err_flg;
ret_val = true;
if (path != NULL) {
// go through each directory one by and and create if not exist
- for (p = path; *p; p++) {
+ for (p = (char *)path; *p; p++) {
if ((p != path) &&
((*p == '/') ||
(*p == '\\'))) {
*p = '\0';
- // test whether directory exist and is writable
- if (access(path, F_OK)) {
+ // if dir does not exist, create it
+ if (access(path, F_OK) != 0) {
log_debug("trying to create directory: %s\n", path);
err_flg = mkdir(path, S_IRWXU);
if (err_flg != 0) {
}
}
if (ret_val == true) {
- // test also the existense of whole directory
- if (access(path, F_OK)) {
+ // if dir does not exist, create it
+ if (access(path, F_OK) != 0) {
log_debug("trying to create directory: %s\n", path);
err_flg = mkdir(path, S_IRWXU);
if (err_flg != 0) {
std::ofstream *W1Util::open_for_writing(const char *f_path) {
char *d_path;
- char *p;
size_t b_count;
- int ii;
ofstream *ret_val;
bool b_flg;
return ret_val;
}
-struct tm *W1Util::parse_date_str(string date_str) {
- stringstream ss(date_str);
- struct tm *ret_val = NULL;
-
- //ss << "2007-07-19 17:18:01";
- int year;
- int month;
- int day;
- int hour;
- int min;
- int sec;
- char c;
-
- ret_val = (struct tm*)malloc(sizeof(struct tm));
- 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;
- mktime(ret_val);
- //printout_date(ret_val);
- return ret_val;
-}
-
-Data *W1Util::parse_data_line(const string& dataline) {
- stringstream ss(dataline);
- string item;
- double dbl;
- Data *ret_val;
- int ii;
- bool suc_flg;
-
- ret_val = new Data();
- ii = 0;
- while(getline(ss, item, '|')) {
- if (ii == 0) {
- // parse date
- ret_val->date_time = parse_date_str(item);
- }
- else if (ii == 1) {
- // skip the device type for now.Maybe better to store only once under the year dir...
- }
- else if (ii >= 2) {
- suc_flg = string_to_number<double>(dbl, item, dec);
- if (suc_flg) {
- ret_val->data_list.push_back(dbl);
- }
- }
- ii++;
- }
- return ret_val;
-}
-
-Data *W1Util::load_first_data_row(const string& datafile_path) {
- Data *ret_val;
- ifstream in;
- string line;
+Date W1Util::parse_date_str(string date_str) {
+ char c;
+ stringstream ss(date_str);
+ Date ret_val;
- ret_val = NULL;
- in.open(datafile_path.c_str());
- if (in.eof() == false) {
- getline(in, line);
- ret_val = parse_data_line(line);
- }
- return ret_val;
-}
-
-Data *W1Util::load_last_data_row(const string& datafile_path) {
- Data *ret_val;
- ifstream in;
- string line;
- string prev_line;
-
- ret_val = NULL;
- in.open(datafile_path.c_str());
- while (in.eof() == false) {
- getline(in, line);
- if (line.empty() == false) {
- prev_line = line;
- }
- }
- if (prev_line.empty() == false) {
- ret_val = parse_data_line(prev_line);
- }
+ ss >>ret_val.year >>c >>ret_val.month >>c >>ret_val.day >>ret_val.hour >>c >>ret_val.min >>c >>ret_val.sec;
return ret_val;
}