]> pilppa.org Git - lib1wire.git/blobdiff - src/Data.cc
initial device configuration support.
[lib1wire.git] / src / Data.cc
index d9c6e1a70eb59dc00a93f009e057e15c4fc10fd1..ac7ed2d38c877416844e822030a0f0c0f649c923 100644 (file)
@@ -5,19 +5,32 @@
  *      Author: lamikr
  */
 
+#include <string>
 #include <iostream>
+#include <sstream>
 
 #include <stdio.h>
 #include <time.h>
 #include <malloc.h>
 
-#include "Data.hh"
 #include <plp/log.h>
 
+#include "Data.hh"
+#include "W1Util.hh"
+
 using namespace std;
 using namespace plp;
 using namespace w1;
 
+template <class NumberDataType>
+bool string_to_number(NumberDataType& result,
+                 const std::string& string_param,
+                 std::ios_base& (*format)(std::ios_base&))
+{
+       std::istringstream iss(string_param);
+       return !(iss >> format >> result).fail();
+}
+
 Data::Data(int size) {
        value_arr.resize(size);
 }
@@ -64,6 +77,37 @@ void Data::printout() {
        }
 }
 
+Data *Data::parse_data_string(const string& dataline) {
+       stringstream    ss(dataline);
+       string          item;
+       double          val;
+       Data            *ret_val;
+       int             ii;
+       bool            suc_flg;
+       vector<double>  v;
+       Date            date;
+
+       ii      = 0;
+       while(getline(ss, item, '|')) {
+               if (ii == 0) {
+                       // parse date
+                       date    = W1Util::parse_date_str(item);
+               }
+               // skip the device type and device id fields
+               // TODO: store device type and id to own file
+               else if (ii >= 3) {
+                       suc_flg = string_to_number<double>(val, item, dec);
+                       if (suc_flg) {
+                               //log_debug("adding number: %f\n", val);
+                               v.push_back(val);
+                       }
+               }
+               ii++;
+       }
+       ret_val = new Data(v, &date);
+       return ret_val;
+}
+
 DataRange::DataRange(int value_count_per_data_item) {
        val_matrix      = NULL;
        column_count    = value_count_per_data_item;