All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines
Public Types | Public Member Functions | Static Public Attributes | Protected Member Functions
GeopsyCore::AsciiSignalFormat Class Reference

Brief description of class still missing. More...

#include <AsciiSignalFormat.h>

Inheritance diagram for GeopsyCore::AsciiSignalFormat:
GeopsyCore::AbstractFileFormat QGpCoreTools::XMLClass QGpCoreTools::SharedObject

List of all members.

Public Types

enum  HeaderType { NoHeader, FixedHeader, HeaderPattern, EndHeaderPattern }

Public Member Functions

 AsciiSignalFormat ()
 AsciiSignalFormat (const AsciiSignalFormat &o)
void assign (Signal *sig)
virtual int complexity () const
const AsciiSignalFormatComponentcomponent (int index) const
AsciiSignalFormatComponentcomponent (int index)
int componentCount () const
virtual SignalFileFormat::Format format () const
QString header (QTextStream &s, int &lineNumber, QAtomicInt *terminate=0)
int headerLineCount () const
const QString & headerPattern () const
HeaderType headerType () const
QString headerTypeString () const
void insertComponent (int index, const AsciiSignalFormatComponent &c)
void insertRule (int index, const AsciiSignalFormatRule &r)
virtual bool isValid (const QString &fileName) const
void operator= (const AsciiSignalFormat &o)
bool parseHeader (const QString &headerText, QAtomicInt *terminate=0)
void removeComponent (int index)
void removeRule (int index)
const AsciiSignalFormatRulerule (int index) const
AsciiSignalFormatRulerule (int index)
int ruleCount () const
void setHeaderLineCount (int h)
void setHeaderPattern (const QString &h)
void setHeaderType (HeaderType t)
void setHeaderType (const QString &t)
void setTimeFormat (const QString &t)
double t0 () const
const QString & timeFormat () const
QDateTime timeReference () const
virtual const QString & xml_tagName () const

Static Public Attributes

static const QString xmlAsciiSignalFormatTag = "AsciiSignalFormat"

Protected Member Functions

virtual XMLMember xml_member (XML_MEMBER_ARGS)
virtual bool xml_setProperty (XML_SETPROPERTY_ARGS)
virtual void xml_writeChildren (XML_WRITECHILDREN_ARGS) const
virtual void xml_writeProperties (XML_WRITEPROPERTIES_ARGS) const

Detailed Description

Brief description of class still missing.

Description of plain ascii parsing: how to determine header size, how to get information from header. Patterns are all regular expressions. Patterns related to data fields must enclose interesting information between '(' and ')' and specify the index.


Member Enumeration Documentation

Enumerator:
NoHeader 
FixedHeader 
HeaderPattern 
EndHeaderPattern 

Constructor & Destructor Documentation

Constructs a basic ascii format without header

References NoHeader.

{
  _headerType=NoHeader;
  _headerLineCount=0;
  _headerPattern="#";
  _timeFormat="yyyy-MM-dd hh:mm:ss";
}

Description of constructor still missing

References TRACE.

    : AbstractFileFormat(o), _rules(o._rules)
{
  TRACE;
  _headerType=o._headerType;
  _headerLineCount=o._headerLineCount;
  _headerPattern=o._headerPattern;
  _timeFormat=o._timeFormat;
  _components=o._components;
  _rules=o._rules;
}

Member Function Documentation

Sets header information to signal sig.

References GeopsyCore::AsciiSignalFormatRule::channel(), GeopsyCore::MetaDataFactory::Component, GeopsyCore::AsciiSignalFormatRule::data(), QGpCoreTools::endl(), QGpCoreTools::exp(), GeopsyCore::AsciiSignalFormatRule::factor(), GeopsyCore::MetaDataIndex::id(), GeopsyCore::MetaDataFactory::instance(), GeopsyCore::AbstractFileFormat::name(), GeopsyCore::Signal::numberInFile(), GeopsyCore::Signal::setComponent(), GeopsyCore::Signal::setHeader(), GeopsyCore::MetaDataFactory::T0, GeopsyCore::MetaDataFactory::TimeReference, QGpCoreTools::tr(), TRACE, and GeopsyCore::AsciiSignalFormatRule::value().

