/**************************************************************************************
 *                                                                                    *
 * This application contains code from OpenDivX and is released as a "Larger Work"    *
 * under that license. Consistant with that license, this application is released     *
 * under the GNU General Public License.                                              *
 *                                                                                    *
 * The OpenDivX license can be found at: http://www.projectmayo.com/opendivx/docs.php *
 * The GPL can be found at: http://www.gnu.org/copyleft/gpl.html                      *
 *                                                                                    *
 * Copyright (c) 2001 - Project Mayo                                                  *
 *                                                                                    *
 * Authors: Damien Chavarria <adrc at projectmayo.com>                                *
 *                                                                                    *
 **************************************************************************************/


#include "InputMedia.h"


/*
 * The main class
 */

InputMedia::InputMedia(LPTSTR lpFilename, int mode, int type) 
{
	if(lpFilename) {
	
		this->file       = NULL;
		this->mode       = -1;

		switch(type) {

		case INPUT_TYPE_FILE:

			lastReadPos=0;
			this->file =CreateFile((LPCTSTR)lpFilename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

			this->mode = INPUT_TYPE_FILE;

			break;

		case INPUT_TYPE_HTTP:
			break;

			default:
				break;
		}
	}
}

InputMedia::~InputMedia() 
{
	if(this->file) {

		CloseHandle(this->file);
	}
}


int InputMedia::Read(char *data, unsigned int size)
{
	if (ReadFile(this->file,(void *) data,size,(LPDWORD) &size,NULL))
	{
		this->lastReadPos+=size;
		return size;
	}
	return 0;
}


int InputMedia::Seek(int size, unsigned int method)
{

//	switch(mode) {
//
//	case INPUT_TYPE_FILE:
//
//		if(this->file) {
//
			switch(method)
			{
			case INPUT_SEEK_SET:
				this->lastReadPos = size;
				SetFilePointer(this->file, size,NULL, FILE_BEGIN);
				return 0;
				break;

			
			case INPUT_SEEK_CUR:

				if(!size) {
					return this->lastReadPos;
				}
				else {
					this->lastReadPos += size;
					SetFilePointer(this->file, size, NULL,FILE_CURRENT);
					return 0;
				}
				break;

			case INPUT_SEEK_END:

				SetFilePointer( this->file, size, NULL,FILE_END);
				return 0;
				break;
			}
//		}
//		break;

//	case INPUT_TYPE_HTTP:
//
//		if(this->file) {
//
//			switch(method)
//			{
//			case INPUT_SEEK_SET:
//
//				this->lastReadPos = size;
//				return 0; 
//				break;
//
//			
//			case INPUT_SEEK_CUR:
//
//				if(size == 0) {
//					return this->lastReadPos;	
//				}
//				else {
//					this->lastReadPos += size;
//					return 0;
//				}
//				break;
//
//			case INPUT_SEEK_END:
//
//				break;
//			}
//		}
//		break;
//
//	default:
//		break;
//	}

	return 0;
}

int InputMedia::Close()
{
	if(this->file) {

		CloseHandle(this->file);
	}


	return 1;
}