Public Member Functions | Protected Member Functions
TimeReader Class Reference

Brief description of class still missing. More...

#include <TimeReader.h>

Inheritance diagram for TimeReader:
QGpCoreTools::ArgumentStdinReader

List of all members.

Public Member Functions

bool setOptions (int &argc, char **argv)
 TimeReader ()

Protected Member Functions

virtual bool parse (QTextStream &s)

Detailed Description

Brief description of class still missing.

Full description of class still missing


Constructor & Destructor Documentation

Description of constructor still missing

References TRACE.

{
  TRACE;
  _action=Convert;
  _mode=Absolute;
  _column=-1;
  _delimiters=StringSection::defaultSeparators;
  _startByte=0;
  _endByte=0;
  _outputPrecision=6;
}

Member Function Documentation

bool TimeReader::parse ( QTextStream &  s) [protected, virtual]

Implements QGpCoreTools::ArgumentStdinReader.

References QGpCoreTools::endl(), QGpCoreTools::LineParser::endsAt(), QGpCoreTools::LineParser::setDelimiters(), sOut(), QGpCoreTools::LineParser::startsAt(), str, QGpCoreTools::LineParser::toString(), QGpCoreTools::tr(), and TRACE.

{
  TRACE;
  QTextStream sOut(stdout);
  QString buf;
  while(!s.atEnd()) {
    buf=s.readLine();
    if(!buf.isEmpty() && buf[0]!='#') {
      switch(_action) {
      case Convert: {
          bool ok=true;
          QString textBefore, textAfter;
          if(_column>-1) {
            LineParser p(buf);
            p.setDelimiters(_delimiters);
            textAfter=buf.mid(p.endsAt(_column, ok)+1);
            textBefore=buf.left(p.startsAt(_column, ok));
            buf=p.toString(_column, ok);
            if(!ok) {
              App::stream() << tr("gptime: error parsing column %1").arg(_column) << endl;
              return false;
            }
          } else {
            if(_endByte>0 && _endByte<buf.length()) {
              textAfter=buf.mid(_endByte+1);
              buf=buf.left(_endByte+1);
            }
            if(_startByte>0 && _startByte<buf.length()) {
              textBefore=buf.left(_startByte);
              buf=buf.mid(_startByte);
            }
          }
          buf=buf.trimmed();
          if(_verbose) {
            sOut << "# converting \"" << buf << "\"" << endl;
          }
          QString str;
          switch(_mode) {
          case Absolute: {
              DateTime dt=inDateTime(buf, ok);
              if(!ok) {
                App::stream() << tr("gptime: error parsing input: '%1'").arg(buf) << endl;
                return false;
              }
              str=outDateTime(dt);
            }
            break;
          case Relative: {
              double t=Number::timeToSeconds(buf);
              str=Number::secondsToTime(t, Number::type(QChar(_outFormat[0]).toAscii()), _outputPrecision);
            }
            break;
          }
          if(!str.isNull()) {
            sOut << textBefore << str << textAfter << endl;
          }
        }
        break;
      case None:
        App::stream() << tr("gptime: no action defined, see -h") << endl;
        return false;
      }
    }
  }
  return true;
}
bool TimeReader::setOptions ( int &  argc,
char **  argv 
)

References QGpCoreTools::endl(), QGpCoreTools::tr(), and TRACE.

Referenced by main().

{
  TRACE;
  int i, j=1;
  for(i=1; i<argc; i++) {
    QByteArray arg=argv[i];
    if(arg[0]=='-') {
      if(arg=="-convert") {
        _action=Convert;
      } else if(arg=="-relative") {
        _mode=Relative;
      } else if(arg=="-i" || arg=="-in-format") {
        CoreApplication::checkOptionArg(i, argc, argv);
        _inFormat=argv[i];
      } else if(arg=="-o" || arg=="-out-format") {
        CoreApplication::checkOptionArg(i, argc, argv);
        _outFormat=argv[i];
      } else if(arg=="-p") {
        CoreApplication::checkOptionArg(i, argc, argv);
        _outputPrecision=atoi(argv[i]);
      } else if(arg=="-c") {
        CoreApplication::checkOptionArg(i, argc, argv);
        _column=atoi(argv[i])-1;
      } else if(arg=="-d") {
        CoreApplication::checkOptionArg(i, argc, argv);
        _delimiters=argv[i];
      } else if(arg=="-s") {
        CoreApplication::checkOptionArg(i, argc, argv);
        _startByte=atoi(argv[i]);
      } else if(arg=="-e") {
        CoreApplication::checkOptionArg(i, argc, argv);
        _endByte=atoi(argv[i]);
      } else if(arg=="-verbose") {
        _verbose=true;
      } else {
        App::stream() << tr("gptime: bad option %1, see -help").arg(argv[i]) << endl;
        return false;
      }
    } else {
      argv[j++]=argv[i];
    }
  }
  if(j < argc) {
    argv[j]=0;
    argc=j;
  }
  switch(_mode) {
  case Absolute:
    if(_inFormat.isEmpty()) {
      _inFormat="time_t";
    }
    if(_outFormat.isEmpty()) {
      _outFormat="yyyy-MM-dd hh:mm:ssz";
    }
    break;
  case Relative:
    if(!_inFormat.isEmpty()) {
      App::stream() << tr("gptime: for relative mode, input format is automatically recognized, '-i' is not valid.") << endl;
      return false;
    }
    if(_outFormat.isEmpty()) {
      _outFormat="w";
    } else {
      if(_outFormat.count()>1) {
        App::stream() << tr("gptime: not a valid format in relative mode (%1).").arg(_outFormat) << endl;
        return false;
      }
    }
    break;
  }
  return true;
}

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