PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: File.h * 00003 * 00004 * Copyright (C) 2002-2012 The PixelLight Team (http://www.pixellight.org/) 00005 * 00006 * This file is part of PixelLight. 00007 * 00008 * PixelLight is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * PixelLight is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public License 00019 * along with PixelLight. If not, see <http://www.gnu.org/licenses/>. 00020 \*********************************************************/ 00021 00022 00023 #ifndef __PLCORE_FILE_H__ 00024 #define __PLCORE_FILE_H__ 00025 #pragma once 00026 00027 00028 //[-------------------------------------------------------] 00029 //[ Includes ] 00030 //[-------------------------------------------------------] 00031 #include "PLCore/File/FileObject.h" 00032 00033 00034 //[-------------------------------------------------------] 00035 //[ Namespace ] 00036 //[-------------------------------------------------------] 00037 namespace PLCore { 00038 00039 00040 //[-------------------------------------------------------] 00041 //[ Classes ] 00042 //[-------------------------------------------------------] 00043 /** 00044 * @brief 00045 * Class for accessing a file in the file system 00046 */ 00047 class File : public FileObject { 00048 00049 00050 //[-------------------------------------------------------] 00051 //[ Public definitions ] 00052 //[-------------------------------------------------------] 00053 public: 00054 /** 00055 * @brief 00056 * File access flags 00057 */ 00058 enum EAccess { 00059 FileRead = 1, /**< File can be read */ 00060 FileWrite = 2, /**< File can be written */ 00061 FileAppend = 4, /**< File will be appended */ 00062 FileCreate = 8, /**< File will be created */ 00063 FileText = 16, /**< File will be opened in text mode (whenever possible, don't set this flag because not each platform may support it) */ 00064 FileMemBuf = 32 /**< File will be opened memory buffered (only usable for reading!) */ 00065 }; 00066 00067 /** 00068 * @brief 00069 * Seek position enumerations 00070 */ 00071 enum ESeek { 00072 SeekSet = 0, /**< Seek relative to the start of the file (default setting) */ 00073 SeekCurrent, /**< Seek relative to the current position */ 00074 SeekEnd /**< Seek relative to the end of the file */ 00075 }; 00076 00077 00078 //[-------------------------------------------------------] 00079 //[ Public static data ] 00080 //[-------------------------------------------------------] 00081 public: 00082 static PLCORE_API File StandardInput; /**< Standard OS input stream (stdin) */ 00083 static PLCORE_API File StandardOutput; /**< Standard OS output stream (stdout) */ 00084 static PLCORE_API File StandardError; /**< Standard OS error stream (stderr) */ 00085 00086 00087 //[-------------------------------------------------------] 00088 //[ Public static functions ] 00089 //[-------------------------------------------------------] 00090 public: 00091 /** 00092 * @brief 00093 * Translates a given C file mode into access flags 00094 * 00095 * @param[in] sMode 00096 * C file mode ("r", "r+", "w", "w+", "a", "a+", "t", "r+t" etc. used for e.g. "fopen") to translate 00097 * 00098 * @return 00099 * The access flags (see EAccess) 00100 */ 00101 static PLCORE_API uint32 CFileModeToAccessFlags(const String &sMode); 00102 00103 00104 //[-------------------------------------------------------] 00105 //[ Public functions ] 00106 //[-------------------------------------------------------] 00107 public: 00108 /** 00109 * @brief 00110 * Constructor 00111 */ 00112 inline File(); 00113 00114 /** 00115 * @brief 00116 * Constructor 00117 * 00118 * @param[in] sUrl 00119 * URL of a file 00120 * @param[in] pAccess 00121 * Additional file access information (can be a null pointer) 00122 */ 00123 inline File(const String &sUrl, const FileAccess *pAccess = nullptr); 00124 00125 /** 00126 * @brief 00127 * Constructor 00128 * 00129 * @param[in] cUrl 00130 * URL of a file 00131 * @param[in] pAccess 00132 * Additional file access information (can be a null pointer) 00133 */ 00134 inline File(const Url &cUrl, const FileAccess *pAccess = nullptr); 00135 00136 /** 00137 * @brief 00138 * Constructor for a standard OS stream 00139 * 00140 * @param[in] pFile 00141 * Standard stream pointer 00142 * @param[in] nAccess 00143 * Access flags that were used to open the stream 00144 */ 00145 inline File(FILE *pFile, uint32 nAccess); 00146 00147 /** 00148 * @brief 00149 * Constructor 00150 * 00151 * @param[in] hFile 00152 * OS file handle 00153 * 00154 * @note 00155 * - On Linux, the handle is a file handle of type int. 00156 * - On Windows, the handle is a file handle of type HANDLE. 00157 * Do not expect an int (fileno) here! If you need it to work with 00158 * low level io functions, you need to convert the HANDLE to a 00159 * file handle by yourself (see _open_osfhandle). 00160 */ 00161 inline File(handle hFile); 00162 00163 /** 00164 * @brief 00165 * Constructor 00166 * 00167 * @param[in] pnData 00168 * The file data, if a null pointer same as the standard constructor 00169 * @param[in] nNumOfBytes 00170 * Number of file data bytes, MUST be valid! 00171 * @param[in] bCopy 00172 * Copy the given data or use this one directly? Do ONLY set bCopy to 'false' 00173 * if you are sure there can't go anything wrong. (data pointer MUST stay valid!) 00174 * @param[in] sUrl 00175 * URL of a file 00176 */ 00177 PLCORE_API File(uint8 *pnData, uint32 nNumOfBytes, bool bCopy = true, const String &sUrl = ""); 00178 00179 /** 00180 * @brief 00181 * Destructor 00182 * 00183 * @note 00184 * - The file is closed automatically 00185 */ 00186 inline virtual ~File(); 00187 00188 /** 00189 * @brief 00190 * Create file 00191 * 00192 * @param[in] bAlways 00193 * If 'true', the file is created (and therefore overwritten) if it already exists 00194 * 00195 * @return 00196 * 'true', if all went fine, else 'false' 00197 */ 00198 inline bool Create(bool bAlways = false); 00199 00200 /** 00201 * @brief 00202 * Delete file 00203 * 00204 * @return 00205 * 'true', if all went fine, else 'false' 00206 */ 00207 inline bool Delete(); 00208 00209 /** 00210 * @brief 00211 * Open the file for reading and/or writing 00212 * 00213 * @param[in] nAccess 00214 * Access mode, combination of 'EAccess' flags 00215 * @param[in] nStringFormat 00216 * String encoding format to use when dealing with string functions (not supported by all file implementations) 00217 * 00218 * @return 00219 * 'false' on error, else 'true' 00220 * 00221 * @note 00222 * - 'FileStandardInput', 'FileStandardOutput' and 'FileStandardError' are special access flags that can't 00223 * be combined with other flags and the filename must be empty 00224 * - It's not recommended to use Unicode by because internally wchar_t is used and this data type has not 00225 * the same size on every platform (use ASCII or UTF8 instead) 00226 */ 00227 PLCORE_API bool Open(uint32 nAccess, String::EFormat nStringFormat = String::ASCII); 00228 00229 /** 00230 * @brief 00231 * Close the file 00232 */ 00233 inline void Close(); 00234 00235 /** 00236 * @brief 00237 * Returns whether the file is memory buffered (opened with flag 'FileMemBuf') 00238 * 00239 * @return 00240 * 'true', if the file is memory buffered, else 'false' 00241 * 00242 * @see 00243 * - GetMemoryBuffer() 00244 */ 00245 inline bool IsMemoryBuffered() const; 00246 00247 /** 00248 * @brief 00249 * Returns whether the file is open 00250 * 00251 * @return 00252 * 'true', if the file is open, else 'false' 00253 */ 00254 inline bool IsOpen() const; 00255 00256 /** 00257 * @brief 00258 * Returns whether the file is readable 00259 * 00260 * @return 00261 * 'true', if the file can be read, else 'false' 00262 */ 00263 inline bool IsReadable() const; 00264 00265 /** 00266 * @brief 00267 * Returns whether the file is writable 00268 * 00269 * @return 00270 * 'true', if the file can be written, else 'false' 00271 */ 00272 inline bool IsWritable() const; 00273 00274 /** 00275 * @brief 00276 * Returns the string encoding format to use when dealing with string functions 00277 * 00278 * @return 00279 * String encoding format to use when dealing with string functions 00280 */ 00281 inline String::EFormat GetStringFormat() const; 00282 00283 /** 00284 * @brief 00285 * Returns whether end of file has been reached 00286 * 00287 * @return 00288 * 'true', if the end of the file has been reached, else 'false' 00289 */ 00290 inline bool IsEof() const; 00291 00292 /** 00293 * @brief 00294 * Reads a character 00295 * 00296 * @return 00297 * A character from file, < 0 if there was an error 00298 */ 00299 inline int GetC(); 00300 00301 /** 00302 * @brief 00303 * Writes a character 00304 * 00305 * @param[in] nChar 00306 * Character to write 00307 * 00308 * @return 00309 * 'true' if all went fine, else 'false' (maybe file is read only) 00310 */ 00311 inline bool PutC(int nChar); 00312 00313 /** 00314 * @brief 00315 * Reads a string 00316 * 00317 * @return 00318 * Read string 00319 */ 00320 inline String GetS(); 00321 00322 /** 00323 * @brief 00324 * Writes a string 00325 * 00326 * @param[in] sString 00327 * String which should be written into the file 00328 * 00329 * @return 00330 * Number of bytes written, 0 if nothing was written, < 0 if there was an error 00331 */ 00332 inline int PutS(const String &sString); 00333 00334 /** 00335 * @brief 00336 * Reads the given number of bytes 00337 * 00338 * @param[out] pBuffer 00339 * Buffer were the data should be copied in (MUST valid and large enough!) 00340 * @param[in] nSize 00341 * Item size in bytes 00342 * @param[in] nCount 00343 * Maximum number of items to be read 00344 * 00345 * @return 00346 * Number of fully read items, if != 'nCount' an error occurred 00347 */ 00348 inline uint32 Read(void *pBuffer, uint32 nSize, uint32 nCount); 00349 00350 /** 00351 * @brief 00352 * Writes the given number of bytes 00353 * 00354 * @param[in] pBuffer 00355 * Buffer with the data which should be written into the file (MUST valid and large enough!) 00356 * @param[in] nSize 00357 * Item size in bytes 00358 * @param[in] nCount 00359 * Maximum number of items to be written 00360 * 00361 * @return 00362 * Number of fully written items, if != 'nCount' an error occurred 00363 */ 00364 inline uint32 Write(const void *pBuffer, uint32 nSize, uint32 nCount); 00365 00366 /** 00367 * @brief 00368 * Flushes the file buffer 00369 * 00370 * @return 00371 * 'true' if all went fine, else 'false' 00372 * 00373 * @remarks 00374 * Writes down all temporarily buffered data. 00375 */ 00376 inline bool Flush(); 00377 00378 /** 00379 * @brief 00380 * Sets the starting position 00381 * 00382 * @param[in] nOffset 00383 * File offset in bytes relative to the given location 00384 * @param[in] nLocation 00385 * Location 00386 * 00387 * @return 00388 * 'true' if all went fine, else 'false' 00389 */ 00390 inline bool Seek(int32 nOffset = 0, ESeek nLocation = SeekSet); 00391 00392 /** 00393 * @brief 00394 * Gets the current position of the file pointer 00395 * 00396 * @return 00397 * The current byte position of the file pointer, < 0 if there was an error 00398 */ 00399 inline int32 Tell() const; 00400 00401 /** 00402 * @brief 00403 * Returns the file size 00404 * 00405 * @return 00406 * File size in bytes 00407 */ 00408 inline uint32 GetSize() const; 00409 00410 /** 00411 * @brief 00412 * Writes a string into the file 00413 * 00414 * @param[in] sString 00415 * String which should be written into the file 00416 * 00417 * @return 00418 * 'true' if all went fine, else 'false' 00419 */ 00420 inline bool Print(const String &sString); 00421 00422 /** 00423 * @brief 00424 * Writes a string and a newline into the file 00425 * 00426 * @param[in] sString 00427 * String which should be written into the file 00428 * 00429 * @return 00430 * 'true' if all went fine, else 'false' 00431 */ 00432 inline bool PrintLn(const String &sString); 00433 00434 /** 00435 * @brief 00436 * Returns a pointer to the buffer for memory buffered file 00437 * 00438 * @return 00439 * Pointer to the buffer for memory buffered file, a null pointer if there's no such buffer 00440 * 00441 * @see 00442 * - IsMemoryBuffered() 00443 */ 00444 inline const uint8 *GetMemoryBuffer() const; 00445 00446 /** 00447 * @brief 00448 * Returns the complete content of the file as string 00449 * 00450 * @return 00451 * The complete file content as string, empty string on error or if the file is just empty 00452 */ 00453 PLCORE_API String GetContentAsString(); 00454 00455 00456 //[-------------------------------------------------------] 00457 //[ Private functions ] 00458 //[-------------------------------------------------------] 00459 private: 00460 /** 00461 * @brief 00462 * Copy constructor 00463 * 00464 * @param[in] cSource 00465 * Source to copy from 00466 */ 00467 File(const File &cSource); 00468 00469 /** 00470 * @brief 00471 * Copy operator 00472 * 00473 * @param[in] cSource 00474 * Source to copy from 00475 * 00476 * @return 00477 * Reference to this instance 00478 */ 00479 File &operator =(const File &cSource); 00480 00481 /** 00482 * @brief 00483 * Open the file (for memory buffered files) 00484 * 00485 * @param[in] nAccess 00486 * Access mode (see EAccess) 00487 * @param[in] nStringFormat 00488 * String encoding format to use when dealing with string functions 00489 * 00490 * @return 00491 * 'false' on error, else 'true' 00492 */ 00493 bool MemBufOpen(uint32 nAccess, String::EFormat nStringFormat = String::ASCII); 00494 00495 /** 00496 * @brief 00497 * Close the file (for memory buffered files) 00498 */ 00499 PLCORE_API void MemBufClose(); 00500 00501 /** 00502 * @brief 00503 * Reads a character (for memory buffered files) 00504 * 00505 * @return 00506 * A character from file, < 0 if there was an error 00507 */ 00508 PLCORE_API int MemBufGetC(); 00509 00510 /** 00511 * @brief 00512 * Reads a string (for memory buffered files) 00513 * 00514 * @return 00515 * Read string 00516 */ 00517 PLCORE_API String MemBufGetS(); 00518 00519 /** 00520 * @brief 00521 * Reads the given number of bytes (for memory buffered files) 00522 * 00523 * @param[out] pBuffer 00524 * Buffer were the data should be copied in (MUST valid and large enough!) 00525 * @param[in] nSize 00526 * Item size in bytes 00527 * @param[in] nCount 00528 * Maximum number of items to be read 00529 * 00530 * @return 00531 * Number of fully read items, if != 'nCount' an error occurred 00532 */ 00533 PLCORE_API uint32 MemBufRead(void *pBuffer, uint32 nSize, uint32 nCount); 00534 00535 /** 00536 * @brief 00537 * Sets the starting position (for memory buffered files) 00538 * 00539 * @param[in] nOffset 00540 * File offset in bytes relative to the given location 00541 * @param[in] nLocation 00542 * Location 00543 * 00544 * @return 00545 * 'true' if all went fine, else 'false' 00546 */ 00547 PLCORE_API bool MemBufSeek(int32 nOffset, ESeek nLocation); 00548 00549 00550 //[-------------------------------------------------------] 00551 //[ Private definitions ] 00552 //[-------------------------------------------------------] 00553 private: 00554 /** 00555 * @brief 00556 * File internal access flags 00557 */ 00558 enum EInternalAccess { 00559 FileMemBufShared = 64 /**< Opened memory buffered is shared and is NOT allowed to be destroyed by this file object! */ 00560 }; 00561 00562 00563 //[-------------------------------------------------------] 00564 //[ Private data ] 00565 //[-------------------------------------------------------] 00566 private: 00567 uint8 *m_pMemBuf; /**< Buffer for memory buffered file, can be a null pointer */ 00568 uint32 m_nMemBufSize; /**< Size of memory buffered file */ 00569 uint32 m_nMemBufPos; /**< Seek position for memory buffered file */ 00570 uint32 m_nMemBufAccess; /**< Access flags for memory buffered file */ 00571 00572 00573 }; 00574 00575 00576 //[-------------------------------------------------------] 00577 //[ Namespace ] 00578 //[-------------------------------------------------------] 00579 } // PLCore 00580 00581 00582 //[-------------------------------------------------------] 00583 //[ Implementation ] 00584 //[-------------------------------------------------------] 00585 #include "PLCore/File/File.inl" 00586 00587 00588 #endif // __PLCORE_FILE_H__
|