forked from efficient/rankselect
-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.cpp
182 lines (157 loc) · 4.18 KB
/
util.cpp
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* sdsl - succinct data structures library
Copyright (C) 2009-2013 Simon Gog
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/ .
*/
#include "sdsl/util.hpp"
#include <sys/types.h> // for file_size
#include <sys/stat.h> // for file_size
#include <iomanip>
#include <vector>
#include <string>
#include <type_traits>
#include <typeinfo>
#ifndef MSVC_COMPILER
#include <cxxabi.h>
#endif
namespace sdsl
{
namespace util
{
uint64_t _id_helper::id = 0;
std::string basename(std::string file)
{
file = disk_file_name(file); // remove RAM-prefix
#ifdef MSVC_COMPILER
char* c = _strdup((const char*)file.c_str());
char file_name[_MAX_FNAME] = { 0 };
::_splitpath_s(c, NULL, 0, NULL, NULL, file_name, _MAX_FNAME, NULL, 0);
std::string res(file_name);
#else
char* c = strdup((const char*)file.c_str());
std::string res = std::string(::basename(c));
#endif
free(c);
return res;
}
std::string dirname(std::string file)
{
bool ram_file = is_ram_file(file);
file = disk_file_name(file); // remove RAM-prefix
#ifdef MSVC_COMPILER
char* c = _strdup((const char*)file.c_str());
char dir_name[_MAX_DIR] = { 0 };
char drive[_MAX_DRIVE] = {0};
::_splitpath_s(c, drive, _MAX_DRIVE, dir_name, _MAX_DIR, NULL,0, NULL,0);
std::string res = std::string(drive) + std::string(dir_name);
#else
char* c = strdup((const char*)file.c_str());
std::string res = std::string(::dirname(c));
#endif
free(c);
if (ram_file) {
if ("." == res) {
res = ram_file_name("");
} else if ("/" ==res) {
res = ram_file_name(res);
}
}
return res;
}
uint64_t pid()
{
#ifdef MSVC_COMPILER
return _getpid();
#else
return getpid();
#endif
}
char* str_from_errno()
{
#ifdef MSVC_COMPILER
#pragma warning(disable:4996)
return strerror(errno);
#pragma warning(default:4996)
#else
return strerror(errno);
#endif
}
uint64_t id()
{
return _id_helper::getId();
}
std::string demangle(const std::string& name)
{
#ifdef HAVE_CXA_DEMANGLE
char buf[4096];
size_t size = 4096;
int status = 0;
abi::__cxa_demangle(name.c_str(), buf, &size, &status);
if (status==0)
return std::string(buf);
return name;
#else
return name;
#endif
}
std::string demangle2(const std::string& name)
{
std::string result = demangle(name);
std::vector<std::string> words_to_delete;
words_to_delete.push_back("sdsl::");
words_to_delete.push_back("(unsigned char)");
words_to_delete.push_back(", unsigned long");
for (size_t k=0; k<words_to_delete.size(); ++k) {
std::string w = words_to_delete[k];
for (size_t i = result.find(w); i != std::string::npos; i = result.find(w, i)) {
result.erase(i, w.length());
++i;
}
}
size_t index = 0;
std::string to_replace = "int_vector<1>";
while ((index = result.find(to_replace, index)) != std::string::npos) {
result.replace(index, to_replace.size(), "bit_vector");
}
return result;
}
void delete_all_files(tMSS& file_map)
{
for (auto file_pair : file_map) {
sdsl::remove(file_pair.second);
}
file_map.clear();
}
std::string to_latex_string(unsigned char c)
{
if (c == '_')
return "\\_";
else if (c == '\0')
return "\\$";
else
return to_string(c);
}
void set_verbose()
{
verbose = true;
}
size_t file_size(const std::string& file)
{
if (is_ram_file(file)) {
return ram_fs::file_size(file);
} else {
struct stat fs;
stat(file.c_str(), &fs);
return fs.st_size;
}
}
}// end namespace util
}// end namespace sdsl