1// This file is part of the ustl library, an STL implementation.
2//
3// Copyright (C) 2005 by Mike Sharov <msharov@users.sourceforge.net>
4// This file is free software, distributed under the MIT License.
5//
6// fstream.h
7//
8
9#ifndef FSTREAM_H_056E10F70EAD416443E3B36A2D6B5FA3
10#define FSTREAM_H_056E10F70EAD416443E3B36A2D6B5FA3
11
12#include "uios.h"
13#include "ustring.h"
14
15struct stat;
16
17namespace ustl {
18
19/// \class fstream fstream.h ustl.h
20///
21/// \brief Implements file operations.
22///
23/// This is not implemented as a stream, but rather as a base for one. You
24/// should use ifstream or ofstream if you want flow operators. Otherwise
25/// this only implements functions for binary i/o.
26///
27class fstream : public ios_base {
28public:
29			fstream (void);
30    explicit		fstream (const char* filename, openmode mode = in | out);
31    explicit		fstream (int nfd, const char* filename = string::empty_string);
32		       ~fstream (void) throw();
33    void		open (const char* filename, openmode mode, mode_t perms = 0644);
34    void		attach (int nfd, const char* filename = string::empty_string);
35    void		detach (void);
36    void		close (void);
37    void		sync (void);
38    off_t		read (void* p, off_t n);
39    off_t		readsome (void* p, off_t n);
40    off_t		write (const void* p, off_t n);
41    off_t		size (void) const;
42    off_t		seek (off_t n, seekdir whence = beg);
43    off_t		pos (void) const;
44    void		stat (struct stat& rs) const;
45    int			ioctl (const char* rname, int request, long argument = 0);
46    inline int		ioctl (const char* rname, int request, int argument)	{ return (fstream::ioctl (rname, request, long(argument))); }
47    inline int		ioctl (const char* rname, int request, void* argument)	{ return (fstream::ioctl (rname, request, intptr_t(argument))); }
48    int			fcntl (const char* rname, int request, long argument = 0);
49    inline int		fcntl (const char* rname, int request, int argument)	{ return (fstream::fcntl (rname, request, long(argument))); }
50    inline int		fcntl (const char* rname, int request, void* argument)	{ return (fstream::fcntl (rname, request, intptr_t(argument))); }
51    memlink		mmap (off_t n, off_t offset = 0);
52    void		munmap (memlink& l);
53    void		msync (memlink& l);
54    void		set_nonblock (bool v = true);
55    inline int		fd (void) const		{ return (m_fd); }
56    inline bool		is_open (void) const	{ return (fd() >= 0); }
57    inline off_t	tellg (void) const	{ return (pos()); }
58    inline off_t	tellp (void) const	{ return (pos()); }
59    inline void		seekg (off_t n, seekdir whence = beg)	{ seek (n, whence); }
60    inline void		seekp (off_t n, seekdir whence = beg)	{ seek (n, whence); }
61    inline void		flush (void)		{ sync(); }
62    inline const string& name (void) const	{ return (m_Filename); }
63private:
64   DLL_LOCAL static int	om_to_flags (openmode m);
65    DLL_LOCAL void	set_and_throw (iostate s, const char* op);
66private:
67    int			m_fd;		///< Currently open file descriptor.
68    string		m_Filename;	///< Currently open filename.
69};
70
71/// Argument macro for fstream::ioctl. Use like fs.ioctl (IOCTLID (TCGETS), &ts).
72#define IOCTLID(r)	"ioctl("#r")", r
73#define FCNTLID(r)	"fcntl("#r")", r
74
75}
76
77#endif
78
79