Qore Programming Language: include/qore/QoreFile.h (6585) Source File

Qore Programming Language 0.8.11

- Main Page
- Related Pages
- Modules
- Classes
- Files

- File List
- File Members

All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
QoreFile.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3 QoreFile.h
4
5 thread-safe File object
6
7 Qore Programming Language
8
9 Copyright 2003 - 2013 David Nichols
10
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 2.1 of the License, or (at your option) any later version.
15
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #ifndef _QORE_QOREFILE_H
27
28 #define _QORE_QOREFILE_H
29
30 #include <fcntl.h>
31
32 #include <sys/file.h>
33 #include <unistd.h>
34
35 #include <string>
36
37 class QoreTermIOS;
38 class Queue;
39
41
46 class QoreFile {
47 private:
49 struct qore_qf_private *priv;
50
51 protected:
53 DLLLOCAL QoreFile(const QoreFile&);
54
56 DLLLOCAL QoreFile& operator=(const QoreFile&);
57
58 public:
60 DLLEXPORT QoreFile(const QoreEncoding *cs = QCS_DEFAULT);
61
63 DLLEXPORT ~QoreFile();
64
66
75 DLLEXPORT int open(const char *fn, int flags = O_RDONLY, int mode = 0777, const QoreEncoding *cs = QCS_DEFAULT);
76
78
88 DLLEXPORT int open2(ExceptionSink *xsink, const char *fn, int flags = O_RDONLY, int mode = 0777, const QoreEncoding *cs = QCS_DEFAULT);
89
91
94 DLLEXPORT int close();
95
97 DLLEXPORT void setEncoding(const QoreEncoding *cs);
98
100 DLLEXPORT const QoreEncoding *getEncoding() const;
101
103 DLLEXPORT int sync();
104
106
113 DLLEXPORT QoreStringNode* readLine(ExceptionSink* xsink);
114
116
125 DLLEXPORT QoreStringNode* readLine(bool incl_eol, ExceptionSink* xsink);
126
128
136 DLLEXPORT int readLine(QoreString &str);
137
139
148 DLLEXPORT int readLine(QoreString &str, bool incl_eol);
149
151
161 DLLEXPORT QoreStringNode* readUntil(const char* bytes, bool incl_bytes, ExceptionSink* xsink);
162
164
174 DLLEXPORT int readUntil(char byte, QoreString& str, bool incl_bytes = true);
175
177
186 DLLEXPORT int readUntil(const char* bytes, QoreString& str, bool incl_bytes = true);
187
189
194 DLLEXPORT int write(const QoreString *str, ExceptionSink *xsink);
195
197
202 DLLEXPORT int write(const BinaryNode *b, ExceptionSink *xsink);
203
205
211 DLLEXPORT int write(const void *data, qore_size_t len, ExceptionSink *xsink);
212
214
219 DLLEXPORT int writei1(char i, ExceptionSink *xsink);
220
222
228 DLLEXPORT int writei2(short i, ExceptionSink *xsink);
229
231
237 DLLEXPORT int writei4(int i, ExceptionSink *xsink);
238
240
246 DLLEXPORT int writei8(int64 i, ExceptionSink *xsink);
247
249
255 DLLEXPORT int writei2LSB(short i, ExceptionSink *xsink);
256
258
264 DLLEXPORT int writei4LSB(int i, ExceptionSink *xsink);
265
267
273 DLLEXPORT int writei8LSB(int64 i, ExceptionSink *xsink);
274
276
282 DLLEXPORT int readu1(unsigned char *val, ExceptionSink *xsink);
283
285
293 DLLEXPORT int readu2(unsigned short *val, ExceptionSink *xsink);
294
296
304 DLLEXPORT int readu4(unsigned int *val, ExceptionSink *xsink);
305
307
315 DLLEXPORT int readu2LSB(unsigned short *val, ExceptionSink *xsink);
316
318
326 DLLEXPORT int readu4LSB(unsigned int *val, ExceptionSink *xsink);
327
329
335 DLLEXPORT int readi1(char *val, ExceptionSink *xsink);
336
338
346 DLLEXPORT int readi2(short *val, ExceptionSink *xsink);
347
349
357 DLLEXPORT int readi4(int *val, ExceptionSink *xsink);
358
360
366 DLLEXPORT int readi8(int64 *val, ExceptionSink *xsink);
367
369
377 DLLEXPORT int readi2LSB(short *val, ExceptionSink *xsink);
378
380
388 DLLEXPORT int readi4LSB(int *val, ExceptionSink *xsink);
389
391
397 DLLEXPORT int readi8LSB(int64 *val, ExceptionSink *xsink);
398
400
406 DLLEXPORT QoreStringNode *read(qore_offset_t size, ExceptionSink *xsink);
407
409
416 DLLEXPORT int read(QoreString &str, qore_offset_t size, ExceptionSink *xsink);
417
419
424 DLLEXPORT BinaryNode *readBinary(qore_offset_t size, ExceptionSink *xsink);
425
427
433 DLLEXPORT int readBinary(BinaryNode &b, qore_offset_t size, ExceptionSink *xsink);
434
436
443 DLLEXPORT QoreStringNode *read(qore_offset_t size, int timeout_ms, ExceptionSink *xsink);
444
446
452 DLLEXPORT BinaryNode *readBinary(qore_offset_t size, int timeout_ms, ExceptionSink *xsink);
453
455
457 DLLEXPORT qore_size_t setPos(qore_size_t pos);
458
460
463 DLLEXPORT qore_size_t getPos();
464
466
468 DLLEXPORT qore_size_t getPos() const;
469
471
474 DLLEXPORT QoreStringNode *getchar();
475
477
482 DLLEXPORT QoreStringNode *getchar(ExceptionSink *xsink);
483
485 DLLEXPORT std::string getFileNameStr() const;
486
488 DLLEXPORT QoreStringNode* getFileName() const;
489
490 #if (!defined _WIN32 && !defined __WIN32__) || defined __CYGWIN__
491 DLLEXPORT int chown(uid_t owner, gid_t group, ExceptionSink *xsink);
493
495 DLLEXPORT int lockBlocking(struct flock &fl, ExceptionSink *xsink);
496
498 DLLEXPORT int lock(const struct flock &fl, ExceptionSink *xsink);
499
501 DLLEXPORT int getLockInfo(struct flock &fl, ExceptionSink *xsink);
502 #endif
503
505
509 DLLEXPORT bool isDataAvailable(int timeout_ms, ExceptionSink *xsink) const;
510
512
515 DLLEXPORT QoreListNode *stat(ExceptionSink *xsink) const;
516
518
521 DLLEXPORT QoreHashNode *hstat(ExceptionSink *xsink) const;
522
524
527 DLLEXPORT QoreHashNode *statvfs(ExceptionSink *xsink) const;
528
529 #if 0
530 DLLEXPORT int preallocate(fstore_t &fs, ExceptionSink *xsink);
532 #endif
533
535 DLLEXPORT int getFD() const;
536
538 DLLEXPORT bool isOpen() const;
539
541 DLLEXPORT bool isTty() const;
542
544 DLLLOCAL int setTerminalAttributes(int action, QoreTermIOS *ios, ExceptionSink *xsink) const;
545
547 DLLLOCAL int getTerminalAttributes(QoreTermIOS *ios, ExceptionSink *xsink) const;
548
549 // NOTE: QoreFile::makeSpecial() can only be called right after the constructor (private API)
550 DLLLOCAL void makeSpecial(int sfd);
551
553 DLLLOCAL void setEventQueue(Queue *cbq, ExceptionSink *xsink);
554
556 DLLLOCAL void cleanup(ExceptionSink *xsink);
557 };
558
559 class QoreFileHelper : QorePrivateObjectAccessHelper {
560 public:
561 DLLEXPORT QoreFileHelper(QoreObject* obj, ExceptionSink* xsink);
562 DLLEXPORT ~QoreFileHelper();
563 DLLEXPORT QoreFile* operator*() const;
564 DLLEXPORT QoreFile* operator->() const;
565 };
566
567 #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:72
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:41
QoreFile::setPos
DLLEXPORT qore_size_t setPos(qore_size_t pos)
sets the absolute file position to "pos"
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:62
qore_offset_t
long qore_offset_t
used for offsets that could be negative
Definition: common.h:65
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's string type supported by the QoreEncoding class.
Definition: QoreString.h:42
QoreStringNode
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:40
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:44
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:46
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's object data type, reference counted, dynamically-allocated only ...
Definition: QoreObject.h:56
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 "thread_exit" ca...
Definition: ExceptionSink.h:35
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
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's pubilc API), must be already referenced before call ...
QoreFile::readLine
DLLEXPORT QoreStringNode * readLine(ExceptionSink *xsink)
reads string data from the file up to and including the terminating EOL characters (can be "\n"...
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::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:33
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) ...