Grid3
FileName.h
Go to the documentation of this file.
1 #ifndef _FILENAME_H
2 #define _FILENAME_H
3 
4 #include <vector>
5 #include <string>
6 using namespace std;
7 
8 inline vector<string> StringSplit(const string &source, const char* delimiter=" "){
9  vector<string> results;
10 
11  size_t prev = 0;
12  size_t next = 0;
13 
14  while(next!=string::npos){
15  next = source.find_first_of(delimiter, prev);
16  if (next - prev != 0){
17  results.push_back(source.substr(prev, next - prev));
18  }
19  prev = next + 1;
20  }
21 
22  return results;
23 }
24 
25 //A class for handling filenames on Windows
26 class FileName{
27 public:
29  FileName(std::string fullPath){
30  path =StringSplit(fullPath,"\\/");
31 
32  if(!(fullPath.back()=='\\' || fullPath.back()=='/')){
33  string tmp=path.back();
34  path.pop_back();
35  size_t ext_delimiter=tmp.find_last_of (".");
36  name=tmp.substr(0,ext_delimiter);
37  if(ext_delimiter!=string::npos){
38  extension=tmp.substr(ext_delimiter+1);
39  }
40  }
41  }
42 
43  std::string GetAsString(){string s;
44  for(int i=0; i<path.size(); i++){
45  s.append(path[i]);
46  s.append("\\");
47  }
48  s.append(name);
49  s.append(".");
50  s.append(extension);
51 
52  return s;
53  }
54 
55  std::string GetName(){return name;}
56  std::string GetExtension(){return extension;}
57 
58  void SetName(std::string s){name=s;}
59  void SetExtension(std::string s){extension=s;}
60 
61  void ClearPath(){path.clear();}
62  void PathAppend(std::string s){path.push_back(s);}
63 
64  void PathPrepend(std::string s){
65  vector<string> new_path(path.size()+1);
66  new_path[0]=s;
67  for(int i=0; i<path.size(); i++){
68  new_path[i+1]=path[i];
69  }
70  path=new_path;
71  }
72 
73 private:
74  std::vector<std::string > path;
75  std::string name;
76  std::string extension;
77 };
78 
79 #endif
void SetName(std::string s)
Definition: FileName.h:58
void PathAppend(std::string s)
Definition: FileName.h:62
std::string GetExtension()
Definition: FileName.h:56
Definition: FileName.h:26
FileName()
Definition: FileName.h:28
void PathPrepend(std::string s)
Definition: FileName.h:64
FileName(std::string fullPath)
Definition: FileName.h:29
std::string GetAsString()
Definition: FileName.h:43
std::string GetName()
Definition: FileName.h:55
void ClearPath()
Definition: FileName.h:61
void SetExtension(std::string s)
Definition: FileName.h:59
vector< string > StringSplit(const string &source, const char *delimiter=" ")
Definition: FileName.h:8