Qore Programming Language - C/C++ Library  0.8.13.5
 All Classes Namespaces 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 (C) 2003 - 2017 Qore Technologies, s.r.o.
10 
11  Permission is hereby granted, free of charge, to any person obtaining a
12  copy of this software and associated documentation files (the "Software"),
13  to deal in the Software without restriction, including without limitation
14  the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  and/or sell copies of the Software, and to permit persons to whom the
16  Software is furnished to do so, subject to the following conditions:
17 
18  The above copyright notice and this permission notice shall be included in
19  all copies or substantial portions of the Software.
20 
21  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  DEALINGS IN THE SOFTWARE.
28 
29  Note that the Qore library is released under a choice of three open-source
30  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
31  information.
32 */
33 
34 #ifndef _QORE_QOREFILE_H
35 
36 #define _QORE_QOREFILE_H
37 
38 #include <qore/AbstractPrivateData.h>
39 
40 #include <fcntl.h>
41 
42 #include <sys/file.h>
43 #include <unistd.h>
44 
45 #include <string>
46 
47 /*
48  getchar from stdio.h(included via string on some platforms)
49  is allowed to be defined as a macro and this can cause
50  problems here since the getchar mentioned in this file might
51  get replaced, so we undefine it if it is defined.
52  Undefining getchar is safe since getchar must be defined as
53  a function, so the function is used if the macro is not available.
54  */
55 #ifdef getchar
56 #undef getchar
57 #endif
58 
59 class QoreTermIOS;
60 class Queue;
61 
63 
68 class QoreFile {
69 protected:
71  struct qore_qf_private *priv;
72 
74  DLLLOCAL QoreFile(const QoreFile&);
75 
77  DLLLOCAL QoreFile& operator=(const QoreFile&);
78 
79 public:
81  DLLEXPORT QoreFile(const QoreEncoding *cs = QCS_DEFAULT);
82 
84  DLLEXPORT ~QoreFile();
85 
87 
96  DLLEXPORT int open(const char *fn, int flags = O_RDONLY, int mode = 0777, const QoreEncoding *cs = QCS_DEFAULT);
97 
99 
109  DLLEXPORT int open2(ExceptionSink *xsink, const char *fn, int flags = O_RDONLY, int mode = 0777, const QoreEncoding *cs = QCS_DEFAULT);
110 
112 
115  DLLEXPORT int close();
116 
118  DLLEXPORT void setEncoding(const QoreEncoding *cs);
119 
121  DLLEXPORT const QoreEncoding *getEncoding() const;
122 
124  DLLEXPORT int sync();
125 
127 
134  DLLEXPORT QoreStringNode* readLine(ExceptionSink* xsink);
135 
137 
146  DLLEXPORT QoreStringNode* readLine(bool incl_eol, ExceptionSink* xsink);
147 
149 
157  DLLEXPORT int readLine(QoreString &str);
158 
160 
169  DLLEXPORT int readLine(QoreString &str, bool incl_eol);
170 
172 
182  DLLEXPORT QoreStringNode* readUntil(const char* bytes, bool incl_bytes, ExceptionSink* xsink);
183 
185 
195  DLLEXPORT int readUntil(char byte, QoreString& str, bool incl_bytes = true);
196 
198 
207  DLLEXPORT int readUntil(const char* bytes, QoreString& str, bool incl_bytes = true);
208 
210 
215  DLLEXPORT int write(const QoreString *str, ExceptionSink *xsink);
216 
218 
223  DLLEXPORT int write(const BinaryNode *b, ExceptionSink *xsink);
224 
226 
232  DLLEXPORT int write(const void *data, qore_size_t len, ExceptionSink *xsink);
233 
235 
240  DLLEXPORT int writei1(char i, ExceptionSink *xsink);
241 
243 
249  DLLEXPORT int writei2(short i, ExceptionSink *xsink);
250 
252 
258  DLLEXPORT int writei4(int i, ExceptionSink *xsink);
259 
261 
267  DLLEXPORT int writei8(int64 i, ExceptionSink *xsink);
268 
270 
276  DLLEXPORT int writei2LSB(short i, ExceptionSink *xsink);
277 
279 
285  DLLEXPORT int writei4LSB(int i, ExceptionSink *xsink);
286 
288 
294  DLLEXPORT int writei8LSB(int64 i, ExceptionSink *xsink);
295 
297 
303  DLLEXPORT int readu1(unsigned char *val, ExceptionSink *xsink);
304 
306 
314  DLLEXPORT int readu2(unsigned short *val, ExceptionSink *xsink);
315 
317 
325  DLLEXPORT int readu4(unsigned int *val, ExceptionSink *xsink);
326 
328 
336  DLLEXPORT int readu2LSB(unsigned short *val, ExceptionSink *xsink);
337 
339 
347  DLLEXPORT int readu4LSB(unsigned int *val, ExceptionSink *xsink);
348 
350 
356  DLLEXPORT int readi1(char *val, ExceptionSink *xsink);
357 
359 
367  DLLEXPORT int readi2(short *val, ExceptionSink *xsink);
368 
370 
378  DLLEXPORT int readi4(int *val, ExceptionSink *xsink);
379 
381 
387  DLLEXPORT int readi8(int64 *val, ExceptionSink *xsink);
388 
390 
398  DLLEXPORT int readi2LSB(short *val, ExceptionSink *xsink);
399 
401 
409  DLLEXPORT int readi4LSB(int *val, ExceptionSink *xsink);
410 
412 
418  DLLEXPORT int readi8LSB(int64 *val, ExceptionSink *xsink);
419 
421 
431  DLLEXPORT QoreStringNode *read(qore_offset_t size, ExceptionSink *xsink);
432 
434 
445  DLLEXPORT QoreStringNode* read(qore_offset_t size, int timeout_ms, ExceptionSink *xsink);
446 
448 
455  DLLEXPORT int read(QoreString &str, qore_offset_t size, ExceptionSink *xsink);
456 
458 
464  DLLEXPORT BinaryNode *readBinary(qore_offset_t size, ExceptionSink *xsink);
465 
467 
473  DLLEXPORT int readBinary(BinaryNode &b, qore_offset_t size, ExceptionSink *xsink);
474 
476 
482  DLLEXPORT BinaryNode *readBinary(qore_offset_t size, int timeout_ms, ExceptionSink *xsink);
483 
485 
492  DLLEXPORT qore_size_t read(void *ptr, qore_size_t limit, int timeout_ms, ExceptionSink *xsink);
493 
495 
497  DLLEXPORT qore_size_t setPos(qore_size_t pos);
498 
500 
503  DLLEXPORT qore_size_t getPos();
504 
506 
508  DLLEXPORT qore_size_t getPos() const;
509 
511 
514  DLLEXPORT QoreStringNode *getchar();
515 
517 
522  DLLEXPORT QoreStringNode *getchar(ExceptionSink *xsink);
523 
525  DLLEXPORT std::string getFileNameStr() const;
526 
528  DLLEXPORT QoreStringNode* getFileName() const;
529 
530 #ifndef _Q_WINDOWS
531  DLLEXPORT int chown(uid_t owner, gid_t group, ExceptionSink *xsink);
533 
535  DLLEXPORT int lockBlocking(struct flock &fl, ExceptionSink *xsink);
536 
538  DLLEXPORT int lock(const struct flock &fl, ExceptionSink *xsink);
539 
541  DLLEXPORT int getLockInfo(struct flock &fl, ExceptionSink *xsink);
542 #endif
543 
545 
549  DLLEXPORT bool isDataAvailable(int timeout_ms, ExceptionSink *xsink) const;
550 
552 
555  DLLEXPORT QoreListNode *stat(ExceptionSink *xsink) const;
556 
558 
561  DLLEXPORT QoreHashNode *hstat(ExceptionSink *xsink) const;
562 
564 
567  DLLEXPORT QoreHashNode *statvfs(ExceptionSink *xsink) const;
568 
569 #if 0
570  DLLEXPORT int preallocate(fstore_t &fs, ExceptionSink *xsink);
572 #endif
573 
575  DLLEXPORT int getFD() const;
576 
578  DLLEXPORT bool isOpen() const;
579 
581  DLLEXPORT bool isTty() const;
582 
584  DLLLOCAL int setTerminalAttributes(int action, QoreTermIOS *ios, ExceptionSink *xsink) const;
585 
587  DLLLOCAL int getTerminalAttributes(QoreTermIOS *ios, ExceptionSink *xsink) const;
588 
589  // NOTE: QoreFile::makeSpecial() can only be called right after the constructor (private API)
590  DLLLOCAL void makeSpecial(int sfd);
591 
593  DLLLOCAL void setEventQueue(Queue *cbq, ExceptionSink *xsink);
594 
596  DLLLOCAL void cleanup(ExceptionSink *xsink);
597 };
598 
599 DLLEXPORT extern qore_classid_t CID_FILE;
600 DLLEXPORT extern QoreClass* QC_FILE;
601 DLLEXPORT extern QoreClass* QC_READONLYFILE;
602 
603 class File : public AbstractPrivateData, public QoreFile {
604 protected:
605  DLLLOCAL virtual ~File();
606 
607 public:
608  DLLLOCAL File(const QoreEncoding *cs);
609  DLLLOCAL virtual void deref(ExceptionSink *xsink);
610  DLLLOCAL virtual void deref();
611 };
612 
613 class QoreFileHelper : QorePrivateObjectAccessHelper {
614 public:
615  DLLEXPORT QoreFileHelper(QoreObject* obj, ExceptionSink* xsink);
616  DLLEXPORT ~QoreFileHelper();
617  DLLEXPORT QoreFile* operator*() const;
618  DLLEXPORT QoreFile* operator->() const;
619 };
620 
621 #endif // _QORE_QOREFILE_H
DLLEXPORT int writei2LSB(short i, ExceptionSink *xsink)
writes 2-byte (16bit) binary integer data in LSB (Least Significant Byte first, little endian) format...
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 ...
DLLEXPORT QoreStringNode * getFileName() const
returns the filename of the file being read (NULL if no file name is set); caller owns the reference ...
defines string encoding functions in Qore
Definition: QoreEncoding.h:85
DLLEXPORT int lock(const struct flock &fl, ExceptionSink *xsink)
perform a file lock operation, does not block
DLLEXPORT int writei8LSB(int64 i, ExceptionSink *xsink)
writes 8-byte (64bit) binary integer data in LSB (Least Significant Byte first, little endian) format...
DLLEXPORT int writei4LSB(int i, ExceptionSink *xsink)
writes 4-byte (32bit) binary integer data in LSB (Least Significant Byte first, little endian)format ...
This is the hash or associative list container type in Qore, dynamically allocated only...
Definition: QoreHashNode.h:50
DLLEXPORT qore_size_t setPos(qore_size_t pos)
sets the absolute file position to &quot;pos&quot;
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 ...
the base class for all data to be used as private data of Qore objects
Definition: AbstractPrivateData.h:44
DLLLOCAL QoreFile & operator=(const QoreFile &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
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) ...
DLLEXPORT const QoreEncoding * QCS_DEFAULT
the default encoding for the Qore library
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...
DLLEXPORT QoreListNode * stat(ExceptionSink *xsink) const
returns a QoreListNode with file status information
DLLEXPORT int writei2(short i, ExceptionSink *xsink)
writes 2-byte (16bit) binary integer data in MSB (Most Significant Byte first, big endian) format to ...
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
DLLLOCAL QoreFile(const QoreFile &)
this function is not implemented; it is here as a private function in order to prohibit it from being...
DLLEXPORT int chown(uid_t owner, gid_t group, ExceptionSink *xsink)
changes ownership of the file (if possible)
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
size_t qore_size_t
used for sizes (same range as a pointer)
Definition: common.h:74
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 ...
DLLEXPORT bool isOpen() const
returns true if the file is open, false if not
DLLEXPORT int getFD() const
get file descriptor
Qore&#39;s string type supported by the QoreEncoding class.
Definition: QoreString.h:82
Qore&#39;s string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
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 ...
DLLEXPORT QoreHashNode * statvfs(ExceptionSink *xsink) const
returns a QoreHashNode with filesystem status information
This is the list container type in Qore, dynamically allocated only, reference counted.
Definition: QoreListNode.h:52
DLLLOCAL int setTerminalAttributes(int action, QoreTermIOS *ios, ExceptionSink *xsink) const
sets terminal attributes
DLLEXPORT void setEncoding(const QoreEncoding *cs)
sets the encoding for the file
defines a Qore-language class
Definition: QoreClass.h:214
provides controlled access to file data through Qore data structures
Definition: QoreFile.h:68
DLLEXPORT QoreHashNode * hstat(ExceptionSink *xsink) const
returns a QoreHashNode with file status information
virtual DLLLOCAL void deref()
decrements the reference count of the object without the possibility of throwing a Qore-language exce...
Definition: AbstractPrivateData.h:67
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...
DLLEXPORT QoreStringNode * getchar()
reads a single byte from the file and returns it as a new string, caller owns the reference count ret...
the implementation of Qore&#39;s object data type, reference counted, dynamically-allocated only ...
Definition: QoreObject.h:62
unsigned qore_classid_t
used for the unique class ID for QoreClass objects
Definition: common.h:80
DLLEXPORT const QoreEncoding * getEncoding() const
returns the encoding used for the file
DLLEXPORT qore_size_t getPos()
returns the absolute byte position in the file
DLLEXPORT int writei8(int64 i, ExceptionSink *xsink)
writes 8-byte (64bit) binary integer data in MSB (Most Significant Byte first, big endian) format to ...
container for holding Qore-language exception information and also for registering a &quot;thread_exit&quot; ca...
Definition: ExceptionSink.h:47
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
DLLEXPORT bool isDataAvailable(int timeout_ms, ExceptionSink *xsink) const
returns true if data is available for the file descriptor
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:241
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 ...
DLLEXPORT int write(const QoreString *str, ExceptionSink *xsink)
writes string data to the file, character encoding is converted if necessary, and returns the number ...
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 ...
intptr_t qore_offset_t
used for offsets that could be negative
Definition: common.h:77
DLLEXPORT QoreStringNode * readLine(ExceptionSink *xsink)
reads string data from the file up to and including the terminating EOL characters (can be &quot;\n&quot;...
DLLEXPORT ~QoreFile()
closes the file and frees all memory allocated to the object
DLLLOCAL int getTerminalAttributes(QoreTermIOS *ios, ExceptionSink *xsink) const
gets terminal attributes
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...
DLLEXPORT bool isTty() const
returns true if the file is a tty
struct qore_qf_private * priv
private implementation
Definition: QoreFile.h:71
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
DLLEXPORT int getLockInfo(struct flock &fl, ExceptionSink *xsink)
get lock info operation, does not block
DLLEXPORT int lockBlocking(struct flock &fl, ExceptionSink *xsink)
perform a file lock operation
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
DLLEXPORT int writei4(int i, ExceptionSink *xsink)
writes 4-byte (32bit) binary integer data in MSB (Most Significant Byte first, big endian) format to ...
DLLEXPORT int sync()
flushes the write buffer to disk
DLLEXPORT int close()
closes the file
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
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
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 ...
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 ...
DLLLOCAL void cleanup(ExceptionSink *xsink)
internal API, must be called before deleting the object if an event queue is set
holds arbitrary binary data
Definition: BinaryNode.h:41
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) ...