]> pilppa.org Git - libplp.git/blob - src/Store.cc
ba29ed17cc66050b2f0ae15e3188df384b97279b
[libplp.git] / src / Store.cc
1 /*
2  * Store.cc
3  *
4  *  Created on: Jan 20, 2011
5  *      Author: lamikr
6  */
7 #include <fstream>
8
9 #include "log.h"
10 #include "Store.hh"
11
12 using namespace std;
13 using namespace plp;
14
15 Store::Store(string device_id_param,
16                 Date *date_param) {
17         device_id       = device_id_param;
18         if (date_param != NULL) {
19                 date            = date_param->clone();
20         }
21         else {
22                 date    = NULL;
23         }
24         store_data      = NULL;
25         range_data      = NULL;
26 }
27
28 Store::~Store() {
29         if (store_data != NULL) {
30                 delete(store_data);
31                 store_data      = NULL;
32         }
33         if (date != NULL) {
34                 delete(date);
35         }
36 }
37
38 bool Store::load(string fname_param) {
39         Data            *data;
40         ifstream        in;
41         string          data_str;
42         bool            ret_val;
43
44         ret_val = false;
45         if (store_data != NULL) {
46                 delete(store_data);
47                 store_data      = NULL;
48         }
49         if (access(fname_param.c_str(), R_OK) == 0) {
50                 //log_debug("opening file: %s\n", fname_param.c_str());
51                 in.open(fname_param.c_str());
52                 if (in.is_open() == true) {
53                         while (in.eof() == false) {
54                                 getline(in, data_str);
55                                 if (data_str.empty() == false) {
56                                         data    = Data::parse_string(data_str);
57                                         if (data != NULL) {
58                                                 if (store_data == NULL) {
59                                                         store_data      = new DataRange(data);
60                                                 }
61                                                 else {
62                                                         store_data->add(data);
63                                                 }
64                                                 delete(data);
65                                         }
66                                 }
67                         }
68                         ret_val = true;
69                 }
70                 else {
71                         log_error("Could not open data file: %s\n", fname_param.c_str());
72                 }
73         }
74         else {
75                 log_error("Could not find file: %s\n", fname_param.c_str());
76         }
77         return ret_val;
78 }