Blame view

stim/parser/filename.h 7.81 KB
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

f92397d2   David Mayerich   fixed a Win32 bug...
6
  	#include <windows.h>

8157c392   David Mayerich   added parser and ...
7
8
9
10
11
12
13
14
15
16
17
      #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>

3d0b6243   David Mayerich   fixed hsiproc bug...
18
  #include <iomanip>

8157c392   David Mayerich   added parser and ...
19
20
21
  #include <vector>

  #include <stack>

  #include <algorithm>

f92397d2   David Mayerich   fixed a Win32 bug...
22
  #include <iostream>

8157c392   David Mayerich   added parser and ...
23
24
  

  #include "../parser/parser.h"

8157c392   David Mayerich   added parser and ...
25
26
27
28
29
30
31
32
33
  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

  

f28202b1   David Mayerich   fixed OS interop ...
47
48
49
  	//replaces win32 dividers with the Linux standard

  	std::string unix_div(std::string s) {

  		std::replace( s.begin(), s.end(), '\\', '/');

8157c392   David Mayerich   added parser and ...
50
51
52
  		return s;

  	}

  

f28202b1   David Mayerich   fixed OS interop ...
53
  	//break up a filename into a prefix and extension

8157c392   David Mayerich   added parser and ...
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  	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){

  

f28202b1   David Mayerich   fixed OS interop ...
69
70
  		loc = unix_div(loc);

  

8157c392   David Mayerich   added parser and ...
71
  		//determine the drive (if Windows)

eb5dfb2b   David Mayerich   fixed linux compa...
72
  		drive = "";

8157c392   David Mayerich   added parser and ...
73
74
75
  		#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...
76
  				absolute = true;			//this is an absolute path

8157c392   David Mayerich   added parser and ...
77
  				drive = loc[0];				//copy the drive letter

8b7be670   David Mayerich   implemented savin...
78
79
80
81
82
83
  				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 ...
84
85
86
  			}

  		#endif

  

8157c392   David Mayerich   added parser and ...
87
  		//determine the file name

f28202b1   David Mayerich   fixed OS interop ...
88
  		std::string fname = loc.substr( loc.find_last_of('/') + 1 );	//find the file name (including extension)

8157c392   David Mayerich   added parser and ...
89
90
91
92
93
  

  		//parse the file name

  		parse_name(fname);

  

  		//find the directory hierarchy

f28202b1   David Mayerich   fixed OS interop ...
94
95
  		std::string dir = loc.substr(0, loc.find_last_of('/') + 1 );

  		path = stim::parser::split(dir, '/');

8157c392   David Mayerich   added parser and ...
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
  	}

  

  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...
129
130
131
132
133
134
135
136
  		//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 ...
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  

  		//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();

  	}

4191c034   Pavel Govyadinov   minor:bug fixes. ...
154
  

945ee13c   Laila Saadatifard   the get_list func...
155
  	//*****************************************************************************************************************

eb5dfb2b   David Mayerich   fixed linux compa...
156
  	// output is the directory and the prefix and the extension of the files, which are looking for in that directory.

945ee13c   Laila Saadatifard   the get_list func...
157
158
  	std::string dir_fname(){

  		std::stringstream ss;

8157c392   David Mayerich   added parser and ...
159
  

945ee13c   Laila Saadatifard   the get_list func...
160
161
162
163
164
165
166
167
  		//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 ...
168
  

eb5dfb2b   David Mayerich   fixed linux compa...
169
  

945ee13c   Laila Saadatifard   the get_list func...
170
171
172
  		for(unsigned int d = 0; d < path.size(); d++)

  			ss<<path[d]<<STIM_FILENAME_DIV;

  		ss<<get_name();

8157c392   David Mayerich   added parser and ...
173
  

945ee13c   Laila Saadatifard   the get_list func...
174
  		return ss.str();

8157c392   David Mayerich   added parser and ...
175
  

945ee13c   Laila Saadatifard   the get_list func...
176
  	}

eb5dfb2b   David Mayerich   fixed linux compa...
177
  

945ee13c   Laila Saadatifard   the get_list func...
178
179
180
  	// output is the directory and the name of the file which are found in that given directory

  	std::string f_name(std::string file_name){

  		std::stringstream ss;

8157c392   David Mayerich   added parser and ...
181
  

945ee13c   Laila Saadatifard   the get_list func...
182
183
184
185
186
187
188
189
  		//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 ...
190
  

945ee13c   Laila Saadatifard   the get_list func...
191
192
193
  		//output the directory

  		for(unsigned int d = 0; d < path.size(); d++)

  			ss<<path[d]<<STIM_FILENAME_DIV;

8157c392   David Mayerich   added parser and ...
194
  

945ee13c   Laila Saadatifard   the get_list func...
195
196
197
198
  		stim::filename fn = file_name;

  			std::string fn_prefix = fn.prefix;

  			std::string fn_ext = fn.ext;

  		ss<<fn_prefix + '.' + fn_ext;

8157c392   David Mayerich   added parser and ...
199
  

945ee13c   Laila Saadatifard   the get_list func...
200
  		return ss.str();

8157c392   David Mayerich   added parser and ...
201
  

945ee13c   Laila Saadatifard   the get_list func...
202
  	}

