-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilename.h
More file actions
186 lines (148 loc) · 5.98 KB
/
Copy pathfilename.h
File metadata and controls
186 lines (148 loc) · 5.98 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Filename: filename.h
// Created by: frang (03June04)
//
////////////////////////////////////////////////////////////////////
#ifndef FILENAME_H
#define FILENAME_H
#include "oconfig.h"
#include "vector_string.h"
#include <assert.h>
class OCSearchPath;
////////////////////////////////////////////////////////////////////
// Class : Filename
// Description : The name of a file, such as a texture file or an XML
// file. Stores the full pathname, and includes
// functions for extracting out the directory prefix
// part and the file extension and stuff.
//
// A Filename is also aware of the mapping between the
// Unix-like filename convention we use internally, and
// the local OS's specific filename convention, and it
// knows how to perform basic OS-specific I/O, like
// testing for file existence and searching a
// searchpath, as well as the best way to open an
// fstream for reading or writing.
////////////////////////////////////////////////////////////////////
class Filename {
public:
enum Type {
// These type values must fit within the bits allocated for
// F_type, below.
T_general = 0x00,
T_dso = 0x01,
T_executable = 0x02,
// Perhaps other types will be added later.
};
enum Flags {
F_type = 0x0f,
F_binary = 0x10,
F_text = 0x20,
};
INLINE Filename(const string &filename = "");
INLINE Filename(const char *filename);
INLINE Filename(const Filename ©);
Filename(const Filename &dirname, const Filename &basename);
INLINE ~Filename();
// Static constructors to explicitly create a filename that refers
// to a text or binary file. This is in lieu of calling set_text()
// or set_binary() or set_type().
INLINE static Filename text_filename(const string &filename);
INLINE static Filename binary_filename(const string &filename);
INLINE static Filename dso_filename(const string &filename);
INLINE static Filename executable_filename(const string &filename);
static Filename from_os_specific(const string &os_specific,
Type type = T_general);
static Filename expand_from(const string &user_string,
Type type = T_general);
static Filename temporary(const string &dirname, const string &prefix,
Type type = T_general);
// Assignment is via the = operator.
INLINE Filename &operator = (const string &filename);
INLINE Filename &operator = (const char *filename);
INLINE Filename &operator = (const Filename ©);
// And retrieval is by any of the classic string operations.
INLINE operator const string & () const;
INLINE const char *c_str() const;
INLINE bool empty() const;
INLINE size_t length() const;
INLINE char operator [] (int n) const;
// Or, you can use any of these.
INLINE string get_fullpath() const;
INLINE string get_dirname() const;
INLINE string get_basename() const;
INLINE string get_fullpath_wo_extension() const;
INLINE string get_basename_wo_extension() const;
INLINE string get_extension() const;
// You can also use any of these to reassign pieces of the filename.
void set_fullpath(const string &s);
void set_dirname(const string &s);
void set_basename(const string &s);
void set_fullpath_wo_extension(const string &s);
void set_basename_wo_extension(const string &s);
void set_extension(const string &s);
// Setting these flags appropriately is helpful when opening or
// searching for a file; it helps the Filename resolve OS-specific
// conventions (for instance, that dynamic library names should
// perhaps be changed from .so to .dll).
INLINE void set_binary();
INLINE void set_text();
INLINE bool is_binary() const;
INLINE bool is_text() const;
INLINE void set_type(Type type);
INLINE Type get_type() const;
void extract_components(vector_string &components) const;
void standardize();
// The following functions deal with the outside world.
INLINE bool is_local() const;
INLINE bool is_fully_qualified() const;
void make_absolute();
void make_absolute(const Filename &start_directory);
bool make_canonical();
string to_os_specific() const;
string to_os_generic() const;
bool exists() const;
bool is_regular_file() const;
bool is_directory() const;
bool is_executable() const;
int compare_timestamps(const Filename &other,
bool this_missing_is_old = true,
bool other_missing_is_old = true) const;
bool resolve_filename(const OCSearchPath &searchpath,
const string &default_extension = string());
bool make_relative_to(Filename directory, bool allow_backups = true);
int find_on_searchpath(const OCSearchPath &searchpath);
bool scan_directory(vector_string &contents) const;
bool open_read(ifstream &stream) const;
bool open_write(ofstream &stream, bool truncate = true) const;
bool open_append(ofstream &stream) const;
bool open_read_write(fstream &stream) const;
bool touch() const;
bool unlink() const;
bool rename_to(const Filename &other) const;
bool make_dir() const;
// Comparison operators are handy.
INLINE bool operator == (const string &other) const;
INLINE bool operator != (const string &other) const;
INLINE bool operator < (const string &other) const;
INLINE void output(ostream &out) const;
private:
void locate_basename();
void locate_extension();
size_t get_common_prefix(const string &other) const;
static int count_slashes(const string &str);
bool r_make_canonical(const Filename &cwd);
string _filename;
// We'll make these size_t instead of string::size_type to help out
// cppParser.
size_t _dirname_end;
size_t _basename_start;
size_t _basename_end;
size_t _extension_start;
int _flags;
};
INLINE ostream &operator << (ostream &out, const Filename &n) {
n.output(out);
return out;
}
#include "filename.I"
#endif