File indexing completed on 2024-12-29 04:11:45

0001 /***************************************************************************
0002  *                                                                         *
0003  *   Copyright : (C) 2010 The University of Toronto                        *
0004  *   email     : netterfield@astro.utoronto.ca                             *
0005  *                                                                         *
0006  *   This program is free software; you can redistribute it and/or modify  *
0007  *   it under the terms of the GNU General Public License as published by  *
0008  *   the Free Software Foundation; either version 2 of the License, or     *
0009  *   (at your option) any later version.                                   *
0010  *                                                                         *
0011  ***************************************************************************/
0012 
0013 #include "asciisourceconfig.h"
0014 
0015 #include "asciisource.h"
0016 #include "datasource.h"
0017 
0018 //
0019 // AsciiSourceConfig
0020 //
0021 
0022 
0023 #define DEFAULT_COMMENT_DELIMITERS "#"
0024 #define DEFAULT_COLUMN_WIDTH 16
0025 
0026 
0027 const char AsciiSourceConfig::Key_fileNamePattern[] = "Filename Pattern";
0028 const char AsciiSourceConfig::Tag_fileNamePattern[] = "filenamepattern";
0029 const char AsciiSourceConfig::Key_delimiters[] = "Comment Delimiters";
0030 const char AsciiSourceConfig::Tag_delimiters[] = "delimiters";
0031 const char AsciiSourceConfig::Key_indexVector[] ="Index";
0032 const char AsciiSourceConfig::Tag_indexVector[] ="vector";
0033 const char AsciiSourceConfig::Key_indexInterpretation[] = "Default INDEX Interpretation";
0034 const char AsciiSourceConfig::Tag_indexInterpretation[] = "interpretation";
0035 const char AsciiSourceConfig::Key_timeAsciiFormatString[] = "ASCII Time format";
0036 const char AsciiSourceConfig::Tag_timeAsciiFormatString[] = "asciiTimeFormat";
0037 const char AsciiSourceConfig::Key_columnType[] = "Column Type";
0038 const char AsciiSourceConfig::Tag_columnType[] = "columntype";
0039 const char AsciiSourceConfig::Key_columnDelimiter[] = "Column Delimiter";
0040 const char AsciiSourceConfig::Tag_columnDelimiter[] = "columndelimiter";
0041 const char AsciiSourceConfig::Key_columnWidth[] = "Column Width";
0042 const char AsciiSourceConfig::Tag_columnWidth[] = "columnwidth";
0043 const char AsciiSourceConfig::Key_columnWidthIsConst[] = "Column Width is const";
0044 const char AsciiSourceConfig::Tag_columnWidthIsConst[] = "columnwidthisconst";
0045 const char AsciiSourceConfig::Key_dataLine[] = "Data Start";
0046 const char AsciiSourceConfig::Tag_dataLine[] = "headerstart";
0047 const char AsciiSourceConfig::Key_readFields[] = "Read Fields";
0048 const char AsciiSourceConfig::Tag_readFields[] = "readfields";
0049 const char AsciiSourceConfig::Key_readUnits[] = "Read Units";
0050 const char AsciiSourceConfig::Tag_readUnits[] = "readunits";
0051 const char AsciiSourceConfig::Key_useDot[] = "Use Dot";
0052 const char AsciiSourceConfig::Tag_useDot[] = "usedot";
0053 const char AsciiSourceConfig::Key_fieldsLine[] = "Fields Line";
0054 const char AsciiSourceConfig::Tag_fieldsLine[] = "fields";
0055 const char AsciiSourceConfig::Key_unitsLine[] = "Units Line";
0056 const char AsciiSourceConfig::Tag_unitsLine[] = "units";
0057 const char AsciiSourceConfig::Key_limitFileBuffer[] = "Limit file buffer size";
0058 const char AsciiSourceConfig::Tag_limitFileBuffer[] = "limitFileBuffer";
0059 const char AsciiSourceConfig::Key_limitFileBufferSize[] = "Size of limited file buffer";
0060 const char AsciiSourceConfig::Tag_limitFileBufferSize[] = "limitFileBufferSize";
0061 const char AsciiSourceConfig::Key_useThreads[] = "Use threads when parsing Ascii data";
0062 const char AsciiSourceConfig::Tag_useThreads[] = "useThreads";
0063 const char AsciiSourceConfig::Key_dataRate[] = "Data Rate for index";
0064 const char AsciiSourceConfig::Tag_dataRate[] = "dataRate";
0065 const char AsciiSourceConfig::Key_offsetDateTime[] = "use an explicit date/time offset";
0066 const char AsciiSourceConfig::Tag_offsetDateTime[] = "offsetDateTime";
0067 const char AsciiSourceConfig::Key_offsetFileDate[] = "use file time/date as offset";
0068 const char AsciiSourceConfig::Tag_offsetFileDate[] = "offsetFileDate";
0069 const char AsciiSourceConfig::Key_offsetRelative[] = "use relative file time offset";
0070 const char AsciiSourceConfig::Tag_offsetRelative[] = "offsetRelavive";
0071 const char AsciiSourceConfig::Key_dateTimeOffset[] = "date/time offset";
0072 const char AsciiSourceConfig::Tag_dateTimeOffset[] = "dateTimeOffset";
0073 const char AsciiSourceConfig::Key_relativeOffset[] = "relative offset";
0074 const char AsciiSourceConfig::Tag_relativeOffset[] = "relativeOffset";
0075 const char AsciiSourceConfig::Key_nanValue[] = "NaN value";
0076 const char AsciiSourceConfig::Tag_nanValue[] = "nanValue";
0077 const char AsciiSourceConfig::Key_updateType[] = "updateType";
0078 const char AsciiSourceConfig::Tag_updateType[] = "updateType";
0079 
0080 AsciiSourceConfig::AsciiSourceConfig() :
0081   _delimiters(DEFAULT_COMMENT_DELIMITERS),
0082   _indexVector("INDEX"),
0083   _indexInterpretation(Unknown),
0084   _timeAsciiFormatString("hh:mm:ss.zzz"),
0085   _fileNamePattern(""),
0086   _columnType(Whitespace),
0087   _columnDelimiter(","),
0088   _columnWidth(DEFAULT_COLUMN_WIDTH),
0089   _columnWidthIsConst(false),
0090   _dataLine(0),
0091   _readFields(false),
0092   _readUnits(false),
0093   _fieldsLine(0),
0094   _unitsLine(0),
0095   _useDot(true),
0096   _limitFileBuffer(false),
0097   _limitFileBufferSize(100),
0098   _useThreads(false),
0099   _dataRate(1.0),
0100   _offsetDateTime(false),
0101   _offsetFileDate(false),
0102   _offsetRelative(true),
0103   _dateTimeOffset(QDateTime::currentDateTime()),
0104   _relativeOffset(0),
0105   _nanValue(0),
0106   _updateType(Kst::DataSource::Timer)
0107 {
0108 }
0109 
0110 
0111 
0112 void AsciiSourceConfig::save(QSettings& cfg) const {
0113   _fileNamePattern >> cfg;
0114   _indexVector >> cfg;
0115   _delimiters >> cfg;
0116   _indexInterpretation >> cfg;
0117   _columnType >> cfg;
0118   _columnDelimiter >> cfg;
0119   _columnWidth >> cfg;
0120   _dataLine >> cfg;
0121   _readFields >> cfg;
0122   _useDot >> cfg;
0123   _fieldsLine >> cfg;
0124   _columnWidthIsConst >> cfg;
0125   _readUnits >> cfg;
0126   _unitsLine >> cfg;
0127   _limitFileBuffer >> cfg;
0128   _limitFileBufferSize >> cfg;
0129   _useThreads >> cfg;
0130   _timeAsciiFormatString >> cfg;
0131   _dataRate >> cfg;
0132   _offsetDateTime >> cfg;
0133   _offsetFileDate >> cfg;
0134   _offsetRelative >> cfg;
0135   _dateTimeOffset >> cfg;
0136   _relativeOffset >> cfg;
0137   _nanValue >> cfg;
0138   _updateType >> cfg;
0139 }
0140 
0141 
0142 void AsciiSourceConfig::saveDefault(QSettings& cfg) const {
0143   cfg.beginGroup(AsciiSource::asciiTypeKey());
0144   save(cfg);
0145   cfg.endGroup();
0146 }
0147 
0148 
0149 void AsciiSourceConfig::saveGroup(QSettings& cfg, const QString& fileName) const {
0150   if (fileName.isEmpty())
0151     return;
0152   cfg.beginGroup(AsciiSource::asciiTypeKey());
0153   cfg.beginGroup(fileName);
0154   save(cfg);
0155   cfg.endGroup();
0156   cfg.endGroup();
0157 }
0158 
0159 
0160 void AsciiSourceConfig::read(QSettings& cfg) {
0161   _fileNamePattern << cfg;
0162   _indexVector << cfg;
0163   _delimiters << cfg;
0164   _indexInterpretation << cfg;
0165   _columnType << cfg;
0166   _columnDelimiter << cfg;
0167   _columnWidth << cfg;
0168   _dataLine << cfg;
0169   _readFields << cfg;
0170   _useDot << cfg;
0171   _fieldsLine << cfg;
0172   _columnWidthIsConst << cfg;
0173   _readUnits << cfg;
0174   _unitsLine << cfg;
0175   _limitFileBuffer << cfg;
0176   _limitFileBufferSize << cfg;
0177   _useThreads << cfg;
0178   _timeAsciiFormatString << cfg;
0179   _dataRate << cfg;
0180   _offsetDateTime << cfg;
0181   _offsetFileDate << cfg;
0182   _offsetRelative << cfg;
0183   _dateTimeOffset << cfg;
0184   _relativeOffset << cfg;
0185   _nanValue << cfg;
0186   _updateType << cfg;
0187 }
0188 
0189 
0190 const AsciiSourceConfig& AsciiSourceConfig::readGroup(QSettings& cfg, const QString& fileName) {
0191   cfg.beginGroup(AsciiSource::asciiTypeKey());
0192   read(cfg);
0193   if (!fileName.isEmpty()) {
0194     cfg.beginGroup(fileName);
0195     read(cfg);
0196     cfg.endGroup();
0197   }
0198   _delimiters = QRegExp::escape(_delimiters).toLatin1();
0199   cfg.endGroup();
0200   return *this;
0201 }
0202 
0203 
0204 void AsciiSourceConfig::save(QXmlStreamWriter& s) {
0205   s.writeStartElement("properties");
0206   if (_indexInterpretation != AsciiSourceConfig::Unknown) {
0207     _indexVector >> s;
0208     _indexInterpretation >> s;
0209   }
0210   _delimiters >> s;
0211 
0212   _columnType >> s;
0213   if (_columnType == Fixed) {
0214     _columnWidth >> s;
0215   } else if (_columnType == Custom) {
0216       _columnDelimiter >> s;
0217   }
0218 
0219   _dataLine >> s;
0220   _fieldsLine >> s;
0221   _readFields >> s;
0222   _useDot >> s;
0223   _columnWidthIsConst >> s;
0224   _readUnits >> s;
0225   _unitsLine >> s;
0226   _limitFileBuffer >> s;
0227   _limitFileBufferSize >> s;
0228   _useThreads >> s;
0229   _timeAsciiFormatString >> s;
0230   _dataRate >> s;
0231   _offsetDateTime >> s;
0232   _offsetFileDate >> s;
0233   _offsetRelative >> s;
0234   _dateTimeOffset >> s;
0235   _relativeOffset >> s;
0236   _nanValue >> s;
0237   _updateType >> s;
0238   s.writeEndElement();
0239 }
0240 
0241 
0242 void AsciiSourceConfig::parseProperties(QXmlStreamAttributes& attributes) {
0243   _fileNamePattern << attributes;
0244   _indexVector << attributes;
0245   _delimiters << attributes;
0246   _indexInterpretation << attributes;
0247   _columnType << attributes;
0248   _columnDelimiter << attributes;
0249   _columnWidth << attributes;
0250   _dataLine << attributes;
0251   _readFields << attributes;
0252   _useDot << attributes;
0253   _fieldsLine << attributes;
0254   _columnWidthIsConst << attributes;
0255   _readUnits << attributes;
0256   _unitsLine << attributes;
0257   _limitFileBuffer << attributes;
0258   _limitFileBufferSize << attributes;
0259   _useThreads << attributes;
0260   _timeAsciiFormatString << attributes;
0261   _dataRate << attributes;
0262   _offsetDateTime << attributes;
0263   _offsetFileDate << attributes;
0264   _offsetRelative << attributes;
0265   _dateTimeOffset << attributes;
0266   _relativeOffset << attributes;
0267   _nanValue << attributes;
0268   _updateType << attributes;
0269 }
0270 
0271 
0272 void AsciiSourceConfig::load(const QDomElement& e) {
0273   QDomNode n = e.firstChild();
0274   while (!n.isNull()) {
0275     QDomElement elem = n.toElement();
0276     if (!elem.isNull()) {
0277       if (elem.tagName() == "properties") {
0278         _fileNamePattern << elem;
0279         _indexVector << elem;
0280         _delimiters << elem;
0281         _indexInterpretation << elem;
0282         _columnType << elem;
0283         _columnDelimiter << elem;
0284         _columnWidth << elem;
0285         _dataLine << elem;
0286         _readFields << elem;
0287         _useDot << elem;
0288         _fieldsLine << elem;
0289         _columnWidthIsConst << elem;
0290         _readUnits << elem;
0291         _unitsLine << elem;
0292         _limitFileBuffer << elem;
0293         _limitFileBufferSize << elem;
0294         _useThreads << elem;
0295         _timeAsciiFormatString << elem;
0296         _dataRate << elem;
0297         _offsetDateTime << elem;
0298         _offsetFileDate << elem;
0299         _offsetRelative << elem;
0300         _dateTimeOffset << elem;
0301         _relativeOffset << elem;
0302         _nanValue << elem;
0303         _updateType << elem;
0304       }
0305     }
0306     n = n.nextSibling();
0307   }
0308 }
0309 
0310 
0311 bool AsciiSourceConfig::operator==(const AsciiSourceConfig& rhs) const
0312 {
0313   return _fileNamePattern == rhs._fileNamePattern &&
0314       _indexVector == rhs._indexVector &&
0315       _delimiters == rhs._delimiters &&
0316       _indexInterpretation == rhs._indexInterpretation &&
0317       _columnType == rhs._columnType &&
0318       _columnDelimiter == rhs._columnDelimiter &&
0319       _columnWidth == rhs._columnWidth &&
0320       _dataLine == rhs._dataLine &&
0321       _readFields == rhs._readFields &&
0322       _useDot == rhs._useDot &&
0323       _fieldsLine == rhs._fieldsLine &&
0324       _columnWidthIsConst == rhs._columnWidthIsConst &&
0325       _readUnits == rhs._readUnits &&
0326       _unitsLine == rhs._unitsLine &&
0327       _limitFileBuffer == rhs._limitFileBuffer &&
0328       _limitFileBufferSize == rhs._limitFileBufferSize &&
0329       _useThreads == rhs._useThreads &&
0330       _timeAsciiFormatString == rhs._timeAsciiFormatString &&
0331       _dataRate == rhs._dataRate &&
0332       _offsetDateTime == rhs._offsetDateTime &&
0333       _offsetFileDate == rhs._offsetFileDate &&
0334       _offsetRelative == rhs._offsetRelative &&
0335       _dateTimeOffset == rhs._dateTimeOffset &&
0336       _relativeOffset == rhs._relativeOffset &&
0337       _nanValue == rhs._nanValue &&
0338       _updateType == rhs._updateType;
0339 
0340 }
0341 
0342 bool AsciiSourceConfig::operator!=(const AsciiSourceConfig& rhs) const
0343 {
0344   return !operator==(rhs);
0345 }
0346 
0347 bool AsciiSourceConfig::isUpdateNecessary(const AsciiSourceConfig& rhs) const
0348 {
0349   return _fileNamePattern != rhs._fileNamePattern ||
0350       _indexVector != rhs._indexVector ||
0351       _delimiters != rhs._delimiters ||
0352       _indexInterpretation != rhs._indexInterpretation ||
0353       _columnType != rhs._columnType ||
0354       _columnDelimiter != rhs._columnDelimiter ||
0355       _columnWidth != rhs._columnWidth ||
0356       _dataLine != rhs._dataLine ||
0357       _readFields != rhs._readFields ||
0358       _useDot != rhs._useDot ||
0359       _fieldsLine != rhs._fieldsLine ||
0360       _columnWidthIsConst != rhs._columnWidthIsConst ||
0361       _readUnits != rhs._readUnits ||
0362       _unitsLine != rhs._unitsLine ||
0363       _timeAsciiFormatString != rhs._timeAsciiFormatString ||
0364       _dataRate != rhs._dataRate ||
0365       _offsetDateTime != rhs._offsetDateTime ||
0366       _offsetFileDate != rhs._offsetFileDate ||
0367       _offsetRelative != rhs._offsetRelative ||
0368       _dateTimeOffset != rhs._dateTimeOffset ||
0369       _relativeOffset != rhs._relativeOffset ||
0370       _nanValue != rhs._nanValue;
0371 }
0372 
0373 
0374