]> pilppa.com Git - lib1wire.git/commitdiff
store each days file separately to year/month/id_year-month-day.txt subdir. Fix also...
authorMika Laitio <lamikr@pilppa.org>
Sun, 31 Oct 2010 23:20:43 +0000 (01:20 +0200)
committerMika Laitio <lamikr@pilppa.org>
Sun, 31 Oct 2010 23:20:43 +0000 (01:20 +0200)
src/W1Device.cc
src/W1Store.cc
src/W1Store.hh

index fef721c55ff7fc757767d9570de0ec66af7567cd..bc86d830fc6580c786d142f173f423a06151f138 100644 (file)
@@ -93,27 +93,5 @@ void W1Device::add_to_memory_cache(std::string formatted_data) {
 }
 
 void W1Device::store() {
-       W1Store::store(id, memory_cache);
-/*
-       string file_path = "/tmp/" + id + ".txt";
-       string text_line;
-       ofstream data_file(file_path.c_str(), ios::app);
-
-       cout << "storing to " << file_path << "data size " << memory_cache.size() << endl;
-       // TODO: add mutex to protect memory_cache while it's read and emptied
-       if (data_file.is_open()) {
-               while(memory_cache.size() > 0) {
-                       text_line       = memory_cache.front();
-                       memory_cache.pop_front();
-                       if (text_line.length() > 0) {
-                               cout << "storing line: " << text_line << endl;
-                               data_file << text_line << endl;
-                       }
-               }
-               data_file.close();
-       }
-       else {
-               cout << "could not open file " << file_path << " for writing data." << endl;
-       }
-*/
+       W1Store::store(id, &memory_cache);
 }
index 98015832e4f042098caa06b0958c41f7db55d155..a6bbaa0b1be78a7ee650fcfa4e7d5061c20139dd 100644 (file)
 #include <fstream>
 
 #include <time.h>
+#include <dirent.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
 
 #include "W1Store.hh"
 
@@ -28,21 +32,57 @@ W1Store::~W1Store() {
 }
 
 void W1Store::set_location(string location_param) {
+       string::size_type pos;
+
        location        = location_param;
+       pos             = location.find_last_of("/");
+       if (pos != location.length()) {
+               location        = location + "/";
+       }
+
 }
 
-void W1Store::store(std::string device_id, std::list<std::string> string_list) {
+void W1Store::store(std::string device_id, std::list<std::string> *string_list) {
 
-       string file_path = location + device_id + ".txt";
+       string file_path;
        string text_line;
-       ofstream data_file(file_path.c_str(), ios::app);
 
-       cout << "storing to " << file_path << ", data size " << string_list.size() << endl;
+       time_t          wtime;
+       struct tm       *ltime;
+       char            buffer[80];
+       string          year;
+       string          month;
+       string          date;
+
+       time(&wtime);
+       ltime   = localtime(&wtime);
+       strftime(buffer, 80, "%Y", ltime);
+       year    = buffer;
+       strftime(buffer, 80, "%m", ltime);
+       month   = buffer;
+       strftime(buffer, 80, "%Y-%m-%d", ltime);
+       date    = buffer;
+
+       struct tm * gmtime(const time_t *timer);
+       struct tm * localtime(const time_t * timer);
+       struct stat st;
+
+       file_path       = location + year;
+       if (stat(file_path.c_str() ,&st) != 0) {
+               mkdir(file_path.c_str(), 0755);
+       }
+       file_path       = file_path + "/" + month;
+       if (stat(file_path.c_str() ,&st) != 0) {
+               mkdir(file_path.c_str(), 0755);
+       }
+       file_path       = file_path + "/" + device_id + "_" + date + ".txt";
+       ofstream data_file(file_path.c_str(), ios::app);
+       cout << "storing to " << file_path << ", data size " << string_list->size() << endl;
        // TODO: add mutex to protect string_list while it's read and emptied
        if (data_file.is_open()) {
-               while(string_list.size() > 0) {
-                       text_line       = string_list.front();
-                       string_list.pop_front();
+               while(string_list->size() > 0) {
+                       text_line       = string_list->front();
+                       string_list->pop_front();
                        if (text_line.length() > 0) {
                                cout << "storing line: " << text_line << endl;
                                data_file << text_line << endl;
index abdc0b656140187c17b837774a8e158e0abb44b2..c096816533e5f9c07bdb2d38ddc5c59910944a55 100644 (file)
@@ -18,7 +18,7 @@ namespace w1 {
                        virtual ~W1Store();
                        static std::string location;
                        static void set_location(std::string location_param);
-                       static void store(std::string device_id, std::list<std::string> string_list);
+                       static void store(std::string device_id, std::list<std::string> *string_list);
        };
 }