-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsupport.hh
More file actions
140 lines (116 loc) · 4.55 KB
/
support.hh
File metadata and controls
140 lines (116 loc) · 4.55 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#pragma once
#include <string>
#include <chrono>
#include "jsonhelper.hh"
#include "nlohmann/json.hpp"
#include "sqlwriter.hh"
#include <mutex>
#include "httplib.h"
#include "sws.hh"
struct DTime
{
void start()
{
d_start = std::chrono::steady_clock::now();
}
uint32_t lapUsec()
{
auto usec = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now()- d_start).count();
start();
return usec;
}
std::chrono::time_point<std::chrono::steady_clock> d_start;
};
template<typename T>
struct TimeKeeper
{
void report(const T* t)
{
std::lock_guard<std::mutex> l(d_lock);
auto now = std::chrono::steady_clock::now();
cleanupWhileLocked(now);
d_store[t] = now;
}
double getMsec(const T* t)
{
std::lock_guard<std::mutex> l(d_lock);
if(auto iter = d_store.find(t) ; iter != d_store.end()) {
auto ret = iter->second;
d_store.erase(iter);
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now()- ret).count() / 1000.0;
}
return -1;
}
void cleanupWhileLocked(std::chrono::time_point<std::chrono::steady_clock>& now)
{
std::erase_if(d_store, [&now](const auto& val) {
auto usec = std::chrono::duration_cast<std::chrono::microseconds>(now-val.second).count();
return usec > 10000000;
});
}
std::unordered_map<const T*, std::chrono::time_point<std::chrono::steady_clock>> d_store;
std::mutex d_lock;
};
// we add the / to prefix for you
std::string makePathForId(const std::string& id, const std::string& prefix="docs", const std::string& suffix="", bool makepath=false);
// get versions of an ID, in order of extension, excluding the unversioned file
std::vector<std::string> getVersionsForId(const std::string& id);
// for external ids
bool haveExternalIdFile(const std::string& id, const std::string& prefix="op", const std::string& suffix=".odt", size_t* siz =0);
// for external ids
bool haveExternalIdFileRightSize(const std::string& id, int64_t siz, const std::string& prefix, const std::string& suffix);
// returns "f3/12"
std::string getSubdirForExternalID(const std::string& in);
std::string makePathForExternalID(const std::string& id, const std::string& prefix="op", const std::string& suffix=".odt", bool makepath=false);
bool isPresentNonEmpty(const std::string& id, const std::string& prefix="docs", const std::string& suffix="", size_t* siz =0);
bool isPresentRightSize(const std::string& id, int64_t size, const std::string& prefix="docs");
bool cacheIsNewer(const std::string& id, const std::string& cacheprefix, const std::string& suffix, const std::string& docprefix);
bool isPDF(const std::string& fname);
bool isDocx(const std::string& fname);
bool isDoc(const std::string& fname);
bool isRtf(const std::string& fname);
bool isXML(const std::string& fname);
uint64_t getRandom64();
std::string getLargeId();
bool endsWith(const std::string& str, const std::string& suffix);
// Fri, 17 Jan 2025 06:07:07 GMT
time_t getTstampRSSFormat(const std::string& str);
time_t getTstamp(const std::string& str);
time_t getTstampUTC(const std::string& str);
// 2024-09-17
time_t getDateTimestamp(const std::string& str);
//
// 2026-02-13T13:31:56.442737079Z
std::string getDBDateTimeString(time_t w);
std::string humanDutchTimestamp(time_t w);
void sendEmail(const std::string& server, const std::string& from, const std::string& to, const std::string& subject, const std::string& textBody, const std::string& htmlBody="", const std::string& bcc="");
void replaceSubstring(std::string &originalString, const std::string &searchString, const std::string &replaceString);
std::string htmlEscape(const std::string& data);
std::string urlEscape(const std::string& data);
std::string getTodayDBFormat();
std::string getDateDBFormat(time_t t);
std::string getNowDBFormat();
std::string toQuotedPrintable(const std::string& in);
std::string deHTML(const std::string& html);
template<typename T, typename R>
R genget(const T& cont, const std::string& fname)
{
R ret{};
auto iter = cont.find(fname);
if(iter == cont.end() || !std::get_if<R>(&iter->second))
return ret;
return std::get<R>(iter->second);
}
template<typename T>
std::string eget(const T& cont, const std::string& fname)
{
return genget<T, std::string>(cont, fname);
}
template<typename T>
int64_t iget(const T& cont, const std::string& fname)
{
return genget<T, int64_t>(cont, fname);
}
std::string convertToSQLiteFTS5(const std::string& in);
std::string enrichHTML(const std::string& html, SQLiteWriter& sqlw);
std::string getContentsOfFile(const std::string& fname);