1 /********************************************************************************
2 *                                                                               *
3 *                        I / O   D e v i c e   C l a s s                        *
4 *                                                                               *
5 *********************************************************************************
6 * Copyright (C) 2005,2006 by Jeroen van der Zijp.   All Rights Reserved.        *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or                 *
9 * modify it under the terms of the GNU Lesser General Public                    *
10 * License as published by the Free Software Foundation; either                  *
11 * version 2.1 of the License, or (at your option) any later version.            *
12 *                                                                               *
13 * This library is distributed in the hope that it will be useful,               *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU             *
16 * Lesser General Public License for more details.                               *
17 *                                                                               *
18 * You should have received a copy of the GNU Lesser General Public              *
19 * License along with this library; if not, write to the Free Software           *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
21 *********************************************************************************
22 * $Id: FXIO.h,v 1.8 2006/01/22 17:58:04 fox Exp $                               *
23 ********************************************************************************/
24 #ifndef FXIO_H
25 #define FXIO_H
26 
27 
28 
29 namespace FX {
30 
31 
32 /**
33 * FXIO manipulates a handle to an abstract i/o device.
34 * The various subclasses of FXIO perform i/o on files, sockets,
35 * pipes, and possibly other devices.
36 */
37 class FXAPI FXIO {
38 protected:
39   FXInputHandle device;         // Device (file/pipe/socket/whatever)
40   FXuint        access;         // Access being performed
41 private:
42   FXIO(const FXIO&);
43   FXIO &operator=(const FXIO&);
44 public:
45 
46   /// File modes
47   enum {
48 
49     /// Permissions
50     OtherRead      = 0x00004,   /// Others have read permission
51     OtherWrite     = 0x00002,   /// Others have write permisson
52     OtherExec      = 0x00001,   /// Others have execute permission
53     OtherReadWrite = 0x00006,   /// Others have read and write permission
54     OtherFull      = 0x00007,   /// Others have full access
55 
56     GroupRead      = 0x00020,   /// Group has read permission
57     GroupWrite     = 0x00010,   /// Group has write permission
58     GroupExec      = 0x00008,   /// Group has execute permission
59     GroupReadWrite = 0x00030,   /// Group has read and write permission
60     GroupFull      = 0x00038,   /// Group has full access
61 
62     OwnerRead      = 0x00100,   /// Owner has read permission
63     OwnerWrite     = 0x00080,   /// Owner has write permission
64     OwnerExec      = 0x00040,   /// Owner has execute permission
65     OwnerReadWrite = 0x00180,   /// Owner has read and write permission
66     OwnerFull      = 0x001C0,   /// Owner has full access
67 
68     /// Other flags
69     Hidden         = 0x00200,   /// Hidden file
70     Directory      = 0x00400,   /// Is directory
71     File           = 0x00800,   /// Is regular file
72     SymLink        = 0x01000,   /// Is symbolic link
73 
74     /// Special mode bits
75     SetUser        = 0x02000,   /// Set user id
76     SetGroup       = 0x04000,   /// Set group id
77     Sticky         = 0x08000,   /// Sticky bit
78 
79     /// Device special files
80     Character      = 0x10000,   /// Character device
81     Block          = 0x20000,   /// Block device
82     Socket         = 0x40000,   /// Socket device
83     Fifo           = 0x80000    /// Fifo device
84     };
85 
86   /// Access modes
87   enum {
88 
89     /// Basic access options
90     NoAccess    =  0,                           /// No access
91     ReadOnly    =  1,                           /// Open for reading
92     WriteOnly   =  2,                           /// Open for writing
93     ReadWrite   =  3,                           /// Open for read and write
94     Append      =  4,                           /// Open for append
95     Truncate    =  8,                           /// Truncate to zero when writing
96     Create      = 16,                           /// Create if it doesn't exist
97     Exclusive   = 32,                           /// Fail if trying to create a file which already exists
98     NonBlocking = 64,                           /// Non-blocking i/o
99 
100     /// Convenience access options
101     Reading     = ReadOnly,                     /// Normal options for reading
102     Writing     = ReadWrite|Create|Truncate     /// Normal options for writing
103     };
104 
105   /// Positioning modes
106   enum {
107     Begin   = 0,                /// Position from the begin (default)
108     Current = 1,                /// Position relative to current position
109     End     = 2                 /// Position from the end
110     };
111 
112 public:
113 
114   /// Construct
115   FXIO();
116 
117   /// Open device with access mode and handle
118   virtual bool open(FXInputHandle handle,FXuint mode);
119 
120   /// Return true if open
121   virtual bool isOpen() const;
122 
123   /// Return access mode
mode()124   FXuint mode() const { return access; }
125 
126   /// Return handle
handle()127   FXInputHandle handle() const { return device; }
128 
129   /// Attach existing device handle
130   virtual void attach(FXInputHandle handle,FXuint mode);
131 
132   /// Detach device handle
133   virtual void detach();
134 
135   /// Get current file position
136   virtual FXlong position() const;
137 
138   /// Change file position, returning new position from start
139   virtual FXlong position(FXlong offset,FXuint from=FXIO::Begin);
140 
141   /// Read block of bytes, returning number of bytes read
142   virtual FXival readBlock(void* data,FXival count);
143 
144   /// Write block of bytes, returning number of bytes written
145   virtual FXival writeBlock(const void* data,FXival count);
146 
147   /// Truncate file
148   virtual FXlong truncate(FXlong size);
149 
150   /// Flush to disk
151   virtual bool flush();
152 
153   /// Test if we're at the end
154   virtual bool eof();
155 
156   /// Return size of i/o device
157   virtual FXlong size();
158 
159   /// Close handle
160   virtual bool close();
161 
162   /// Destroy and close
163   virtual ~FXIO();
164   };
165 
166 }
167 
168 #endif
169