Blame view

stim/parser/filename.h 11.2 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"

9b766f1f   Pavel Govyadinov   completed merge f...
25
26
27
  #ifdef BOOST_PRECOMPILED

  #include <boost/filesystem.hpp>

  #endif

8157c392   David Mayerich   added parser and ...
28
29
30
31
32
33
34
35
36
  namespace stim{

  

  //filename class designed to work with both Windows and Unix

  

  class filename{

  

  private:

  	void init(){

  

8b7be670   David Mayerich   implemented savin...
37
  		absolute = false;

8157c392   David Mayerich   added parser and ...
38
39
40
41
42
43
44
45
46
47
  

  	}

  

  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...
48
49
  	bool absolute;					//filename is an absolute path

  

f28202b1   David Mayerich   fixed OS interop ...
50
51
52
  	//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 ...
53
54
55
  		return s;

  	}

  

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

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

963d0676   David Mayerich   bug fixes related...
71
  		if(loc.size() == 0) return;

8157c392   David Mayerich   added parser and ...
72
  

f28202b1   David Mayerich   fixed OS interop ...
73
74
  		loc = unix_div(loc);

  

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

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

8157c392   David Mayerich   added parser and ...
77
78
79
  		#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...
80
  				absolute = true;			//this is an absolute path

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

8b7be670   David Mayerich   implemented savin...
82
83
84
85
86
87
  				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 ...
88
89
90
  			}

  		#endif

  

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

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

8157c392   David Mayerich   added parser and ...
93
94
95
96
97
  

  		//parse the file name

  		parse_name(fname);

  

  		//find the directory hierarchy

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

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

8157c392   David Mayerich   added parser and ...
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  	}

  

  public:

  

  	filename(){

  		init();

  	}

  

  	filename(std::string loc){

  		init();

  		parse(loc);

  	}

  

  

4f007f77   David Mayerich   added better comm...
114
  	/// Outputs the file name (including prefix and extension)

8157c392   David Mayerich   added parser and ...
115
116
117
118
119
120
121
  	std::string get_name(){

  		if(prefix == "" && ext == "")

  			return "";

  		else

  			return prefix + "." + ext;

  	}

  

4f007f77   David Mayerich   added better comm...
122
  	/// Output the file extension only (usually three characters after a '.')

8157c392   David Mayerich   added parser and ...
123
124
125
126
  	std::string get_extension(){

  		return ext;

  	}

  

4f007f77   David Mayerich   added better comm...
127
  	/// Output the file prefix only (name before the extension)

8157c392   David Mayerich   added parser and ...
128
129
130
131
  	std::string get_prefix(){

  		return prefix;

  	}

  

4f007f77   David Mayerich   added better comm...
132
133
  	/// Output the entire path (not including the filename)

  	///		ex. "c:\this\is\the\directory\"

8157c392   David Mayerich   added parser and ...
134
135
136
  	std::string dir(){

  		std::stringstream ss;

  

8b7be670   David Mayerich   implemented savin...
137
138
139
140
141
142
143
144
  		//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 ...
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();

  

  	}

  

6422f96a   Pavel Govyadinov   minor bug fixes a...
154
155
156
157
158
159
160
161
  	void clear()

  	{

  		drive = "";				//drive letter (for Windows)

  		path.resize(0);

  		prefix= "";				//file prefix (without extension)

  		ext = "";				//file extension

  	}

  

4f007f77   David Mayerich   added better comm...
162
163
  	/// Output the entire string, including the path and filename

  	///		ex. "c:\this\is\a\directory\file.txt"

