//*** Important update: epg.dat format will change!

//Please note: Software, that wants to access the epg.dat or epg. tmp file must first
//read the file UserMode.ini (from the DVBViewer directory) in order to find out
//where it is located:

case UserMode of
  0: //configuration files are located in the DVBViewer directory (exe path)
  1: //configuration files are located in the application data folder
     //(documents and settings\username\application data\...)
  2: //configuration files are located in the common application data folder
     //(documents and settings\all users\application data\...)

//In case of user mode 1 or 2 use the SHGetFolderPath function (from Shell32.dll)
//with the appropriate CSIDL value (CSIDL_APPDATA or CSIDL_COMMON_APPDATA) to
//retrieve the path, and append the directory specified by the Root entry
//of the UserMode.ini to it. See

http://msdn2.microsoft.com/en-us/library/ms647764.aspx

//---------------------------------------------------------------------

//The String data type is stored this way:
NrOfChars: DWord;
for i = 0 to NrOfChars - 1 do
  Char: Byte;

//---------------------------------------------------------------------

//epg.dat file structure

FormatVersion: Word //*** currently 0x0107, 0x0108 will be used soon!
TimeZone: Double; //TDateTime
  //TDateTime is a double floating point value. The integer part contains the
  //date (number of days since 12.30.1989). The fractional part contains the time.
  //e.g. time zone = 2 hours is stored as 2/24. The value can be negative!
  //All following date/time values are stored as UTC resp. GMT TDateTime.
  //Thus the time zone must be added in order to retrieve the local time.

  //now x channel entries in ascending order
while not EndOfFile do
    //channel identification:
  ServiceID: Word;
  TransportStreamID: Word;

  if FormatVersion >= 0x0108 //*** new format!
    OriginalNetworkID: Word;
    Tunertype: Byte;
      //0 = undefined, 1 = cable, 2 = satellite, 3 = terrestrial, 4 = ATSC
    Reserved: Byte; //= 0
  endif

  NrOfEvents: DWord; //Number of stored events for this channel
  for i := 0 to NrOfEvents-1 do
    EventID: Word;
      //see ETSI EN 300 468, 5.2.4 Event Information Table
    Reserved: Word;
      //Always 0 in DVBViewer GE.
      //May be used for other purpose in DVBViewer Pro
    StartTime: Double; //TDateTime
    Duration: Double; //TDateTime
    Event: String;
    Title: String;
    Description: String;
    CharSet: Byte;
    Content: Byte; //See ETSI EN 300 468, 6.2.9, Content Descriptor

      //the following data block is reserved for various data. Skip it.
    if FormatVersion >= 0x0108 //*** new format!
      DataBlockLength: DWord
      for j := 0 to DataBlockLength-1 do
        Data: Byte;
    endif

//---------------------------------------------------------------------

//An external application can let DVBViewer import/export an epg.tmp file from/to
//the configuration folder(according to the user mode, see UserMode.ini) by sending
//the following messages:

SendMessage(Handle,0xB2C2,0x2312,LParam);
//DVBviewer exports EPG data from now to epg.tmp.
//LParam specifies the desired channel:
//LowWord = ServiceID, HighWord = TransportStreamID
//LParam = 0 -> all channels
//Message result is the number of written bytes.

SendMessage(Handle,0xB2C2,0x2313,LParam);
//DVBViewer exports all EPG data to epg.tmp.
//LParam specifies the desired channel:
//LowWord = ServiceID, HighWord = TransportStreamID
//LParam = 0 -> all channels
//Message result is the number of written bytes:

PostMessage(Handle,0xB2C2,0x2314,0);
//DVBViewer imports data from epg.tmp and deletes the file.
//If SendMessage is used: Message result is the number of read bytes:

//The handle of the DVBViewer main window can be obtained this way:
Handle := FindWindow('TfrmMain','DVB Viewer');
if Handle <> 0 then...

// The format of the epg.tmp transfer file

FormatVersion: Word; //*** currently 0x0100, 0x0101 will be used in future!
TimeZone: Double; //TDateTime

while not EndOfFile do
    //channel identification
  ServiceID: Word;
  TransportStreamID: Word;

  if FormatVersion >= 0x0101 //*** new format!
    OriginalNetworkID: Word;
      //OriginalNetworkID can be undefined (= 0) when exporting to DVBViewer,
      //but should be specified if possible
    Tunertype: Byte;
      //0 = undefined, 1 = cable, 2 = satellite, 3 = terrestrial, 4 = ATSC
      //Tunertype can be undefined (= 0) when exporting to DVBViewer,
      //but should be specified if possible.
    Reserved: Byte; //= 0
  endif

  EventID: Word;
    //see ETSI EN 300 468, 5.2.4 Event Information Table
  Reserved: Word;
    //Always 0 in DVBViewer GE.
    //May be used for other purpose in DVBViewer Pro
  StartTime: Double; //TDateTime
  Duration: Double; //TDateTime
  Event: String;
  Title: String;
  Description: String;
  CharSet: Byte;
  Content: Byte; //See ETSI EN 300 468, 6.2.9, Content Descriptor

    //the following data block is reserved for various data. Skip it.
  if FormatVersion >= 0x0101 //*** new format!
    DataBlockLength: DWord
    for j := 0 to DataBlockLength - 1 do
      DataByte: Byte;
  endif

//---------------------------------------------------------------------

//DVBViewer plugins, that are running in the same process can transfer EPG data
//via memory by sending the following messages.
//Memory transfer uses the same format as epg.tmp file transfer (see above),
//starting with FormatVersion. There is no alignment to DWord or other boundaries!

SendMessage(Handle,0xB2C2,0x2310,LParam);
//DVBViewer imports EPG data from memory. LParam is a pointer to a TEPGSetData
//structure (record) with the following members:

Memory: Pointer; //pointer to the memory block that shall be imported
Size: DWord;     //size of the memory block

//Message result is the number of read bytes.
//The caller is responsible for freeing the memory!

SendMessage(Handle,0xB2C2,0x2311,LParam);
//DVBViewer exports EPG data to memory. LParam is a pointer to a TEPGExSearchRec
//structure (record). It specifies the conditions that exported EPG entries must
//fulfil and contains the following members:

ServiceID: Word;
TransportstreamID: Word
StartTime: Double; //TDateTime, see above
EndTime: Double; //TDateTime
Callback: procedure(P: Pointer; Size: LongInt); stdcall;

//ServiceID and TransportStreamID specify the desired channel.
//Set both to 0 if you want results from all channels.

//StartTime and EndTime set the following condition for events exported by DVBViewer:
//(EventEndTime > StartTime) and (EventStartTime <= EndTime)
//Set StartTime and/or EndTime to 0 for "no limitation".
//If both entries are equal and non-zero, DVBViewer exports only one entry
//per channel (the event that is running at this time).
//A special handling is applied if EndTime is negative. It lets DVBViewer answer
//the tricky question "If there is an Event X with a given start and end time, which
//Event Y from another channel is running at the same time?" There may be several
//intersecting Events Y. If you specify the start time of Event X and the end time
//as negative value, DVBViewer will select the intersecting Event Y (one per
//channel) with the nearest start time compared to the start time of Event X.

//Callback is a pointer to a procedure (function without result) with the stdcall
//calling convention (same as Windows API). It is called by DVBViewer after it has
//collected the requested data. The first parameter is a pointer to a memory block.
//The second one specifies its size. DVBViewer frees the memory when the call
//returns, so the pointer must only be used locally!

//---------------------------------------------------------------------

