blob: de7ec4571df3f12094ea6de3b82a9904c6ecc640 [file] [log] [blame]
Ratan Gupta309ac442016-12-13 20:40:06 +05301#pragma once
Patrick Venture537ff142018-11-01 16:37:09 -07002
3#include <array>
Ratan Gupta309ac442016-12-13 20:40:06 +05304#include <fstream>
Patrick Venture537ff142018-11-01 16:37:09 -07005#include <iostream>
Ratan Gupta309ac442016-12-13 20:40:06 +05306#include <sstream>
Patrick Venture537ff142018-11-01 16:37:09 -07007#include <string>
8
Ratan Gupta309ac442016-12-13 20:40:06 +05309namespace slp
10{
11struct ConfigData
12{
13 std::string name;
14 std::string type;
15 std::string port;
16
17 friend std::istream& operator>>(std::istream& str, ConfigData& data)
18 {
19 std::string line;
20 constexpr auto DELIMITER = " ";
Patrick Venture537ff142018-11-01 16:37:09 -070021 size_t delimtrPos = 0;
22 size_t delimtrPrevPos = 0;
23 std::array<std::string, 3> tokens;
Ratan Gupta309ac442016-12-13 20:40:06 +053024 std::getline(str, line);
25 size_t count = 0;
26
27 delimtrPos = line.find(DELIMITER, delimtrPrevPos);
28 while (delimtrPos != std::string::npos)
29 {
Patrick Venture537ff142018-11-01 16:37:09 -070030 tokens[count] =
31 line.substr(delimtrPrevPos, (delimtrPos - delimtrPrevPos));
Ratan Gupta309ac442016-12-13 20:40:06 +053032 delimtrPrevPos = delimtrPos + 1;
33
34 delimtrPos = line.find(DELIMITER, delimtrPrevPos);
Patrick Venture537ff142018-11-01 16:37:09 -070035 if (delimtrPos == std::string::npos &&
36 delimtrPrevPos < line.length())
Ratan Gupta309ac442016-12-13 20:40:06 +053037 {
38 delimtrPos = line.length();
39 }
40
41 count++;
42 }
43
44 if (count > 2)
45 {
46 data.name = tokens[0];
47 data.type = tokens[1];
48 data.port = tokens[2];
49 }
50 else
51 {
52 str.setstate(std::ios::failbit);
53 }
54 return str;
55 }
Ratan Gupta309ac442016-12-13 20:40:06 +053056};
Patrick Venture537ff142018-11-01 16:37:09 -070057} // namespace slp