Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 1 | #pragma once |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 2 | |
| 3 | #include <array> |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 4 | #include <fstream> |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 5 | #include <iostream> |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 6 | #include <sstream> |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 7 | #include <string> |
| 8 | |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 9 | namespace slp |
| 10 | { |
| 11 | struct 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 Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 21 | size_t delimtrPos = 0; |
| 22 | size_t delimtrPrevPos = 0; |
| 23 | std::array<std::string, 3> tokens; |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 24 | std::getline(str, line); |
| 25 | size_t count = 0; |
| 26 | |
| 27 | delimtrPos = line.find(DELIMITER, delimtrPrevPos); |
| 28 | while (delimtrPos != std::string::npos) |
| 29 | { |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 30 | tokens[count] = |
| 31 | line.substr(delimtrPrevPos, (delimtrPos - delimtrPrevPos)); |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 32 | delimtrPrevPos = delimtrPos + 1; |
| 33 | |
| 34 | delimtrPos = line.find(DELIMITER, delimtrPrevPos); |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 35 | if (delimtrPos == std::string::npos && |
| 36 | delimtrPrevPos < line.length()) |
Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 37 | { |
| 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 Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 56 | }; |
Patrick Venture | 537ff14 | 2018-11-01 16:37:09 -0700 | [diff] [blame] | 57 | } // namespace slp |