/* -*- mode: c++; indent-tabs-mode: nil -*- */
/** @file QC_FileInputStream.qpp FileInputStream class definition */
/*
  Qore Programming Language

  Copyright (C) 2016 - 2023 Qore Technologies, s.r.o.

  Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  DEALINGS IN THE SOFTWARE.

  Note that the Qore library is released under a choice of three open-source
  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
  information.
*/

#include <qore/Qore.h>
#include "qore/intern/FileInputStream.h"

extern qore_classid_t CID_FILEINPUTSTREAM;

static void FILEINPUTSTREAM_system_constructor(QoreObject* self, int fd, va_list args) {
   self->setPrivate(CID_FILEINPUTSTREAM, new FileInputStream(fd));
}

//! This class implements the @ref InputStream interface for reading bytes from a file
/** @par Example: FileInputStream basic usage
    @code{.py}
    FileInputStream fis("file.ext");
    binary *b;
    while (b = fis.read(100)) {
        printf("read %s\n", make_hex_string(b));
    }
    @endcode

    @note stream classes are not designed to be accessed from multiple threads; they have been implemented without
    locking for fast and efficient use when used from a single thread.  For methods that would be unsafe to use in
    another thread, any use of such methods in threads other than the thread where the constructor was called will
    cause a \c STREAM-THREAD-ERROR to be thrown, unless the stream is handed
    off to another thread using the @ref Qore::StreamBase::unassignThread() "StreamBase::unassignThread()"
    method in the thread that currently owns the stream, and the
    @ref Qore::StreamBase::reassignThread() "StreamBase::reassignThread()" method in the new thread.

    @see @ref Qore::StreamReader "StreamReader" for a class that can be used to read various kinds of data from an @ref Qore::InputStream "InputStream"

    @since %Qore 0.8.13
 */
qclass FileInputStream [system_constructor=FILEINPUTSTREAM_system_constructor; arg=FileInputStream* is; ns=Qore; vparent=InputStream; flags=final; dom=FILESYSTEM];

//! Creates the FileInputStream by opening a file
/**
    @param fileName the name of the file to open
    @param timeout_ms a timeout period with a resolution of milliseconds (a @ref relative_dates
           "relative date/time value"; integer arguments will be assumed to be milliseconds);
           if not given or negative the operations will never time out
    @param nonblocking_open if @ref True, then the \c O_NONBLOCK flag will be set in the call to <tt>open() (2)</tt>

    @throw FILE-OPEN2-ERROR if the file cannot be opened (does not exist, permission error, etc)

    @since %Qore 0.9.3 added the \a nonblocking_open flag
 */
FileInputStream::constructor(string fileName, timeout timeout_ms = -1, *bool nonblocking_open) {
    int flags = nonblocking_open ? O_NONBLOCK : 0;
    SimpleRefHolder<FileInputStream> fis(new FileInputStream(fileName, timeout_ms, flags, xsink));
    self->setPrivate(CID_FILEINPUTSTREAM, fis.release());
}

//! Reads bytes (up to a specified limit) from the input stream; returns \ref NOTHING if there are no more bytes in the stream
/**

    @param limit the maximum number of bytes to read
    @return the read bytes (the length is between 1 and `limit` inclusive) or \ref NOTHING if no more bytes are available

    @par Example:
    @code{.py}
    FileInputStream fis("file.ext");
    *binary b;
    while (b = fis.read(2)) {
        printf("read %s\n", make_hex_string(b));
    }
    @endcode

    @throw FILE-READ-ERROR if an I/O error occurs
    @throw FILE-READ-TIMEOUT-ERROR if no byte could be read in the timeout specified in the constructor

    @throw INPUT-STREAM-ERROR \a limit is not positive
    @throw STREAM-THREAD-ERROR this exception is thrown if this method is called from any thread other than the thread that created the object

    @see FileInputStream::readString()
 */
*binary FileInputStream::read(int limit) {
   return is->readHelper(limit, xsink);
}

//! Peeks the next byte available from the input stream; returns -1 if no more data available
/**
    @return the next byte available from the input stream or -1 if no more data is available

    @par Example:
    @code{.py}
    FileInputStream fis("file.ext");
    int nextByte = fis.peek();
    @endcode

    @throw FILE-READ-ERROR if an I/O error occurs
    @throw FILE-READ-TIMEOUT-ERROR if no byte could be read in the timeout specified in the constructor

    @throw STREAM-THREAD-ERROR this exception is thrown if this method is called from any thread other than the thread that created the object
 */
int FileInputStream::peek() {
   return is->peekHelper(xsink);
}
