-
Notifications
You must be signed in to change notification settings - Fork 9
/
posix_utils.cpp
157 lines (140 loc) · 3.67 KB
/
posix_utils.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
/* -*- c++ -*-
POSIX環境下で使うユーティリティ類。
*/
#include <fstream>
#include <string>
#include <memory>
#include "posix_utils.h"
std::string lc(const std::string& fname) {
std::string result;
for (std::string::const_iterator ite = fname.begin(); ite != fname.end(); ite++) {
if (*ite >= 'A' && *ite <= 'Z') {
result += (*ite + 0x20);
}
else {
result += *ite;
}
}
return result;
}
std::string get_fname(const std::string& fpath) {
std::string::size_type slash_pos = fpath.rfind('/');
if (slash_pos == std::string::npos) {
return fpath;
}
else {
return fpath.substr(slash_pos + 1);
}
}
std::string get_extension(const std::string& fname) {
std::string::size_type period_pos = fname.rfind('.');
if (period_pos == std::string::npos) {
return std::string();
}
else {
return fname.substr(period_pos + 1);
}
}
std::string drop_extension(const std::string& fname) {
std::string::size_type period_pos = fname.rfind('.');
if (period_pos == std::string::npos) {
return fname;
}
else {
return fname.substr(0, period_pos);
}
}
std::string change_extension(const std::string& fname, const std::string& extension) {
return drop_extension(fname) + '.' + extension;
}
std::string::size_type file_content_search(const std::string& file, const std::string& str) {
std::string content;
std::ifstream is(file.c_str());
char buf[1024];
std::streamsize len;
while (is.good()) {
len = is.read(buf, sizeof buf).gcount();
if (len == 0) {
break;
}
content.append(buf,(size_t)len);
}
is.close();
return bm_search(content, str);
}
std::string::size_type bm_search(const std::string& world, const std::string& data) {
std::string::size_type data_len = data.length();
std::unique_ptr<std::string::size_type[]> skip(new std::string::size_type[256]);
for (std::string::size_type i = 0; i < 256; i++) {
skip[i] = data_len;
}
for (std::string::size_type i = 0; i < data_len-1; i++) {
skip[static_cast<unsigned char>(data[i])] =
data_len - i - 1;
}
std::string::size_type limit = world.length() - data.length();
for (std::string::size_type i = 0;
i <= limit;
i += skip[static_cast<unsigned char>(world[i+data_len-1])]) {
if (world[i+data_len-1] != data[data_len-1]) {
continue;
}
bool matched = true;
for (std::string::size_type j = 0; j < data_len; j++) {
if (world[i+j] != data[j]) {
matched = false;
break;
}
}
if (matched) {
return i;
}
}
return std::string::npos;
}
std::wstring widen(const std::string& str) {
std::wstring ws;
ws.reserve(str.size());
for (std::string::const_iterator ite = str.begin(); ite != str.end(); ite++) {
ws += static_cast<wchar_t>(*ite);
}
return ws;
}
std::string narrow(const std::wstring& str) {
std::string s;
s.reserve(str.size());
for (std::wstring::const_iterator ite = str.begin(); ite != str.end(); ite++) {
s += static_cast<char>(*ite);
}
return s;
}
void fix_filepath(std::string& str) {
// \は/にし、重複した/を消して一つにする。
for (std::string::iterator ite = str.begin(); ite != str.end(); ite++) {
if (*ite == '\\') {
*ite = '/';
}
}
while (true) {
std::string::size_type pos = str.find("//");
if (pos == std::string::npos) {
break;
}
str.erase(pos, 1);
}
}
void fix_filepath(std::wstring& str) {
// \は/にし、重複した/を消して一つにする。
for (std::wstring::iterator ite = str.begin(); ite != str.end(); ite++) {
if (*ite == L'\\') {
*ite = L'/';
}
}
while (true) {
std::wstring::size_type pos = str.find(L"//");
if (pos == std::wstring::npos) {
break;
}
str.erase(pos, 1);
}
}