8157c392   David Mayerich   added parser and ...
164
  	std::string str(){

4f007f77   David Mayerich   added better comm...
165
166
167
  		std::stringstream ss;					//create a string stream

  		ss<<dir()<<get_name();					//push the directory and filename to that stream

  		return ss.str();						//convert the stream to a string and return it

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

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

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

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

4f007f77   David Mayerich   added better comm...
172
  	/// David: I have no idea what this is doing. It looks identical to dir()

945ee13c   Laila Saadatifard   the get_list func...
173
174
  	std::string dir_fname(){

  		std::stringstream ss;

8157c392   David Mayerich   added parser and ...
175
  

945ee13c   Laila Saadatifard   the get_list func...
176
177
178
179
180
181
182
183
  		//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 ...
184
  

eb5dfb2b   David Mayerich   fixed linux compa...
185
  

945ee13c   Laila Saadatifard   the get_list func...
186
187
188
  		for(unsigned int d = 0; d < path.size(); d++)

  			ss<<path[d]<<STIM_FILENAME_DIV;

  		ss<<get_name();

8157c392   David Mayerich   added parser and ...
189
  

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

8157c392   David Mayerich   added parser and ...
191
  

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

eb5dfb2b   David Mayerich   fixed linux compa...
193
  

945ee13c   Laila Saadatifard   the get_list func...
194
195
196
  	// 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 ...
197
  

4f007f77   David Mayerich   added better comm...
198
  		if(absolute){ 								//if the path is absolute

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

4f007f77   David Mayerich   added better comm...
200
  				ss<<drive<<":";						//output the drive letter if in Windows

945ee13c   Laila Saadatifard   the get_list func...
201
  			#endif

4f007f77   David Mayerich   added better comm...
202
  			ss<<STIM_FILENAME_DIV;					//output a path divider

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

8157c392   David Mayerich   added parser and ...
204
  

4f007f77   David Mayerich   added better comm...
205
206
  		for(unsigned int d = 0; d < path.size(); d++)	//for each directory in the current path

  			ss<<path[d]<<STIM_FILENAME_DIV;				//add that directory, interspersing path dividers

8157c392   David Mayerich   added parser and ...
207
  

4f007f77   David Mayerich   added better comm...
208
  		stim::filename fn = file_name;					

945ee13c   Laila Saadatifard   the get_list func...
209
210
211
  			std::string fn_prefix = fn.prefix;

  			std::string fn_ext = fn.ext;

  		ss<<fn_prefix + '.' + fn_ext;

8157c392   David Mayerich   added parser and ...
212
  

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

8157c392   David Mayerich   added parser and ...
214
  

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

8157c392   David Mayerich   added parser and ...
216
  

4f007f77   David Mayerich   added better comm...
217
218
219
220
  	/// Returns a list of files using the current filename as a template.

  	/// For example:

  	///			C:\this\is\a\path\file*.txt

  	///		can be used as a template to find a series of files file001.txt, file002.txt, file003.txt, etc.

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

4f007f77   David Mayerich   added better comm...
222
223
  		//this is OS dependent, so we're starting with Windows

  		//the Unix version requires Boost

8157c392   David Mayerich   added parser and ...
224
  

6d242237   David Mayerich   added comments an...
225
  #ifdef _WIN32

4f007f77   David Mayerich   added better comm...
226
  		stim::filename filepath(dir_fname());									//initialize filepath with the mask					

f28202b1   David Mayerich   fixed OS interop ...
227
  

4f007f77   David Mayerich   added better comm...
228
  		HANDLE            hFind = INVALID_HANDLE_VALUE;							//allocate data structures for looping through files

945ee13c   Laila Saadatifard   the get_list func...
229
  		WIN32_FIND_DATAA   FindFileData;

4f007f77   David Mayerich   added better comm...
230
  		std::vector<stim::filename> file_list;									//initialize a list to hold all matching filenames

eb5dfb2b   David Mayerich   fixed linux compa...
231
  

4f007f77   David Mayerich   added better comm...
232
  		hFind = FindFirstFileA((filepath.str().c_str()), &FindFileData);		//find the first file that matches the specified file path

945ee13c   Laila Saadatifard   the get_list func...
233
  

4f007f77   David Mayerich   added better comm...
234
235
  		if (hFind == INVALID_HANDLE_VALUE) { 									//if there are no matching files

  			printf ("Invalid file handle. Error is %u.\n", GetLastError());		//print an error

eb5dfb2b   David Mayerich   fixed linux compa...
236
  		}

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

4f007f77   David Mayerich   added better comm...
238
239
240
241
  			std::string file_name = FindFileData.cFileName;						//save the file name

  			std::string file_path = dir();										//the file is in the specified directory, so save it

  			stim::filename current_file(file_path + file_name);					//create a stim::filename structure representing this file

  			file_list.push_back(current_file);									//push the new stim::filename to the file list

945ee13c   Laila Saadatifard   the get_list func...
242
243
  

  			// List all the other files in the directory.

4f007f77   David Mayerich   added better comm...
244
245
246
247
  			while (FindNextFileA(hFind, &FindFileData) != 0){ 					//iterate until there are no more matching files

  				file_name = FindFileData.cFileName;								//save the next file

  				current_file = (f_name(file_name));								//append the directory

  				file_list.push_back(current_file);								//push it to the list

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

4f007f77   David Mayerich   added better comm...
249
  			FindClose(hFind);													//close the file data structure

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

4f007f77   David Mayerich   added better comm...
251
  		return file_list;														//return the list of files

4e2809fa   David Mayerich   fixed an error pr...
252
253
  

  #elif BOOST_PRECOMPILED

9b766f1f   Pavel Govyadinov   completed merge f...
254
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
  

  		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

  						}

  

  					}

  

  

  

  				}

  

  			}

  

