12 typedef std::vector<string> tokens;
14 tokens
string_split(
const string& str,
const string& token);
24 char buf[G_SMALL_BUFFER];
25 bool controller_found =
false;
29 cout <<
"Searching...\n";
32 e(GIpRequests(buf, G_SMALL_BUFFER));
34 }
while (strlen(buf) <= 1);
39 for (tokens::iterator it = controllers.begin(); it != controllers.end(); it++)
41 const string& controller = *it;
43 tokens controller_params =
string_split(controller,
", ");
46 if (controller_params.size() < 5)
48 cerr <<
"Unexpected controller format";
49 return GALIL_EXAMPLE_ERROR;
51 const char* mac = controller_params[2].c_str();
52 const char* ip = controller_params[4].c_str();
54 if (
string(serial_num) == controller_params[1])
56 controller_found =
true;
57 int ip1 = 0, ip2 = 0, ip3 = 0, ip4 = 0;
59 sscanf(ip,
"%d.%d.%d.%d", &ip1, &ip2, &ip3, &ip4);
61 char ipaddress[G_SMALL_BUFFER];
63 sprintf(ipaddress,
"%d.%d.%d.%d", ip1, ip2, ip3, address);
66 e(GAssign(ipaddress, mac));
68 char info[G_SMALL_BUFFER];
70 e(GOpen(ipaddress, &g));
76 e(GInfo(g, info, G_SMALL_BUFFER));
83 if (!controller_found)
85 cout <<
"No controller matched the entered serial number";
88 return GALIL_EXAMPLE_OK;
95 int prev_position = 0 - token.length();
96 int position = str.find(token);
98 while (position != string::npos)
101 split.push_back(str.substr(prev_position + token.length(),
102 position - prev_position - token.length()));
103 prev_position = position;
104 position = str.find(token, prev_position + token.length());
108 if (str.length() > prev_position + token.length())
111 split.push_back(str.substr(prev_position + token.length(),
112 str.length() - prev_position - token.length()));
tokens string_split(const string &str, const string &token)
Splits a string into a vector based on a token.
GReturn ip_assigner(char *serial_num, int address)
Assigns controller an IP Adress given a serial number and a 1 byte address.
void e(GReturn rc)
A trivial, C++ style return code check used in Galil's examples and demos.