/* -*- mode:c++; coding: koi8-r -*- */

/* $Id: FileByteSource.cpp,v 1.1 2003/04/14 20:53:07 cher Exp $ */
/* Copyright (C) 2003 Alexander Chernov <cher@unicorn.cmc.msu.ru> */

/*
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 See the `COPYING' file for the full terms and conditions.
*/

#include "FileByteSource.hpp"

#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
//#include <unistd.h>

#include <io.h>

FileByteSource::FileByteSource(char const *path)
  throw (OpenError)
{
  this->fd = open(path, O_RDONLY);
  if (this->fd < 0) {
    throw OpenError(path, errno);
  }
}

FileByteSource::FileByteSource(int _fd)
  throw ()
{
  /* FIXME: do sanity check? */
  this->fd = _fd;
}

FileByteSource::~FileByteSource()
  throw ()
{
  close(this->fd);
}

void
FileByteSource::read(int n, void *addr)
  throw (ReadError)
{
  errno = 0;
  if (::_read(this->fd, addr, n) != n) {
    throw ReadError();
  }
}

/*
 * Local variables:
 *  compile-command: "make -C .."
 *  c++-font-lock-extra-types: ("[A-Z]\\sw*[a-z]\\sw*")
 * End:
 */
