Qore Programming Language  0.9.1
FileInputStream.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  FileInputStream.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2016 - 2018 Qore Technologies, s.r.o.
8 
9  Permission is hereby granted, free of charge, to any person obtaining a
10  copy of this software and associated documentation files (the "Software"),
11  to deal in the Software without restriction, including without limitation
12  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  and/or sell copies of the Software, and to permit persons to whom the
14  Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  DEALINGS IN THE SOFTWARE.
26 
27  Note that the Qore library is released under a choice of three open-source
28  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29  information.
30 */
31 
32 #ifndef _QORE_FILEINPUTSTREAM_H
33 #define _QORE_FILEINPUTSTREAM_H
34 
35 #include <stdint.h>
36 #include "qore/InputStream.h"
37 
41 class FileInputStream : public InputStream {
42 public:
43  DLLLOCAL FileInputStream(const QoreStringNode *fileName, int64 timeout, ExceptionSink *xsink) : timeout(timeout) {
44  f.open2(xsink, fileName->getBuffer(), O_RDONLY);
45  }
46 
47  DLLLOCAL FileInputStream(int fd) : timeout(-1) {
48  f.makeSpecial(fd);
49  }
50 
51  DLLLOCAL const char *getName() override {
52  return "FileInputStream";
53  }
54 
55  DLLLOCAL int64 read(void *ptr, int64 limit, ExceptionSink *xsink) override {
56  assert(limit > 0);
57  return f.read(ptr, limit, timeout, xsink);
58  }
59 
60  DLLLOCAL int64 peek(ExceptionSink *xsink) override {
61  qore_size_t pos = f.getPos(); // Save initial position.
62  unsigned char c;
63  qore_size_t rc = f.read(&c, 1, -1, xsink);
64  if (*xsink)
65  return -2;
66  if (rc == 0)
67  return -1;
68  f.setPos(pos); // Restore initial position.
69  return c;
70  }
71 
72  DLLLOCAL QoreFile& getFile() { return f; }
73 
74  DLLLOCAL int64 getTimeout() const { return timeout; }
75 
76 private:
77  QoreFile f;
78  int64 timeout;
79 };
80 
81 #endif // _QORE_FILEINPUTSTREAM_H
DLLLOCAL const char * getName() override
Returns the name of the class.
Definition: FileInputStream.h:51
DLLEXPORT qore_size_t setPos(qore_size_t pos)
sets the absolute file position to "pos"
size_t qore_size_t
used for sizes (same range as a pointer)
Definition: common.h:73
Qore&#39;s string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
DLLEXPORT const char * getBuffer() const
returns the string&#39;s buffer; this data should not be changed
provides controlled access to file data through Qore data structures
Definition: QoreFile.h:66
DLLEXPORT QoreStringNode * read(qore_offset_t size, ExceptionSink *xsink)
reads string data from the file and returns the string read (caller owns the reference count returned...
Private data for the Qore::FileInputStream class.
Definition: FileInputStream.h:41
DLLEXPORT qore_size_t getPos()
returns the absolute byte position in the file
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:46
Interface for private data of input streams.
Definition: InputStream.h:44
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260
DLLEXPORT int open2(ExceptionSink *xsink, const char *fn, int flags=O_RDONLY, int mode=0777, const QoreEncoding *cs=QCS_DEFAULT)
opens the file and raises a Qore-language exception if an error occurs
DLLLOCAL int64 read(void *ptr, int64 limit, ExceptionSink *xsink) override
Reads up to `limit` bytes from the input stream.
Definition: FileInputStream.h:55
DLLLOCAL int64 peek(ExceptionSink *xsink) override
Peeks the next byte from the input stream.
Definition: FileInputStream.h:60