[project @ 1999-03-01 09:23:58 by sof]
[ghc-hetmet.git] / ghc / lib / std / cbits / fileObject.h
1 #ifndef FILEOBJECT_H
2 #define FILEOBJECT_H
3
4 /* a good idea? */
5 #include <stdio.h>
6
7 /*
8   IOFileObjects are used as part of the IO.Handle
9   implementation, ensuring that when handles are
10   finalised, buffers are flushed and FILE* objects
11   are closed (we really should be using file descriptors
12   here..)
13   
14  */
15
16 typedef struct _IOFileObject {
17    int     fd;
18    void*   buf;
19    int     bufWPtr;  /* points to next position to write,
20                           bufRPtr >= bufWPtr <= bufSize.
21                           
22                         For read-only files, bufWPtr = bufSize
23
24                         bufWPtr = 0 => buffer is empty.
25
26                      */
27    int     bufRPtr;  /* points to the next char to read 
28                           -1 >= bufRPtr <= bufWPtr 
29                           
30                         For write-only files, bufRPtr = 0
31
32                         bufRPtr == -1 => buffer is empty.
33                      */
34    int     bufSize;
35    int     flags;
36    struct _IOFileObject*   connectedTo;
37 } IOFileObject;
38
39 #define FILEOBJ_FLUSH    1
40 #define FILEOBJ_LB       2
41 #define FILEOBJ_BB       4
42 #define FILEOBJ_EOF      8
43 #define FILEOBJ_READ    16
44 #define FILEOBJ_WRITE   32
45 #define FILEOBJ_STD     64
46 #define FILEOBJ_NONBLOCKING_IO 128
47 /* The next two flags are used for RW file objects only.
48    They indicate whether the last operation was a read or a write.
49    (Need this info to determine whether a RW file object's
50     buffer should be flushed before doing a subsequent
51     read or write).
52 */
53 #define FILEOBJ_RW_READ 256
54 #define FILEOBJ_RW_WRITE 512
55
56 #define FILEOBJ_IS_EOF(x)     ((x)->flags & FILEOBJ_EOF)
57 #define FILEOBJ_SET_EOF(x)    ((x)->flags |= FILEOBJ_EOF)
58 #define FILEOBJ_CLEAR_EOF(x)  ((x)->flags &= ~FILEOBJ_EOF)
59 #define FILEOBJ_CLEAR_ERR(x)  FILEOBJ_CLEAR_EOF(x)
60
61 #define FILEOBJ_BLOCKED_READ   -5
62 #define FILEOBJ_BLOCKED_WRITE  -6
63 #define FILEOBJ_BLOCKED_CONN_WRITE  -7
64
65 #define FILEOBJ_UNBUFFERED(x)     (!((x)->flags & FILEOBJ_LB) && !((x)->flags & FILEOBJ_BB))
66 #define FILEOBJ_LINEBUFFERED(x)   ((x)->flags & FILEOBJ_LB)
67 #define FILEOBJ_BLOCKBUFFERED(x)  ((x)->flags & FILEOBJ_BB)
68 #define FILEOBJ_BUFFER_FULL(x)    ((x)->bufWPtr >= (x)->bufSize)
69 #define FILEOBJ_BUFFER_EMPTY(x)   ((x)->bufRPtr == (x)->bufWPtr)
70 #define FILEOBJ_HAS_PUSHBACKS(x)  ((x)->buf != NULL && (x)->bufRPtr >= 0 && (x)->bufRPtr < (x)->bufWPtr)
71 #define FILEOBJ_READABLE(x)       ((x)->flags & FILEOBJ_READ)
72 #define FILEOBJ_WRITEABLE(x)      ((x)->flags & FILEOBJ_WRITE)
73 #define FILEOBJ_JUST_READ(x)      ((x)->flags & FILEOBJ_RW_READ)
74 #define FILEOBJ_JUST_WRITTEN(x)   ((x)->flags & FILEOBJ_RW_WRITE)
75 #define FILEOBJ_NEEDS_FLUSHING(x) (!FILEOBJ_BUFFER_EMPTY(x))
76 #define FILEOBJ_RW(x)             (FILEOBJ_READABLE(x) && FILEOBJ_WRITEABLE(x))
77
78 #endif /* FILEOBJECT_H */