1 #pragma once 2 3 #include <array> 4 #include <fstream> 5 #include <iostream> 6 #include <sstream> 7 #include <string> 8 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 = " "; 21 size_t delimtrPos = 0; 22 size_t delimtrPrevPos = 0; 23 std::array<std::string, 3> tokens; 24 std::getline(str, line); 25 size_t count = 0; 26 27 delimtrPos = line.find(DELIMITER, delimtrPrevPos); 28 while (delimtrPos != std::string::npos) 29 { 30 tokens[count] = line.substr(delimtrPrevPos, 31 (delimtrPos - delimtrPrevPos)); 32 delimtrPrevPos = delimtrPos + 1; 33 34 delimtrPos = line.find(DELIMITER, delimtrPrevPos); 35 if (delimtrPos == std::string::npos && 36 delimtrPrevPos < line.length()) 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 } 56 }; 57 } // namespace slp 58