/*
** PBM DataType
**
** Written by Gunther Nikl (gnikl@informatik.uni-rostock.de)
** FreeWare
**
*/

  // includes

#include <dos/dos.h>
#include <datatypes/datatypes.h>
#include <proto/exec.h>
#include <proto/dos.h>
#include "compiler.h"

  // handy macro

#define IsWhiteSpace(c) (((c)==' ') || ((c)=='\t') || ((c)=='\r') || ((c)=='\n'))

  /* DTHook():
   *
   *  Examine a file and return if we know its format.
   */

ASM(LONG)
DTHook(REG(a0,struct DTHookContext *Hook))
{ LREG(a6,APTR DOSBase) = Hook->dthc_DOSBase;
  UBYTE buf[4];

    // Did we get everything we needed?

  if (Hook->dthc_Lock && Hook->dthc_FIB && Hook->dthc_FileHandle) {

      // Examine the file

    if (Examine(Hook->dthc_Lock,Hook->dthc_FIB)) {

        // Is this really a file?

      if (Hook->dthc_FIB->fib_DirEntryType < 0) {

          // Seek back to the beginning

        if (Seek(Hook->dthc_FileHandle,0,OFFSET_BEGINNING) != -1) {

            // Try to read header

          if (Read(Hook->dthc_FileHandle,buf,3) == 3) {

              // check header!

            if (buf[0] == 'P' && buf[1] >= '1' && buf[1] <= '6' && IsWhiteSpace(buf[2]))
              return TRUE;
          }
        }
      }
    }
  }

    // No success!

  return FALSE;
}
