예제 #1
0
//-------------------------------------------------------------------------------------------
void AsciiDataReader::toDouble(const LexicalCast& lexc, const char* buffer, qint64 bufread, qint64 ch, double* v, int) const
{
  if (   isDigit(buffer[ch])
         || buffer[ch] == '-'
         || buffer[ch] == '.'
         || buffer[ch] == '+'
         || isWhiteSpace(buffer[ch])) {
    *v = lexc.toDouble(&buffer[0] + ch);
  } else if ( ch + 2 < bufread
              && tolower(buffer[ch]) == 'i'
              && tolower(buffer[ch + 1]) == 'n'
              && tolower(buffer[ch + 2]) == 'f') {
    *v = INF;
  }

#if 0
  // TODO enable by option: "Add unparsable lines as strings"
  else {
    if (_rowIndex.size() > row + 1) {
      QString unparsable = QString::fromAscii(&buffer[_rowIndex[row]], _rowIndex[row + 1] - _rowIndex[row]);
      _strings[QString("Unparsable %1").arg(row)] = unparsable.trimmed();
    }
  }
#endif
}
예제 #2
0
//-------------------------------------------------------------------------------------------
int AsciiSource::readField(double *v, const QString& field, int s, int n) 
{
  LexicalCast lexc;
  lexc.setDecimalSeparator(_config._useDot, _config._localSeparator);

  if (n < 0) {
    n = 1; /* n < 0 means read one sample, not frame - irrelevent here */
  }

  if (field == "INDEX") {
    for (int i = 0; i < n; i++) {
      v[i] = double(s + i);
    }
    return n;
  }

  int col = columnOfField(field);
  if (col == -1) {
    return 0;
  }

  int bufstart = _rowIndex[s];
  int bufread = _rowIndex[s + n] - bufstart;
  if (bufread <= 0) {
    return 0;
  }

  QFile file(_filename);
  if (!openValidFile(file)) {
    return 0;
  }
  bufread = readFromFile(file, _tmpBuffer, bufstart, bufread);


#ifdef KST_DONT_CHECK_INDEX_IN_DEBUG
  const char* buffer = _tmpBuffer.constData();
#else
  const QVarLengthArray<char, KST_PREALLOC>& buffer = _tmpBuffer;
#endif


  if (_config._columnType == AsciiSourceConfig::Fixed) {
    for (int i = 0; i < n; ++i, ++s) {
      // Read appropriate column and convert to double
      v[i] = lexc.toDouble(&buffer[0] + _rowIndex[i] - _rowIndex[0] + _config._columnWidth * (col - 1));
    }
  } else if (_config._columnType == AsciiSourceConfig::Custom) {
    const QString delimiters = _config._delimiters.value();
    const QString columnDelimiter = _config._columnDelimiter.value();
    for (int i = 0; i < n; ++i, ++s) {
      bool incol = false;
      int i_col = 0;
      v[i] = Kst::NOPOINT;
      for (int ch = _rowIndex[s] - bufstart; ch < bufread; ++ch) {
        if (columnDelimiter.contains(buffer[ch])) {
          incol = false;
        } else if (buffer[ch] == '\n' || buffer[ch] == '\r') {
          break;
        } else if (delimiters.contains(buffer[ch])) {
          break;
        } else {
          if (!incol) {
            incol = true;
            ++i_col;
            if (i_col == col) {
              if (isdigit((unsigned char)buffer[ch]) || buffer[ch] == '-' || buffer[ch] == '.' || buffer[ch] == '+') {
                v[i] = lexc.toDouble(&buffer[0] + ch);
              } else if (ch + 2 < bufread && tolower(buffer[ch]) == 'i' &&
                  tolower(buffer[ch + 1]) == 'n' && tolower(buffer[ch + 2]) == 'f') {
                v[i] = INF;
              }
              break;
            }
          }
        }
      }
    }
  } else if (_config._columnType == AsciiSourceConfig::Whitespace) {
    const QString delimiters = _config._delimiters.value();
    for (int i = 0; i < n; i++, s++) {
      bool incol = false;
      int i_col = 0;

      v[i] = Kst::NOPOINT;
      for (int ch = _rowIndex[s] - bufstart; ch < bufread; ++ch) {
        if (isspace((unsigned char)buffer[ch])) {
          if (buffer[ch] == '\n' || buffer[ch] == '\r') {
            break;
          } else {
            incol = false;
          }
        } else if (delimiters.contains(buffer[ch])) {
          break;
        } else {
          if (!incol) {
            incol = true;
            ++i_col;
            if (i_col == col) {
              if (isdigit((unsigned char)buffer[ch]) || buffer[ch] == '-' || buffer[ch] == '.' || buffer[ch] == '+') {
                v[i] = lexc.toDouble(&buffer[0] + ch);
              } else if (ch + 2 < bufread && tolower(buffer[ch]) == 'i' &&
                  tolower(buffer[ch + 1]) == 'n' && tolower(buffer[ch + 2]) == 'f') {
                v[i] = INF;
              }
              break;
            }
          }
        }
      }
    }
  } else {
    return 0;
  }

  return n;
}