/* Blinker
   Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org>
   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
   a blinkenarea.org project */

#include <sstream>
#include <string>

#include "Format.h"
#include "StringParser.h"

namespace Blinker {

/// constructor
Format::Format():
  m_width(1),
  m_height(1),
  m_channels(1),
  m_maxval(1)
{
}

/**
 * @brief parse from string format
 * @param[in] str string format
 * @return if parsing was successful
 */
bool Format::fromStr(const std::string &str)
{
  StringParser parser(str);
  unsigned int width, height, channels, maxval1;

  if (!parser.uintMin(1, width) ||
      !parser.fixChr('x') ||
      !parser.uintMin(1, height) ||
      !parser.fixChr('-') ||
      !parser.uintMin(1, channels) ||
      !parser.fixChr('/') ||
      !parser.uintMin(2, maxval1) ||
      !parser.isDone())
    return false;
  m_width = width;
  m_height = height;
  m_channels = channels;
  m_maxval = maxval1 - 1;
  return true;
}

/**
 * @brief convert to string format
 * @return string format
 */
std::string Format::toStr() const
{
  std::stringstream strm;
  strm << m_width << "x" << m_height << "-"
       << m_channels << "/" << (m_maxval + 1);
  return strm.str();
}

} // namespace Blinker

