4c36977a5a56352c536d8c118e4bb10b940277e9
[ghc-hetmet.git] / ghc / lib / std / cbits / fileObject.h
1 #ifndef FILEOBJECT_H
2 #define FILEOBJECT_H
3
4 /*
5   IOFileObjects are used as part of the IO.Handle
6   implementation, ensuring that when handles are
7   finalised, buffers are flushed and FILE* objects
8   are closed (we really should be using file descriptors
9   here..)
10   
11  */
12
13 typedef struct _IOFileObject {
14    int     fd;
15    void*   buf;
16
17    int     bufStart; /* offset of start of data waiting to
18                         be written.  This may be non-zero in
19                         the case where we wrote out some of the
20                         buffer, and then blocked.
21
22                         NOTE: this field should be non-zero *only*
23                         when we just blocked on a call to writeBuffer,
24                         and we're going to restart the call when
25                         we unblock.  It should be zero at all other
26                         times.
27                      */
28
29    int     bufWPtr;  /* points to next position to write,
30                           bufRPtr >= bufWPtr <= bufSize.
31                           
32                         For read-only files, bufWPtr = bufSize
33
34                         bufWPtr = 0 => buffer is empty.
35
36                      */
37    int     bufRPtr;  /* points to the next char to read 
38                           -1 >= bufRPtr <= bufWPtr 
39                           
40                         For write-only files, bufRPtr = 0
41
42                         bufRPtr == -1 => buffer is empty.
43                      */
44    int     bufSize;
45    int     flags;
46    struct _IOFileObject*   connectedTo;
47 } IOFileObject;
48
49 #define FILEOBJ_LB       2
50 #define FILEOBJ_BB       4
51 #define FILEOBJ_EOF      8
52 #define FILEOBJ_READ    16
53 #define FILEOBJ_WRITE   32
54 #define FILEOBJ_STD     64
55 /* The next two flags are used for RW file objects only.
56    They indicate whether the last operation was a read or a write.
57    (Need this info to determine whether a RW file object's
58     buffer should be flushed before doing a subsequent
59     read or write).
60 */
61 #define FILEOBJ_RW_READ 256
62 #define FILEOBJ_RW_WRITE 512
63 /* 
64  * Under Win32, a file fd is not the same as a socket fd, so
65  * we need to use separate r/w calls.
66  */ 
67 #define FILEOBJ_WINSOCK  1024
68 #define FILEOBJ_BINARY   2048
69
70 #define FILEOBJ_IS_EOF(x)     ((x)->flags & FILEOBJ_EOF)
71 #define FILEOBJ_SET_EOF(x)    ((x)->flags |= FILEOBJ_EOF)
72 #define FILEOBJ_CLEAR_EOF(x)  ((x)->flags &= ~FILEOBJ_EOF)
73 #define FILEOBJ_CLEAR_ERR(x)  FILEOBJ_CLEAR_EOF(x)
74
75 #define FILEOBJ_BLOCKED_READ   -5
76 #define FILEOBJ_BLOCKED_WRITE  -6
77 #define FILEOBJ_BLOCKED_CONN_WRITE  -7
78
79 #define FILEOBJ_UNBUFFERED(x)     (!((x)->flags & FILEOBJ_LB) && !((x)->flags & FILEOBJ_BB))
80 #define FILEOBJ_LINEBUFFERED(x)   ((x)->flags & FILEOBJ_LB)
81 #define FILEOBJ_BLOCKBUFFERED(x)  ((x)->flags & FILEOBJ_BB)
82 #define FILEOBJ_BUFFER_FULL(x)    ((x)->bufWPtr >= (x)->bufSize)
83 #define FILEOBJ_BUFFER_EMPTY(x)   ((x)->bufRPtr == (x)->bufWPtr)
84 #define FILEOBJ_HAS_PUSHBACKS(x)  ((x)->buf != NULL && (x)->bufRPtr >= 0 && (x)->bufRPtr < (x)->bufWPtr)
85 #define FILEOBJ_READABLE(x)       ((x)->flags & FILEOBJ_READ)
86 #define FILEOBJ_WRITEABLE(x)      ((x)->flags & FILEOBJ_WRITE)
87 #define FILEOBJ_JUST_READ(x)      ((x)->flags & FILEOBJ_RW_READ)
88 #define FILEOBJ_JUST_WRITTEN(x)   ((x)->flags & FILEOBJ_RW_WRITE)
89 #define FILEOBJ_NEEDS_FLUSHING(x) (!FILEOBJ_BUFFER_EMPTY(x))
90 #define FILEOBJ_RW(x)             (FILEOBJ_READABLE(x) && FILEOBJ_WRITEABLE(x))
91
92 #endif /* FILEOBJECT_H */