aboutsummaryrefslogtreecommitdiff
path: root/apps/SoilMoistureIo.h
diff options
context:
space:
mode:
Diffstat (limited to 'apps/SoilMoistureIo.h')
-rw-r--r--apps/SoilMoistureIo.h141
1 files changed, 88 insertions, 53 deletions
diff --git a/apps/SoilMoistureIo.h b/apps/SoilMoistureIo.h
index b8f0b52..473c098 100644
--- a/apps/SoilMoistureIo.h
+++ b/apps/SoilMoistureIo.h
@@ -11,6 +11,7 @@
#include <boost/lexical_cast.hpp>
#include <functional>
+// TODO: rename to trygvis::sample
namespace trygvis {
namespace soil_moisture {
@@ -43,10 +44,9 @@ unique_ptr<SampleStreamParser> open_sample_input_stream(
sample_format_type type = sample_format_type::AUTO);
unique_ptr<SampleOutputStream> open_sample_output_stream(
- KeyDictionary &dict,
- sample_format_type type,
unique_ptr<ostream> output,
- o<vector<SampleKey>> fields = o<vector<SampleKey>>());
+ KeyDictionary &dict,
+ sample_format_type type);
class sample_exception : public runtime_error {
public:
@@ -54,61 +54,102 @@ public:
}
};
+class KeyDictionary;
+
+using SampleKeyVector = vector<SampleKey *>;
+using SampleKeyIndex = SampleKeyVector::size_type;
+
struct SampleKey {
- // TODO: only the dictionary should be able to create keys
- SampleKey(string &name) : name(name) {
+private:
+ SampleKey(const SampleKey& that) = delete;
+ SampleKey(SampleKeyIndex index, const string &name) : index(index), name(name) {
if (name.length() == 0) {
throw sample_exception("Bad sample key.");
}
}
+public:
+ friend class KeyDictionary;
+
inline
bool operator==(const SampleKey &that) const {
return name == that.name;
}
- string name;
+ const SampleKeyIndex index;
+ const string name;
};
class KeyDictionary {
public:
- typedef vector<SampleKey> v;
- typedef v::size_type index_t;
-
KeyDictionary() {
}
- index_t indexOf(const SampleKey key) {
- index_t i = 0;
- for (auto ptr = keys.begin(); ptr != keys.end(); ptr++, i++) {
- if (*ptr == key) {
- return i;
+ ~KeyDictionary() {
+ std::for_each(keys.begin(), keys.end(), std::default_delete<SampleKey>());
+ }
+ KeyDictionary(KeyDictionary& that) = delete;
+
+ SampleKey *indexOf(const string key) {
+ SampleKeyIndex i = 0;
+ for (auto ptr = keys.cbegin(); ptr != keys.cend(); ptr++, i++) {
+ if ((*ptr)->name == key) {
+ return *ptr;
}
}
- keys.push_back(key);
+ i = keys.size();
+ auto sample_key = new SampleKey(i, key);
+ keys.push_back(sample_key);
+
+ return sample_key;
+ }
+
+ SampleKey *at(SampleKeyIndex i) const {
+ if (i >= keys.size()) {
+ throw sample_exception("Out of bounds");
+ }
+
+ return keys.at(i);
+ }
+
+ vector<SampleKey *> findIndexes(SampleKeyVector &keys) {
+ vector<SampleKey *> indexes;
+
+ for (auto &key: keys) {
+ auto index = indexOf(key->name);
+ indexes.push_back(index);
+ }
- return keys.size() - 1;
+ return move(indexes);
}
- vector<index_t> findIndexes(v keys);
+ inline
+ SampleKeyVector::const_iterator end() const {
+ return keys.cend();
+ }
inline
- v::const_iterator begin() {
- return keys.begin();
+ SampleKeyVector::const_iterator begin() const {
+ return keys.cbegin();
}
+// string nameOf(SampleKeyIndex index) {
+// return keys.at(index).name;
+// }
+
inline
- v::const_iterator end() {
- return keys.end();
+ SampleKeyVector::size_type size() const {
+ return keys.size();
}
- string nameOf(index_t index) {
- return keys.at(index).name;
+ inline
+ bool empty() const {
+ return keys.empty();
}
private:
- v keys;
+ SampleKeyVector keys;
};
class SampleRecord {
@@ -137,7 +178,8 @@ public:
return values.empty();
}
- o<string> at(size_t index) {
+ o<string> at(const SampleKey *key) const {
+ SampleKeyIndex index = key->index;
if (index >= values.size()) {
return o<string>();
}
@@ -145,15 +187,15 @@ public:
return values.at(index);
}
- void set(const KeyDictionary::index_t index, const std::string &value) {
- values.resize(max(values.size(), index + 1));
+ void set(const SampleKey *key, const std::string &value) {
+ values.resize(max(values.size(), key->index + 1));
- values[index] = o<string>(value);
+ values.assign(key->index, o<string>(value));
}
template<class A>
- const o<A> lexical_at(KeyDictionary::index_t index) {
- auto value = at(index);
+ const o<A> lexical_at(const SampleKey *key) {
+ auto value = at(key);
if (!value) {
return o<A>();
@@ -163,7 +205,7 @@ public:
}
string to_string() {
- KeyDictionary::index_t i = 0;
+ SampleKeyIndex i = 0;
string s;
for (auto ptr = values.begin(); ptr != values.end(); ptr++, i++) {
auto o = *ptr;
@@ -174,13 +216,13 @@ public:
auto value = o.get();
- s += dict.nameOf(i) + " = " + value + ", ";
+ s += dict.at(i)->name + " = " + value + ", ";
}
return s;
}
-private:
KeyDictionary &dict;
+private:
vec values;
};
@@ -200,49 +242,42 @@ public:
class CsvSampleOutputStream : public SampleOutputStream {
public:
- CsvSampleOutputStream(KeyDictionary &dict, unique_ptr<ostream> stream);
-
- CsvSampleOutputStream(KeyDictionary &dict, unique_ptr<ostream> stream, vector<SampleKey> fields);
+ CsvSampleOutputStream(unique_ptr<ostream> stream, KeyDictionary &dict);
- void write(SampleRecord values);
+ void write(SampleRecord sample);
+ const KeyDictionary &getDict() {
+ return dict;
+ }
+
private:
void writeHeader();
KeyDictionary &dict;
unique_ptr<ostream> stream;
bool headerWritten;
- vector<KeyDictionary::index_t> fields;
};
class JsonSampleOutputStream : public SampleOutputStream {
public:
- JsonSampleOutputStream(KeyDictionary &dict, unique_ptr<ostream> stream);
-
- JsonSampleOutputStream(KeyDictionary &dict, unique_ptr<ostream> stream, vector<SampleKey> fields);
+ JsonSampleOutputStream(unique_ptr<ostream> stream, KeyDictionary &dict);
- void write(SampleRecord values);
+ void write(SampleRecord sample) override;
private:
KeyDictionary &dict;
unique_ptr<ostream> stream;
- bool filterFields;
- vector<KeyDictionary::index_t> fields;
};
class SqlSampleOutputStream : public SampleOutputStream {
public:
- SqlSampleOutputStream(KeyDictionary &dict, unique_ptr<ostream> stream, string table_name);
-
- SqlSampleOutputStream(KeyDictionary &dict, unique_ptr<ostream> stream, string table_name, vector<SampleKey> fields);
+ SqlSampleOutputStream(unique_ptr<ostream> stream, KeyDictionary &dict, string table_name);
- void write(SampleRecord values);
+ void write(SampleRecord sample) override;
private:
KeyDictionary &dict;
unique_ptr<ostream> stream;
- bool filter_fields;
- vector<KeyDictionary::index_t> fields;
const string table_name;
};
@@ -264,8 +299,8 @@ protected:
class CsvSampleParser : public SampleStreamParser {
public:
- CsvSampleParser(KeyDictionary &dict, shared_ptr<SampleOutputStream> output) :
- SampleStreamParser(sample_format_type::CSV), dict(dict), output(output),
+ CsvSampleParser(shared_ptr<SampleOutputStream> output, KeyDictionary &dict) :
+ SampleStreamParser(sample_format_type::CSV), output(output), dict(dict),
line(make_shared<vector<uint8_t>>()) {
}
@@ -282,7 +317,7 @@ private:
class AutoSampleParser : public SampleStreamParser {
public:
- AutoSampleParser(KeyDictionary &dict, shared_ptr<SampleOutputStream> output);
+ AutoSampleParser(shared_ptr<SampleOutputStream> output, KeyDictionary &dict);
private:
unique_ptr<SampleStreamParser> parser;