PixelLightAPI  .
FileObject.h
Go to the documentation of this file.
00001 /*********************************************************\
00002  *  File: FileObject.h                                   *
00003  *
00004  *  Copyright (C) 2002-2011 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_FILEOBJECT_H__
00024 #define __PLCORE_FILEOBJECT_H__
00025 #pragma once
00026 
00027 
00028 //[-------------------------------------------------------]
00029 //[ Includes                                              ]
00030 //[-------------------------------------------------------]
00031 #include "PLCore/File/StdIo.h"
00032 #include "PLCore/File/Url.h"
00033 
00034 
00035 //[-------------------------------------------------------]
00036 //[ Namespace                                             ]
00037 //[-------------------------------------------------------]
00038 namespace PLCore {
00039 
00040 
00041 //[-------------------------------------------------------]
00042 //[ Forward declarations                                  ]
00043 //[-------------------------------------------------------]
00044 class FileImpl;
00045 class FileAccess;
00046 
00047 
00048 //[-------------------------------------------------------]
00049 //[ Classes                                               ]
00050 //[-------------------------------------------------------]
00051 /**
00052 *  @brief
00053 *    Base class for files and directories
00054 *
00055 *  @remarks
00056 *    When working with archive file objects, you have to differ whether you want to use the
00057 *    archive files as 'file' or whether you want do use it as 'directory'. Here some examples:
00058 *      FileObject("test.zip") .IsFile()      -> true
00059 *      FileObject("test.zip") .IsDirectory() -> false
00060 *      FileObject("test.zip/").IsFile()      -> false
00061 *      FileObject("test.zip/").IsDirectory() -> true
00062 *
00063 *  @note
00064 *    - Implementation of the state design pattern, this class is the context
00065 */
00066 class FileObject {
00067 
00068 
00069     //[-------------------------------------------------------]
00070     //[ Public functions                                      ]
00071     //[-------------------------------------------------------]
00072     public:
00073         /**
00074         *  @brief
00075         *    Constructor
00076         */
00077         inline FileObject();
00078 
00079         /**
00080         *  @brief
00081         *    Constructor
00082         *
00083         *  @param[in] sUrl
00084         *    URL of the file or directory
00085         *  @param[in] pAccess
00086         *    Additional file access information (can be a null pointer)
00087         */
00088         inline FileObject(const String &sUrl, const FileAccess *pAccess = nullptr);
00089 
00090         /**
00091         *  @brief
00092         *    Constructor
00093         *
00094         *  @param[in] cUrl
00095         *    URL of the file or directory
00096         *  @param[in] pAccess
00097         *    Additional file access information (can be a null pointer)
00098         */
00099         inline FileObject(const Url &cUrl, const FileAccess *pAccess = nullptr);
00100 
00101         /**
00102         *  @brief
00103         *    Destructor
00104         *
00105         *  @note
00106         *    - The file object is closed automatically
00107         */
00108         inline virtual ~FileObject();
00109 
00110         /**
00111         *  @brief
00112         *    Assign a new URL
00113         *
00114         *  @param[in] sUrl
00115         *    URL of the file or directory
00116         *  @param[in] pAccess
00117         *    Additional file access information (can be a null pointer)
00118         */
00119         inline void Assign(const String &sUrl, const FileAccess *pAccess = nullptr);
00120 
00121         /**
00122         *  @brief
00123         *    Assign a new URL
00124         *
00125         *  @param[in] cUrl
00126         *    URL of the file or directory
00127         *  @param[in] pAccess
00128         *    Additional file access information (can be a null pointer)
00129         */
00130         PLCORE_API void Assign(const Url &cUrl, const FileAccess *pAccess = nullptr);
00131 
00132         /**
00133         *  @brief
00134         *    Assign a new file
00135         *
00136         *  @param[in] pFile
00137         *    Standard stream pointer
00138         *  @param[in] nAccess
00139         *    Access flags that were used to open the stream
00140         */
00141         PLCORE_API void Assign(FILE *pFile, uint32 nAccess);
00142 
00143         /**
00144         *  @brief
00145         *    Assign a new file
00146         *
00147         *  @param[in] hFile
00148         *    OS file handle
00149         *
00150         *  @note
00151         *    - On Linux, the handle is a file handle of type int.
00152         *    - On Windows, the handle is a file handle of type HANDLE.
00153         *      Do not expect an int (fileno) here! If you need it to work with
00154         *      low level io functions, you need to convert the HANDLE to a
00155         *      file handle by yourself (see _open_osfhandle).
00156         */
00157         PLCORE_API void Assign(handle hFile);
00158 
00159         /**
00160         *  @brief
00161         *    Get URL
00162         *
00163         *  @return
00164         *    URL of the file
00165         *
00166         *  @note
00167         *    - The URL of a 'FileObject' always contains a filename that never ends with "/"! (so, you can safely
00168         *      use 'Url::GetFilename()' to get the name of any file or directory)
00169         *    - e.g.: 'Directory("C:\\test")' and 'Directory("C:\\test\\")' are equal, but
00170         *      'GetUrl()' will always return "C:/test".
00171         */
00172         inline const Url &GetUrl() const;
00173 
00174         /**
00175         *  @brief
00176         *    Check if the file or directory exists
00177         *
00178         *  @return
00179         *    'true', if the object is existing, else 'false'
00180         */
00181         inline bool Exists() const;
00182 
00183         /**
00184         *  @brief
00185         *    Returns if the object is a regular file
00186         *
00187         *  @return
00188         *    'true', if the object is a file, else 'false'
00189         */
00190         inline bool IsFile() const;
00191 
00192         /**
00193         *  @brief
00194         *    Returns if the object is a directory
00195         *
00196         *  @return
00197         *    'true', if the object is a directory, else 'false'
00198         */
00199         inline bool IsDirectory() const;
00200 
00201         /**
00202         *  @brief
00203         *    Copy the file or directory to a new location
00204         *
00205         *  @param[in] sDestination
00206         *    URL of the destination
00207         *  @param[in] bOverwrite
00208         *    Shall the file be overwritten if it already exists?
00209         *
00210         *  @return
00211         *    'true', if all went fine, else 'false'
00212         */
00213         inline bool Copy(const String &sDestination, bool bOverwrite = false) const;
00214 
00215         /**
00216         *  @brief
00217         *    Move the file or directory to a new location
00218         *
00219         *  @param[in] sDestination
00220         *    URL of the destination
00221         *
00222         *  @return
00223         *    'true', if all went fine, else 'false'
00224         */
00225         inline bool Move(const String &sDestination);
00226 
00227         /**
00228         *  @brief
00229         *    Rename the file or directory
00230         *
00231         *  @param[in] sName
00232         *    New filename (filename only, no path)
00233         *
00234         *  @return
00235         *    'true', if all went fine, else 'false'
00236         */
00237         inline bool Rename(const String &sName);
00238 
00239         /**
00240         *  @brief
00241         *    Close the file or directory
00242         */
00243         inline void Close();
00244 
00245 
00246     //[-------------------------------------------------------]
00247     //[ Protected functions                                   ]
00248     //[-------------------------------------------------------]
00249     protected:
00250         /**
00251         *  @brief
00252         *    Constructor for a standard OS stream
00253         *
00254         *  @param[in] pFile
00255         *    Standard stream pointer
00256         *  @param[in] nAccess
00257         *    Access flags that were used to open the stream
00258         */
00259         PLCORE_API FileObject(FILE *pFile, uint32 nAccess);
00260 
00261         /**
00262         *  @brief
00263         *    Constructor for a OS file handle
00264         *
00265         *  @param[in] hFile
00266         *    OS file handle
00267         */
00268         PLCORE_API FileObject(handle hFile);
00269 
00270         /**
00271         *  @brief
00272         *    Generic implementation of 'Copy()' (if system-implementation does not work)
00273         *
00274         *  @param[in] sDestination
00275         *    URL of the destination
00276         *  @param[in] bOverwrite
00277         *    Shall the file be overwritten if it already exists?
00278         *
00279         *  @return
00280         *    'true', if all went fine, else 'false'
00281         */
00282         PLCORE_API bool GenericCopy(const String &sDestination, bool bOverwrite = false) const;
00283 
00284         /**
00285         *  @brief
00286         *    Generic implementation of 'Move()' (if system-implementation does not work)
00287         *
00288         *  @param[in] sDestination
00289         *    URL of the destination
00290         *
00291         *  @return
00292         *    'true', if all went fine, else 'false'
00293         */
00294         PLCORE_API bool GenericMove(const String &sDestination);
00295 
00296 
00297     //[-------------------------------------------------------]
00298     //[ Private functions                                     ]
00299     //[-------------------------------------------------------]
00300     private:
00301         /**
00302         *  @brief
00303         *    Copy constructor
00304         *
00305         *  @param[in] cSource
00306         *    Source to copy from
00307         */
00308         FileObject(const FileObject &cSource);
00309 
00310         /**
00311         *  @brief
00312         *    Copy operator
00313         *
00314         *  @param[in] cSource
00315         *    Source to copy from
00316         *
00317         *  @return
00318         *    Reference to this instance
00319         */
00320         FileObject &operator =(const FileObject &cSource);
00321 
00322 
00323     //[-------------------------------------------------------]
00324     //[ Protected data                                        ]
00325     //[-------------------------------------------------------]
00326     protected:
00327         FileImpl *m_pFileImpl;  /**< Platform implementation for file object (can be a null pointer) */
00328         Url       m_cNullUrl;   /**< Empty URL */
00329 
00330 
00331 };
00332 
00333 
00334 //[-------------------------------------------------------]
00335 //[ Namespace                                             ]
00336 //[-------------------------------------------------------]
00337 } // PLCore
00338 
00339 
00340 //[-------------------------------------------------------]
00341 //[ Implementation                                        ]
00342 //[-------------------------------------------------------]
00343 #include "PLCore/File/FileObject.inl"
00344 
00345 
00346 #endif // __PLCORE_FILEOBJECT_H__


PixelLight PixelLight 0.9.10-R1
Copyright (C) 2002-2011 by The PixelLight Team
Last modified Fri Dec 23 2011 15:50:53
The content of this PixelLight document is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported