]> pilppa.org Git - lib1wire.git/blob - src/W1Store.cc
storage data cleanup. allow specifying storage dir location
[lib1wire.git] / src / W1Store.cc
1 /*
2  * W1Store.cc
3  *
4  *  Created on: Oct 31, 2010
5  *      Author: lamikr
6  */
7
8 #include <list>
9 #include <string>
10 #include <iostream>
11 #include <fstream>
12
13 #include <time.h>
14
15 #include "W1Store.hh"
16
17 using namespace std;
18 using namespace w1;
19
20 std::string W1Store::location = "/tmp/";
21
22 W1Store::W1Store() {
23         // TODO Auto-generated constructor stub
24 }
25
26 W1Store::~W1Store() {
27         // TODO Auto-generated destructor stub
28 }
29
30 void W1Store::set_location(string location_param) {
31         location        = location_param;
32 }
33
34 void W1Store::store(std::string device_id, std::list<std::string> string_list) {
35
36         string file_path = location + device_id + ".txt";
37         string text_line;
38         ofstream data_file(file_path.c_str(), ios::app);
39
40         cout << "storing to " << file_path << ", data size " << string_list.size() << endl;
41         // TODO: add mutex to protect string_list while it's read and emptied
42         if (data_file.is_open()) {
43                 while(string_list.size() > 0) {
44                         text_line       = string_list.front();
45                         string_list.pop_front();
46                         if (text_line.length() > 0) {
47                                 cout << "storing line: " << text_line << endl;
48                                 data_file << text_line << endl;
49                         }
50                 }
51                 data_file.close();
52         }
53         else {
54                 cout << "could not open file " << file_path << " for writing data." << endl;
55         }
56 }