{
  TRACE;
  for(QVector<AsciiSignalFormatRule>::const_iterator it=_rules.begin();it!=_rules.end();it++) {
    const AsciiSignalFormatRule& r=*it;
    if((r.channel()==-1 || sig->numberInFile()==r.channel()) && r.value().isValid()) {
      switch(r.data().id()) {
      case MetaDataFactory::Component: {
          for(QVector<AsciiSignalFormatComponent>::const_iterator it=_components.begin();it!=_components.end();it++) {
            QRegExp exp(it->pattern());
            if(exp.exactMatch(r.value().toString())) {
              sig->setComponent(it->component());
            }
          }
        }
        break;
      case MetaDataFactory::T0: // Does nothing here get them directly with t0() and timeReference()
      case MetaDataFactory::TimeReference:
        break;
      default: {
          bool ret;
          if(r.value().convert(QVariant::Double)) {
            ret=sig->setHeader(r.data(), r.value().toDouble()*r.factor());
          } else {
            ret=sig->setHeader(r.data(), r.value());
          }
          if(!ret) {
            App::stream() << tr("Cannot set '%1' from ascii file header")
                .arg(MetaDataFactory::instance()->name(r.data())) << endl;
          }
        }
        break;
      }
    }
  }
}

Count the number of mandatory rules. This is used to determine format from header content. The formats with more rules (more complex) are first checked to avoid less restrictive formats to be selected instead.

Reimplemented from GeopsyCore::AbstractFileFormat.

References TRACE.

{
  TRACE;
  int n=0;
  for(QVector<AsciiSignalFormatRule>::const_iterator it=_rules.begin();it!=_rules.end();it++) {
    if(it->mandatory()) {
      n++;
    }
  }
  return n;
}
{return _components[index];}

Referenced by GeopsyGui::AsciiSignalComponentItem::rowCount().

{return _components.count();}
QString GeopsyCore::AsciiSignalFormat::header ( QTextStream &  s,
int &  lineNumber,
QAtomicInt *  terminate = 0 
)

Returns the full header and stream (s) pos is set just after the end of header. Optionaly terminate is a flag that can be turned to true to stop the parser.

References EndHeaderPattern, QGpCoreTools::endl(), FixedHeader, HeaderPattern, NoHeader, QGpCoreTools::tr(), and TRACE.

Referenced by isValid().

{
  TRACE;
  QString headerText;
  // Identify the whole header before extracting information
  switch(_headerType) {
  case NoHeader:
    return QString::null;
  case FixedHeader:
    while(!s.atEnd() && lineNumber<_headerLineCount) {
      lineNumber++;
      headerText+=s.readLine()+"\n";
      if(terminate && terminate->testAndSetOrdered(true, true)) {
        return QString::null;
      }
    }
    if(lineNumber<_headerLineCount) {
      App::stream() << tr("File contains %1 lines while header must be %2 lines long").arg(lineNumber).arg(_headerLineCount) << endl;
      return QString::null;
    }
    break;
  case HeaderPattern: {
      QRegExp rexp(_headerPattern);
      qint64 offset=s.pos();
      QString line=s.readLine();
      while(!s.atEnd() && rexp.indexIn(line)>-1) {
        headerText+=line+"\n";
        lineNumber++;
        offset=s.pos();
        line=s.readLine();
        if(terminate && terminate->testAndSetOrdered(true, true)) {
          return QString::null;
        }
      }
      s.seek(offset); // back to last line which matched regexp
    }
    break;
  case EndHeaderPattern: { // Line just after match contains data
      QRegExp rexp(_headerPattern);
      QString line=s.readLine();
      lineNumber++;
      while(!s.atEnd() && rexp.indexIn(line)==-1) {
        headerText+=line+"\n";
        line=s.readLine();
        lineNumber++;
        if(terminate && terminate->testAndSetOrdered(true, true)) {
          return QString::null;
        }
      }
      // Check if next lines match EndHeaderPattern
      qint64 offset=s.pos();
      line=s.readLine();
      while(!s.atEnd() && rexp.indexIn(line)>-1) {
        lineNumber++;
        offset=s.pos();
        line=s.readLine();
        if(terminate && terminate->testAndSetOrdered(true, true)) {
          return QString::null;
        }
      }
      s.seek(offset); // back to last line which matched regexp
    }
    break;
  }
  return headerText;
}

