-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbcpload.cxx
114 lines (100 loc) · 2.52 KB
/
rbcpload.cxx
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <cctype>
#include <algorithm>
#include "rbcp.h"
std::vector<std::string> strsplit(std::string str)
{
std::vector<std::string> vstr;
std::string separator = std::string(" ");
int separator_len = separator.length();
size_t offset = std::string::size_type(0);
while(true) {
size_t pos = str.find(separator, offset);
if (pos == std::string::npos) {
if (str.substr(offset).length() > 0) {
vstr.push_back(str.substr(offset));
}
break;
} else {
if (pos - offset > 0) {
vstr.push_back(str.substr(offset, pos - offset));
}
offset = pos + separator_len;
}
}
return vstr;
}
int readfile(char *filename)
{
int port = 0x1234;
std::string host("192.168.10.16");
std::ifstream ifs(filename);
std::string str;
if (ifs.fail()) {
std::cerr << "Fail to read " << filename << std::endl;
return -1;
}
while (std::getline(ifs, str)) {
//std::cout << "> " << str << std::endl;
std::vector<std::string> list = strsplit(str);
/*
std::cout << "#";
for (unsigned int i = 0 ; i < list.size() ; i++) {
std::cout << "/" << list[i];
}
std::cout << "/" << std::endl;
*/
for (unsigned int i = 0 ; i < list.size() ; i++) {
if ((list.size() > i + 1) &&
((list[i] == "HOST") || list[i] == "host")) {
host = list[++i];
std::cout << "HOST : " << host << std::endl;
}
if ((list.size() > i + 1) &&
((list[i] == "PORT") || list[i] == "port")) {
std::istringstream ss(list[++i]);
ss >> port;
std::cout << "PORT : " << port << std::endl;
}
}
if (list.size() > 1) {
try {
unsigned int addr = std::stoi(list[0], NULL, 0);
std::cout << std::setw(8) << addr << " : ";
std::vector<int> values;
for (unsigned int i = 1 ; i < list.size() ; i++) {
try {
values.push_back(std::stoi(list[i], NULL, 0));
std::cout << " " << values.back();
} catch (const std::exception &e) {
std::cout << "#E " << e.what() << std::endl;
}
}
std::cout << std::endl;
if (values.size() > 0) {
char buf[values.size() + 16];
for (unsigned int i = 0 ; i < values.size() ; i++) {
buf[i] = static_cast<char>(values[i] & 0xff);
}
int sock = rbcp_open(const_cast<char *>(host.c_str()), port);
rbcp_write(sock, buf, addr, values.size());
rbcp_close(sock);
}
} catch (const std::exception &e) {
}
}
}
return 0;
}
int main(int argc, char* argv[])
{
for (int i = 1 ; i < argc ; i++) {
readfile(argv[i]);
}
return 0;
}