-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileName.cpp
183 lines (171 loc) · 4.89 KB
/
FileName.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#ifndef __WIN32
# include <wordexp.h>
#endif
#include <cstdio> // FILE, fopen
#include "FileName.h"
#include "CpptrajStdio.h"
#ifndef __WIN32
static void WexpErr(int err) {
switch ( err ) {
case WRDE_BADCHAR :
mprinterr("Error: Illegal occurrence of newline or one of |, &, ;, <, >, (, ), {, }.\n");
break;
//case WRDE_BADVAL
case WRDE_CMDSUB :
mprinterr("Error: Command substitution is not allowed in file names.\n");
break;
case WRDE_NOSPACE :
mprinterr("Error: Out of memory.\n");
break;
case WRDE_SYNTAX :
mprinterr("Error: Bad syntax (unbalanced parentheses, unmatched quotes.\n");
break;
}
}
#endif /* __WIN32 */
// COPY CONSTRUCTOR
FileName::FileName( const FileName& rhs ) : fullPathName_(rhs.fullPathName_),
baseName_(rhs.baseName_), extension_(rhs.extension_),
compressExt_(rhs.compressExt_), dirPrefix_(rhs.dirPrefix_) {}
// ASSIGNMENT
FileName& FileName::operator=(const FileName& rhs) {
if (this != &rhs) {
fullPathName_ = rhs.fullPathName_;
baseName_ = rhs.baseName_;
extension_ = rhs.extension_;
compressExt_ = rhs.compressExt_;
dirPrefix_ = rhs.dirPrefix_;
}
return *this;
}
// FileName::clear()
void FileName::clear() {
fullPathName_.clear();
baseName_.clear();
extension_.clear();
compressExt_.clear();
dirPrefix_.clear();
}
bool FileName::MatchFullOrBase(std::string const& rhs) const {
if (!fullPathName_.empty()) {
// Prefer full filename match.
if (fullPathName_ == rhs) return true;
if (baseName_ == rhs) return true;
}
return false;
}
int FileName::SetFileName(std::string const& nameIn) {
// null filename allowed (indicates STDIN/STDOUT)
if (nameIn.empty()) {
clear();
return 0;
}
#ifndef __WIN32
wordexp_t expanded;
int err = wordexp( nameIn.c_str(), &expanded, WRDE_NOCMD );
WexpErr( err );
if (err == 0) {
if (expanded.we_wordc < 1) { // Sanity check
mprinterr("Internal Error: Word expansion failed.\n");
err = 1;
} else
err = SetFileName_NoExpansion( expanded.we_wordv[0] );
wordfree( &expanded );
}
return err;
#else
SetFileName_NoExpansion(nameIn);
return 0;
#endif
}
int FileName::SetFileName_NoExpansion(std::string const& nameIn) {
// null filename allowed (indicates STDIN/STDOUT)
if (nameIn.empty()) {
clear();
return 0;
}
// Assign filename with full path
fullPathName_.assign( nameIn );
// Get position of last occurence of '/' to determine base filename
size_t found = fullPathName_.find_last_of("/");
if (found == std::string::npos) {
baseName_ = fullPathName_;
dirPrefix_.clear();
} else {
baseName_ = fullPathName_.substr(found+1);
dirPrefix_ = fullPathName_.substr(0, found+1);
}
// Get the filename extension
found = baseName_.find_last_of(".");
if (found == std::string::npos) {
extension_.clear();
} else {
extension_ = baseName_.substr(found);
}
// See if the extension is one of the 2 recognized compression extensions.
// If file has a compression format extension, look for another extension.
if ( extension_ == ".gz" || extension_ == ".bz2" ) {
compressExt_ = extension_;
// Get everything before the compressed extension
std::string compressPrefix = baseName_.substr(0,found);
// See if there is another extension
found = compressPrefix.find_last_of(".");
if (found == std::string::npos)
// No other extension
extension_.clear();
else
extension_ = compressPrefix.substr(found);
} else
compressExt_.clear();
return 0;
}
int FileName::AppendFileName( std::string const& suffix ) {
if (fullPathName_.empty()) return 1;
fullPathName_.append( suffix );
baseName_.append( suffix );
return 0;
}
FileName FileName::PrependFileName( std::string const& prefix ) const {
FileName out;
out.SetFileName_NoExpansion(dirPrefix_ + prefix + baseName_);
return out;
}
// =============================================================================
File::NameArray File::ExpandToFilenames(std::string const& fnameArg) {
NameArray fnames;
#ifdef __WIN32
fnames.push_back( fnameArg );
#else
if (fnameArg.empty()) return fnames;
wordexp_t expanded;
int err = wordexp( fnameArg.c_str(), &expanded, WRDE_NOCMD );
WexpErr( err );
if ( err == 0 ) {
for (unsigned int i = 0; i != expanded.we_wordc; i++) {
if (expanded.we_wordv[i] == 0)
mprinterr("Internal Error: Bad expansion at %i\n", i);
else {
FileName fn;
fn.SetFileName_NoExpansion( expanded.we_wordv[i] );
fnames.push_back( fn );
}
}
wordfree( &expanded );
}
#endif /* __WIN32 */
return fnames;
}
bool File::Exists(FileName const& fn) {
if (!fn.empty()) {
FILE* infile = fopen(fn.full(), "rb");
if (infile==0) {
return false;
}
fclose(infile);
return true;
}
return false;
}
bool File::Exists(std::string const& fname) {
return File::Exists( FileName(fname) );
}