84bae1ab   David Mayerich   fixed an error pr...
282
283
  		}

  		return file_list;

4e2809fa   David Mayerich   fixed an error pr...
284
285
286
  #else

  		std::cout<<"ERROR: UNIX systems don't support file lists without the Boost precompiled libraries."<<std::endl;

  		exit(1);

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

84bae1ab   David Mayerich   fixed an error pr...
288
  		

eb5dfb2b   David Mayerich   fixed linux compa...
289
  	}

945ee13c   Laila Saadatifard   the get_list func...
290
291
  	//**************************************************************************************************

  

8157c392   David Mayerich   added parser and ...
292
293
294
295
296
297
298
299
300
301
302
  	//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...
303
  		 std::cout<<cCurrentPath<<std::endl;

8157c392   David Mayerich   added parser and ...
304
305
306
307
308
309
310
  		 return stim::filename(std::string(cCurrentPath) + STIM_FILENAME_DIV);

  	}

  

  	void set_name(std::string fname){

  		parse_name(fname);

  	}

  

963d0676   David Mayerich   bug fixes related...
311
312
313
314
315
316
317
  	//append a string to the filename and return a new filename

  	stim::filename append(std::string s){

  		stim::filename result = *this;		//create a new filename, copy the current filename

  		result.prefix = prefix + s;			//append the string to the filename

  		return result;

  	}

  

8157c392   David Mayerich   added parser and ...
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
  	//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...
345
  		stim::filename result = *this;

8157c392   David Mayerich   added parser and ...
346
347
348
349
350
351
  		result.path = result_path;

  		result.set_name(rel_path.back());

  

  		return result;

  	}

  

3d0b6243   David Mayerich   fixed hsiproc bug...
352
353
354
355
356
357
  	/// 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...
358
359
360
  			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...
361
362
363
  		return result;								//return the result

  	}

  

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

3d0b6243   David Mayerich   fixed hsiproc bug...
365
366
367
368
369
370
  

  		std::stringstream ss;

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

  		return insert(ss.str());

  	}

  

8b7be670   David Mayerich   implemented savin...
371
372
373
374
375
376
377
378
379
  	bool is_relative(){

  		return !absolute;

  	}

  

  	bool is_absolute(){

  		return absolute;

  	}

  

  

8157c392   David Mayerich   added parser and ...
380
381
382
383
384
385
386
387
388
389
390
391
  

  

  

  

  

  

  

  };

  

  

  }	//end namespace stim

  

f92397d2   David Mayerich   fixed a Win32 bug...
392
  

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

f92397d2   David Mayerich   fixed a Win32 bug...
394