/*
 * Taken For Telecable From
 * Image plugin to VDR (C++)
 *
 * (C) 2003 Kai Tobias Burwieck <kai@burwieck.net>
 *
 * based on MP3/MPlayer plugin to VDR (C++)
 * (C) 2001,2002 Stefan Huelswitt <huels@iname.com>
 *
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
 */

#include <ctype.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "data.h"

#include <vdr/tools.h>


extern int UrlCount;
extern char *UrlPath[40] ;
extern char *UrlDesc[40] ;


char *AddPath(const char *dir, const char *filename)
{
  int l = strlen(dir);
  char *name = MALLOC(char, l + strlen(filename) + 2);
  if(name)
	{
    strcpy(name, dir);
    name[l] = '/';
    strcpy(name + l + 1, filename);
  } else {
    esyslog("imageplugin: ERROR: no memory for filename");
	}
    return name;
}

// -- cFileSource --------------------------------------------------------------

cFileSource::cFileSource(void)
{
  basedir = description = 0;
  useCount = 0;
}

cFileSource::cFileSource(const char *Basedir, const char *Description)
{
  basedir = description = 0;
  useCount = 0;
  Set(Basedir, Description);
}

cFileSource::~cFileSource()
{
  free(basedir);
  free(description);
}

void cFileSource::Set(const char *Basedir, const char *Description)
{
  free(basedir);
  basedir = strdup(Basedir);
  free(description);
  description = strdup(Description);
}

char *cFileSource::BuildName(const char *filename)
{
  return AddPath(basedir, filename);
}

bool cFileSource::Parse(char *s)
{
  char base[256], des[256];
  int n;
  if((n =
    sscanf(s, "%255[^;];%255[^;]", base, des)) >= 2)
  {
    char *base2 = skipspace(stripspace(base));
    Set(base2, skipspace(stripspace(des)));
    return true;
  }
  return false;
}

// -- cFileSources --------------------------------------------------------------

bool cFileSources::Load(const char *filename, bool dummy)
{
  if(cConfig < cFileSource >::Load(filename, true)) {
    SetSource(First());
    return true;
  }
  return false;
}

cFileSource *cFileSources::FindSource(const char *filename)
{
  cFileSource *src = First();
  while(src) {
    if(startswith(filename, src->BaseDir()))
  		return src;
    src = Next(src);
	}
  return 0;
}
