• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/DGVFile.cpp

Go to the documentation of this file.
00001 
00022 #include "DGVFile.h"
00023 
00024 DGVFile::DGVFile()
00025 {
00026     commentSymbol = "#";
00027     separator = " ";
00028     comments = true;
00029     openedIn = false;
00030     openedOut = false;
00031     openedAppend = false;
00032     verboseMode = false;
00033 }
00034 
00035 DGVFile::DGVFile(QString name, QString mode)
00036 {
00037     commentSymbol = "#";
00038     separator = " ";
00039     comments = true;
00040     openedIn = false;
00041     openedOut = false;
00042     openedAppend = false;
00043     verboseMode = false;
00044 
00045     open(name,mode);
00046 }
00047 
00048 DGVFile::~DGVFile()
00049 {
00050     close();
00051 }
00052 
00053 bool DGVFile::open(QString name, QString mode)
00054 {
00055     fileName = name;
00056     fileMode = mode;
00057 
00058     if(!fileName.isEmpty())
00059     {
00060         file.setFileName(fileName);
00061 
00062         fileMode.toLower();
00063         if(fileMode == "r")
00064         {
00065             if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
00066                 return false;
00067 
00068             in = new QTextStream(&file);
00069 
00070             openedIn = true;
00071         }
00072         else if(fileMode == "w")
00073         {
00074             if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
00075                 return false;
00076 
00077             out = new QTextStream(&file);
00078 
00079             openedOut = true;
00080         }
00081         else if(fileMode == "a")
00082         {
00083             if(!file.open(QIODevice::Append | QIODevice::Text))
00084                 return false;
00085 
00086             append = new QTextStream(&file);
00087 
00088             openedAppend = true;
00089         }
00090         else
00091         {
00092             cerr << "File ERROR: Mode not supported!" << endl;
00093             return false;
00094         }
00095         return true;
00096     }
00097     else
00098         return false;
00099 }
00100 
00101 void DGVFile::close()
00102 {
00103     if(openedIn)
00104         delete in;
00105     else if(openedOut)
00106         delete out;
00107     else if(openedAppend)
00108         delete append;
00109 
00110     if(file.isOpen())
00111         file.close();
00112     //file.reset();
00113 
00114     openedIn = false;
00115     openedOut = false;
00116     openedAppend = false;
00117 }
00118 
00119 void DGVFile::writeLine(QString data)
00120 {
00121     if(!fileName.isEmpty())
00122     {
00123         if(openedOut)
00124         {
00125             (*out) << data << endl;
00126         }
00127         else
00128             cerr << "File ERROR: File not opened for writing!" << endl;
00129     }
00130 }
00131 
00132 void DGVFile::writeIntegerArrayAsLine(Array<long,1> &data)
00133 {
00134     QString tmpNum, line;
00135 
00136     for(int j = 0; j < data.size(); j ++)
00137         line += tmpNum.setNum(data(j)) + separator + " ";
00138 
00139     writeLine(line);
00140 }
00141 
00142 void DGVFile::writeDoubleArrayAsLine(Array<double,1> &data)
00143 {
00144     QString tmpNum, line;
00145 
00146     for(int j = 0; j < data.size(); j ++)
00147         line += tmpNum.setNum(data(j)) + separator + " ";
00148 
00149     writeLine(line);
00150 }
00151 
00152 inline void DGVFile::writeComment(QString comment)
00153 {
00154     writeLine(commentSymbol + " " + comment);
00155 }
00156 
00157 /*void DGVFile::writeIntegerArray(Array<int,1> &data)
00158 {
00159     if(!fileName.isEmpty())
00160     {
00161         if(openedOut)
00162         {
00163             (*out) << data << endl;
00164         }
00165         else
00166             cerr << "File ERROR: File not opened for writing!" << endl;
00167     }
00168 }
00169 
00170 void DGVFile::writeArray(Array<imageType,1> &data)
00171 {
00172     if(!fileName.isEmpty())
00173     {
00174         if(openedOut)
00175         {
00176             (*out) << data << endl;
00177         }
00178         else
00179             cerr << "File ERROR: File not opened for writing!" << endl;
00180     }
00181 }
00182 
00183 void DGVFile::writeArray(Array<imageType,2> &data)
00184 {
00185     if(!fileName.isEmpty())
00186     {
00187         if(openedOut)
00188         {
00189             (*out) << data << endl;
00190         }
00191         else
00192             cerr << "File ERROR: File not opened for writing!" << endl;
00193     }
00194 }*/
00195 
00196 QStringList DGVFile::readNextLine()
00197 {
00198     QString line;
00199     QStringList lineElements;
00200 
00201     if(openedIn)
00202     {
00203         int commentLocation = 0;
00204 
00205         line = in->readLine();
00206 
00209         if(line.contains(commentSymbol) && comments)
00210         {
00211             commentLocation = line.indexOf(commentSymbol);
00212             line.truncate(commentLocation); 
00213         }
00214     }
00215     else
00216         cerr << "File ERROR: File not opened for reading!" << endl;
00217 
00218     //cerr << "Line Read: " << line.toStdString() << endl;
00219     if(verboseMode)
00220         lineElements = line.split(separator);
00221     else
00222         lineElements = line.split(separator,QString::SkipEmptyParts);
00223 
00224     if(lineElements.size() == 1 && lineElements[0] == separator)
00225         lineElements.clear(); //Error: Split returns separator if separator not present.
00226 
00227     return lineElements;
00228 }
00229 
00230 void DGVFile::readNextLineAsIntegers(Array<long,1> &values)
00231 {
00232     QStringList lineElements;
00233 
00234     lineElements = readNextLine();
00235 
00236     values.resize(lineElements.size());
00237     for(int j = 0; j < lineElements.size(); j ++)
00238         values(j) = lineElements[j].toLong();
00239 }
00240 
00241 void DGVFile::readNextLineAsDoubles(Array<double,1> &values)
00242 {
00243     QStringList lineElements;
00244 
00245     lineElements = readNextLine();
00246 
00247     values.resize(lineElements.size());
00248     for(int j = 0; j < lineElements.size(); j ++)
00249         values(j) = lineElements[j].toDouble();
00250 }
00251 
00252 bool DGVFile::atEnd()
00253 {
00254     if(openedIn)
00255         return in->atEnd();
00256     else if(openedOut)
00257         return out->atEnd();
00258     else if(openedAppend)
00259         return append->atEnd();
00260     else
00261         return file.atEnd();
00262 }

Generated on Wed Sep 8 2010 01:36:51 for DGV by  doxygen 1.7.1