This repository was archived by the owner on Sep 15, 2020. It is now read-only.
forked from coolwanglu/pdf2htmlEX
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpath.cc
More file actions
147 lines (127 loc) · 3.36 KB
/
path.cc
File metadata and controls
147 lines (127 loc) · 3.36 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
141
142
143
144
145
146
147
/*
* Functions manipulating filenames and paths
*
* by WangLu
* 2012.11.29
*/
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <cstring>
#include <libgen.h>
#include "path.h"
#ifdef __MINGW32__
#include "util/mingw.h"
#endif
using std::string;
namespace pdf2htmlEX {
void create_directories(const string & path)
{
if(path.empty()) return;
size_t idx = path.rfind('/');
if(idx != string::npos)
{
create_directories(path.substr(0, idx));
}
int r = mkdir(path.c_str(), S_IRWXU);
if(r != 0)
{
if(errno == EEXIST)
{
struct stat stat_buf;
if((stat(path.c_str(), &stat_buf) == 0) && S_ISDIR(stat_buf.st_mode))
return;
}
throw string("Cannot create directory: ") + path;
}
}
bool sanitize_filename(string & filename)
{
string sanitized;
bool format_specifier_found = false;
for(size_t i = 0; i < filename.size(); i++)
{
if('%' == filename[i])
{
if(format_specifier_found)
{
sanitized.push_back('%');
sanitized.push_back('%');
}
else
{
// We haven't found the format specifier yet, so see if we can use this one as a valid formatter
size_t original_i = i;
string tmp;
tmp.push_back('%');
while(++i < filename.size())
{
tmp.push_back(filename[i]);
// If we aren't still in option specifiers, stop looking
if(!strchr("0123456789", filename[i]))
{
break;
}
}
// Check to see if we yielded a valid format specifier
if('d' == tmp[tmp.size()-1])
{
// Found a valid integer format
sanitized.append(tmp);
format_specifier_found = true;
}
else
{
// Not a valid format specifier. Just append the protected %
// and keep looking from where we left of in the search
sanitized.push_back('%');
sanitized.push_back('%');
i = original_i;
}
}
}
else
{
sanitized.push_back(filename[i]);
}
}
// Only sanitize if it is a valid format.
if(format_specifier_found)
{
filename.assign(sanitized);
}
return format_specifier_found;
}
bool is_truetype_suffix(const string & suffix)
{
return (suffix == ".ttf") || (suffix == ".ttc") || (suffix == ".otf");
}
string get_filename (const string & path)
{
char* s= basename(strdup(path.c_str()));
std::string str(s);
return str;
/*
size_t idx = path.rfind('/');
if(idx == string::npos)
return path;
else if (idx == path.size() - 1)
return "";
return path.substr(idx + 1);
*/
}
string get_suffix(const string & path)
{
string fn = get_filename(path);
size_t idx = fn.rfind('.');
if(idx == string::npos)
return "";
else
{
string s = fn.substr(idx);
for(auto & c : s)
c = tolower(c);
return s;
}
}
} //namespace pdf2htmlEX