8157c392
David Mayerich
added parser and ...
|
1
2
3
4
|
#ifndef STIM_FILENAME_H
#define STIM_FILENAME_H
#include <stdio.h> /* defines FILENAME_MAX */
|
5cda84ab
David Mayerich
putting pranathi ...
|
5
|
#ifdef _WIN32
|
8157c392
David Mayerich
added parser and ...
|
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
|
#include <direct.h>
#define GetCurrentDir _getcwd
#define STIM_FILENAME_DIV '\\'
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#define STIM_FILENAME_DIV '/'
#endif
#include <string>
#include <sstream>
#include <vector>
#include <stack>
#include <algorithm>
#include "../parser/parser.h"
#include <boost/filesystem.hpp>
namespace stim{
//filename class designed to work with both Windows and Unix
class filename{
private:
void init(){
|
8b7be670
David Mayerich
implemented savin...
|
34
|
absolute = false;
|
8157c392
David Mayerich
added parser and ...
|
35
36
37
38
39
40
41
42
43
44
|
}
protected:
std::string drive; //drive letter (for Windows)
std::vector<std::string> path; //directory hierarchy
std::string prefix; //file prefix (without extension)
std::string ext; //file extension
|
8b7be670
David Mayerich
implemented savin...
|
45
46
|
bool absolute; //filename is an absolute path
|
8157c392
David Mayerich
added parser and ...
|
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
|
//replace any incorrect dividers with the appropriate version for the OS
std::string parse_div(std::string s) {
#ifdef _WIN32
std::replace( s.begin(), s.end(), '/', '\\');
#else
std::replace( s.begin(), s.end(), '\\', '/');
#endif
return s;
}
//parse the file name (prefix and extension)
void parse_name(std::string fname){
//find the extension
size_t xpos = fname.find_last_of('.'); //find the position of the extension period (if there is one)
if(xpos != std::string::npos){ //split the prefix and extension
prefix = fname.substr(0, xpos);
ext = fname.substr(xpos + 1);
}
else
prefix = fname;
}
//parse a file locator string
void parse(std::string loc){
//determine the drive (if Windows)
drive = "";
#ifdef _WIN32
//if the second character is a colon, the character right before it is the drive
if(loc[1] == ':'){
|
8b7be670
David Mayerich
implemented savin...
|
79
|
absolute = true; //this is an absolute path
|
8157c392
David Mayerich
added parser and ...
|
80
|
drive = loc[0]; //copy the drive letter
|
8b7be670
David Mayerich
implemented savin...
|
81
82
83
84
85
86
|
loc = loc.substr(3); //remove the drive information from the locator string
}
#else
if(loc[0] == STIM_FILENAME_DIV){
absolute = true;
loc = loc.substr(1);
|
8157c392
David Mayerich
added parser and ...
|
87
88
89
|
}
#endif
|
8157c392
David Mayerich
added parser and ...
|
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
|
//determine the file name
std::string fname = loc.substr( loc.find_last_of(STIM_FILENAME_DIV) + 1 ); //find the file name (including extension)
//parse the file name
parse_name(fname);
//find the directory hierarchy
std::string dir = loc.substr(0, loc.find_last_of(STIM_FILENAME_DIV) + 1 );
path = stim::parser::split(dir, STIM_FILENAME_DIV);
}
public:
filename(){
init();
}
filename(std::string loc){
init();
parse(loc);
}
std::string get_name(){
if(prefix == "" && ext == "")
return "";
else
return prefix + "." + ext;
}
std::string get_extension(){
return ext;
}
std::string get_prefix(){
return prefix;
}
std::string dir(){
std::stringstream ss;
|
8b7be670
David Mayerich
implemented savin...
|
132
133
134
135
136
137
138
139
|
//if the path is absolute
if(absolute){
//output the drive letter if in Windows
#ifdef _WIN32
ss<<drive<<":";
#endif
ss<<STIM_FILENAME_DIV;
}
|
8157c392
David Mayerich
added parser and ...
|
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
//output the directory
for(unsigned int d = 0; d < path.size(); d++)
ss<<path[d]<<STIM_FILENAME_DIV;
return ss.str();
}
std::string str(){
std::stringstream ss;
ss<<dir()<<get_name();
return ss.str();
}
//get a list of files matching the current tamplate
std::vector<stim::filename> get_list(){
boost::filesystem::path p(dir()); //create a path from the current filename
std::vector<stim::filename> file_list;
if(boost::filesystem::exists(p)){
if(boost::filesystem::is_directory(p)){
typedef std::vector<boost::filesystem::path> vec; // store paths,
vec v; // so we can sort them later
std::copy(boost::filesystem::directory_iterator(p), boost::filesystem::directory_iterator(), back_inserter(v));
std::sort(v.begin(), v.end()); // sort, since directory iteration
// is not ordered on some file systems
//compare file names to the current template (look for wild cards)
for (vec::const_iterator it(v.begin()), it_end(v.end()); it != it_end; ++it)
{
//if the filename is a wild card *or* it matches the read file name
if( prefix == "*" || prefix == (*it).filename().stem().string()){
//if the extension is a wild card *or* it matches the read file extension
if( ext == "*" || "." + ext == (*it).filename().extension().string()){
file_list.push_back((*it).string()); //include it in the final list
}
}
}
}
}
return file_list;
}
//gets the current working directory
static stim::filename cwd(){
char cCurrentPath[FILENAME_MAX];
if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath))){
std::cout<<"ERROR getting current working directory."<<std::endl;
exit(1);
}
|
8b7be670
David Mayerich
implemented savin...
|
208
|
std::cout<<cCurrentPath<<std::endl;
|
8157c392
David Mayerich
added parser and ...
|
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
return stim::filename(std::string(cCurrentPath) + STIM_FILENAME_DIV);
}
void set_name(std::string fname){
parse_name(fname);
}
//get a path relative to the current one
stim::filename get_relative(std::string rel){
std::vector<std::string> rel_path = stim::parser::split(rel, STIM_FILENAME_DIV);
//if the relative path doesn't contain a file, add a blank string to be used as the filename
if(rel[rel.size()-1] == STIM_FILENAME_DIV)
rel_path.push_back("");
//create a stack representing the current absolute path
std::stack<std::string, std::vector<std::string> > s(path);
//for each token in the relative path
for(int d=0; d<rel_path.size() - 1; d++){
//if the token is a double-dot, pop a directory off of the stack
if(rel_path[d] == "..")
s.pop();
else
s.push(rel_path[d]);
}
std::string* end = &s.top() + 1;
std::string* begin = end - s.size();
std::vector<std::string> result_path(begin, end);
//create a new path to be returned
|
8b7be670
David Mayerich
implemented savin...
|
243
|
stim::filename result = *this;
|
8157c392
David Mayerich
added parser and ...
|
244
245
246
247
248
249
|
result.path = result_path;
result.set_name(rel_path.back());
return result;
}
|
8b7be670
David Mayerich
implemented savin...
|
250
251
252
253
254
255
256
257
258
|
bool is_relative(){
return !absolute;
}
bool is_absolute(){
return absolute;
}
|
8157c392
David Mayerich
added parser and ...
|
259
260
261
262
263
264
265
266
267
268
269
270
271
|
};
} //end namespace stim
#endif
|