// streaml.h - class cStreamlist header file
// 
// MPEG2AVI
// --------
//
//  v0.16B34 changed I/O implementation from _open(), _close(), _read() to
//      CreateFile(), ReadFilePointer(), CloseHandle(), ReadFile(), etc.
//      moved #define ARBITRARY_VALUE from streaml.cpp to streaml.h
//  v0.16B31- converted fprintf(stderr, "..." ) to TextLogErr() calls
//  v0.16B3 - initial release
//    (replaces some code that used to be in mpv_read.cpp)
//
//  see streaml.cpp for more detailed explanation of this object's purpose
//

#include <windows.h>

// error code for streamlist system
#define STERR_LIST_OPEN -3	// list is already open 
#define STERR_FILE_NOT_FOUND -2 // file cannot be found
#define STERR_FAIL -1		// generic failure error code
#define STERR_OK 0			// generic success code

#define MAXLINELENGTH 256 // maximum line-length of input listfile
#define MAXFILES	16 //PX3 8 before // maximum # support files per streamlist
#define SKIP_CHAR ';' // comment character (line in listfile is ignored)

#define _NOFILE -1	// special # for indicating no-file-open

#define ARBITRARY_VALUE 11 // returned aliashandle is always this value

// v0.16B31 the following four defines are for the housekeeping functions/vars
#define TEXTMSG_LENGTH 1024
	// maximum text length of a single log-entry (message or error)
#define LOG_LENGTH 32767
	// maximum text length of messagelog and errorlog
#define LOG_NEW_ENTRY 0x0000001L // flag
#define LOG_RESET 0x0000002L	// flag

// this structure describes one file ( a 'element') of a streamlist
struct StreamListFile_struct
{
	__int64 qwLength;	// file length in bytes
	char filename[ MAXLINELENGTH ]; 
};
typedef struct StreamListFile_struct tStreamListFile;


// the object definition
class cStreamlist
{
protected:
	tStreamListFile m_list[MAXFILES];	// the structures
	int m_dwFileCount;	// number of files in streamlist

	// the state vars store the streamlist's current position and status

	// internally, cStreamlist operates on m_stCFhandle (the file handle)
	// When another application calls cStreamlist (::my_open()) to open
	// a new file, cStreamlist opens the file and returns an "alias handle"
	//
	// the client application uses "alias handle" as it would a real file
	// handle
	int m_stCFindex;		// state var - index (into array) of current file
//	int m_stCFhandle;	// state var - actual file handle of current file
	HANDLE m_stCFhandle;	// v0.16B34 state var - file handle of current file
	int m_stAliasHandle;	// state var - public alias (of file handle)
	int m_st_oflag;	// state var - opened file mode (captured in my_open() )

	// list management functions, manipulates the list of filenames
	int list_insert_file( const char *filename, int index ); 
	int list_clear( void );	// clear the stream-list
//	int validate_handle( int input_handle );	// validate file handle
	int validate_handle( int input_aliashandle );	// validate file handle
	HANDLE list_nextfile( void );	// close current file, move to next
//SVE 20.05.2001 public gemacht	__int64 qwStreamLength( void );	// returns total length of stream
	// housekeeping functions, text log
//SVE 03.06.2001 TextLogMsg nur im Debug-Modus
#ifdef _DEBUG
	void TextLogErr( DWORD errorID, char text[] );
	void TextLogMsg( DWORD dwFlags, char text[] );
#endif

	//
	// housekeeping variables for logging purposes
	//

//SVE 03.06.2001 TextLogMsg nur im Debug-Modus
#ifdef _DEBUG
	char *m_szErrorLog;	// text log for error messages
	char *m_szMessageLog;	// text log for miscellaneous messages
	char *m_szText;		// scratch space for functions, temp only!
	char *m_szText2;	// more scratch space for functions, temp only!
	char *m_szLastLogErr; // last error message, set by TextLogErr(),
		// reset by TextLogMsg( LOG_NEW_ENTRY, ... )
#endif
	DWORD m_lastError; // last error code, set by TextLogErr(),
	DWORD m_logCounter; // message ID counter


public:
	// constructor, destructor
	cStreamlist();
	~cStreamlist();

//SVE 20.05.2001
	__int64 qwStreamLength( void );	// returns total length of stream
	// get_list from file
	int list_get_ff( const char *listname ); // parse list from file
//	int open_stream( const char *streamname, int mode );
//	int close_stream( const char *streamname, int mode );
//	int read_stream( const char *streamname, int mode );

	// emulated low-level I/O functions
	int my_read( int handle, void *buffer, unsigned int count );
	int my_open( const char *filename, int oflag );
	int my_close( int handle );
	__int64 my_lseeki64( int handle, __int64 offset, int origin );
	__int64 my_telli64( int handle );

};
