]> pilppa.org Git - lib1wire.git/blobdiff - src/W1Util.cc
Optimization for year and monthly data calculation.
[lib1wire.git] / src / W1Util.cc
index 001f9f63c293d5677f3aa9a9bfd67ba4e1d18329..a9fc351b9755a28e3cd18c17a94f693127108aea 100644 (file)
@@ -22,6 +22,7 @@
 
 using namespace std;
 using namespace w1;
+using namespace plp;
 
 template <class NumberDataType>
 bool string_to_number(NumberDataType& result,
@@ -38,20 +39,6 @@ W1Util::W1Util() {
 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;
@@ -76,7 +63,7 @@ char *W1Util::parse_directory_path(const char *file_path) {
        return ret_val;
 }
 
-bool W1Util::mkdirs(char *path) {
+bool W1Util::mkdirs(const char *path) {
        bool    ret_val;
        char    *p;
        int     err_flg;
@@ -84,16 +71,17 @@ bool W1Util::mkdirs(char *path) {
        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)) {
-                                       log_debug("trying to create directory: %s\n", path);
+                               // 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) {
+                                               log_error("Could not create directory: %s\n", path);
                                                ret_val = false;
                                                break;
                                        }
@@ -102,11 +90,12 @@ bool W1Util::mkdirs(char *path) {
                        }
                }
                if (ret_val == true) {
-                       // test also the existense of whole directory
-                       if (access(path, F_OK)) {
-                               log_debug("trying to create directory: %s\n", path);
+                       // 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) {
+                                       log_error("Could not create directory: %s\n", path);
                                        ret_val = false;
                                }
                        }
@@ -121,9 +110,7 @@ bool W1Util::mkdirs(char *path) {
 
 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;
 
@@ -244,7 +231,7 @@ vector<string> W1Util::get_subdirectories(const string& path) {
        bool            bool_flg;
        vector<string>  ret_val;
 
-       log_debug("scanning path: %s\n", path.c_str());
+       //log_debug("scanning path: %s\n", path.c_str());
        errno   = 0;
        if (path.empty() == false) {
                dir     = opendir(path.c_str());
@@ -301,85 +288,11 @@ vector<string> W1Util::get_data_files(const string& path) {
        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;
 }