File indexing completed on 2024-12-22 04:17:22

0001 /***************************************************************************
0002                     stdinsource.cpp  -  data source for stdin
0003                              -------------------
0004     begin                : Fri Oct 31 2003
0005     copyright            : (C) 2003 The University of Toronto
0006     email                : netterfield@astro.utoronto.ca
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #include "kst_i18n.h"
0019 #include "config.h"
0020 #include "stdinsource.h"
0021 
0022 #include <QTemporaryFile>
0023 #include <QXmlStreamWriter>
0024 
0025 #include <stdlib.h>
0026 #include <unistd.h>
0027 
0028 #ifdef TIME_WITH_SYS_TIME
0029 # include <sys/time.h>
0030 # include <time.h>
0031 #else
0032 # ifdef HAVE_SYS_TIME_H
0033 #  include <sys/time.h>
0034 # else
0035 #  include <time.h>
0036 # endif
0037 #endif
0038 
0039 namespace Kst {
0040 
0041 const QString StdinSource::staticTypeString = I18N_NOOP("Stdin Data Source");
0042 
0043 StdinSource::StdinSource(ObjectStore *store, QSettings *cfg)
0044 : DataSource(store, cfg, "stdin", "stdin") {
0045   _file = new QTemporaryFile;
0046   _filename = _file->fileName();
0047   // Unfortunately we have to update here.  stdin is a special case.
0048   update();
0049   _src = DataSource::loadSource(this->store(), _filename, "ASCII");
0050   if (_src && _src->isValid()) {
0051     _valid = true;
0052   }
0053 }
0054 
0055 
0056 StdinSource::~StdinSource() {
0057   _file->close();
0058   delete _file;
0059   _file = 0L;
0060 }
0061 
0062 
0063 const QString& StdinSource::typeString() const {
0064   return staticTypeString;
0065 }
0066 
0067 
0068 Object::UpdateType StdinSource::internalDataSourceUpdate() {
0069   if (!_valid) {
0070     _src = DataSource::loadSource(store(), _filename, "ASCII");
0071     if (_src && _src->isValid()) {
0072       _valid = true;
0073     } else {
0074       return NoChange;
0075     }
0076   }
0077 
0078   fd_set rfds;
0079   struct timeval tv;
0080   int retval;
0081   char instr[4097];
0082   int i = 0;
0083   bool new_data = false;
0084   bool got_some = false;
0085 
0086   int handle = _file->handle();
0087   FILE *fp = fdopen(handle, "w+");
0088 
0089   if (!fp) {
0090     return Object::NO_CHANGE;
0091   }
0092 
0093   do {
0094     /* Watch stdin (fd 0) to see when it has input. */
0095     FD_ZERO(&rfds);
0096     FD_SET(0, &rfds);
0097     /* Wait up to 0 seconds. */
0098     tv.tv_sec = 0;
0099     tv.tv_usec = 0;
0100 
0101     retval = select(1, &rfds, NULL, NULL, &tv);
0102 
0103     new_data = false;
0104     if (retval > 0) {
0105       char *fgs = fgets(instr, 4096, stdin);
0106       if (fgs && fp) {
0107         got_some = true;
0108         fputs(instr, fp);
0109         new_data = true;
0110       }
0111     }
0112   } while (++i < 100000 && new_data);
0113 
0114   fclose(fp);
0115 
0116   if (got_some && _src) {
0117     return _src->update();
0118   }
0119   return NoChange;
0120 }
0121 
0122 
0123 int StdinSource::readField(double *v, const QString& field, int s, int n) {
0124   if (isValid()) {
0125     return _src->readField(v, field, s, n);
0126   }
0127   return -1;
0128 }
0129 
0130 
0131 bool StdinSource::isValidField(const QString& field) const {
0132   if (isValid()) {
0133     return _src->isValidField(field);
0134   }
0135   return false;
0136 }
0137 
0138 
0139 int StdinSource::samplesPerFrame(const QString& field) {
0140   if (isValid()) {
0141     return _src->samplesPerFrame(field);
0142   }
0143   return 0;
0144 }
0145 
0146 
0147 int StdinSource::frameCount(const QString& field) const {
0148   if (isValid()) {
0149     return _src->frameCount(field);
0150   }
0151   return 0;
0152 }
0153 
0154 
0155 QString StdinSource::fileType() const {
0156   if (isValid()) {
0157     return _src->fileType();
0158   }
0159   return QString::null;
0160 }
0161 
0162 
0163 void StdinSource::save(QXmlStreamWriter &s) {
0164   if (isValid()) {
0165     return _src->save(s);
0166   }
0167   DataSource::save(s);
0168 }
0169 
0170 
0171 bool StdinSource::isValid() const {
0172   return _valid && _src && _src->isValid();
0173 }
0174 
0175 
0176 bool StdinSource::isEmpty() const {
0177   return !isValid() || _src->isEmpty();
0178 }
0179 
0180 }
0181 // vim: ts=2 sw=2 et