-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.cpp
162 lines (139 loc) · 6.34 KB
/
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
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(const char *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;
} else if (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(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
std::string ret= string_vprintf(fmt, args);
va_end(args);
return ret;
}
#ifdef _WIN32
#define stat _stat
#define fstat _fstat
#endif
bool
filename_exists(const std::string &filename)
{
struct stat statbuf;
int ret= stat(filename.c_str(), &statbuf);
return (ret == 0);
}
unsigned long long microtime() {
#ifdef WIN32
return ((long long)GetTickCount() * 1000);
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return ((long long) tv.tv_sec * (long long) 1000000 +
(long long) tv.tv_usec);
#endif
}
unsigned long long millitime() { return microtime() / 1000; }
double doubletime() { return microtime() / 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
#define ALLOWABLE_DIRECTORY_DELIMITERS "/\\"
#define DIRECTORY_DELIMITER '\\'
#define DIRECTORY_DELIMITER_STRING "\\"
#define FILENAMES_CASE_SENSITIVE 0
#elif defined(__MWERKS__)
// mac os <=9
#define ALLOWABLE_DIRECTORY_DELIMITERS ":"
#define DIRECTORY_DELIMITER ':'
#define DIRECTORY_DELIMITER_STRING ":"
#define FILENAMES_CASE_SENSITIVE 0
#else
// UNIX
#define ALLOWABLE_DIRECTORY_DELIMITERS "/"
#define DIRECTORY_DELIMITER '/'
#define DIRECTORY_DELIMITER_STRING "/"
#define FILENAMES_CASE_SENSITIVE 1
#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);
}