36 lines
791 B
C++
36 lines
791 B
C++
|
|
#include <string>
|
||
|
|
#include <vector>
|
||
|
|
#include <fstream>
|
||
|
|
#include "util.hpp"
|
||
|
|
|
||
|
|
namespace util {
|
||
|
|
|
||
|
|
// Read a file as lines.
|
||
|
|
const stringvec read_lines(const std::string &path) {
|
||
|
|
stringvec result;
|
||
|
|
std::ifstream f;
|
||
|
|
f.open(path);
|
||
|
|
if (f) {
|
||
|
|
std::string line;
|
||
|
|
while (std::getline(f, line)) {
|
||
|
|
result.push_back(line);
|
||
|
|
}
|
||
|
|
f.close();
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Strip leading and trailing whitespace and comments.
|
||
|
|
const stringvec trim_and_uncomment(const stringvec &lines) {
|
||
|
|
stringvec result;
|
||
|
|
for (int i = 0; i < lines.size(); i++) {
|
||
|
|
std::string trimmed = trim(lines[i]);
|
||
|
|
if ((trimmed.size() > 0) && (trimmed[0] != '#')) {
|
||
|
|
result.push_back(trimmed);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace util
|