USGS

Isis 3.0 Object Programmers' Reference

Home

FileList.cpp
Go to the documentation of this file.
1 
23 #include "FileList.h"
24 #include "IException.h"
25 #include "Message.h"
26 #include "FileName.h"
27 #include "IString.h"
28 #include <iostream>
29 #include <fstream>
30 
31 using namespace std;
32 namespace Isis {
33 
38  FileList::FileList() {
39  }
40 
46  FileList::FileList(FileName listFile) {
47  read(listFile);
48  }
49 
55  /*FileList::FileList(IString listFileString) {
56  read(FileName(listFileString));
57  }*/
58 
66  void FileList::read(FileName listFile) {
67  // Open the file
68  ifstream istm;
69  istm.open(listFile.toString().toAscii().data(), std::ios::in);
70  if(!istm) {
71  QString message = Isis::Message::FileOpen(listFile.toString());
72  throw IException(IException::Io, message, _FILEINFO_);
73  }
74 
75  // Internalize
76  try {
77  read(istm);
78 
79  // Close the file
80  istm.close();
81  }
82  catch (IException &e) {
83  printf("debugB\n");
84  istm.close();
85  QString msg = "File [" + listFile.toString() + "] contains no data";
86  throw IException(IException::User, msg, _FILEINFO_);
87  }
88 
89  }
90 
101  void FileList::read(std::istream &in) {
102  // Read each file and put it in the vector
103  char buf[65536];
104  Isis::IString s;
105  bool bHasQuotes = false;
106 
107  in.getline(buf, 65536);
108  bool isComment = false;
109  while (!in.eof()) {
110  s = buf;
111  string::size_type loc = s.find("\"", 0);
112 
113  if (loc != string::npos) {
114  bHasQuotes = true;
115  }
116 
117  if (bHasQuotes) {
118  s = s.TrimHead("\"");
119  s = s.TrimTail("\"");
120  }
121 
122  s = s.TrimHead(" \n\r\t\v");
123 
124  isComment = false;
125  if(strlen(buf) == 0) {
126  in.getline(buf, 65536);
127  continue;
128  }
129  for (int index = 0; index < (int)strlen(buf); index++) {
130  if (buf[index] == '#' || (buf[index] == '/' && buf[index+1] == '/')) {
131  isComment = true;
132  break;
133  }
134  else if(buf[index] == ' ') {
135  continue;
136  }
137  else {
138  isComment = false;
139  break;
140  }
141  }
142  if (isComment) {
143  in.getline(buf, 65536);
144  continue;
145  }
146  else {
147 
148  if(bHasQuotes) {
149  s = s.Token(" \n\r\t\v");
150  }
151  else {
152  s = s.Token(" \n\r\t\v,");
153  }
154 
155  this->push_back(s.ToQt());
156  in.getline(buf, 65536);
157  }
158  }
159  if (this->size() == 0) {
160  string msg = "Input Stream Empty";
161  throw IException(IException::User, msg, _FILEINFO_);
162  }
163  }
164 
165 
174  void FileList::write(FileName outputFileList) {
175  // Open the file
176  ofstream ostm;
177  ostm.open(outputFileList.toString().toAscii().data(), std::ios::out);
178  if (!ostm) {
179  QString message = Message::FileOpen(outputFileList.toString());
180  throw IException(IException::Io, message, _FILEINFO_);
181  }
182 
183  // Internalize
184  write(ostm);
185 
186  // Close the file
187  ostm.close();
188  }
189 
190 
196  void FileList::write(std::ostream &out) {
197  for (int i = 0; i < this->size(); i++) {
198  out << (*this)[i].toString() << endl;
199  }
200  }
201 } // end isis namespace