Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathutils.cpp
More file actions
Latest commit
162 lines (139 loc) · 6.34 KB
/
Copy pathutils.cpp
File metadata and controls
162 lines (139 loc) · 6.34 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
148
149
150
151
152
153
154
155
156
157
158
159
160
// C
#include<sys/stat.h>
#include<sys/time.h>
#include<stdio.h>
// C++
#include<iostream>
// Self
#include"utils.h"
std::string string_vprintf(constchar *fmt, va_list args)
{
int size= 500;
char *buf = (char*)malloc(size);
while (1) {
va_list copy;
va_copy(copy, args);
#if defined(_WIN32)
int nwritten= _vsnprintf(buf, size, fmt, copy);
#else
int nwritten= vsnprintf(buf, size, fmt, copy);
#endif
va_end(copy);
// Some c libraries return -1 for overflow, some return
// a number larger than size-1
if (nwritten < 0) {
size *= 2;
} elseif (nwritten >= size) {
size = nwritten + 1;
} else {
if (nwritten && buf[nwritten-1] == 0) nwritten--;
std::string ret(buf, buf+nwritten);
free(buf);
return ret;
}
buf = (char*)realloc(buf, size);
}
}
std::string string_printf(constchar *fmt, ...)
{
va_list args;
va_start(args, fmt);
std::string ret= string_vprintf(fmt, args);
va_end(args);
return ret;
}
#ifdef _WIN32
#definestat _stat
#definefstat _fstat
#endif
bool
filename_exists(const std::string &filename)
{
structstat statbuf;
int ret= stat(filename.c_str(), &statbuf);
return (ret == 0);
}
unsignedlonglongmicrotime() {
#ifdef WIN32
return ((longlong)GetTickCount() * 1000);
#else
structtimeval tv;
gettimeofday(&tv, NULL);
return ((longlong) tv.tv_sec * (longlong) 1000000 +
(longlong) tv.tv_usec);
#endif
}
unsignedlonglongmillitime() { returnmicrotime() / 1000; }
doubledoubletime() { returnmicrotime() / 1000000.0; }
std::string rtrim(const std::string &x) {
return x.substr(0, x.find_last_not_of("\r\n\t\f")+1);
}
#ifdef _WIN32
// Windows
#defineALLOWABLE_DIRECTORY_DELIMITERS"/\\"
#defineDIRECTORY_DELIMITER'\\'
#defineDIRECTORY_DELIMITER_STRING"\\"
#defineFILENAMES_CASE_SENSITIVE0
#elif defined(__MWERKS__)
// mac os <=9
#defineALLOWABLE_DIRECTORY_DELIMITERS":"
#defineDIRECTORY_DELIMITER':'
#defineDIRECTORY_DELIMITER_STRING":"
#defineFILENAMES_CASE_SENSITIVE0
#else
// UNIX
#defineALLOWABLE_DIRECTORY_DELIMITERS"/"
#defineDIRECTORY_DELIMITER'/'
#defineDIRECTORY_DELIMITER_STRING"/"
#defineFILENAMES_CASE_SENSITIVE1
#endif
std::string
filename_sans_directory(const std::string &filename)
{
size_t lastDirDelim= filename.find_last_of(ALLOWABLE_DIRECTORY_DELIMITERS);
// No directory delimiter, so return filename
if (lastDirDelim == std::string::npos) return filename;
// Return everything after the delimiter
return filename.substr(lastDirDelim+1);
}
std::string
filename_directory(const std::string &filename)
{
size_t lastDirDelim= filename.find_last_of(ALLOWABLE_DIRECTORY_DELIMITERS);
// No directory delimiter, so return nothing
if (lastDirDelim == std::string::npos) return"";
// Return everything up to just before the last delimiter
return filename.substr(0, lastDirDelim);
}
std::string
filename_sans_suffix(const std::string &filename)
{
// Find the last '.'
size_t lastDot= filename.find_last_of(".");
if (lastDot == std::string::npos) return filename;
// Find the last directory delimiter
size_t lastDirDelim= filename.find_last_of(ALLOWABLE_DIRECTORY_DELIMITERS);
if (lastDirDelim != std::string::npos &&
lastDot < lastDirDelim) {
// The last dot was inside the directory name, so return as is
return filename;
}
// Return everything up to the last dot
return filename.substr(0, lastDot);
}
std::string
filename_suffix(const std::string &filename)
{
// Find the last '.'
size_t lastDot= filename.find_last_of(".");
if (lastDot == std::string::npos) return"";
// Find the last directory delimiter
size_t lastDirDelim= filename.find_last_of(ALLOWABLE_DIRECTORY_DELIMITERS);
if (lastDirDelim != std::string::npos &&
lastDot < lastDirDelim) {
// The last dot was inside the directory name, so return as is
return"";
}
// Return everything after to the last dot
return filename.substr(lastDot+1);
}