/*------------------------------------------------------------------------------
 src/lib/dj/djFileIo.h
 
 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

#ifndef DJ_FILE_IO_H
#define DJ_FILE_IO_H
#include <djBase.h>

namespace dj {

// File I/O.
//
class FileIo {
public:

  FileIo();
  ~FileIo();

  // Open and close.
  //
  enum MODE { READ, WRITE };
  bool open(const Str &, MODE);
  void close();
  bool isValid() const { return _f != -1; }

  // Set.
  //
  bool set8(const int8_t *, int size);
  bool set8(const int8_t & in) { return set8(&in, 1); }
  bool setU8(const uint8_t *, int size);
  bool setU8(const uint8_t & in) { return setU8(&in, 1); }
  bool set16(const int16_t *, int size);
  bool set16(const int16_t & in) { return set16(&in, 1); }
  bool setU16(const uint16_t *, int size);
  bool setU16(const uint16_t & in) { return setU16(&in, 1); }
  bool set32(const int32_t *, int size);
  bool set32(const int32_t & in) { return set32(&in, 1); }
  bool setU32(const uint32_t *, int size);
  bool setU32(const uint32_t & in) { return setU32(&in, 1); }
  bool setF32(const float *, int size);
  bool setF32(const float & in) { return setF32(&in, 1); }
  bool set(const void *, int size, int wordSize = 1);

  // Get.
  //
  bool get8(int8_t *, int size = 1);
  bool getU8(uint8_t *, int size = 1);
  bool get16(int16_t *, int size = 1);
  bool getU16(uint16_t *, int size = 1);
  bool get32(int32_t *, int size = 1);
  bool getU32(uint32_t *, int size = 1);
  bool getF32(float *, int size = 1);
  bool get(void *, int size, int wordSize = 1);
  
  // XXX Ill but fast.
  //
# ifdef DJ_MMAP
  const uint8_t * mmapP() const { return _mmapP; }
  const uint8_t * mmapEnd() const { return _mmapEnd; }
# endif
  
  // File position.
  //
  enum SEEK { SET, CURRENT };
  bool seek(uint64_t offset, SEEK);
  uint64_t position() const;
  
  // Endian.
  //
  void endian(bool);
  bool endian() const { return _endian; }

private:

  int _f;
  
# ifdef DJ_MMAP
  void * _mmap;
  const uint8_t * _mmapP, * _mmapEnd;
  uint64_t _mmapSize;
# endif

  MODE _mode;
  bool _endian;
};

}

#endif
