62 lines
1.3 KiB
C++
62 lines
1.3 KiB
C++
#include <string>
|
|
#include <vector>
|
|
#include <fstream>
|
|
#include "util.hpp"
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
#ifndef WIN32
|
|
#include <unistd.h>
|
|
#define stat _stat
|
|
#endif
|
|
|
|
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;
|
|
}
|
|
|
|
// Get a string encoding the modification time and size of the file.
|
|
std::string get_file_fingerprint(const std::string &fn) {
|
|
struct stat result;
|
|
if(stat(fn.c_str(), &result)==0)
|
|
{
|
|
std::stringstream ss;
|
|
ss << result.st_mtime;
|
|
return ss.str();
|
|
}
|
|
return "";
|
|
}
|
|
|
|
std::string get_file_contents(const std::string &fn) {
|
|
std::ifstream fs(fn);
|
|
std::stringstream buffer;
|
|
buffer << fs.rdbuf();
|
|
return buffer.str();
|
|
}
|
|
|
|
|
|
// Strip leading and trailing whitespace and comments.
|
|
const stringvec trim_and_uncomment(const stringvec &lines) {
|
|
stringvec result;
|
|
for (int i = 0; i < int(lines.size()); i++) {
|
|
std::string trimmed = trim(lines[i]);
|
|
if ((trimmed.size() > 0) && (trimmed[0] != '#')) {
|
|
result.push_back(trimmed);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
} // namespace util
|