Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/embree/common/sys/filename.cpp
9912 views
1
// Copyright 2009-2021 Intel Corporation
2
// SPDX-License-Identifier: Apache-2.0
3
4
#include "filename.h"
5
#include "sysinfo.h"
6
7
namespace embree
8
{
9
#ifdef __WIN32__
10
const char path_sep = '\\';
11
#else
12
const char path_sep = '/';
13
#endif
14
15
/*! create an empty filename */
16
FileName::FileName () {}
17
18
/*! create a valid filename from a string */
19
FileName::FileName (const char* in) {
20
filename = in;
21
for (size_t i=0; i<filename.size(); i++)
22
if (filename[i] == '\\' || filename[i] == '/')
23
filename[i] = path_sep;
24
while (!filename.empty() && filename[filename.size()-1] == path_sep)
25
filename.resize(filename.size()-1);
26
}
27
28
/*! create a valid filename from a string */
29
FileName::FileName (const std::string& in) {
30
filename = in;
31
for (size_t i=0; i<filename.size(); i++)
32
if (filename[i] == '\\' || filename[i] == '/')
33
filename[i] = path_sep;
34
while (!filename.empty() && filename[filename.size()-1] == path_sep)
35
filename.resize(filename.size()-1);
36
}
37
38
/*! returns path to executable */
39
FileName FileName::executableFolder() {
40
return FileName(getExecutableFileName()).path();
41
}
42
43
/*! returns the path */
44
FileName FileName::path() const {
45
size_t pos = filename.find_last_of(path_sep);
46
if (pos == std::string::npos) return FileName();
47
return filename.substr(0,pos);
48
}
49
50
/*! returns the basename */
51
std::string FileName::base() const {
52
size_t pos = filename.find_last_of(path_sep);
53
if (pos == std::string::npos) return filename;
54
return filename.substr(pos+1);
55
}
56
57
/*! returns the extension */
58
std::string FileName::ext() const {
59
size_t pos = filename.find_last_of('.');
60
if (pos == std::string::npos) return "";
61
return filename.substr(pos+1);
62
}
63
64
/*! returns the extension */
65
FileName FileName::dropExt() const {
66
size_t pos = filename.find_last_of('.');
67
if (pos == std::string::npos) return filename;
68
return filename.substr(0,pos);
69
}
70
71
/*! returns the basename without extension */
72
std::string FileName::name() const {
73
size_t start = filename.find_last_of(path_sep);
74
if (start == std::string::npos) start = 0; else start++;
75
size_t end = filename.find_last_of('.');
76
if (end == std::string::npos || end < start) end = filename.size();
77
return filename.substr(start, end - start);
78
}
79
80
/*! replaces the extension */
81
FileName FileName::setExt(const std::string& ext) const {
82
size_t start = filename.find_last_of(path_sep);
83
if (start == std::string::npos) start = 0; else start++;
84
size_t end = filename.find_last_of('.');
85
if (end == std::string::npos || end < start) return FileName(filename+ext);
86
return FileName(filename.substr(0,end)+ext);
87
}
88
89
/*! adds the extension */
90
FileName FileName::addExt(const std::string& ext) const {
91
return FileName(filename+ext);
92
}
93
94
/*! concatenates two filenames to this/other */
95
FileName FileName::operator +( const FileName& other ) const {
96
if (filename == "") return FileName(other);
97
else return FileName(filename + path_sep + other.filename);
98
}
99
100
/*! concatenates two filenames to this/other */
101
FileName FileName::operator +( const std::string& other ) const {
102
return operator+(FileName(other));
103
}
104
105
/*! removes the base from a filename (if possible) */
106
FileName FileName::operator -( const FileName& base ) const {
107
size_t pos = filename.find_first_of(base);
108
if (pos == std::string::npos) return *this;
109
return FileName(filename.substr(pos+1));
110
}
111
112
/*! == operator */
113
bool operator== (const FileName& a, const FileName& b) {
114
return a.filename == b.filename;
115
}
116
117
/*! != operator */
118
bool operator!= (const FileName& a, const FileName& b) {
119
return a.filename != b.filename;
120
}
121
122
/*! output operator */
123
std::ostream& operator<<(std::ostream& cout, const FileName& filename) {
124
return cout << filename.filename;
125
}
126
}
127
128