Referenced by GeopsyGui::AsciiSignalFormatEditor::setFormat().

{return _headerLineCount;}
const QString& GeopsyCore::AsciiSignalFormat::headerPattern ( ) const [inline]

Referenced by GeopsyGui::AsciiSignalFormatEditor::setFormat().

{return _headerPattern;}

Referenced by GeopsyGui::AsciiSignalFormatEditor::setFormat().

{return _headerType;}

References EndHeaderPattern, FixedHeader, HeaderPattern, NoHeader, and TRACE.

Referenced by xml_writeProperties().

{
  TRACE;
  switch(_headerType) {
  case NoHeader:
    break;
  case FixedHeader:
    return "FixedHeader";
  case HeaderPattern:
    return "HeaderPattern";
  case EndHeaderPattern:
    return "EndHeaderPattern";
  }
  return "NoHeader";
}
void GeopsyCore::AsciiSignalFormat::insertComponent ( int  index,
const AsciiSignalFormatComponent c 
) [inline]

Referenced by GeopsyGui::AsciiSignalComponentItem::insertRows().

{_components.insert(index, c);}
void GeopsyCore::AsciiSignalFormat::insertRule ( int  index,
const AsciiSignalFormatRule r 
) [inline]

Referenced by GeopsyGui::AsciiSignalRulesItem::insertRows(), and TitanProcess::TitanProcess().

{_rules.insert(index, r);}
bool GeopsyCore::AsciiSignalFormat::isValid ( const QString &  fileName) const [virtual]

Returns true if the file fileName contains a header that can be correctly parsed.

Reimplemented from GeopsyCore::AbstractFileFormat.

References header(), parseHeader(), and TRACE.

{
  TRACE;
  QFile f(fileName);
  if(!f.open(QIODevice::ReadOnly)) {
    return false;
  }
  QTextStream s(&f);
  int lineNumber=0;
  AsciiSignalFormat fileFormat(*this);
  QString h=fileFormat.header(s, lineNumber);
  return !h.isEmpty() && fileFormat.parseHeader(h);
}
void GeopsyCore::AsciiSignalFormat::operator= ( const AsciiSignalFormat o)

References TRACE.

{
  TRACE;
  AbstractFileFormat::operator=(o);
  _headerType=o._headerType;
  _headerLineCount=o._headerLineCount;
  _headerPattern=o._headerPattern;
  _timeFormat=o._timeFormat;
  _components=o._components;
  _rules=o._rules;
}
bool GeopsyCore::AsciiSignalFormat::parseHeader ( const QString &  headerText,
QAtomicInt *  terminate = 0 
)

Optionaly terminate is a flag that can be turned to true to stop the parser.

References GeopsyCore::AsciiSignalFormatRule::constant(), GeopsyCore::AsciiSignalFormatRule::data(), QGpCoreTools::endl(), GeopsyCore::MetaDataFactory::instance(), GeopsyCore::AsciiSignalFormatRule::mandatory(), GeopsyCore::AbstractFileFormat::name(), GeopsyCore::AsciiSignalFormatRule::pattern(), GeopsyCore::AsciiSignalFormatRule::patternIndex(), GeopsyCore::AsciiSignalFormatRule::setValue(), QGpCoreTools::tr(), and TRACE.

Referenced by isValid(), and GeopsyGui::AsciiSignalRulesItem::setHeader().

{
  TRACE;
  //App::stream() << headerText << endl;
  for(QVector<AsciiSignalFormatRule>::iterator it=_rules.begin();it!=_rules.end();it++) {
    AsciiSignalFormatRule& r=*it;
    if(!r.constant()) {
      QRegExp rexp(r.pattern());
      if(rexp.indexIn(headerText)>-1) {
        r.setValue(rexp.cap(r.patternIndex()));
      } else if(r.mandatory()) {
        App::stream() << tr("Cannot identify '%1' in ascii file header from regular expression '%2'")
            .arg(MetaDataFactory::instance()->name(r.data()))
                    .arg(r.pattern()) << endl;
        r.setValue(QVariant());
        return false;
      }
    }
    if(terminate && terminate->testAndSetOrdered(true, true)) {
      return false;
    }
  }
  return true;
}
void GeopsyCore::AsciiSignalFormat::removeComponent ( int  index) [inline]

