fileio.h
Classes:
class
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _FILEIO_H_ 25#define _FILEIO_H_ 26 27#ifndef _PLATFORM_H_ 28#include "platform/platform.h" 29#endif 30 31class File 32{ 33public: 34 /// What is the status of our file handle? 35 enum FileStatus 36 { 37 Ok = 0, ///< Ok! 38 IOError, ///< Read or Write error 39 EOS, ///< End of Stream reached (mostly for reads) 40 IllegalCall, ///< An unsupported operation used. Always accompanied by AssertWarn 41 Closed, ///< Tried to operate on a closed stream (or detached filter) 42 UnknownError ///< Catchall 43 }; 44 45 /// How are we accessing the file? 46 enum AccessMode 47 { 48 Read = 0, ///< Open for read only, starting at beginning of file. 49 Write = 1, ///< Open for write only, starting at beginning of file; will blast old contents of file. 50 ReadWrite = 2, ///< Open for read-write. 51 WriteAppend = 3 ///< Write-only, starting at end of file. 52 }; 53 54 /// Flags used to indicate what we can do to the file. 55 enum Capability 56 { 57 FileRead = BIT(0), 58 FileWrite = BIT(1) 59 }; 60 61private: 62 void *handle; ///< Pointer to the file handle. 63 FileStatus currentStatus; ///< Current status of the file (Ok, IOError, etc.). 64 U32 capability; ///< Keeps track of file capabilities. 65 66 File(const File&); ///< This is here to disable the copy constructor. 67 File& operator=(const File&); ///< This is here to disable assignment. 68 69public: 70 File(); ///< Default constructor 71 virtual ~File(); ///< Destructor 72 73 /// Opens a file for access using the specified AccessMode 74 /// 75 /// @returns The status of the file 76 FileStatus open(const char *filename, const AccessMode openMode); 77 78 /// Gets the current position in the file 79 /// 80 /// This is in bytes from the beginning of the file. 81 U32 getPosition() const; 82 83 /// Sets the current position in the file. 84 /// 85 /// You can set either a relative or absolute position to go to in the file. 86 /// 87 /// @code 88 /// File *foo; 89 /// 90 /// ... set up file ... 91 /// 92 /// // Go to byte 32 in the file... 93 /// foo->setPosition(32); 94 /// 95 /// // Now skip back 20 bytes... 96 /// foo->setPosition(-20, false); 97 /// 98 /// // And forward 17... 99 /// foo->setPosition(17, false); 100 /// @endcode 101 /// 102 /// @returns The status of the file 103 FileStatus setPosition(S32 position, bool absolutePos = true); 104 105 /// Returns the size of the file 106 U32 getSize() const; 107 108 /// Make sure everything that's supposed to be written to the file gets written. 109 /// 110 /// @returns The status of the file. 111 FileStatus flush(); 112 113 /// Closes the file 114 /// 115 /// @returns The status of the file. 116 FileStatus close(); 117 118 /// Gets the status of the file 119 FileStatus getStatus() const; 120 121 /// Reads "size" bytes from the file, and dumps data into "dst". 122 /// The number of actual bytes read is returned in bytesRead 123 /// @returns The status of the file 124 FileStatus read(U32 size, char *dst, U32 *bytesRead = NULL); 125 126 /// Writes "size" bytes into the file from the pointer "src". 127 /// The number of actual bytes written is returned in bytesWritten 128 /// @returns The status of the file 129 FileStatus write(U32 size, const char *src, U32 *bytesWritten = NULL); 130 131 /// Returns whether or not this file is capable of the given function. 132 bool hasCapability(Capability cap) const; 133 134 const void* getHandle() { return handle; } 135 136protected: 137 FileStatus setStatus(); ///< Called after error encountered. 138 FileStatus setStatus(FileStatus status); ///< Setter for the current status. 139}; 140 141#endif // _FILE_IO_H_ 142