Repo-Init
 
Loading...
Searching...
No Matches
FileHelpers.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <fstream>
4#include <regex>
5#include <string>
6#include <vector>
7
15inline std::vector<std::string> findFromFile(const std::string &filePath, const std::string &pattern,
16 std::string &lastWord)
17{
18 const std::regex regExp(pattern);
19 std::ifstream inFile(filePath);
20 std::vector<std::string> matchedLines;
21
22 std::string readLine;
23 while (getline(inFile, readLine))
24 {
25 if (std::regex_search(readLine, regExp))
26 {
27 matchedLines.push_back(readLine);
28 }
29 }
30
31 if (!matchedLines.empty())
32 {
33 auto pos = matchedLines.front().find_last_of(' ');
34 if (pos != std::string::npos && pos != matchedLines.front().size())
35 {
36 lastWord = matchedLines.front().substr(pos + 1);
37 }
38 }
39
40 return matchedLines;
41}
42
49inline std::vector<std::string> findFromFile(const std::string &filePath, const std::string &pattern)
50{
51 std::string lastWord;
52 return findFromFile(filePath, pattern, lastWord);
53}
std::vector< std::string > findFromFile(const std::string &filePath, const std::string &pattern, std::string &lastWord)