All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreFile.h
/* -*- mode: c++; indent-tabs-mode: nil -*- */
/*
QoreFile.h
thread-safe File object
Qore Programming Language
Copyright (C) 2003 - 2016 David Nichols
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.
*/
#ifndef _QORE_QOREFILE_H
#define _QORE_QOREFILE_H
#include <fcntl.h>
#include <sys/file.h>
#include <unistd.h>
#include <string>
/*
getchar from stdio.h(included via string on some platforms)
is allowed to be defined as a macro and this can cause
problems here since the getchar mentioned in this file might
get replaced, so we undefine it if it is defined.
Undefining getchar is safe since getchar must be defined as
a function, so the function is used if the macro is not available.
*/
#ifdef getchar
#undef getchar
#endif
class QoreTermIOS;
class Queue;
class QoreFile {
protected:
struct qore_qf_private *priv;
DLLLOCAL QoreFile(const QoreFile&);
DLLLOCAL QoreFile& operator=(const QoreFile&);
public:
DLLEXPORT QoreFile(const QoreEncoding *cs = QCS_DEFAULT);
DLLEXPORT ~QoreFile();
DLLEXPORT int open(const char *fn, int flags = O_RDONLY, int mode = 0777, const QoreEncoding *cs = QCS_DEFAULT);
DLLEXPORT int open2(ExceptionSink *xsink, const char *fn, int flags = O_RDONLY, int mode = 0777, const QoreEncoding *cs = QCS_DEFAULT);
DLLEXPORT int close();
DLLEXPORT void setEncoding(const QoreEncoding *cs);
DLLEXPORT const QoreEncoding *getEncoding() const;
DLLEXPORT int sync();
DLLEXPORT QoreStringNode* readLine(ExceptionSink* xsink);
DLLEXPORT QoreStringNode* readLine(bool incl_eol, ExceptionSink* xsink);
DLLEXPORT int readLine(QoreString &str);
DLLEXPORT int readLine(QoreString &str, bool incl_eol);
DLLEXPORT QoreStringNode* readUntil(const char* bytes, bool incl_bytes, ExceptionSink* xsink);
DLLEXPORT int readUntil(char byte, QoreString& str, bool incl_bytes = true);
DLLEXPORT int readUntil(const char* bytes, QoreString& str, bool incl_bytes = true);
DLLEXPORT int write(const QoreString *str, ExceptionSink *xsink);
DLLEXPORT int write(const BinaryNode *b, ExceptionSink *xsink);
DLLEXPORT int write(const void *data, qore_size_t len, ExceptionSink *xsink);
DLLEXPORT int writei1(char i, ExceptionSink *xsink);
DLLEXPORT int writei2(short i, ExceptionSink *xsink);
DLLEXPORT int writei4(int i, ExceptionSink *xsink);
DLLEXPORT int writei8(int64 i, ExceptionSink *xsink);
DLLEXPORT int writei2LSB(short i, ExceptionSink *xsink);
DLLEXPORT int writei4LSB(int i, ExceptionSink *xsink);
DLLEXPORT int writei8LSB(int64 i, ExceptionSink *xsink);
DLLEXPORT int readu1(unsigned char *val, ExceptionSink *xsink);
DLLEXPORT int readu2(unsigned short *val, ExceptionSink *xsink);
DLLEXPORT int readu4(unsigned int *val, ExceptionSink *xsink);
DLLEXPORT int readu2LSB(unsigned short *val, ExceptionSink *xsink);
DLLEXPORT int readu4LSB(unsigned int *val, ExceptionSink *xsink);
DLLEXPORT int readi1(char *val, ExceptionSink *xsink);
DLLEXPORT int readi2(short *val, ExceptionSink *xsink);
DLLEXPORT int readi4(int *val, ExceptionSink *xsink);
DLLEXPORT int readi8(int64 *val, ExceptionSink *xsink);
DLLEXPORT int readi2LSB(short *val, ExceptionSink *xsink);
DLLEXPORT int readi4LSB(int *val, ExceptionSink *xsink);
DLLEXPORT int readi8LSB(int64 *val, ExceptionSink *xsink);
DLLEXPORT QoreStringNode *read(qore_offset_t size, ExceptionSink *xsink);
DLLEXPORT int read(QoreString &str, qore_offset_t size, ExceptionSink *xsink);
DLLEXPORT BinaryNode *readBinary(qore_offset_t size, ExceptionSink *xsink);
DLLEXPORT int readBinary(BinaryNode &b, qore_offset_t size, ExceptionSink *xsink);
DLLEXPORT QoreStringNode *read(qore_offset_t size, int timeout_ms, ExceptionSink *xsink);
DLLEXPORT BinaryNode *readBinary(qore_offset_t size, int timeout_ms, ExceptionSink *xsink);
DLLEXPORT qore_size_t setPos(qore_size_t pos);
DLLEXPORT qore_size_t getPos();
DLLEXPORT qore_size_t getPos() const;
DLLEXPORT QoreStringNode *getchar();
DLLEXPORT QoreStringNode *getchar(ExceptionSink *xsink);
DLLEXPORT std::string getFileNameStr() const;
DLLEXPORT QoreStringNode* getFileName() const;
#ifndef _Q_WINDOWS
DLLEXPORT int chown(uid_t owner, gid_t group, ExceptionSink *xsink);
DLLEXPORT int lockBlocking(struct flock &fl, ExceptionSink *xsink);
DLLEXPORT int lock(const struct flock &fl, ExceptionSink *xsink);
DLLEXPORT int getLockInfo(struct flock &fl, ExceptionSink *xsink);
#endif
DLLEXPORT bool isDataAvailable(int timeout_ms, ExceptionSink *xsink) const;
DLLEXPORT QoreListNode *stat(ExceptionSink *xsink) const;
DLLEXPORT QoreHashNode *hstat(ExceptionSink *xsink) const;
DLLEXPORT QoreHashNode *statvfs(ExceptionSink *xsink) const;
#if 0
DLLEXPORT int preallocate(fstore_t &fs, ExceptionSink *xsink);
#endif
DLLEXPORT int getFD() const;
DLLEXPORT bool isOpen() const;
DLLEXPORT bool isTty() const;
DLLLOCAL int setTerminalAttributes(int action, QoreTermIOS *ios, ExceptionSink *xsink) const;
DLLLOCAL int getTerminalAttributes(QoreTermIOS *ios, ExceptionSink *xsink) const;
// NOTE: QoreFile::makeSpecial() can only be called right after the constructor (private API)
DLLLOCAL void makeSpecial(int sfd);
DLLLOCAL void setEventQueue(Queue *cbq, ExceptionSink *xsink);
DLLLOCAL void cleanup(ExceptionSink *xsink);
};
class QoreFileHelper : QorePrivateObjectAccessHelper {
public:
DLLEXPORT QoreFileHelper(QoreObject* obj, ExceptionSink* xsink);
DLLEXPORT ~QoreFileHelper();
DLLEXPORT QoreFile* operator*() const;
DLLEXPORT QoreFile* operator->() const;
};
#endif // _QORE_QOREFILE_H
QoreFile::writei2LSB
DLLEXPORT int writei2LSB(short i, ExceptionSink *xsink)
writes 2-byte (16bit) binary integer data in LSB (Least Significant Byte first, little endian) format...
QoreFile::readi2LSB
DLLEXPORT int readi2LSB(short *val, ExceptionSink *xsink)
reads a 2-byte signed integer from the file in LSB (Least Significant Byte first, big endian) format ...
QoreFile::getFileName
DLLEXPORT QoreStringNode * getFileName() const
returns the filename of the file being read (NULL if no file name is set); caller owns the reference ...
QoreEncoding
defines string encoding functions in Qore
Definition: QoreEncoding.h:85
QoreFile::lock
DLLEXPORT int lock(const struct flock &fl, ExceptionSink *xsink)
perform a file lock operation, does not block
QoreFile::writei8LSB
DLLEXPORT int writei8LSB(int64 i, ExceptionSink *xsink)
writes 8-byte (64bit) binary integer data in LSB (Least Significant Byte first, little endian) format...
QoreFile::writei4LSB
DLLEXPORT int writei4LSB(int i, ExceptionSink *xsink)
writes 4-byte (32bit) binary integer data in LSB (Least Significant Byte first, little endian)format ...
QoreHashNode
This is the hash or associative list container type in Qore, dynamically allocated only...
Definition: QoreHashNode.h:49
QoreFile::setPos
DLLEXPORT qore_size_t setPos(qore_size_t pos)
sets the absolute file position to &quot;pos&quot;
QoreFile::readUntil
DLLEXPORT QoreStringNode * readUntil(const char *bytes, bool incl_bytes, ExceptionSink *xsink)
reads string data from the file up to and optionally including the terminating EOL characters passed ...
QoreFile::operator=
DLLLOCAL QoreFile & operator=(const QoreFile &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
QoreFile::writei1
DLLEXPORT int writei1(char i, ExceptionSink *xsink)
writes 1-byte binary integer data to the file and returns the number of bytes written (normally 1) ...
QCS_DEFAULT
DLLEXPORT const QoreEncoding * QCS_DEFAULT
the default encoding for the Qore library
QoreFile::readi4
DLLEXPORT int readi4(int *val, ExceptionSink *xsink)
reads a 4-byte signed integer from the file in MSB (Most Significant Byte first, big endian) format a...
QoreFile::stat
DLLEXPORT QoreListNode * stat(ExceptionSink *xsink) const
returns a QoreListNode with file status information
QoreFile::writei2
DLLEXPORT int writei2(short i, ExceptionSink *xsink)
writes 2-byte (16bit) binary integer data in MSB (Most Significant Byte first, big endian) format to ...
QoreFile::readi8LSB
DLLEXPORT int readi8LSB(int64 *val, ExceptionSink *xsink)
reads an 8-byte signed integer from the file in LSB (Least Significant Byte first, big endian) format and returns the value read as an output parameter
QoreFile::QoreFile
DLLLOCAL QoreFile(const QoreFile &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
QoreFile::chown
DLLEXPORT int chown(uid_t owner, gid_t group, ExceptionSink *xsink)
changes ownership of the file (if possible)
QoreFile::readu2
DLLEXPORT int readu2(unsigned short *val, ExceptionSink *xsink)
reads a 2-byte unsigned integer from the file in MSB (Most Significant Byte first, big endian) format and returns the value read as an output parameter
qore_size_t
size_t qore_size_t
used for sizes (same range as a pointer)
Definition: common.h:71
QoreFile::readi4LSB
DLLEXPORT int readi4LSB(int *val, ExceptionSink *xsink)
reads a 4-byte signed integer from the file in LSB (Least Significant Byte first, big endian) format ...
QoreFile::isOpen
DLLEXPORT bool isOpen() const
returns true if the file is open, false if not
QoreFile::getFD
DLLEXPORT int getFD() const
get file descriptor
QoreString
Qore&#39;s string type supported by the QoreEncoding class.
Definition: QoreString.h:82
QoreStringNode
Qore&#39;s string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
QoreFile::readi1
DLLEXPORT int readi1(char *val, ExceptionSink *xsink)
reads a 1-byte signed integer from the file and returns the value read as an output parameter ...
QoreFile::statvfs
DLLEXPORT QoreHashNode * statvfs(ExceptionSink *xsink) const
returns a QoreHashNode with filesystem status information
QoreListNode
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
QoreFile::setTerminalAttributes
DLLLOCAL int setTerminalAttributes(int action, QoreTermIOS *ios, ExceptionSink *xsink) const
sets terminal attributes
QoreFile::setEncoding
DLLEXPORT void setEncoding(const QoreEncoding *cs)
sets the encoding for the file
QoreFile
provides controlled access to file data through Qore data structures
Definition: QoreFile.h:66
QoreFile::hstat
DLLEXPORT QoreHashNode * hstat(ExceptionSink *xsink) const
returns a QoreHashNode with file status information
QoreFile::read
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...
QoreFile::getchar
DLLEXPORT QoreStringNode * getchar()
reads a single byte from the file and returns it as a new string, caller owns the reference count ret...
QoreObject
the implementation of Qore&#39;s object data type, reference counted, dynamically-allocated only ...
Definition: QoreObject.h:64
QoreFile::getEncoding
DLLEXPORT const QoreEncoding * getEncoding() const
returns the encoding used for the file
QoreFile::getPos
DLLEXPORT qore_size_t getPos()
returns the absolute byte position in the file
QoreFile::writei8
DLLEXPORT int writei8(int64 i, ExceptionSink *xsink)
writes 8-byte (64bit) binary integer data in MSB (Most Significant Byte first, big endian) format to ...
ExceptionSink
container for holding Qore-language exception information and also for registering a &quot;thread_exit&quot; ca...
Definition: ExceptionSink.h:43
QoreFile::readu2LSB
DLLEXPORT int readu2LSB(unsigned short *val, ExceptionSink *xsink)
reads a 2-byte unsigned integer from the file in LSB (Least Significant Byte first, big endian) format and returns the value read as an output parameter
QoreFile::isDataAvailable
DLLEXPORT bool isDataAvailable(int timeout_ms, ExceptionSink *xsink) const
returns true if data is available for the file descriptor
int64
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:228
QoreFile::readu1
DLLEXPORT int readu1(unsigned char *val, ExceptionSink *xsink)
reads a 1-byte unsigned integer from the file and returns the value read as an output parameter ...
QoreFile::write
DLLEXPORT int write(const QoreString *str, ExceptionSink *xsink)
writes string data to the file, character encoding is converted if necessary, and returns the number ...
QoreFile::setEventQueue
DLLLOCAL void setEventQueue(Queue *cbq, ExceptionSink *xsink)
sets the event queue (not part of the library&#39;s pubilc API), must be already referenced before call ...
qore_offset_t
intptr_t qore_offset_t
used for offsets that could be negative
Definition: common.h:74
QoreFile::readLine
DLLEXPORT QoreStringNode * readLine(ExceptionSink *xsink)
reads string data from the file up to and including the terminating EOL characters (can be &quot;\n&quot;...
QoreFile::~QoreFile
DLLEXPORT ~QoreFile()
closes the file and frees all memory allocated to the object
QoreFile::getTerminalAttributes
DLLLOCAL int getTerminalAttributes(QoreTermIOS *ios, ExceptionSink *xsink) const
gets terminal attributes
QoreFile::readi2
DLLEXPORT int readi2(short *val, ExceptionSink *xsink)
reads a 2-byte signed integer from the file in MSB (Most Significant Byte first, big endian) format a...
QoreFile::isTty
DLLEXPORT bool isTty() const
returns true if the file is a tty
QoreFile::priv
struct qore_qf_private * priv
private implementation
Definition: QoreFile.h:69
QoreFile::open2
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
QoreFile::getLockInfo
DLLEXPORT int getLockInfo(struct flock &fl, ExceptionSink *xsink)
get lock info operation, does not block
QoreFile::lockBlocking
DLLEXPORT int lockBlocking(struct flock &fl, ExceptionSink *xsink)
perform a file lock operation
QoreFile::open
DLLEXPORT int open(const char *fn, int flags=O_RDONLY, int mode=0777, const QoreEncoding *cs=QCS_DEFAULT)
opens the file and returns 0 for success, non-zero for error
QoreFile::writei4
DLLEXPORT int writei4(int i, ExceptionSink *xsink)
writes 4-byte (32bit) binary integer data in MSB (Most Significant Byte first, big endian) format to ...
QoreFile::sync
DLLEXPORT int sync()
flushes the write buffer to disk
QoreFile::close
DLLEXPORT int close()
closes the file
QoreFile::readu4
DLLEXPORT int readu4(unsigned int *val, ExceptionSink *xsink)
reads a 4-byte unsigned integer from the file in MSB (Most Significant Byte first, big endian) format and returns the value read as an output parameter
QoreFile::readu4LSB
DLLEXPORT int readu4LSB(unsigned int *val, ExceptionSink *xsink)
reads a 4-byte unsigned integer from the file in LSB (Least Significant Byte first, big endian) format and returns the value read as an output parameter
QoreFile::readi8
DLLEXPORT int readi8(int64 *val, ExceptionSink *xsink)
reads an 8-byte signed integer from the file in MSB (Most Significant Byte first, big endian) format ...
QoreFile::getFileNameStr
DLLEXPORT std::string getFileNameStr() const
returns the filename of the file being read as a std::string (the string is empty if no file name is ...
QoreFile::cleanup
DLLLOCAL void cleanup(ExceptionSink *xsink)
internal API, must be called before deleting the object if an event queue is set
BinaryNode
holds arbitrary binary data
Definition: BinaryNode.h:41
QoreFile::readBinary
DLLEXPORT BinaryNode * readBinary(qore_offset_t size, ExceptionSink *xsink)
reads binary data from the file and returns the data read (caller owns the reference count returned) ...