Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tdoubleparamfile.h"
Toshihiro Shimizu 890ddd
#include "tfilepath_io.h"
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool parseDouble(double &value, char *&s) {
Shinya Kitaoka 120a6e
  if (!(*s == '-' || *s == '.' || '0' <= *s && *s <= '9')) return false;
Shinya Kitaoka 120a6e
  char *t = s;
Shinya Kitaoka 120a6e
  if (*s == '-') s++;
Shinya Kitaoka 120a6e
  while ('0' <= *s && *s <= '9') s++;
Shinya Kitaoka 120a6e
  if (*s == '.') {
Shinya Kitaoka 120a6e
    s++;
Shinya Kitaoka 120a6e
    while ('0' <= *s && *s <= '9') s++;
Shinya Kitaoka 120a6e
    if (*s == 'e' || *s == 'E') {
Shinya Kitaoka 120a6e
      s++;
Shinya Kitaoka 120a6e
      if (*s == '+' || *s == '-') s++;
Shinya Kitaoka 120a6e
      while ('0' <= *s && *s <= '9') s++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  std::string w(t, s - t);
Shinya Kitaoka 120a6e
  value = std::stod(w);
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int split(std::vector<double> &values, char *s) {</double>
Shinya Kitaoka 120a6e
  int count = 0;
Shinya Kitaoka 120a6e
  for (;;) {
Shinya Kitaoka 120a6e
    while (*s == ' ' || *s == '\t') s++;
Shinya Kitaoka 120a6e
    if (*s == '\0') break;
Shinya Kitaoka 120a6e
    double value = 0;
Shinya Kitaoka 120a6e
    bool found   = parseDouble(value, s);
Shinya Kitaoka 120a6e
    if (found) {
Shinya Kitaoka 120a6e
      count++;
Shinya Kitaoka 120a6e
      values.push_back(value);
Shinya Kitaoka 120a6e
      while (*s == ' ' || *s == '\t') s++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    if (*s == ',' || *s == ';') {
Shinya Kitaoka 120a6e
      if (!found) {
Shinya Kitaoka 120a6e
        count++;
Shinya Kitaoka 120a6e
        values.push_back(0);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      s++;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      if (!found) break;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return count;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TDoubleParamFileData::TDoubleParamFileData() : m_params(), m_dirtyFlag(false) {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TDoubleParamFileData::~TDoubleParamFileData() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TDoubleParamFileData::setParams(
Shinya Kitaoka 120a6e
    const TDoubleKeyframe::FileParams ¶ms) {
Shinya Kitaoka 120a6e
  m_params = params;
Shinya Kitaoka 120a6e
  invalidate();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TDoubleParamFileData::invalidate() {
Shinya Kitaoka 120a6e
  m_dirtyFlag = true;
Shinya Kitaoka 120a6e
  m_values.clear();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
double TDoubleParamFileData::getValue(double frame, double defaultValue) {
Shinya Kitaoka 120a6e
  if (frame < 0) return defaultValue;
Shinya Kitaoka 120a6e
  int intFrame = (int)frame;
Shinya Kitaoka 120a6e
  if (m_dirtyFlag) read();
Shinya Kitaoka 120a6e
  if (intFrame >= (int)m_values.size())
Shinya Kitaoka 120a6e
    return defaultValue;
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return m_values[intFrame];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void TDoubleParamFileData::read() {
Shinya Kitaoka 120a6e
  m_dirtyFlag = false;
Shinya Kitaoka 120a6e
  m_values.clear();
Shinya Kitaoka 120a6e
  int k = m_params.m_fieldIndex;
Shinya Kitaoka 120a6e
  if (k < 0) return;
Shinya Kitaoka 120a6e
  TFilePath fp = m_params.m_path;
Shinya Kitaoka 120a6e
  Tifstream is(fp);
Shinya Kitaoka 120a6e
  char buffer[2048];
Shinya Kitaoka 120a6e
  memset(buffer, 0, sizeof buffer);
Shinya Kitaoka 120a6e
  while (is) {
Shinya Kitaoka 120a6e
    is.getline(buffer, sizeof(buffer) - 1);
Shinya Kitaoka 120a6e
    std::vector<double> fields;</double>
Shinya Kitaoka 120a6e
    QString line(buffer);
Shinya Kitaoka 120a6e
    if (line.size() == 0 || line.startsWith("#")) continue;
Shinya Kitaoka 120a6e
    split(fields, buffer);
Shinya Kitaoka 120a6e
    double value                      = 0;
Shinya Kitaoka 120a6e
    if (k < (int)fields.size()) value = fields[k];
Shinya Kitaoka 120a6e
    m_values.push_back(value);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------