// C++ code Copyright (C) David R. Evans G4AMJ/NQ0I

// UNIX magtape handling class

#ifndef MAGTAPEH
#define MAGTAPEH

#include <defines.h>

#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/ioctl.h>
#include <sys/mtio.h>
#include <unistd.h>

class magtape
{ int fd;

  void _execute(short int operation, daddr_t count = 1);

public:


// see open(2V) for parameter possibilities
  magtape(char* filename = "/dev/rmt4", int mode = O_RDONLY);
  ~magtape(void);

  inline void rewind(void) { _execute(MTREW); }
// rewind and offline
  inline void offline(void) { _execute(MTOFFL); }

// skip forward by files; n == 0 => start of current file
  inline void forward_files(int n = 0) { _execute(MTFSF, n); }

// skip backward by files; n == 0 => start of current file
  inline void backward_files(int n = 0) { _execute(MTNBSF, n); }

// skip by records
  inline void forward_records(int n = 1) { _execute(MTFSR, n); }
  inline void backward_records(int n = 1) { _execute(MTBSR, n); }

  inline void write_eof(void) { _execute(MTWEOF); }

// go to end of last file on tape, ready to append another file
  inline void end_of_tape(void) { _execute(MTEOM); }

  inline void next_file(void) { forward_files(1); }
  inline void start_file(void) { forward_files(); }

  inline int read(byte* buffer, const int length)
    { return ::read(fd, buffer, length); }
  inline int write(byte* buffer, const int length)
    { return ::write(fd, buffer, length); }

};

#endif