8157c392   David Mayerich   added parser and ...
203
  

eb5dfb2b   David Mayerich   fixed linux compa...
204
  	std::vector<stim::filename> get_list(){

945ee13c   Laila Saadatifard   the get_list func...
205
  #ifdef _WIN32

945ee13c   Laila Saadatifard   the get_list func...
206
  	//get a list of files matching the current template

eb5dfb2b   David Mayerich   fixed linux compa...
207
  

8157c392   David Mayerich   added parser and ...
208
  

945ee13c   Laila Saadatifard   the get_list func...
209
210
  		//stim::filename file_path;

  		stim::filename filepath(dir_fname());

f28202b1   David Mayerich   fixed OS interop ...
211
  

945ee13c   Laila Saadatifard   the get_list func...
212
213
214
  		HANDLE            hFind = INVALID_HANDLE_VALUE;

  		WIN32_FIND_DATAA   FindFileData;

  		std::vector<stim::filename> file_list;

eb5dfb2b   David Mayerich   fixed linux compa...
215
  

945ee13c   Laila Saadatifard   the get_list func...
216
217
218
  		hFind = FindFirstFileA((filepath.str().c_str()), &FindFileData);

  

  		if (hFind == INVALID_HANDLE_VALUE) {

43dec788   David Mayerich   added code for si...
219
  			printf ("Invalid file handle. Error is %u.\n", GetLastError());

eb5dfb2b   David Mayerich   fixed linux compa...
220
  		}

945ee13c   Laila Saadatifard   the get_list func...
221
  		else {

f28202b1   David Mayerich   fixed OS interop ...
222
223
224
  			std::string file_name = FindFileData.cFileName;					//get the file name

  			std::string file_path = dir();

  			stim::filename current_file(file_path + file_name);

945ee13c   Laila Saadatifard   the get_list func...
225
226
227
228
229
230
231
  			file_list.push_back(current_file);

  

  			// List all the other files in the directory.

  			while (FindNextFileA(hFind, &FindFileData) != 0){

  				file_name = FindFileData.cFileName;

  				current_file = (f_name(file_name));

  				file_list.push_back(current_file);

8157c392   David Mayerich   added parser and ...
232
  			}

945ee13c   Laila Saadatifard   the get_list func...
233
  			FindClose(hFind);

43dec788   David Mayerich   added code for si...
234
  		}

8157c392   David Mayerich   added parser and ...
235
  		return file_list;

eb5dfb2b   David Mayerich   fixed linux compa...
236
237
238
  #else

  	std::cout<<"File lists aren't currently supported on Unix/Linux systems."<<std::endl;

  	exit(1);

788862df   Tianshu Cheng   added a BOOST_PRE...
239
  #endif

eb5dfb2b   David Mayerich   fixed linux compa...
240
  	}

945ee13c   Laila Saadatifard   the get_list func...
241
242
  	//**************************************************************************************************

  

8157c392   David Mayerich   added parser and ...
243
244
245
246
247
248
249
250
251
252
253
  	//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...
254
  		 std::cout<<cCurrentPath<<std::endl;

8157c392   David Mayerich   added parser and ...
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  		 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...
289
  		stim::filename result = *this;

8157c392   David Mayerich   added parser and ...
290
291
292
293
294
295
  		result.path = result_path;

  		result.set_name(rel_path.back());

  

  		return result;

  	}

  

3d0b6243   David Mayerich   fixed hsiproc bug...
296
297
298
299
300
301
  	/// This function replaces a wildcard in the prefix with the specified string

  	stim::filename insert(std::string str){

  

  		stim::filename result = *this;				//initialize the result filename to the current filename

  		size_t loc = result.prefix.find('*');		//find a wild card in the string

  		if(loc == std::string::npos)						//if a wildcard isn't found

a2bf1d08   David Mayerich   general bug fixes...
302
303
304
  			result.prefix += str;							//append the value to the prefix

  		else

  			result.prefix.replace(loc, 1, str);			//replace the wildcard with the string

3d0b6243   David Mayerich   fixed hsiproc bug...
305
306
307
  		return result;								//return the result

  	}

  

9d3ba0b1   David Mayerich   added stim::hsi a...
308
  	stim::filename insert(size_t i, size_t n = 2){

3d0b6243   David Mayerich   fixed hsiproc bug...
309
310
311
312
313
314
  

  		std::stringstream ss;

  		ss << std::setw(n) << std::setfill('0') << i;

  		return insert(ss.str());

  	}

  

8b7be670   David Mayerich   implemented savin...
315
316
317
318
319
320
321
322
323
  	bool is_relative(){

  		return !absolute;

  	}

  

  	bool is_absolute(){

  		return absolute;

  	}

  

  

8157c392   David Mayerich   added parser and ...
324
325
326
327
328
329
330
331
332
333
334
335
  

  

  

  

  

  

  

  };

  

  

  }	//end namespace stim

  

f92397d2   David Mayerich   fixed a Win32 bug...
336
  

8157c392   David Mayerich   added parser and ...
337
  #endif

f92397d2   David Mayerich   fixed a Win32 bug...
338