Referenced by GeopsyGui::AsciiSignalComponentItem::removeRows().

{_components.remove(index);}
void GeopsyCore::AsciiSignalFormat::removeRule ( int  index) [inline]

Referenced by GeopsyGui::AsciiSignalRulesItem::removeRows().

{_rules.remove(index);}
const AsciiSignalFormatRule& GeopsyCore::AsciiSignalFormat::rule ( int  index) const [inline]
{return _rules[index];}

Referenced by GeopsyGui::AsciiSignalRulesItem::rowCount().

{return _rules.count();}
{_headerLineCount=h;}
void GeopsyCore::AsciiSignalFormat::setHeaderPattern ( const QString &  h) [inline]

Referenced by TitanProcess::TitanProcess().

{_headerPattern=h;}

Referenced by TitanProcess::TitanProcess(), and xml_setProperty().

{_headerType=t;}
void GeopsyCore::AsciiSignalFormat::setHeaderType ( const QString &  t)

References EndHeaderPattern, FixedHeader, HeaderPattern, NoHeader, and TRACE.

{
  TRACE;
  if(t.isEmpty()) return;
  switch(t[0].unicode()) {
  case 'N':
    _headerType=NoHeader;
    break;
  case 'F':
    _headerType=FixedHeader;
    break;
  case 'H':
    _headerType=HeaderPattern;
    break;
  case 'E':
    _headerType=EndHeaderPattern;
    break;
  default:
    break;
  }
}
void GeopsyCore::AsciiSignalFormat::setTimeFormat ( const QString &  t) [inline]

Referenced by TitanProcess::TitanProcess().

{_timeFormat=t;}

References GeopsyCore::AsciiSignalFormatRule::data(), GeopsyCore::AsciiSignalFormatRule::factor(), GeopsyCore::MetaDataIndex::id(), GeopsyCore::MetaDataFactory::T0, TRACE, and GeopsyCore::AsciiSignalFormatRule::value().

{
  TRACE;
  for(int i=_rules.count()-1; i>=0; i--) {
    const AsciiSignalFormatRule& r=_rules.at(i);
    if(r.data().id()==MetaDataFactory::T0) {
      //App::stream() << "t0 " << r.value().toDouble() << endl;
      return r.value().toDouble()*r.factor();
    }
  }
  return 0.0;
}
const QString& GeopsyCore::AsciiSignalFormat::timeFormat ( ) const [inline]

Referenced by GeopsyGui::AsciiSignalFormatEditor::setFormat().

{return _timeFormat;}

References GeopsyCore::AsciiSignalFormatRule::data(), GeopsyCore::MetaDataIndex::id(), GeopsyCore::MetaDataFactory::TimeReference, TRACE, and GeopsyCore::AsciiSignalFormatRule::value().

{
  TRACE;
  for(int i=_rules.count()-1; i>=0; i--) {
    const AsciiSignalFormatRule& r=_rules.at(i);
    if(r.data().id()==MetaDataFactory::TimeReference) {
      //App::stream() << "tref " << r.value().toString() << endl;
      return QDateTime::fromString(r.value().toString(), _timeFormat);
    }
  }
  return QDateTime();
}

Re-implement this function to offer XML restore (children and properties) support to your class.

From tag and map (with contains the attibute value) return a unique identifier under the format of a XMLMember. XMLMember is initialized with 3 types of contructors:

  • An integer: id number of a property
  • A XMLClass * : a child of this object identified by tag
  • Default constructor: error, unknow child or property

Map of attributes can be inspected in this way (can be achived also in xml_setProperty()):

    static const QString tmp("childrenName");
    XMLRestoreAttributeIterator it=map.find(tmp);
    if(it!=map.end()) {
      // found attribute "childrenName"
    }

