-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRTDFileData.cpp
More file actions
139 lines (119 loc) · 2.94 KB
/
RTDFileData.cpp
File metadata and controls
139 lines (119 loc) · 2.94 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#include "RTDFileData.h"
#include "RTDFileDLL.h" // RTDFile_Version
#include "time.h"
#include "Tuple.h"
#include "stdio.h"
#define EMPTY ""
static std::string Now()
{
char now[200];
time_t t;
struct tm* tmp;
t = time(NULL);
tmp = localtime(&t);
if (tmp != NULL)
strftime(now, sizeof(now), "%H:%M:%S", tmp);
return now;
}
bool SplitCell(const char* Cell, int* row, int* col)
{
bool okay = false;
char c = '\0';
if (sscanf(Cell, "%c%d", &c, row) == 2) {
*row = *row - 1; // 0-relative
if (c >= 'a' && c <= 'z') {
*col = c - 'a';
okay = true;
} else if (c >= 'A' && c <= 'Z') {
*col = c - 'A';
okay = true;
}
}
return okay;
}
/**
* Returns the value in the specified RTD tab-separated file
*
* @param[in] Args A 2-tuple of filename and cell
*
* @return The value of the cell in the filename. If the filename/cell
* cannot be found, the EMPTY string is returned.
*/
std::string RTDFileData::LookupData(const std::string& Args)
{
// See http://www.cplusplus.com/reference/string/string/
int argc = 0;
std::string out, filename, cell;
if ((argc = Tuple::Split(Args, &filename, &cell)) > 0) {
if (filename == "=version") {
out = RTDFile_Version;
} else if (filename == "=email") {
out = "rtdfile@p1software.com";
} else if (filename == "=web") {
out = "http://p1sofware.com/rtdfile/";
} else if (filename == "=help") {
out = "http://p1sofware.com/rtdfile/help.html";
} else if (filename == "=now") {
out = Now();
} else {
// out = m_Data[Args];
// if (out == "") {
FILE *f = fopen(filename.c_str(), "r");
if (f == NULL) {
out = "Err opening '" + filename + "'";
} else {
if (cell == "=rows") {
char line[256] = "";
int rows = 0;
while (fgets(line, sizeof(line), f) != 0) {
rows++;
}
char str[20] = "";
itoa(rows, str, 10);
out = str;
} else {
int cellcol = 0;
int cellrow = 0;
if (SplitCell(cell.c_str(), &cellrow, &cellcol)) {
char line[256] = "";
for (int row = 0; row <= cellrow; row++) {
if (fgets(line, sizeof(line), f) == 0) {
#if 0
// Don't want this to display in the Excel cell
out = "No row "; // No such row
char num[10] = "";
itoa(cellrow, num, 10);
out += num;
#else
out = EMPTY;
#endif
} else if (row == cellrow) {
int len = strlen(line);
while (len > 0 && (line[len-1] == '\n' || line[len-1] == '\r')) {
line[len-1] = '\0';
len--;
}
if (!Tuple::Get(line, cellcol, &out)) {
#if 0
// Don't want this to display in the Excel cell
out = "No col "; // No such col
char num[10] = "";
itoa(cellcol, num, 10);
out += num;
#else
out = EMPTY;
#endif
}
}
}
} else {
out = "Err reading cell '" + cell + "' in file '" + filename + "'";
}
}
fclose(f);
}
// }
}
}
return out;
}