]> pilppa.org Git - lib1wire.git/blob - src/W1Store.cc
a6bbaa0b1be78a7ee650fcfa4e7d5061c20139dd
[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 #include <dirent.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18
19 #include "W1Store.hh"
20
21 using namespace std;
22 using namespace w1;
23
24 std::string W1Store::location = "/tmp/";
25
26 W1Store::W1Store() {
27         // TODO Auto-generated constructor stub
28 }
29
30 W1Store::~W1Store() {
31         // TODO Auto-generated destructor stub
32 }
33
34 void W1Store::set_location(string location_param) {
35         string::size_type pos;
36
37         location        = location_param;
38         pos             = location.find_last_of("/");
39         if (pos != location.length()) {
40                 location        = location + "/";
41         }
42
43 }
44
45 void W1Store::store(std::string device_id, std::list<std::string> *string_list) {
46
47         string file_path;
48         string text_line;
49
50         time_t          wtime;
51         struct tm       *ltime;
52         char            buffer[80];
53         string          year;
54         string          month;
55         string          date;
56
57         time(&wtime);
58         ltime   = localtime(&wtime);
59         strftime(buffer, 80, "%Y", ltime);
60         year    = buffer;
61         strftime(buffer, 80, "%m", ltime);
62         month   = buffer;
63         strftime(buffer, 80, "%Y-%m-%d", ltime);
64         date    = buffer;
65
66         struct tm * gmtime(const time_t *timer);
67         struct tm * localtime(const time_t * timer);
68         struct stat st;
69
70         file_path       = location + year;
71         if (stat(file_path.c_str() ,&st) != 0) {
72                 mkdir(file_path.c_str(), 0755);
73         }
74         file_path       = file_path + "/" + month;
75         if (stat(file_path.c_str() ,&st) != 0) {
76                 mkdir(file_path.c_str(), 0755);
77         }
78         file_path       = file_path + "/" + device_id + "_" + date + ".txt";
79         ofstream data_file(file_path.c_str(), ios::app);
80         cout << "storing to " << file_path << ", data size " << string_list->size() << endl;
81         // TODO: add mutex to protect string_list while it's read and emptied
82         if (data_file.is_open()) {
83                 while(string_list->size() > 0) {
84                         text_line       = string_list->front();
85                         string_list->pop_front();
86                         if (text_line.length() > 0) {
87                                 cout << "storing line: " << text_line << endl;
88                                 data_file << text_line << endl;
89                         }
90                 }
91                 data_file.close();
92         }
93         else {
94                 cout << "could not open file " << file_path << " for writing data." << endl;
95         }
96 }