Diana Software
QFileList.cc
Go to the documentation of this file.
1 #include "QFileList.hh"
2 #include <fstream>
3 
4 
6 {
7  fFileList.clear();
8  fFilesPath = ".";
9 }
10 
12 {
13 }
14 
15 void QFileList::Add(const std::list<std::string>& list)
16 {
17  std::list<std::string>::const_iterator iter = list.begin();
18  while(iter != list.end()) {
19  Add(*iter);
20  iter++;
21  }
22 }
23 
24 std::list<std::string> QFileList::GetList(const std::string& option)
25 {
26 /* if(option == "FULLPATH") {
27  std::list<std::string> list;
28  std::list<std::string>::const_iterator iter = fFileList.begin();
29  while(iter != fFileList.end()) {
30  std::string fileFullPath = fFilesPath + "/" + *iter;
31  list.push_back(fileFullPath);
32  iter++;
33  }
34  return list;
35  }
36 */
37  return fFileList;
38 }
39 
40 std::vector<std::string> QFileList::GetListVec(const std::string& option)
41 {
42  std::list<std::string> flist = GetList(option);
43  return std::vector<std::string> (flist.begin(),flist.end());
44 }
45 void QFileList::Write(const std::string& fileListPath) const
46 {
47  if(fFileList.size() > 0) {
48  std::list<std::string>::const_iterator filesListIter;
49  std::ofstream of(fileListPath.c_str());
50  of<<"DATAPATH"<<" "<<fFilesPath<<std::endl;
51  of<<"START"<<std::endl;
52  for(filesListIter = fFileList.begin(); filesListIter != fFileList.end(); filesListIter++)
53  of<<(*filesListIter)<<std::endl;
54  of<<"END"<<std::endl;
55  of.close();
56  }
57 }
58 
59 void QFileList::Write(const std::string& fileListFile, const std::list<std::string>& fileList, const std::string& filesPath)
60 {
61  QFileList qFileList;
62  qFileList.Add(fileList);
63  qFileList.SetFilesPath(filesPath);
64  qFileList.Write(fileListFile);
65 }
66 
67 void QFileList::Read(const std::string& fileListPath)
68 {
69  std::ifstream inputFileList (fileListPath.c_str());
70  if (inputFileList.is_open()) {
71  bool startFound = false;
72  std::string line;
73  std::string dataPath;
74  while (! inputFileList.eof() ) {
75  getline(inputFileList,line);
76  // start parsing code
77  if(line == "") continue;
78  else if(line.at(0) == '#') continue;
79  else if(line == "END") break;
80  else if(line == "START") startFound = true;
81  else if(line.substr(0,8) == "DATAPATH") {
82  int valStart = line.find_first_not_of(' ', 8);
83  int valEnd = line.find_first_of(' ', valStart);
84  if(valStart - valEnd > 0)
85  dataPath = line.substr(valStart,valEnd - valStart);
86  // Debug("DataPath is \"%s\"", dataPath.c_str());
87  if(fFilesPath == ".") fFilesPath = dataPath;
88  }
89  else if(startFound) {
90  if (line.size() > 5
91  && line.rfind(".list") != std::string::npos
92  && line.rfind(".list") == line.size() - 5) {
93  // line has extension ".list"
94  // parse it as a file list
95  // Debug("Found file list %s", line.c_str());
96  this->Read(dataPath + "/" + line);
97  }
98  else {
99  fFileList.push_back(dataPath + "/" + line);
100  // Debug("Found file %s", line.c_str());
101  }
102  }
103  // end of parsing code
104  }
105  // if(!startFound) Panic("Could not find the START tag in yout filelist");
106  inputFileList.close();
107  }
108 }
109 
110 std::list<std::string> QFileList::Read(const std::string& fileListFile, const std::string& option)
111 {
112  QFileList qFileList;
113  qFileList.Read(fileListFile);
114  return qFileList.GetList(option);
115 
116 }
QOptimumFilter of(ap, an,-1, false)
File list handler.
Definition: QFileList.hh:13
void Write(const std::string &fileListPath) const
Definition: QFileList.cc:45
~QFileList()
Definition: QFileList.cc:11
std::vector< std::string > GetListVec(const std::string &option="")
Definition: QFileList.cc:40
void Read(const std::string &fileListPath)
Definition: QFileList.cc:67
void SetFilesPath(const std::string &path)
Definition: QFileList.hh:23
void Add(const std::string &file)
Definition: QFileList.hh:21
std::list< std::string > fFileList
Definition: QFileList.hh:36
QFileList()
Definition: QFileList.cc:5
std::list< std::string > GetList(const std::string &option="")
Definition: QFileList.cc:24
std::string fFilesPath
Definition: QFileList.hh:37