[project @ 2001-05-18 14:18:34 by simonmar]
[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     bufWPtr;  /* points to next position to write,
18                           bufRPtr >= bufWPtr <= bufSize.
19                           
20                         For read-only files, bufWPtr = bufSize
21
22                         bufWPtr = 0 => buffer is empty.
23
24                      */
25    int     bufRPtr;  /* points to the next char to read 
26                           -1 >= bufRPtr <= bufWPtr 
27                           
28                         For write-only files, bufRPtr = 0
29
30                         bufRPtr == -1 => buffer is empty.
31                      */
32    int     bufSize;  /* the size of the buffer, i.e. the number of bytes
33                         malloced */
34    int     flags;
35    struct _IOFileObject*   connectedTo;
36
37 } IOFileObject;
38
39 #define FILEOBJ_LB       2
40 #define FILEOBJ_BB       4
41 #define FILEOBJ_EOF      8
42 #define FILEOBJ_READ    16
43 #define FILEOBJ_WRITE   32
44 #define FILEOBJ_STD     64
45 /* The next two flags are used for RW file objects only.
46    They indicate whether the last operation was a read or a write.
47    (Need this info to determine whether a RW file object's
48     buffer should be flushed before doing a subsequent
49     read or write).
50 */
51 #define FILEOBJ_RW_READ 256
52 #define FILEOBJ_RW_WRITE 512
53 /* 
54  * Under Win32, a file fd is not the same as a socket fd, so
55  * we need to use separate r/w calls.
56  */ 
57 #define FILEOBJ_WINSOCK  1024
58 #define FILEOBJ_BINARY   2048
59
60 #define FILEOBJ_IS_EOF(x)     ((x)->flags & FILEOBJ_EOF)
61 #define FILEOBJ_SET_EOF(x)    ((x)->flags |= FILEOBJ_EOF)
62 #define FILEOBJ_CLEAR_EOF(x)  ((x)->flags &= ~FILEOBJ_EOF)
63 #define FILEOBJ_CLEAR_ERR(x)  FILEOBJ_CLEAR_EOF(x)
64
65 #define FILEOBJ_BLOCKED_READ   -5
66 #define FILEOBJ_BLOCKED_WRITE  -6
67 #define FILEOBJ_BLOCKED_CONN_WRITE  -7
68
69 #define FILEOBJ_UNBUFFERED(x)     (!((x)->flags & FILEOBJ_LB) && !((x)->flags & FILEOBJ_BB))
70 #define FILEOBJ_LINEBUFFERED(x)   ((x)->flags & FILEOBJ_LB)
71 #define FILEOBJ_BLOCKBUFFERED(x)  ((x)->flags & FILEOBJ_BB)
72 #define FILEOBJ_BUFFER_FULL(x)    ((x)->bufWPtr >= (x)->bufSize)
73 #define FILEOBJ_BUFFER_EMPTY(x)   ((x)->bufRPtr == (x)->bufWPtr)
74 #define FILEOBJ_HAS_PUSHBACKS(x)  ((x)->buf != NULL && (x)->bufRPtr >= 0 && (x)->bufRPtr < (x)->bufWPtr)
75 #define FILEOBJ_READABLE(x)       ((x)->flags & FILEOBJ_READ)
76 #define FILEOBJ_WRITEABLE(x)      ((x)->flags & FILEOBJ_WRITE)
77 #define FILEOBJ_JUST_READ(x)      ((x)->flags & FILEOBJ_RW_READ)
78 #define FILEOBJ_JUST_WRITTEN(x)   ((x)->flags & FILEOBJ_RW_WRITE)
79 #define FILEOBJ_NEEDS_FLUSHING(x) (!FILEOBJ_BUFFER_EMPTY(x))
80 #define FILEOBJ_RW(x)             (FILEOBJ_READABLE(x) && FILEOBJ_WRITEABLE(x))
81
82 #endif /* FILEOBJECT_H */