Line data Source code
1 : #include <exception> 2 : #include <filesystem> 3 : #include <fstream> 4 : #include <iostream> 5 : #include <sstream> 6 : #include <string> 7 : #include <vector> 8 : 9 : #include "exceptions.hpp" 10 : #include "io.hpp" 11 : 12 : // Overloaded operator << to print out a formatted vector 13 1 : std::ostream &operator<<(std::ostream &out, const std::vector<double> &v) 14 : { 15 1 : size_t s{v.size() - 1}; 16 1 : out << "("; 17 31 : for (size_t i{0}; i < s; i++) out << v[ i ] << ", "; 18 1 : out << v[ s ] << ")\n"; 19 1 : return out; 20 : } 21 : 22 : // Function to convert a string of space-separated numbers into a vector 23 2673 : std::vector<double> read_numbers(const std::string &in) 24 : { 25 : // A vector to store the converted numbers 26 2673 : std::vector<double> v; 27 : // A string to store each substring we read with getline 28 5346 : std::string s; 29 : // Create an input string stream based on the function's argument 30 5346 : std::istringstream string_stream(in); 31 : // Read space-separated values with getline 32 90882 : while (std::getline(string_stream, s, ' ')) 33 : { 34 : // Convert each substring to a double with stod, and store it in the vector 35 88209 : v.push_back(stod(s)); 36 : } 37 5346 : return v; 38 : } 39 : 40 : // Fully qualified pathname to repository expected to be a parent directory 41 : // of the current path 42 3 : std::filesystem::path cwd_path_to(const std::string &name) 43 : { 44 4 : std::filesystem::path cwd = std::filesystem::current_path(); 45 3 : std::filesystem::path dn; 46 17 : for (auto it = cwd.begin(); it != cwd.end(); ++it) 47 : { 48 16 : dn /= *it; 49 18 : if (*it == name) return dn; 50 : } 51 2 : std::string message{"Folder is not a subdirectory of the current path"}; 52 1 : throw sayMessage(message); 53 : }