If the map of attributes is not used:

    Q_UNUSED(attributes);
    if(tag=="x1") return XMLMember(0);
    else if(tag=="y1") return XMLMember(1);
    else if(tag=="x2") return XMLMember(2);
    else if(tag=="y2") return XMLMember(3);
    else return XMLMember(XMLMember::Unknown);

Arithmetic operations + and - apply to XMLMember to avoid confusion of property id numbers between inherited objects. Offset 3 corresponds to the number of properties defined in this object.

    if(tag=="anInteger") return XMLMember(0);
    else if(tag=="aString") return XMLMember(1);
    else if(tag=="aDouble") return XMLMember(2);
    return AbstractLine::xml_member(tag, attributes, context)+3;

For the arguments of this function use Macro XML_MEMBER_ARGS.

Reimplemented from GeopsyCore::AbstractFileFormat.

References TRACE, GeopsyCore::AsciiSignalFormatComponent::xmlComponentTag, and GeopsyCore::AsciiSignalFormatRule::xmlRuleTag.

{
  TRACE;
  if(tag==AsciiSignalFormatRule::xmlRuleTag) {
    _rules.append(AsciiSignalFormatRule());
    return XMLMember(&_rules.last());
  } else if(tag==AsciiSignalFormatComponent::xmlComponentTag) {
    _components.append(AsciiSignalFormatComponent());
    return XMLMember(&_components.last());
  }  else if(tag=="headerType") return XMLMember(0);
  else if(tag=="headerPattern") return XMLMember(1);
  else if(tag=="headerLineCount") return XMLMember(2);
  else if(tag=="timeFormat") return XMLMember(3);
  else return AbstractFileFormat::xml_member(tag, attributes, context)+4;
}

Re-implement this function to offer XML restore properties support to your class.

From memberID set the corresponding property with value content. The map of attributes is given as a supplementary information (not useful in all cases).

For a general case:

  Q_UNUSED(attributes);
  double val=content.toDouble();
  switch (memberID) {
  case 0:
    _x1=val;
    return true;
  case 1:
    _y1=val;
    return true;
  case 2:
    _x2=val;
    return true;
  case 3:
    _y2=val;
    return true;
  default:
    return false;
  }

For classes inheriting other classes (see also xml_member())

  switch (memberID) {
  case 0:
    _anInteger=content.toString();
    return true;
  case 1:
    _aString=content.toInt();
    return true;
  case 2:
    _aDouble=content.toDouble();
    return true;
  default:
    return AbstractLine::xml_setProperty(memberID-3, map, content);

For the arguments of this function use Macro XML_SETPROPERTY_ARGS.

Reimplemented from GeopsyCore::AbstractFileFormat.

References setHeaderType(), and TRACE.

{
  TRACE;
  switch (memberID) {
  case 0: setHeaderType(content.toString()); return true;
  case 1: _headerPattern=content.toString(); return true;
  case 2: _headerLineCount=content.toInt(); return true;
  case 3: _timeFormat=content.toString(); return true;
  default:
    break;
  }
  return AbstractFileFormat::xml_setProperty(memberID-4, tag, attributes, content, context);
}
virtual const QString& GeopsyCore::AsciiSignalFormat::xml_tagName ( ) const [inline, virtual]

Reimplemented from QGpCoreTools::XMLClass.

References TRACE.

{
  TRACE;
  for(QVector<AsciiSignalFormatComponent>::const_iterator it=_components.begin();it!=_components.end();it++) {
    it->xml_save(s, context);
  }
  for(QVector<AsciiSignalFormatRule>::const_iterator it=_rules.begin();it!=_rules.end();it++) {
    it->xml_save(s, context);
  }
}

Reimplemented from GeopsyCore::AbstractFileFormat.

References headerTypeString(), TRACE, and QGpCoreTools::XMLClass::writeProperty().

{
  TRACE;
  AbstractFileFormat::xml_writeProperties(s, context);
  writeProperty(s, "headerType", headerTypeString());
  writeProperty(s, "headerPattern", _headerPattern);
  writeProperty(s, "headerLineCount", _headerLineCount);
  writeProperty(s, "timeFormat", _timeFormat);
}

Member Data Documentation

const QString GeopsyCore::AsciiSignalFormat::xmlAsciiSignalFormatTag = "AsciiSignalFormat" [static]

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines