/* -*- mode: c++; indent-tabs-mode: nil -*- */
/** @file QC_BufferedStreamReader.qpp BufferedStreamReader 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/InputStream.h"
#include "qore/intern/BufferedStreamReader.h"
#include "qore/intern/QoreObjectIntern.h"
#include "qore/intern/QoreClassIntern.h"

/** @defgroup BufferedStreamReader Constants
*/
///@{
//! The default buffer size for the @ref Qore::BufferedStreamReader "BufferedStreamReader" class
const DefaultStreamBufferSize = 4096;
///@}

//! This class defines a buffered stream reader for input streams.
/** @since %Qore 0.8.13

    @par Example: BufferedStreamReader basic usage
    @code{.py}
FileInputStream is("data.txt");
BufferedStreamReader sr(is, "UTF-8");

*string line = sr.readLine();
*binary b = sr.readBinary(256);
int i = sr.readi4();
    @endcode

    @see @ref Qore::InputStream
 */
qclass BufferedStreamReader [arg=BufferedStreamReader* sr; ns=Qore; vparent=StreamReader; internal_members=InputStream is];

//! Creates the BufferedStreamReader for reading data from the given @ref InputStream.
/** @param is the @ref InputStream for reading data
    @param encoding the character encoding of the data from the @ref InputStream; if not present, the @ref default_encoding "default character encoding" is assumed
    @param bufsize the size of the internal buffer in bytes; must be a positive integer

    @throw STREAM-BUFFER-ERROR an invalid stream buffer size was specified; the buffer size must be a positive integer
 */
BufferedStreamReader::constructor(Qore::InputStream[InputStream] is, *string encoding, int bufsize = DefaultStreamBufferSize) {
   SimpleRefHolder<BufferedStreamReader> reader(new BufferedStreamReader(xsink, is, encoding ? QEM.findCreate(encoding) : QCS_DEFAULT, bufsize));
   if (*xsink)
      return;
   self->setPrivate(CID_BUFFEREDSTREAMREADER, reader.release());
   qore_object_private* o = qore_object_private::get(*self);
   const qore_class_private* cls = qore_class_private::get(*QC_BUFFEREDSTREAMREADER);
   o->setValueIntern(cls, "is", static_cast<QoreObject*>(obj_is->refSelf()), xsink);
}
