/* 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 "SyncRecv.h"

namespace Blinker {

/**
 * @brief check if playlist entry name matches sync name
 * @param[in] playlist name in playlist
 * @param[in] sync name from synchronization information
 * @return true if name matches, false otherwise
 */
bool SyncRecv::checkName(const std::string &playlist, const std::string &sync)
{
  std::string::size_type pos, noprefix;
  std::string noext;
  unsigned int underscore;

  // full match
  if (playlist == sync)
    return true;

  // match without file extension
  pos = playlist.rfind('.');
  noext = playlist.substr(0, pos);
  if (noext == sync)
    return true;

  // match without ignore prefix ".*__"
  noprefix = 0;
  underscore = 0;
  for (pos = 0; pos < noext.length(); ++pos) {
    if (noext.at(pos) == '_') {
      underscore++;
      if (underscore >= 2)
        noprefix = pos + 1;
    } else {
      underscore = 0;
    }
  }
  if (noext.substr(noprefix) == sync)
    return true;

  // no match
  return false;
}

/// constructor
SyncRecv::SyncRecv()
{
}

/// virtual destructor
SyncRecv::~SyncRecv()
{
}

} // namespace Blinker

