Repository navigation
Expand file tree
/
Copy pathgoogle-sheets-writer-from-file.cpp
More file actions
222 lines (194 loc) · 4.98 KB
/
Copy pathgoogle-sheets-writer-from-file.cpp
File metadata and controls
222 lines (194 loc) · 4.98 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* google-sheets-writer-ffrom-file.cpp
*/
#include <string.h>
#include <stdio.h>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <glog/logging.h>
#include <google/protobuf/message.h>
#if defined(_WIN32) || defined(_WIN64)
#else
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netdb.h>
#include <arpa/inet.h>
#endif
#include "google-sheets-writer-from-file.h"
#include "output-message.h"
#include "errorcodes.h"
#include "utilprotobuf.h"
#include "json/json.h"
#include "pbjson.hpp"
#include "pkt2optionscache.h"
#include "messagedecomposer.h"
#include "utilstring.h"
#include "fieldnamevalueindexstrings.h"
#include "str-pkt2.h"
#include "env-pkt2.h"
#include "packet-protobuf-message.h"
using namespace google::protobuf;
int format_number;
/**
* @brief MessageDecomposer callback. Use in conjunction with FieldNameValueIndexStrings class(see first parameter).
* @param env accumulates field names and values in the InsertSQLStrings object
* @param message_descriptor message
* @param field_type type of the data
* @param field_name field name
* @param value pointer to the data
* @param size size occupied by data
*
* @see FieldNameValueIndexStrings
*/
void addFieldValueString2
(
MessageDecomposer *decomposer,
void *env,
const google::protobuf::Descriptor *message_descriptor,
const google::protobuf::FieldDescriptor *field,
void* value,
int size,
int index
)
{
((FieldNameValueIndexStrings *) env)->add(field, decomposer->toString(message_descriptor, field, value, size, format_number), index, false);
}
/**
* @brief Print packet to the Google Sheet
* @param messageTypeNAddress message type
* @param message message
* @return 0 success
*/
int put
(
Config *config,
Pkt2OptionsCache *options,
MessageTypeNAddress *messageTypeNAddress,
const google::protobuf::Message *message
)
{
FieldNameValueIndexStrings vals(options, messageTypeNAddress->message_type);
MessageDecomposer md(&vals, messageTypeNAddress->message_type, options, message, addFieldValueString2);
if (config->verbosity > 1)
std::cerr << vals.toStringTab(&config->tableAliases, &config->fieldAliases, &config->properties);
int count = vals.values.size();
ValueRange newcells;
newcells.range = GoogleSheets::A1(config->sheet, 0, 0, count - 1, 0);
std::vector<std::string> row;
for (int i = 0; i < count; i++)
{
row.push_back(vals.values[i].value);
}
newcells.values.push_back(row);
if (!config->google_sheets->append(newcells))
return ERRCODE_GOOGLE_SHEET_WRITE;
return ERR_OK;
}
/**
* @brief Write line loop
* @param config configuration
* @return 0- success
* >0- error (see errorcodes.h)
*/
int run
(
Config *config
)
{
START:
// IN file
FILE *fin;
if (config->filename_input.empty()) {
fin = stdin;
} else {
fin = fopen(config->filename_input.c_str(), "r");
}
int fdin = 0;
if (fin)
fdin = fileno(fin);
if (fdin < 0)
{
LOG(ERROR) << ERR_NN_BIND << config->filename_input << " " << errno << ": " << strerror(errno);;
return ERRCODE_NN_BIND;
}
EnvPkt2 *strpkt2env = (EnvPkt2 *) initPkt2(config->proto_path, config->verbosity);
void *data = malloc(config->buffer_size);
while (!config->stop_request)
{
size_t packet_length = read(fdin, data, config->buffer_size);
if (packet_length == 0)
break;
if (config->allowed_packet_sizes.size())
{
if (std::find(config->allowed_packet_sizes.begin(), config->allowed_packet_sizes.end(), packet_length) == config->allowed_packet_sizes.end()) {
LOG(INFO) << MSG_PACKET_REJECTED << packet_length;
continue;
}
}
std::string s((char *) data, packet_length);
google::protobuf::Message *message;
if (parsePacket2Message(&message, strpkt2env, config->input_mode, s, config->force_message,
&config->tableAliases, &config->fieldAliases, &config->properties)) {
if (config->allowed_messages.size())
{
if (std::find(config->allowed_messages.begin(), config->allowed_messages.end(), message->GetTypeName()) == config->allowed_messages.end())
{
LOG(INFO) << MSG_PACKET_REJECTED << message->GetTypeName();
continue;
}
}
MessageTypeNAddress mta(message->GetTypeName());
int code = put(config, strpkt2env->options_cache, &mta, message);
if (code) {
LOG(ERROR) << ERR_GOOGLE_SHEET_WRITE;
} else {
if (config->verbosity >= 1) {
LOG(INFO) << MSG_SENT;
}
}
}
if (message)
delete message;
}
free(data);
donePkt2(strpkt2env);
if (fdin) {
fclose(fin);
fin = NULL;
fdin = 0;
}
if (config->stop_request == 2)
goto START;
return ERR_OK;
}
/**
* @brief Stop writer
* @param config
* @return 0- success
* >0- config is not initialized yet
*/
int stop
(
Config *config
)
{
if (!config)
{
LOG(ERROR) << ERR_STOP;
return ERRCODE_STOP;
}
config->stop_request = 1;
return ERR_OK;
}
int reload(Config *config)
{
if (!config)
return ERRCODE_NO_CONFIG;
LOG(ERROR) << MSG_RELOAD_BEGIN;
config->stop_request = 2;
return ERR_OK;
}