/*
  data source for libisoburn.

  Copyright 2007 Vreixo Formoso Lopes <metalpain2002@yahoo.es>
*/

#include <assert.h>
#include <stdlib.h>

#include <libburn/libburn.h>
#include <libisofs/libisofs.h>
#include "isoburn.h"

struct disc_data_src {
 struct burn_drive *d;
 int nblocks;
};

static int 
ds_read_block(struct data_source *src, int lba, unsigned char *buffer)
{
 int ret;
 struct disc_data_src *data;
 off_t count;

 assert(src && buffer);

 data = (struct disc_data_src*)src->data;

 /* if (lba >= data->nblocks) 
  *	return BLOCK_OUT_OF_FILE;
  */
	
 ret = burn_read_data(data->d, (off_t) lba * (off_t) 2048, (char *) buffer, 
                      2048, &count, 0);
 if (ret <= 0 )
   return -1;

 return 0;
}
	
static int 
ds_get_size(struct data_source *src)
{
 struct disc_data_src *data;

 assert(src);
	
 data = (struct disc_data_src*)src->data;
 return data->nblocks;
}
	
static void 
ds_free_data(struct data_source *src)
{
 free(src->data);
}

struct data_source *
isoburn_data_source_new(struct burn_drive *d)
{
 struct disc_data_src *data;
 struct data_source *ret;

 assert(d);
	
 data = malloc(sizeof(struct disc_data_src));
 if (!data)
   return NULL;
 data->d = d;
	
 //TODO should be filled with the size of disc (or track?)
 data->nblocks = 0;
	
 ret = malloc(sizeof(struct data_source));
 if (!ret) {
   free(data);
   return NULL;
 }
 ret->refcount = 1;
 ret->read_block = ds_read_block;
 ret->get_size = ds_get_size;
 ret->free_data = ds_free_data;
 ret->data = data;
 return ret;
}

