blob: 86182c1afbbcbf5ecfe34abb7b7d8b2e4184d2b8 (
plain)
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
|
// Copyright 2009-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
#include "filename.h"
#include "sysinfo.h"
namespace embree
{
#ifdef __WIN32__
const char path_sep = '\\';
#else
const char path_sep = '/';
#endif
/*! create an empty filename */
FileName::FileName () {}
/*! create a valid filename from a string */
FileName::FileName (const char* in) {
filename = in;
for (size_t i=0; i<filename.size(); i++)
if (filename[i] == '\\' || filename[i] == '/')
filename[i] = path_sep;
while (!filename.empty() && filename[filename.size()-1] == path_sep)
filename.resize(filename.size()-1);
}
/*! create a valid filename from a string */
FileName::FileName (const std::string& in) {
filename = in;
for (size_t i=0; i<filename.size(); i++)
if (filename[i] == '\\' || filename[i] == '/')
filename[i] = path_sep;
while (!filename.empty() && filename[filename.size()-1] == path_sep)
filename.resize(filename.size()-1);
}
/*! returns path to home folder */
FileName FileName::homeFolder()
{
#ifdef __WIN32__
const char* home = getenv("UserProfile");
#else
const char* home = getenv("HOME");
#endif
if (home) return home;
return "";
}
/*! returns path to executable */
FileName FileName::executableFolder() {
return FileName(getExecutableFileName()).path();
}
/*! returns the path */
FileName FileName::path() const {
size_t pos = filename.find_last_of(path_sep);
if (pos == std::string::npos) return FileName();
return filename.substr(0,pos);
}
/*! returns the basename */
std::string FileName::base() const {
size_t pos = filename.find_last_of(path_sep);
if (pos == std::string::npos) return filename;
return filename.substr(pos+1);
}
/*! returns the extension */
std::string FileName::ext() const {
size_t pos = filename.find_last_of('.');
if (pos == std::string::npos) return "";
return filename.substr(pos+1);
}
/*! returns the extension */
FileName FileName::dropExt() const {
size_t pos = filename.find_last_of('.');
if (pos == std::string::npos) return filename;
return filename.substr(0,pos);
}
/*! returns the basename without extension */
std::string FileName::name() const {
size_t start = filename.find_last_of(path_sep);
if (start == std::string::npos) start = 0; else start++;
size_t end = filename.find_last_of('.');
if (end == std::string::npos || end < start) end = filename.size();
return filename.substr(start, end - start);
}
/*! replaces the extension */
FileName FileName::setExt(const std::string& ext) const {
size_t start = filename.find_last_of(path_sep);
if (start == std::string::npos) start = 0; else start++;
size_t end = filename.find_last_of('.');
if (end == std::string::npos || end < start) return FileName(filename+ext);
return FileName(filename.substr(0,end)+ext);
}
/*! adds the extension */
FileName FileName::addExt(const std::string& ext) const {
return FileName(filename+ext);
}
/*! concatenates two filenames to this/other */
FileName FileName::operator +( const FileName& other ) const {
if (filename == "") return FileName(other);
else return FileName(filename + path_sep + other.filename);
}
/*! concatenates two filenames to this/other */
FileName FileName::operator +( const std::string& other ) const {
return operator+(FileName(other));
}
/*! removes the base from a filename (if possible) */
FileName FileName::operator -( const FileName& base ) const {
size_t pos = filename.find_first_of(base);
if (pos == std::string::npos) return *this;
return FileName(filename.substr(pos+1));
}
/*! == operator */
bool operator== (const FileName& a, const FileName& b) {
return a.filename == b.filename;
}
/*! != operator */
bool operator!= (const FileName& a, const FileName& b) {
return a.filename != b.filename;
}
/*! output operator */
std::ostream& operator<<(std::ostream& cout, const FileName& filename) {
return cout << filename.filename;
}
}
|