-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.h
More file actions
77 lines (69 loc) · 1.86 KB
/
util.h
File metadata and controls
77 lines (69 loc) · 1.86 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
/*
* util.h
*
* Created on: 14.02.2013
* Author: Christoph Schaefer
*/
#ifndef __BitmapIndex__util__
#define __BitmapIndex__util__
#include <sys/time.h>
#include <sstream>
#include <string>
#include <cwchar>
#include "forward.h"
#include <dirent.h>
#include <string.h>
#include <algorithm>
typedef unsigned long long timestamp_t;
class util {
public:
static timestamp_t getTimestamp()
{
struct timeval now;
gettimeofday (&now, NULL);
return now.tv_usec + (timestamp_t)now.tv_sec * 1000000;
}
static std::wstring toWString(const std::string& utf8){
std::mbstate_t state = std::mbstate_t();
const char* utf8Chars = utf8.c_str();
size_t len = std::mbsrtowcs(NULL, &utf8Chars, 0, &state);
if(len != size_t(-1)) {
std::vector<wchar_t> wstr = std::vector<wchar_t>(len+1);
std::mbsrtowcs(&wstr[0], &utf8Chars, len+1, &state);
return std::wstring(&wstr[0], len);
}
else {
return std::wstring();
}
}
static wchar_t firstCharToWChar(const std::string& utf8) {
wchar_t result;
mbrtowc(&result, &utf8[0], MB_CUR_MAX, NULL);
return result;
}
static std::string wCharToMultiByte(const wchar_t& source) {
char buffer[MB_CUR_MAX];
int length = wctomb(buffer, source);
return std::string(buffer, length);
}
static StringVecPtr getAllItemsInDir(const std::string& path) {
DIR *dp;
struct dirent *entry;
dp = opendir(path.c_str());
if (dp == NULL) {
std::cout << "Path \"" << path << "\" does not exist or could not be read." << std::endl;
throw;
}
StringVecPtr filenames = std::make_shared<std::vector<std::string>>();
while ((entry = readdir(dp))){
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
continue;
}
filenames->push_back(entry->d_name);
}
closedir(dp);
std::sort(filenames->begin(), filenames->end());
return filenames;
}
};
#endif /* __BitmapIndex__util__ */