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