Isis 3 Programmer Reference
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 
41 
47  FileList::FileList(FileName listFile) {
48  read(listFile);
49  }
50 
56  FileList::FileList(std::istream &in) {
57  read(in);
58  }
59 
60 
68  void FileList::read(FileName listFile) {
69  // Open the file
70  ifstream istm;
71  istm.open(listFile.toString().toLatin1().data(), std::ios::in);
72  if(!istm) {
73  QString message = Isis::Message::FileOpen(listFile.toString());
74  throw IException(IException::Io, message, _FILEINFO_);
75  }
76 
77  // Internalize
78  try {
79  read(istm);
80 
81  // Close the file
82  istm.close();
83  }
84  catch (IException &e) {
85  istm.close();
86  QString msg = "File [" + listFile.toString() + "] contains no data";
87  throw IException(IException::User, msg, _FILEINFO_);
88  }
89 
90  }
91 
102  void FileList::read(std::istream &in) {
103  // Read each file and put it in the vector
104  char buf[65536];
105  Isis::IString s;
106  bool bHasQuotes = false;
107 
108  in.getline(buf, 65536);
109  bool isComment = false;
110  while (!in.eof()) {
111  s = buf;
112  string::size_type loc = s.find("\"", 0);
113 
114  if (loc != string::npos) {
115  bHasQuotes = true;
116  }
117 
118  if (bHasQuotes) {
119  s = s.TrimHead("\"");
120  s = s.TrimTail("\"");
121  }
122 
123  s = s.TrimHead(" \n\r\t\v");
124 
125  isComment = false;
126  if(strlen(buf) == 0) {
127  in.getline(buf, 65536);
128  continue;
129  }
130  for (int index = 0; index < (int)strlen(buf); index++) {
131  if (buf[index] == '#' || (buf[index] == '/' && buf[index+1] == '/')) {
132  isComment = true;
133  break;
134  }
135  else if(buf[index] == ' ') {
136  continue;
137  }
138  else {
139  isComment = false;
140  break;
141  }
142  }
143  if (isComment) {
144  in.getline(buf, 65536);
145  continue;
146  }
147  else {
148 
149  if(bHasQuotes) {
150  s = s.Token(" \n\r\t\v");
151  }
152  else {
153  s = s.Token(" \n\r\t\v,");
154  }
155 
156  this->push_back(s.ToQt());
157  in.getline(buf, 65536);
158  }
159  }
160  if (this->size() == 0) {
161  string msg = "Input Stream Empty";
162  throw IException(IException::User, msg, _FILEINFO_);
163  }
164  }
165 
166 
175  void FileList::write(FileName outputFileList) {
176  // Open the file
177  ofstream ostm;
178  ostm.open(outputFileList.toString().toLatin1().data(), std::ios::out);
179  if (!ostm) {
180  QString message = Message::FileOpen(outputFileList.toString());
181  throw IException(IException::Io, message, _FILEINFO_);
182  }
183 
184  // Internalize
185  write(ostm);
186 
187  // Close the file
188  ostm.close();
189  }
190 
191 
197  void FileList::write(std::ostream &out) {
198  for (int i = 0; i < this->size(); i++) {
199  out << (*this)[i].toString() << endl;
200  }
201  }
202 } // end isis namespace
File name manipulation and expansion.
Definition: FileName.h:116
Namespace for the standard library.
IString TrimHead(const std::string &chars)
Trims The input characters from the beginning of the object IString.
Definition: IString.cpp:573
IString Token(const IString &separator)
Returns the first token in the IString.
Definition: IString.cpp:912
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:40
QString ToQt() const
Retuns the object string as a QString.
Definition: IString.cpp:884
QString toString() const
Returns a QString of the full file name including the file path, excluding the attributes with any Is...
Definition: FileName.cpp:531
Isis exception class.
Definition: IException.h:107
Adds specific functionality to C++ strings.
Definition: IString.h:181
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
QString FileOpen(const QString &filename)
This error should be used when a file could not be opened.
Definition: FileOpen.cpp:28
IString TrimTail(const std::string &chars)
Trims the input characters from the end of the object IString.
Definition: IString.cpp:602