1 | /* $Id: kFile.h,v 1.7 2000-10-05 07:27:57 bird Exp $
|
---|
2 | *
|
---|
3 | * kFile - Simple (for the time being) file class.
|
---|
4 | *
|
---|
5 | * Copyright (c) 2000 knut st. osmundsen (knut.stange.osmundsen@pmsc.no)
|
---|
6 | *
|
---|
7 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
8 | *
|
---|
9 | */
|
---|
10 |
|
---|
11 | #ifndef _kFile_h_
|
---|
12 | #define _kFile_h_
|
---|
13 |
|
---|
14 |
|
---|
15 |
|
---|
16 | /**
|
---|
17 | * Simple file class.
|
---|
18 | * It uses a lazy algorithm for chaning the file position.
|
---|
19 | * It is extenable to do buffered reading too.
|
---|
20 | * @author knut st. osmundsen
|
---|
21 | */
|
---|
22 | class kFile
|
---|
23 | {
|
---|
24 | protected:
|
---|
25 | /** @cat Main datamembers */
|
---|
26 | HFILE hFile; /* Pointer to stdio filehandle */
|
---|
27 | BOOL fReadOnly; /* True if readonly access, False is readwrite. */
|
---|
28 | BOOL fStdDev; /* True if stdio, stderr or stdin. Filestatus is invalid with this is set.*/
|
---|
29 | APIRET rc; /* Last error (return code). */
|
---|
30 | FILESTATUS4 filestatus; /* Filestatus data. */
|
---|
31 | BOOL fStatusClean; /* True if filestatus is clean. False is not clean */
|
---|
32 | BOOL fThrowErrors; /* When true we'll throw the rc on errors, else return FALSE. */
|
---|
33 | PSZ pszFilename; /* Pointer to filename. */
|
---|
34 |
|
---|
35 | /** @cat Position datamembers */
|
---|
36 | unsigned long offVirtual; /* Virtual file position - lazy set. */
|
---|
37 | unsigned long offReal; /* Real file position. */
|
---|
38 |
|
---|
39 | /** @cat Buffering datamembers */
|
---|
40 | char * pachBuffer; /* Pointer to the buffer. NULL if not buffering. */
|
---|
41 | unsigned long cbBuffer; /* Count of allocated bytes. */
|
---|
42 | unsigned long offBuffer; /* Virtual file offset where the buffer starts. */
|
---|
43 | unsigned long cbBufferValid; /* Count of valid bytes in the buffer. */
|
---|
44 | BOOL fBufferDirty; /* Dirty flag. Set when the buffer needs to be committed. */
|
---|
45 |
|
---|
46 | /** @cat internal buffer methods */
|
---|
47 | BOOL bufferRead(ULONG offFile) throw (int);
|
---|
48 | BOOL bufferCommit(void) throw (int);
|
---|
49 |
|
---|
50 | /** @cat internal methods for maintaing internal structures. */
|
---|
51 | BOOL refreshFileStatus() throw(int);
|
---|
52 | BOOL position() throw(int);
|
---|
53 |
|
---|
54 | /** @cat constructors */
|
---|
55 | private:
|
---|
56 | kFile(HFILE hFile, BOOL fReadOnly);
|
---|
57 | public:
|
---|
58 | kFile(const char *pszFilename, BOOL fReadOnly = TRUE) throw(int);
|
---|
59 | ~kFile();
|
---|
60 |
|
---|
61 | /** @cat File I/O methods */
|
---|
62 | BOOL read(void *pvBuffer, long cbBuffer) throw(int);
|
---|
63 | BOOL readAt(void *pvBuffer, long cbBuffer, long off) throw(int);
|
---|
64 | void * readFile() throw(int);
|
---|
65 | BOOL readln(char *pszBuffer, long cchBuffer);
|
---|
66 |
|
---|
67 | BOOL write(void *pvBuffer, long cbBuffer) throw(int);
|
---|
68 | BOOL writeAt(void *pvBuffer, long cbBuffer, long off) throw(int);
|
---|
69 |
|
---|
70 | int printf(const char *pszFormat, ...) throw (int);
|
---|
71 |
|
---|
72 | BOOL setSize(unsigned long cbFile = ~0UL);
|
---|
73 |
|
---|
74 | kFile & operator+=(kFile &AppendFile);
|
---|
75 |
|
---|
76 | /** @cat File seek methods */
|
---|
77 | BOOL move(long off) throw(int);
|
---|
78 | BOOL set(long off) throw(int);
|
---|
79 | BOOL end() throw(int);
|
---|
80 | BOOL start();
|
---|
81 |
|
---|
82 | /** @cat Query methods */
|
---|
83 | long getSize() throw(int);
|
---|
84 | long getPos() const throw(int);
|
---|
85 | BOOL isEOF() throw(int);
|
---|
86 | const char * getFilename() { return pszFilename; }
|
---|
87 |
|
---|
88 | /** @cat Error handling */
|
---|
89 | BOOL setThrowOnErrors();
|
---|
90 | BOOL setFailOnErrors();
|
---|
91 | int getLastError() const;
|
---|
92 |
|
---|
93 | /** standard files */
|
---|
94 | static kFile StdOut;
|
---|
95 | static kFile StdIn;
|
---|
96 | static kFile StdErr;
|
---|
97 | };
|
---|
98 |
|
---|
99 | #endif
|
---|
100 |
|
---|