File: Util.cpp

package info (click to toggle)
rdkit 202503.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 222,000 kB
  • sloc: cpp: 411,111; python: 78,482; ansic: 26,181; java: 8,285; javascript: 4,404; sql: 2,393; yacc: 1,626; lex: 1,267; cs: 1,090; makefile: 581; xml: 229; fortran: 183; sh: 121
file content (119 lines) | stat: -rw-r--r-- 2,710 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
//  Copyright (C) 2020 Gareth Jones, Glysade LLC
//
//   @@ All Rights Reserved @@
//  This file is part of the RDKit.
//  The contents are covered by the terms of the BSD license
//  which is included in the file license.txt, found at the root
//  of the RDKit source tree.
//

#include <string>
#include <cstdlib>

#include <RDGeneral/BoostStartInclude.h>
#include <boost/algorithm/string.hpp>
#include <RDGeneral/BoostEndInclude.h>

#include "Util.h"

namespace GarethUtil {

using namespace std;

string currentTime() {
  time_t rawtime;
  struct tm *timeinfo;
  char buffer[80];

  time(&rawtime);
  timeinfo = localtime(&rawtime);

  strftime(buffer, 80, "%m-%d-%Y %H:%M:%S", timeinfo);

  return buffer;
}

bool startsWith(string str, string prefix) {
  if (prefix.length() > str.length()) {
    return false;
  }
  return str.compare(0, prefix.length(), prefix) == 0;
}

string getUserName() {
#ifdef WIN32
  return "UNKNOWN";
#else
  const int bufsize = 100;
  char buffer[bufsize];

  if (!getlogin_r(buffer, bufsize)) {
    return string(buffer);
  } else {
    return string("");
  }
#endif
}

string &removeTrailingLF(string &line) {
  if (!line.empty() && line[line.length() - 1] == '\r') {
    line.erase(line.length() - 1);
  }
  return line;
}

string &trim(string &str) {
  boost::trim(str);
  return str;
}

string &toUpperCase(string &str) {
  boost::to_upper(str);
  return str;
}

string &toLowerCase(string &str) {
  boost::to_lower(str);
  return str;
}

bool equals(const string &str1, const string &str2) {
  return str1.compare(str2) == 0;
}

bool equalsIgnoreCase(const string &str1, const string &str2) {
  return boost::iequals(str1, str2);
}

bool endsWith(const string &str, const string &suffix) {
  if (suffix.length() > str.length()) {
    return false;
  }
  return str.compare(str.length() - suffix.length(), string::npos, suffix) == 0;
}

bool equals(const double d1, const double d2, const int ulp) {
  // the machine epsilon has to be scaled to the magnitude of the values used
  // and multiplied by the desired precision in ULPs (units in the last place)
  return abs(d1 - d2) <
             numeric_limits<double>::epsilon() * std::abs(d1 + d1) * ulp
         // unless the result is subnormal
         || abs(d1 - d2) < numeric_limits<double>::min();
}

bool equals(const double d1, const double d2, const double epsilon) {
  return equals(d1, d2, 1) || abs(d1 - d2) < epsilon;
}

bool equals(const double d1, const double d2) { return equals(d1, d2, 1); }

boost::optional<string> getEnv(const string &name) {
  const char *value = getenv(name.c_str());
  if (value == nullptr) {
    return boost::none;
  }
  return string(value);
}

}  // namespace GarethUtil