This repository was archived by the owner on Jan 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv.cpp
More file actions
122 lines (110 loc) · 2.1 KB
/
Copy pathcsv.cpp
File metadata and controls
122 lines (110 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* FogLAMP south service plugin
*
* Copyright (c) 2018 Dianomic Systems
*
* Released under the Apache 2.0 Licence
*
* Author: Mark Riddoch
*/
#include <csv.h>
#include <config_category.h>
#include <reading.h>
#include <stdio.h>
#include <stdlib.h>
#include <logger.h>
#include <stdexcept>
using namespace std;
/**
* Constructor for the csv "sensor"
*/
Csv::Csv()
{
}
/**
* Destructor for the csv "sensor"
*/
Csv::~Csv()
{
}
void Csv::configure(ConfigCategory *config)
{
setAssetName(config->getValue("asset"));
setFile(config->getValue("file"));
setDatapoint(config->getValue("datapoint"));
setMultiColumn(config->getValue("multicolumn"));
m_fp = fopen(m_file.c_str(), "r");
if (m_fp == NULL)
{
throw runtime_error("Unable to open file");
}
if (m_multiColumn)
{
char buf[1024];
if (fgets(buf, sizeof(buf), m_fp))
{
m_columnCount = 1;
for (char *p = buf; *p; ++p)
{
if (*p == ',')
{
m_columnCount++;
}
}
}
fseek(m_fp, 0L, SEEK_SET);
}
}
/**
* Take a reading from the csv "sensor"
*/
Reading Csv::nextValue()
{
char buffer[132], *ptr, *eptr;
int ch;
vector<Datapoint *> values;
if (m_multiColumn)
{
if (fgets(buffer, sizeof(buffer), m_fp) == NULL)
{
fseek(m_fp, 0L, SEEK_SET);
(void)fgets(buffer, sizeof(buffer), m_fp);
}
ptr = buffer;
int column = 1;
do {
double val = strtof(ptr, &eptr);
DatapointValue dpv((double)val);
char dpName[80];
snprintf(dpName, sizeof(dpName), "%s%d", m_datapoint.c_str(), column++);
values.push_back(new Datapoint(dpName, dpv));
if (eptr)
{
ptr = eptr;
while (*ptr && (*ptr == ',' || isspace(*ptr)))
{
ptr++;
}
}
} while (eptr && *ptr);
}
else
{
ptr = buffer;
while ((ch = fgetc(m_fp)) != EOF && ! (ch == '\n' || ch == ',')
&& ptr - buffer < sizeof(buffer))
{
*ptr++ = ch;
}
*ptr = 0;
if (ch == EOF)
{
fseek(m_fp, 0L, SEEK_SET);
}
double val = strtof(buffer, NULL);
DatapointValue value(val);
values.push_back(new Datapoint(m_datapoint, value));
}
Reading reading(m_asset, values);
return reading;
}