| 1 | /* $Id: kFile.h,v 1.10 2002-02-24 02:47:24 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 | /** @cat constructors */
|
|---|
| 25 | private:
|
|---|
| 26 | #ifndef __OS2DEFS__
|
|---|
| 27 | kFile(unsigned long hFile, KBOOL fReadOnly);
|
|---|
| 28 | #else
|
|---|
| 29 | kFile(HFILE hFile, KBOOL fReadOnly);
|
|---|
| 30 | #endif
|
|---|
| 31 | public:
|
|---|
| 32 | kFile(const char *pszFilename, KBOOL fReadOnly = TRUE) throw(kError);
|
|---|
| 33 | ~kFile();
|
|---|
| 34 |
|
|---|
| 35 | /** @cat File I/O methods */
|
|---|
| 36 | int read(void *pvBuffer, long cbBuffer) throw(kError);
|
|---|
| 37 | int readAt(void *pvBuffer, long cbBuffer, long off) throw(kError);
|
|---|
| 38 | int readln(char *pszBuffer, long cchBuffer);
|
|---|
| 39 |
|
|---|
| 40 | int write(const void *pvBuffer, long cbBuffer) throw(kError);
|
|---|
| 41 | int writeAt(const void *pvBuffer, long cbBuffer, long off) throw(kError);
|
|---|
| 42 |
|
|---|
| 43 | int printf(const char *pszFormat, ...) throw(kError);
|
|---|
| 44 |
|
|---|
| 45 | int setSize(unsigned long cbFile = ~0UL);
|
|---|
| 46 |
|
|---|
| 47 | kFile & operator+=(kFile &AppendFile);
|
|---|
| 48 |
|
|---|
| 49 | void * mapFile() throw(kError);
|
|---|
| 50 | static void * mapFile(const char *pszFilename) throw(kError);
|
|---|
| 51 | static void mapFree(void *pvFileMapping) throw(kError);
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | /** @cat File seek methods */
|
|---|
| 55 | int move(long off) throw(kError);
|
|---|
| 56 | int set(long off) throw(kError);
|
|---|
| 57 | int end() throw(kError);
|
|---|
| 58 | int start();
|
|---|
| 59 |
|
|---|
| 60 | /** @cat Query methods */
|
|---|
| 61 | long getSize() throw(kError);
|
|---|
| 62 | long getPos() const throw(kError);
|
|---|
| 63 | KBOOL isEOF() throw(kError);
|
|---|
| 64 | const char * getFilename() { return pszFilename; }
|
|---|
| 65 |
|
|---|
| 66 | /** @cat Error handling */
|
|---|
| 67 | void setThrowOnErrors();
|
|---|
| 68 | void setFailOnErrors();
|
|---|
| 69 | int getLastError() const;
|
|---|
| 70 |
|
|---|
| 71 | /** standard files */
|
|---|
| 72 | static kFile StdOut;
|
|---|
| 73 | static kFile StdIn;
|
|---|
| 74 | static kFile StdErr;
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | protected:
|
|---|
| 78 | /** @cat Main datamembers */
|
|---|
| 79 | KBOOL fReadOnly; /* True if readonly access, False is readwrite. */
|
|---|
| 80 | KBOOL fStdDev; /* True if stdio, stderr or stdin. Filestatus is invalid with this is set.*/
|
|---|
| 81 | unsigned long rc; /* Last error (return code). */
|
|---|
| 82 | KBOOL fStatusClean; /* True if filestatus is clean. False is not clean */
|
|---|
| 83 | KBOOL fThrowErrors; /* When true we'll throw the rc on errors, else return FALSE. */
|
|---|
| 84 | char * pszFilename; /* Pointer to filename. */
|
|---|
| 85 |
|
|---|
| 86 | /** @cat Position datamembers */
|
|---|
| 87 | unsigned long offVirtual; /* Virtual file position - lazy set. */
|
|---|
| 88 | unsigned long offReal; /* Real file position. */
|
|---|
| 89 |
|
|---|
| 90 | /** @cat Buffering datamembers */
|
|---|
| 91 | char * pachBuffer; /* Pointer to the buffer. NULL if not buffering. */
|
|---|
| 92 | unsigned long cbBuffer; /* Count of allocated bytes. */
|
|---|
| 93 | unsigned long offBuffer; /* Virtual file offset where the buffer starts. */
|
|---|
| 94 | unsigned long cbBufferValid; /* Count of valid bytes in the buffer. */
|
|---|
| 95 | KBOOL fBufferDirty; /* Dirty flag. Set when the buffer needs to be committed. */
|
|---|
| 96 |
|
|---|
| 97 | /** @cat OS specific data */
|
|---|
| 98 | union _kFileOSSpecific
|
|---|
| 99 | {
|
|---|
| 100 | char achFiller[14+2 + 16]; /* OS2 uses 16 bytes currently. Add 16-bytes just in case. */
|
|---|
| 101 | #ifdef __OS2DEF__
|
|---|
| 102 | struct _kFileOS2
|
|---|
| 103 | {
|
|---|
| 104 | HFILE hFile; /* Pointer to stdio filehandle */
|
|---|
| 105 | FILESTATUS4 filestatus; /* Filestatus data. */
|
|---|
| 106 | } os2;
|
|---|
| 107 | #endif
|
|---|
| 108 | } OSData;
|
|---|
| 109 |
|
|---|
| 110 | /** @cat internal buffer methods */
|
|---|
| 111 | KBOOL bufferRead(unsigned long offFile) throw(kError);
|
|---|
| 112 | KBOOL bufferCommit(void) throw(kError);
|
|---|
| 113 |
|
|---|
| 114 | /** @cat internal methods for maintaing internal structures. */
|
|---|
| 115 | KBOOL refreshFileStatus() throw(kError);
|
|---|
| 116 | KBOOL position() throw(kError);
|
|---|
| 117 |
|
|---|
| 118 | };
|
|---|
| 119 |
|
|---|
| 120 | #endif
|
|---|
| 121 |
|
|---|