#ifndef LIBREV__DELTA_H
#define LIBREV__DELTA_H

/*
 * Copyright (c) 2003  Petr Baudis.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials
 *     provided with the distribution.
 *   * Neither the name of the author nor the names of the product's
 *     contributors may be used to endorse or promote products
 *     derived from this software without specific prior written
 *     permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ifdef __cplusplus
extern "C" {
#endif


#include <librev/dataobj.h>
#include <librev/librev.h>


/* This header file contains the dataobjs differences container, a delta ({struct
 * rev_delta}).  This object is used to hold complete description of differences
 * between two given dataobjs (both data and metadata). This is only a basic abstration
 * of a difference object, the main contents is subject to further specialization
 * (text, symlinks, changesets, ...). */


rev_object (delta,
	/* Public attributes */

	/* These may be NULL normally, they are used only during a
	 * diff/apply. Both have to be of a same type, though. */
	/* {rev_delta_get_dataobj1()},{rev_delta_set_dataobj2()} */
	struct rev_dataobj *dataobj1;
	/* {rev_delta_get_dataobj2()},{rev_delta_set_dataobj2()} */
	struct rev_dataobj *dataobj2;


	/* Data: */

	void *data; /* Backend's data. */

	struct rev_delta_dataops {
		/* Make a diff between dataobj1 and dataobj2. */
		/* Returns 1 on success, 0 if not enough data, < 0 on error. */
		int (*diff) (struct rev_delta *obj);
		/* Apply a diff on dataobj1, resulting in dataobj2. */
		/* Returns 1 on success, 0 if not enough data, < 0 on error. */
		int (*apply) (struct rev_delta *obj);
		/* Unapply a diff from dataobj2, resulting in dataobj1. */
		/* Returns 1 on success, 0 if not enough data, < 0 on error. */
		int (*unapply) (struct rev_delta *obj);
		/* Release obj->data. */
		void (*done) (struct rev_delta *obj);
	} *dataops;


	/* Metadata : */

	/* The dataobj's file name. */
	/* {rev_delta_get_filename1()},{rev_delta_set_filename1()} */
	char *filename1;
	/* {rev_delta_get_filename2()},{rev_delta_set_filename2()} */
	char *filename2;

	/* TODO: More metadata. */


	/* User's data (just in case ;) : */

	void *udata; /* User's data. */

	struct rev_delta_userops {
		/* Release obj->udata. */
		void (*done) (struct rev_delta *obj);
	} *userops;
);

/* Constructor */

/* This creates the delta. The relevant data must be filled manually. */
/* Returns the pointer on success, NULL otherwise. */
struct rev_delta *rev_delta_init ();

/* Actions */

/* Make a diff between dataobj1 and dataobj2. */
/* Returns 1 on success, 0 if not enough data, < 0 on error. */
int rev_delta_diff (struct rev_delta *obj);

/* Apply a diff on dataobj1, resulting in dataobj2. */
/* Returns 1 on success, 0 if not enough data, < 0 on error. */
int rev_delta_apply (struct rev_delta *obj);

/* Unapply a diff from dataobj2, resulting in dataobj1. */
/* Returns 1 on success, 0 if not enough data, < 0 on error. */
int rev_delta_unapply (struct rev_delta *obj);

/* Attributes */

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

/* This returns the dataobj. */
static inline struct rev_dataobj *
rev_delta_get_dataobj1 (struct rev_delta *obj)
{
	assert (obj);
	return obj->dataobj1;
}

/* This sets the dataobj. */
static inline void
rev_delta_set_dataobj1 (struct rev_delta *obj, struct rev_dataobj *dataobj)
{
	assert (obj);
	if (obj->dataobj1) {
		rev_dataobj_dissociate (obj->dataobj1);
		obj->dataobj1 = NULL;
	}
	if (dataobj) {
		rev_dataobj_associate (dataobj);
		obj->dataobj1 = dataobj;
	}
}

/* This returns the dataobj. */
static inline struct rev_dataobj *
rev_delta_get_dataobj2 (struct rev_delta *obj)
{
	assert (obj);
	return obj->dataobj2;
}

/* This sets the dataobj. */
static inline void
rev_delta_set_dataobj2 (struct rev_delta *obj, struct rev_dataobj *dataobj)
{
	assert (obj);
	if (obj->dataobj2) {
		rev_dataobj_dissociate (obj->dataobj2);
		obj->dataobj2 = NULL;
	}
	if (dataobj) {
		rev_dataobj_associate (dataobj);
		obj->dataobj2 = dataobj;
	}
}

/* This returns the filename string. */
static inline char *
rev_delta_get_filename1 (struct rev_delta *obj)
{
	assert (obj);
	return obj->filename1;
}

/* This sets the filename string. */
static inline void
rev_delta_set_filename1 (struct rev_delta *obj, char *filename)
{
	assert (obj);
	if (obj->filename1) free (obj->filename1);
	obj->filename1 = filename ? strdup (filename) : NULL;
}

/* This returns the filename string. */
static inline char *
rev_delta_get_filename2 (struct rev_delta *obj)
{
	assert (obj);
	return obj->filename2;
}

/* This sets the filename string. */
static inline void
rev_delta_set_filename2 (struct rev_delta *obj, char *filename)
{
	assert (obj);
	if (obj->filename2) free (obj->filename2);
	obj->filename2 = filename ? strdup (filename) : NULL;
}

	
#ifdef __cplusplus
}
#endif

#endif
