]> pilppa.org Git - libplp.git/blobdiff - src/Store.cc
w1 independent file cleanups.
[libplp.git] / src / Store.cc
diff --git a/src/Store.cc b/src/Store.cc
new file mode 100644 (file)
index 0000000..ba29ed1
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * Store.cc
+ *
+ *  Created on: Jan 20, 2011
+ *      Author: lamikr
+ */
+#include <fstream>
+
+#include "log.h"
+#include "Store.hh"
+
+using namespace std;
+using namespace plp;
+
+Store::Store(string device_id_param,
+               Date *date_param) {
+       device_id       = device_id_param;
+       if (date_param != NULL) {
+               date            = date_param->clone();
+       }
+       else {
+               date    = NULL;
+       }
+       store_data      = NULL;
+       range_data      = NULL;
+}
+
+Store::~Store() {
+       if (store_data != NULL) {
+               delete(store_data);
+               store_data      = NULL;
+       }
+       if (date != NULL) {
+               delete(date);
+       }
+}
+
+bool Store::load(string fname_param) {
+       Data            *data;
+       ifstream        in;
+       string          data_str;
+       bool            ret_val;
+
+       ret_val = false;
+       if (store_data != NULL) {
+               delete(store_data);
+               store_data      = NULL;
+       }
+       if (access(fname_param.c_str(), R_OK) == 0) {
+               //log_debug("opening file: %s\n", fname_param.c_str());
+               in.open(fname_param.c_str());
+               if (in.is_open() == true) {
+                       while (in.eof() == false) {
+                               getline(in, data_str);
+                               if (data_str.empty() == false) {
+                                       data    = Data::parse_string(data_str);
+                                       if (data != NULL) {
+                                               if (store_data == NULL) {
+                                                       store_data      = new DataRange(data);
+                                               }
+                                               else {
+                                                       store_data->add(data);
+                                               }
+                                               delete(data);
+                                       }
+                               }
+                       }
+                       ret_val = true;
+               }
+               else {
+                       log_error("Could not open data file: %s\n", fname_param.c_str());
+               }
+       }
+       else {
+               log_error("Could not find file: %s\n", fname_param.c_str());
+       }
+       return ret_val;
+}