00001 // 00002 // File.cpp 00003 // 00004 // $Id: //poco/1.3/Foundation/src/File.cpp#6 $ 00005 // 00006 // Library: Foundation 00007 // Package: Filesystem 00008 // Module: File 00009 // 00010 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. 00011 // and Contributors. 00012 // 00013 // Permission is hereby granted, free of charge, to any person or organization 00014 // obtaining a copy of the software and accompanying documentation covered by 00015 // this license (the "Software") to use, reproduce, display, distribute, 00016 // execute, and transmit the Software, and to prepare derivative works of the 00017 // Software, and to permit third-parties to whom the Software is furnished to 00018 // do so, all subject to the following: 00019 // 00020 // The copyright notices in the Software and this entire statement, including 00021 // the above license grant, this restriction and the following disclaimer, 00022 // must be included in all copies of the Software, in whole or in part, and 00023 // all derivative works of the Software, unless such copies or derivative 00024 // works are solely in the form of machine-executable object code generated by 00025 // a source language processor. 00026 // 00027 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00028 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00029 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 00030 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 00031 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 00032 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00033 // DEALINGS IN THE SOFTWARE. 00034 // 00035 00036 00037 #include "Poco/File.h" 00038 #include "Poco/Path.h" 00039 #include "Poco/DirectoryIterator.h" 00040 00041 00042 #if defined(POCO_OS_FAMILY_WINDOWS) && defined(POCO_WIN32_UTF8) 00043 #include "File_WIN32U.cpp" 00044 #elif defined(POCO_OS_FAMILY_WINDOWS) 00045 #include "File_WIN32.cpp" 00046 #elif defined(POCO_OS_FAMILY_UNIX) 00047 #include "File_UNIX.cpp" 00048 #else 00049 #include "File_VMS.cpp" 00050 #endif 00051 00052 00053 namespace Poco { 00054 00055 00056 File::File() 00057 { 00058 } 00059 00060 00061 File::File(const std::string& path): FileImpl(path) 00062 { 00063 } 00064 00065 00066 File::File(const char* path): FileImpl(std::string(path)) 00067 { 00068 } 00069 00070 00071 File::File(const Path& path): FileImpl(path.toString()) 00072 { 00073 } 00074 00075 00076 File::File(const File& file): FileImpl(file.getPathImpl()) 00077 { 00078 } 00079 00080 00081 File::~File() 00082 { 00083 } 00084 00085 00086 File& File::operator = (const File& file) 00087 { 00088 setPathImpl(file.getPathImpl()); 00089 return *this; 00090 } 00091 00092 00093 File& File::operator = (const std::string& path) 00094 { 00095 setPathImpl(path); 00096 return *this; 00097 } 00098 00099 00100 File& File::operator = (const char* path) 00101 { 00102 poco_check_ptr (path); 00103 setPathImpl(path); 00104 return *this; 00105 } 00106 00107 00108 File& File::operator = (const Path& path) 00109 { 00110 setPathImpl(path.toString()); 00111 return *this; 00112 } 00113 00114 00115 void File::swap(File& file) 00116 { 00117 swapImpl(file); 00118 } 00119 00120 00121 bool File::exists() const 00122 { 00123 return existsImpl(); 00124 } 00125 00126 00127 bool File::canRead() const 00128 { 00129 return canReadImpl(); 00130 } 00131 00132 00133 bool File::canWrite() const 00134 { 00135 return canWriteImpl(); 00136 } 00137 00138 00139 bool File::canExecute() const 00140 { 00141 return canExecuteImpl(); 00142 } 00143 00144 00145 bool File::isFile() const 00146 { 00147 return isFileImpl(); 00148 } 00149 00150 00151 bool File::isDirectory() const 00152 { 00153 return isDirectoryImpl(); 00154 } 00155 00156 00157 bool File::isLink() const 00158 { 00159 return isLinkImpl(); 00160 } 00161 00162 00163 bool File::isDevice() const 00164 { 00165 return isDeviceImpl(); 00166 } 00167 00168 00169 bool File::isHidden() const 00170 { 00171 return isHiddenImpl(); 00172 } 00173 00174 00175 Timestamp File::created() const 00176 { 00177 return createdImpl(); 00178 } 00179 00180 00181 Timestamp File::getLastModified() const 00182 { 00183 return getLastModifiedImpl(); 00184 } 00185 00186 00187 void File::setLastModified(const Timestamp& ts) 00188 { 00189 setLastModifiedImpl(ts); 00190 } 00191 00192 00193 File::FileSize File::getSize() const 00194 { 00195 return getSizeImpl(); 00196 } 00197 00198 00199 void File::setSize(FileSizeImpl size) 00200 { 00201 setSizeImpl(size); 00202 } 00203 00204 00205 void File::setWriteable(bool flag) 00206 { 00207 setWriteableImpl(flag); 00208 } 00209 00210 00211 void File::setReadOnly(bool flag) 00212 { 00213 setWriteableImpl(!flag); 00214 } 00215 00216 00217 void File::setExecutable(bool flag) 00218 { 00219 setExecutableImpl(flag); 00220 } 00221 00222 00223 void File::copyTo(const std::string& path) const 00224 { 00225 Path src(getPathImpl()); 00226 Path dest(path); 00227 File destFile(path); 00228 if ((destFile.exists() && destFile.isDirectory()) || dest.isDirectory()) 00229 { 00230 dest.makeDirectory(); 00231 dest.setFileName(src.getFileName()); 00232 } 00233 if (isDirectory()) 00234 copyDirectory(dest.toString()); 00235 else 00236 copyToImpl(dest.toString()); 00237 } 00238 00239 00240 void File::copyDirectory(const std::string& path) const 00241 { 00242 File target(path); 00243 target.createDirectories(); 00244 00245 Path src(getPathImpl()); 00246 src.makeFile(); 00247 DirectoryIterator it(src); 00248 DirectoryIterator end; 00249 for (; it != end; ++it) 00250 { 00251 it->copyTo(path); 00252 } 00253 } 00254 00255 00256 void File::moveTo(const std::string& path) 00257 { 00258 copyTo(path); 00259 remove(true); 00260 setPathImpl(path); 00261 } 00262 00263 00264 void File::renameTo(const std::string& path) 00265 { 00266 renameToImpl(path); 00267 setPathImpl(path); 00268 } 00269 00270 00271 void File::remove(bool recursive) 00272 { 00273 if (recursive && !isLink() && isDirectory()) 00274 { 00275 std::vector<File> files; 00276 list(files); 00277 for (std::vector<File>::iterator it = files.begin(); it != files.end(); ++it) 00278 { 00279 it->remove(true); 00280 } 00281 } 00282 removeImpl(); 00283 } 00284 00285 00286 bool File::createFile() 00287 { 00288 return createFileImpl(); 00289 } 00290 00291 00292 bool File::createDirectory() 00293 { 00294 return createDirectoryImpl(); 00295 } 00296 00297 00298 void File::createDirectories() 00299 { 00300 if (!exists()) 00301 { 00302 Path p(getPathImpl()); 00303 p.makeDirectory(); 00304 if (p.depth() > 1) 00305 { 00306 p.makeParent(); 00307 File f(p); 00308 f.createDirectories(); 00309 } 00310 createDirectoryImpl(); 00311 } 00312 } 00313 00314 00315 void File::list(std::vector<std::string>& files) const 00316 { 00317 files.clear(); 00318 DirectoryIterator it(*this); 00319 DirectoryIterator end; 00320 while (it != end) 00321 { 00322 files.push_back(it.name()); 00323 ++it; 00324 } 00325 } 00326 00327 00328 void File::list(std::vector<File>& files) const 00329 { 00330 files.clear(); 00331 DirectoryIterator it(*this); 00332 DirectoryIterator end; 00333 while (it != end) 00334 { 00335 files.push_back(*it); 00336 ++it; 00337 } 00338 } 00339 00340 00341 void File::handleLastError(const std::string& path) 00342 { 00343 handleLastErrorImpl(path); 00344 } 00345 00346 00347 } // namespace Poco