-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.h
More file actions
99 lines (75 loc) · 2.45 KB
/
Utils.h
File metadata and controls
99 lines (75 loc) · 2.45 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
#ifndef UTILS_H
#define UTILS_H
#include <string.h>
#include <string>
#include <fstream>
#include <filesystem>
#include <list>
#include <set>
#include <sstream>
#include <initializer_list>
#include "sha1.h"
// include headers that implement a archive in simple text format
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/unordered_map.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/string.hpp>
namespace fs = std::filesystem;
std::string sha1(std::initializer_list<std::string> lst);
std::string readContent(const std::string& filename);
std::string readContent(const fs::path& filename);
void writeContents(const std::string& file_name, std::initializer_list<std::string> lst);
void writeContents(const fs::path& filename, std::initializer_list<std::string> lst);
void message(const std::string& msg, std::initializer_list<std::string> lst = {});
void create_file(const fs::path &p);
// read/write an object is something wrong
template <typename T>
void writeObject(const std::string& file_name, const T& object) {
try {
std::ofstream ofs(file_name);
boost::archive::text_oarchive oa(ofs);
oa << object;
}
catch (std::ofstream::failure const& ex) {
std::cout << ex.what() << std::endl;
}
}
template <typename T>
void writeObject(const std::filesystem::path& p, const T& object) {
try {
std::ofstream ofs(p);
boost::archive::text_oarchive oa(ofs);
oa << object;
}
catch (std::ofstream::failure const& ex) {
std::cout << ex.what() << std::endl;
}
}
template <typename T>
void readObject(const std::string& file_name, T& object) {
try {
std::ifstream ifs(file_name);
boost::archive::text_iarchive ia(ifs);
ia >> object;
}
catch (std::ifstream::failure const& ex) {
std::cout << ex.what() << std::endl;
}
}
template <typename T>
void readObject(const std::filesystem::path& p, T& object) {
try{
std::ifstream ifs(p);
boost::archive::text_iarchive ia(ifs);
ia >> object;
}
catch (std::ifstream::failure const& ex) {
std::cout << ex.what() << std::endl;
}
}
std::set<std::string> plainFilenamesIn(const fs::path& dir_name);
std::fstream join(const std::string& first, std::initializer_list<std::string> lst);
// output unordered_map as string format
std::string toString(std::unordered_map<std::string, std::string>& map);
#endif // UTILS_H