More work on command parser

This commit is contained in:
2021-02-02 16:29:07 -05:00
parent df3ec8ab99
commit 0721d29c72
10 changed files with 239 additions and 106 deletions

View File

@@ -1,6 +1,7 @@
#include <string>
#include <vector>
#include <fstream>
#include <cstdlib>
#include "util.hpp"
#include <sys/types.h>
#include <sys/stat.h>
@@ -11,8 +12,41 @@
namespace util {
// Read a file as lines.
const stringvec read_lines(const std::string &path) {
int64_t strtoint(const std::string &value, int64_t errval) {
char *endptr;
int64_t result = strtoll(value.c_str(), &endptr, 10);
if (endptr == value.c_str() + value.size()) {
return result;
} else {
return errval;
}
}
std::string ltrim(std::string s) {
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
std::not1(std::ptr_fun<int, int>(std::isspace))));
return s;
}
std::string rtrim(std::string s) {
s.erase(std::find_if(s.rbegin(), s.rend(),
std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
std::string trim(std::string s) {
return ltrim(rtrim(s));
}
std::string get_file_contents(const std::string &fn) {
std::ifstream fs(fn);
std::stringstream buffer;
buffer << fs.rdbuf();
return buffer.str();
}
stringvec get_file_lines(const std::string &path) {
stringvec result;
std::ifstream f;
f.open(path);
@@ -26,7 +60,6 @@ const stringvec read_lines(const std::string &path) {
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)
@@ -38,34 +71,13 @@ std::string get_file_fingerprint(const std::string &fn) {
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;
}
double distance_squared(double x1, double y1, double x2, double y2) {
double dx = x1 - x2;
double dy = y1 - y2;
return dx*dx + dy*dy;
}
// These are Bob Jenkins' Lookup3.
// These hash functions are part of Bob Jenkins' Lookup3.
#define hash_rot(x,k) (((x)<<(k)) | ((x)>>(32-(k))))
#define hash_mix(a,b,c) { \
@@ -94,9 +106,15 @@ uint32_t hash3(uint32_t a, uint32_t b, uint32_t c) {
double hash_to_float(double lo, double hi, uint32_t a, uint32_t b, uint32_t c) {
double result = hash3(a, b, c); // Lossless.
result *= ((hi-lo) * (1.0 / 0xFFFFFFFF));
result *= (1.0 / 0xFFFFFFFF);
result *= (hi-lo);
result += lo;
return result;
}
std::ostream & operator << (std::ostream &out, const XYZ &xyz) {
out << "(" << xyz.x << "," << xyz.y << "," << xyz.z << ")";
return out;
}
} // namespace util