Isis 3 Programmer Reference
FileList.cpp
1
6/* SPDX-License-Identifier: CC0-1.0 */
7#include "FileList.h"
8#include "IException.h"
9#include "Message.h"
10#include "FileName.h"
11#include "IString.h"
12#include <iostream>
13#include <fstream>
14
15using namespace std;
16namespace Isis {
17
24
25
32 read(listFile);
33 }
34
40 FileList::FileList(std::istream &in) {
41 read(in);
42 }
43
44
52 void FileList::read(FileName listFile) {
53 // Open the file
54 ifstream istm;
55 istm.open(listFile.toString().toLatin1().data(), std::ios::in);
56 if(!istm) {
57 QString message = Isis::Message::FileOpen(listFile.toString());
58 throw IException(IException::Io, message, _FILEINFO_);
59 }
60
61 // Internalize
62 try {
63 read(istm);
64
65 // Close the file
66 istm.close();
67 }
68 catch (IException &e) {
69 istm.close();
70 QString msg = "File [" + listFile.toString() + "] contains no data";
71 throw IException(IException::User, msg, _FILEINFO_);
72 }
73
74 }
75
86 void FileList::read(std::istream &in) {
87 // Read each file and put it in the vector
88 char buf[65536];
90 bool bHasQuotes = false;
91
92 bool isComment = false;
93 do {
94 in.getline(buf, 65536);
95
96 s = buf;
97 string::size_type loc = s.find("\"", 0);
98
99 if (loc != string::npos) {
100 bHasQuotes = true;
101 }
102
103 if (bHasQuotes) {
104 s = s.TrimHead("\"");
105 s = s.TrimTail("\"");
106 }
107
108 s = s.TrimHead(" \n\r\t\v");
109
110 isComment = false;
111 if(strlen(buf) == 0) {
112 continue;
113 }
114 for (int index = 0; index < (int)strlen(buf); index++) {
115 if (buf[index] == '#' || (buf[index] == '/' && buf[index+1] == '/')) {
116 isComment = true;
117 break;
118 }
119 else if(buf[index] == ' ') {
120 continue;
121 }
122 else {
123 isComment = false;
124 break;
125 }
126 }
127 if (isComment) {
128 continue;
129 }
130 else {
131
132 if(bHasQuotes) {
133 s = s.Token(" \n\r\t\v");
134 }
135 else {
136 s = s.Token(" \n\r\t\v,");
137 }
138
139 this->push_back(s.ToQt());
140 }
141
142 } while(!in.eof());
143 if (this->size() == 0) {
144 string msg = "Input Stream Empty";
145 throw IException(IException::User, msg, _FILEINFO_);
146 }
147 }
148
149
158 void FileList::write(FileName outputFileList) {
159 // Open the file
160 ofstream ostm;
161 ostm.open(outputFileList.toString().toLatin1().data(), std::ios::out);
162 if (!ostm) {
163 QString message = Message::FileOpen(outputFileList.toString());
164 throw IException(IException::Io, message, _FILEINFO_);
165 }
166
167 // Internalize
168 write(ostm);
169
170 // Close the file
171 ostm.close();
172 }
173
174
180 void FileList::write(std::ostream &out) {
181 for (int i = 0; i < this->size(); i++) {
182 out << (*this)[i].toString() << endl;
183 }
184 }
185} // end isis namespace
void read(FileName listFile)
reads in a FileName obj
Definition FileList.cpp:52
FileList()
Creates an empty FileList obj.
Definition FileList.cpp:22
void write(FileName outputFileList)
writes to a FileName obj
Definition FileList.cpp:158
File name manipulation and expansion.
Definition FileName.h:100
Isis exception class.
Definition IException.h:91
@ User
A type of error that could only have occurred due to a mistake on the user's part (e....
Definition IException.h:126
@ Io
A type of error that occurred when performing an actual I/O operation.
Definition IException.h:155
Adds specific functionality to C++ strings.
Definition IString.h:165
IString TrimTail(const std::string &chars)
Trims the input characters from the end of the object IString.
Definition IString.cpp:587
IString Token(const IString &separator)
Returns the first token in the IString.
Definition IString.cpp:897
IString TrimHead(const std::string &chars)
Trims The input characters from the beginning of the object IString.
Definition IString.cpp:558
QString FileOpen(const QString &filename)
This error should be used when a file could not be opened.
Definition FileOpen.cpp:11
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16
Namespace for the standard library.