/*
 * (c) 2005 Research Group Dr. Fuhrmann, University of Karlsruhe, Germany
 * All rights reserved.
 *
 * Author:
 *         Roman Krenicky
 *
 * $Id: epgtypes.cpp,v 1.8 2005/11/21 09:39:01 kutzner Exp $
 *
 */

/*
  epgtypes.cpp
  created: 12.2.2005
  last change: 18.6.2005
*/
/*! @file
    plugin-internal EPG-related types and functions
*/


#include "globalheader.h"

#include "epgtypes.h"




/*! @brief initialises a vdr @c cChannel object corresponding to an
           @c epgchannel object
    @param channel  the channel in its plugin internal representation
    @param vdrchannel  the output (on success)
    @return true on success, false otherwise
*/
bool epgchannel2vdrchannel (const epgchannel &channel, cChannel *vdrchannel)
{
   if (vdrchannel == NULL)
      return false;

   return vdrchannel->Parse (channel.c_str());
} // epgchannel2vdrchannel (...)



/*! @brief initialises an @c epgchannel object corresponding to a @c cChannel
           object
    @param vdrchannel  the channel in its vdr-representation
    @param channel  the output (the channel in its plugin internal
                    representation) (on success)
    @return true on success, false otherwise
*/
bool vdrchannel2epgchannel (const cChannel &vdrchannel, epgchannel *channel)
{
   if (channel == NULL)
      return false;

   cString channel_as_text = vdrchannel.ToText();
   string::size_type pos = 0;

   if ((const char*)channel_as_text == NULL)
      return false;
   *channel = (const char*)channel_as_text;
   // strip trailing newline characters:
   pos = channel->find ('\n');
   if (pos != string::npos)
      channel->resize (pos, 0);
   return true;
} // vdrchannel2epgchannel (...)



/*! @brief maps a channel to its id in the IGOR (for destination calculation)
    @param channel  the channel to calculate the destination for
    @param igor_channel_id  the @a channel's IGOR-id is stored here
    @return true on success, false otherwise
*/
bool epgchannel2igorchannelid (const epgchannel &channel,string *igor_channel_id)
{
   cChannel vdrchannel;
   cString channelID_as_text;
   string::size_type pos = 0;
   unsigned int minus_count = 0;

   if (igor_channel_id == NULL)
      return false;

   // print the channel id of 'chanel':
   if ( !vdrchannel.Parse(channel.c_str()) )
      return false;
   channelID_as_text = vdrchannel.GetChannelID().ToString();
   if ((const char*)channelID_as_text == NULL)
      return false;
   *igor_channel_id = (const char*)channelID_as_text;
   // strip possibly trailing newline characters:
   pos = igor_channel_id->find ('\n');
   if (pos != string::npos)
      igor_channel_id->resize (pos, 0);
   // append a '-0' if it has been omitted by tChannelID::ToString():
      // count the minus characters
   for (pos=0; pos < igor_channel_id->size(); pos++)
      if (igor_channel_id->at(pos) == '-')
         minus_count++;
   if (minus_count == 3)  // '-0' has been omitted
      *igor_channel_id += "-0";
   else if (minus_count != 4)  // fatal, this shouldn't ever happen !!!
      return false;

   return true;
} // epgchannel2igorchannelid (...)
