source: trunk/tools/common/kFile.h@ 7093

Last change on this file since 7093 was 7093, checked in by bird, 24 years ago

Corrected buffering algorithm.

File size: 3.6 KB
Line 
1/* $Id: kFile.h,v 1.9 2001-10-17 14:22:33 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 */
22class kFile
23{
24protected:
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 */
55private:
56 kFile(HFILE hFile, BOOL fReadOnly);
57public:
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 static void * readFile(const char *pszFilename) throw(int);
66 BOOL readln(char *pszBuffer, long cchBuffer);
67
68 BOOL write(const void *pv, long cb) throw(int);
69 BOOL writeAt(void *pvBuffer, long cbBuffer, long off) throw(int);
70
71 int printf(const char *pszFormat, ...) throw (int);
72
73 BOOL setSize(unsigned long cbFile = ~0UL);
74
75 kFile & operator+=(kFile &AppendFile);
76
77 /** @cat File seek methods */
78 BOOL move(long off) throw(int);
79 BOOL set(long off) throw(int);
80 BOOL end() throw(int);
81 BOOL start();
82
83 /** @cat Query methods */
84 long getSize() throw(int);
85 long getPos() const throw(int);
86 BOOL isEOF() throw(int);
87 const char * getFilename() { return pszFilename; }
88
89 /** @cat Error handling */
90 BOOL setThrowOnErrors();
91 BOOL setFailOnErrors();
92 int getLastError() const;
93
94 /** standard files */
95 static kFile StdOut;
96 static kFile StdIn;
97 static kFile StdErr;
98};
99
100#endif
101
Note: See TracBrowser for help on using the repository browser.