00001 /******************************************************************************* 00002 00003 Copyright (C) 2007 by Brendon Costa 00004 00005 This library is free software; you can redistribute it and/or modify 00006 it under the terms of the "LGPL Like" License defined in the file COPYING 00007 that should have been distributed along with this source. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00012 00013 You should have received a copy of the "LGPL Like" License 00014 along with this library; see the file COPYING. if not, it can be 00015 obtained from the EDoc++ website: 00016 http://edoc.sourceforge.net/license.html 00017 00018 *******************************************************************************/ 00019 #ifndef EDOC_MANAGEDFILE_H 00020 #define EDOC_MANAGEDFILE_H 00021 00022 #include <string> 00023 #include <list> 00024 00025 namespace EDoc 00026 { 00027 class ManagedFileCleanupIFace; 00028 00029 //=========================================================================== 00030 /** \brief Represents a file that can have some sort of cleanup action 00031 * associated with it when it is no longer being used. 00032 * 00033 * This implements an internal reference counting pattern where when no more 00034 * copies of this managed file exist it will perform a cleanup action. This 00035 * cleanup action is done on calling the virtual destructor of: 00036 * ManagedFileCleanupIFace::~ManagedFileCleanupIFace() a derived class for 00037 * ManagedFileCleanupIFace will implement various actions that need to be 00038 * performed in order to cleanup the file correctly. This may include: Do 00039 * nothing. Delete temporary file, Write temporary file into archive before 00040 * delete. 00041 */ 00042 class ManagedFile 00043 { 00044 public: 00045 00046 //------------------------------------------------------------------------ 00047 /** \brief Create a new managed file that is identified by the given 00048 * list of archives. 00049 * 00050 * If the list contains just a single member then that is a direct 00051 * filename, otheriwse the list contains the identifiers for recursive 00052 * archives. For example if this was managing a file called Blah.edc 00053 * inside a zipfile call foo.zip which itself was part of another zip 00054 * file bar.zip you would pass the following: 00055 * -# bar.zip 00056 * -# foo.zip 00057 * -# Blah.edc 00058 */ 00059 explicit ManagedFile(std::list<std::string> identifier_in); 00060 00061 //------------------------------------------------------------------------ 00062 /** \brief Create a new managed file for the given filename. 00063 * 00064 * This is just a wrapper around the above constructor. 00065 */ 00066 explicit ManagedFile(std::string identifier_in); 00067 00068 //------------------------------------------------------------------------ 00069 /** \brief Create a new managed file that is operating on a given 00070 * temporary file and has a given cleanup callback that needs to be 00071 * called when we are finsihed using it. 00072 */ 00073 ManagedFile(std::list<std::string> identifier_in, 00074 std::string work_file_name_in, 00075 ManagedFileCleanupIFace* cleanup_in); 00076 00077 //------------------------------------------------------------------------ 00078 /** \brief Copy constructor. 00079 */ 00080 ManagedFile(const ManagedFile& right); 00081 00082 //------------------------------------------------------------------------ 00083 /** \brief Assignment operator. 00084 */ 00085 ManagedFile& operator=(const ManagedFile& right); 00086 00087 //------------------------------------------------------------------------ 00088 /** \brief Destructor. 00089 * 00090 * If this is the last reference to the particular file being managed 00091 * then it will perform any cleanup operations that may have been 00092 * associated with the file. For example this could include adding it 00093 * to a zip archive or deleteing a temporary file etc. 00094 */ 00095 virtual ~ManagedFile(); 00096 00097 //------------------------------------------------------------------------ 00098 /** \brief Retruns the name of the file that is to be used for 00099 * reading/writing operations. 00100 * 00101 * This will often be a temporary file which will later be merged 00102 * into an archive. 00103 */ 00104 const std::string& GetWorkFileName() const 00105 { 00106 if (work_file_name.size()) 00107 { 00108 return work_file_name; 00109 } 00110 else 00111 { 00112 return identifier.back(); 00113 } 00114 } 00115 00116 //------------------------------------------------------------------------ 00117 /** \brief Returns the identifier for this file. 00118 */ 00119 const std::list<std::string>& GetIdentifier() const 00120 { 00121 return identifier; 00122 } 00123 00124 //------------------------------------------------------------------------ 00125 /** \brief Returns the name of the final file this represents without 00126 * any archive context. 00127 */ 00128 const std::string& GetName() const 00129 { 00130 return identifier.back(); 00131 } 00132 00133 //------------------------------------------------------------------------ 00134 /** \brief Returns a string that represents the name of the file 00135 * including its archive context. This is used for displaying data ot a 00136 * user. 00137 */ 00138 std::string GetFileNameRep() const; 00139 00140 //------------------------------------------------------------------------ 00141 00142 private: 00143 00144 //------------------------------------------------------------------------ 00145 /** \brief Increment the reference count to the cleanup instance. 00146 */ 00147 void IncRef(); 00148 00149 //------------------------------------------------------------------------ 00150 /** \brief Decrement the reference count to the cleanup instance and 00151 * perform any cleanup operations if necessary when the reference 00152 * count reaches 0 00153 */ 00154 void DecRef(); 00155 00156 //------------------------------------------------------------------------ 00157 00158 00159 00160 //------------------------------------------------------------------------ 00161 //------------------------------------------------------------------------ 00162 /** \brief Reference to object that implementes operations to perform to 00163 * close/cleanup the file. 00164 */ 00165 ManagedFileCleanupIFace* cleanup; 00166 00167 //------------------------------------------------------------------------ 00168 /** \brief Reference to a temporary file that all work is to be 00169 * performed on. 00170 */ 00171 std::string work_file_name; 00172 00173 //------------------------------------------------------------------------ 00174 /** \brief List of identifiers used to identify this file within an 00175 * abrbitrary list of archives. 00176 */ 00177 std::list<std::string> identifier; 00178 }; 00179 //=========================================================================== 00180 typedef std::list<ManagedFile> MngdFileList; 00181 } 00182 00183 #endif // EDOC_MANAGEDFILE_H