summaryrefslogtreecommitdiff
path: root/tools/file.h
blob: de63e74ed543515b4a1e1897fcb68785eadbdf50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#ifndef TOOLBOX_FILE_H
#define TOOLBOX_FILE_H

#include "tools/tools.h"
#include "tools/source.h"
#include "tools/string.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

/* cTBFile provides a cTBSource-derived interface for input and output on UNIX 
   files. */

class cTBFile: public cTBSource {
private:
	bool m_Anonymous;
	cTBString m_Filename;

	/* Unhide and forbid baseclass method */
	virtual bool Open(int Fd, bool IsUnixFd = false) { return false; }

public:
	cTBFile(void);
	virtual ~cTBFile();

	/* enum eFileType represents the modes a file can be opened with. The full
	   open mode is one of the first three, maybe or'ed with one of the others.
	   */
	enum eFileType {
		ReadOnly  = O_RDONLY,
		WriteOnly = O_WRONLY,
		ReadWrite = O_RDWR,
		
		Create    = O_CREAT,
		Exclude   = O_EXCL,
		Truncate  = O_TRUNC,
		Append    = O_APPEND
	};

	/* See cTBSource::SysRead() 
	   Reimplemented for UNIX files. */
	virtual ssize_t SysRead(void *Buffer, size_t Length) const;

	/* See cTBSource::SysWrite() 
	   Reimplemented for UNIX files. */
	virtual ssize_t SysWrite(const void *Buffer, size_t Length) const;

	/* Open() opens the file referred to by Filename according to the given 
	   Mode. If the file is created, it receives the attributes given by 
	   Attribs, defaulting to rw-------. Returns true on success and false on
	   error, setting errno appropriately. */
	virtual bool Open(const cTBString &Filename, int Mode, 
			mode_t Attribs = S_IRUSR + S_IWUSR);

	/* Open() associates this file object with Fileno. Fileno must refer to a
	   previously opened file descriptor, which will be set non-blocking by
	   this call. If successful, true is returned, false otherwise and errno
	   is set appropriately. */
	virtual bool Open(uint Fileno);

	/* Close() closes the associated file descriptor and releases all 
	   structures. Returns true on success and false otherwise, setting errno
	   appropriately. The object is in the closed state afterwards, even if
	   an error occured. */
	virtual bool Close(void);
	
	/* Unlink() unlinks (deletes) the associated file from the underlying 
	   filesystem. Returns true on success and false otherwise, setting errno
	   appropriately. The file must be opened by filename to use this. */
	virtual bool Unlink(void) const;

	/* Unlink() unlinks (deletes) the file referred to by Filename from the
	   underlying filesystem. Returns true on success and false otherwise, 
	   setting errno appropriately. */
	static  bool Unlink(const cTBString &Filename);

	/* Size() returns the current size of the associated file. Returns the 
	   exact size of the file in bytes. Returns -1 on error, setting errno to
	   an appropriate value. */
	virtual ssize_t Size(void) const;

	/* Size() returns the current size of the file referred to by Filename.
	   Symbolic links are followed (the size of the link-target is returned).
	   Returns the exact size of the file in bytes. Returns -1 on error, 
	   setting errno to an appropriate value. */
	static  ssize_t Size(const cTBString &Filename);
};

inline ssize_t cTBFile::SysRead(void *Buffer, size_t Length) const {
	return ::read(*this, Buffer, Length);
}

inline ssize_t cTBFile::SysWrite(const void *Buffer, size_t Length) const {
	return ::write(*this, Buffer, Length);
}


#endif // TOOLBOX_FILE_H