ndm  0.1.2
Utils.hh
1 #pragma once
2 #include <sys/stat.h>
3 #include <libgen.h>
4 #include <cmath>
5 #include <limits>
6 #include <fstream>
7 #include <string>
8 #include <sstream>
9 #include <bitset>
10 #include "ndm.hh"
11 
12 
13 namespace NDM {
14 namespace Utils {
15 
16 inline bool is_equal(double a, double b, double error_factor = 1.0)
17 {
18  return a == b ||
19  std::abs(a - b) < std::abs(std::fmin(a, b)) * std::numeric_limits<double>::epsilon() * error_factor;
20 }
21 
22 inline void asci_to_protobuf(std::string a, std::string & p)
23 {
24  std::stringstream sstream(a);
25  p.clear();
26  while (sstream.good()) {
27  std::bitset<8> bits;
28  sstream >> bits;
29  char c = char(bits.to_ulong());
30  p += c;
31  }
32 }
33 
34 inline void protobuf_to_asci(std::string p, std::string & a)
35 {
36  for (std::size_t i = 0; i < p.size(); ++i) {
37  std::bitset<8> xx = std::bitset<8>(p[i]);
38  a += xx.to_string();
39  }
40 }
41 inline int mkpath(const char * dir, mode_t mode)
42 {
43  struct stat st;
44  if (!dir) {
45  errno = EINVAL;
46  return 1;
47  }
48  if (!stat(dir, &st)) return 0;
49  mkpath(dirname(strdupa(dir)), mode);
50  return mkdir(dir, mode);
51 }
52 
53 } // namespace Utils
54 } // namespace NDM