Ratan Gupta | 309ac44 | 2016-12-13 20:40:06 +0530 | [diff] [blame] | 1 | #pragma once |
| 2 | #include <iostream> |
| 3 | #include <fstream> |
| 4 | #include <sstream> |
| 5 | namespace slp |
| 6 | { |
| 7 | struct ConfigData |
| 8 | { |
| 9 | std::string name; |
| 10 | std::string type; |
| 11 | std::string port; |
| 12 | |
| 13 | friend std::istream& operator>>(std::istream& str, ConfigData& data) |
| 14 | { |
| 15 | std::string line; |
| 16 | constexpr auto DELIMITER = " "; |
| 17 | size_t delimtrPos = 0; |
| 18 | size_t delimtrPrevPos = 0; |
| 19 | std::array<std::string, 3>tokens; |
| 20 | std::getline(str, line); |
| 21 | size_t count = 0; |
| 22 | |
| 23 | delimtrPos = line.find(DELIMITER, delimtrPrevPos); |
| 24 | while (delimtrPos != std::string::npos) |
| 25 | { |
| 26 | tokens[count] = line.substr(delimtrPrevPos, (delimtrPos - delimtrPrevPos)); |
| 27 | delimtrPrevPos = delimtrPos + 1; |
| 28 | |
| 29 | delimtrPos = line.find(DELIMITER, delimtrPrevPos); |
| 30 | if (delimtrPos == std::string::npos && delimtrPrevPos < line.length()) |
| 31 | { |
| 32 | delimtrPos = line.length(); |
| 33 | } |
| 34 | |
| 35 | count++; |
| 36 | } |
| 37 | |
| 38 | if (count > 2) |
| 39 | { |
| 40 | data.name = tokens[0]; |
| 41 | data.type = tokens[1]; |
| 42 | data.port = tokens[2]; |
| 43 | } |
| 44 | else |
| 45 | { |
| 46 | str.setstate(std::ios::failbit); |
| 47 | } |
| 48 | return str; |
| 49 | } |
| 50 | |
| 51 | }; |
| 52 | } |