| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- //
- // File.cpp
- //
- // $Id: //poco/1.4/Foundation/src/File.cpp#3 $
- //
- // Library: Foundation
- // Package: Filesystem
- // Module: File
- //
- // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
- // and Contributors.
- //
- // SPDX-License-Identifier: BSL-1.0
- //
- #include "Poco/File.h"
- #include "Poco/Path.h"
- #include "Poco/DirectoryIterator.h"
- #if defined(POCO_OS_FAMILY_WINDOWS) && defined(POCO_WIN32_UTF8)
- #if defined(_WIN32_WCE)
- #include "File_WINCE.cpp"
- #else
- #include "File_WIN32U.cpp"
- #endif
- #elif defined(POCO_OS_FAMILY_WINDOWS)
- #include "File_WIN32.cpp"
- #elif defined(POCO_VXWORKS)
- #include "File_VX.cpp"
- #elif defined(POCO_OS_FAMILY_UNIX)
- #include "File_UNIX.cpp"
- #else
- #include "File_VMS.cpp"
- #endif
- namespace Poco {
- File::File()
- {
- }
- File::File(const std::string& path): FileImpl(path)
- {
- }
- File::File(const char* path): FileImpl(std::string(path))
- {
- }
- File::File(const Path& path): FileImpl(path.toString())
- {
- }
- File::File(const File& file): FileImpl(file.getPathImpl())
- {
- }
- File::~File()
- {
- }
- File& File::operator = (const File& file)
- {
- setPathImpl(file.getPathImpl());
- return *this;
- }
- File& File::operator = (const std::string& path)
- {
- setPathImpl(path);
- return *this;
- }
- File& File::operator = (const char* path)
- {
- poco_check_ptr (path);
- setPathImpl(path);
- return *this;
- }
- File& File::operator = (const Path& path)
- {
- setPathImpl(path.toString());
- return *this;
- }
- void File::swap(File& file)
- {
- swapImpl(file);
- }
- bool File::exists() const
- {
- return existsImpl();
- }
-
- bool File::canRead() const
- {
- return canReadImpl();
- }
-
- bool File::canWrite() const
- {
- return canWriteImpl();
- }
- bool File::canExecute() const
- {
- return canExecuteImpl();
- }
- bool File::isFile() const
- {
- return isFileImpl();
- }
-
- bool File::isDirectory() const
- {
- return isDirectoryImpl();
- }
- bool File::isLink() const
- {
- return isLinkImpl();
- }
- bool File::isDevice() const
- {
- return isDeviceImpl();
- }
- bool File::isHidden() const
- {
- return isHiddenImpl();
- }
- Timestamp File::created() const
- {
- return createdImpl();
- }
-
- Timestamp File::getLastModified() const
- {
- return getLastModifiedImpl();
- }
-
- File& File::setLastModified(const Timestamp& ts)
- {
- setLastModifiedImpl(ts);
- return *this;
- }
-
- File::FileSize File::getSize() const
- {
- return getSizeImpl();
- }
-
- File& File::setSize(FileSizeImpl size)
- {
- setSizeImpl(size);
- return *this;
- }
-
- File& File::setWriteable(bool flag)
- {
- setWriteableImpl(flag);
- return *this;
- }
- File& File::setReadOnly(bool flag)
- {
- setWriteableImpl(!flag);
- return *this;
- }
- File& File::setExecutable(bool flag)
- {
- setExecutableImpl(flag);
- return *this;
- }
-
- void File::copyTo(const std::string& path) const
- {
- Path src(getPathImpl());
- Path dest(path);
- File destFile(path);
- if ((destFile.exists() && destFile.isDirectory()) || dest.isDirectory())
- {
- dest.makeDirectory();
- dest.setFileName(src.getFileName());
- }
- if (isDirectory())
- copyDirectory(dest.toString());
- else
- copyToImpl(dest.toString());
- }
- void File::copyDirectory(const std::string& path) const
- {
- File target(path);
- target.createDirectories();
- Path src(getPathImpl());
- src.makeFile();
- DirectoryIterator it(src);
- DirectoryIterator end;
- for (; it != end; ++it)
- {
- it->copyTo(path);
- }
- }
- void File::moveTo(const std::string& path)
- {
- copyTo(path);
- remove(true);
- setPathImpl(path);
- }
-
- void File::renameTo(const std::string& path)
- {
- renameToImpl(path);
- setPathImpl(path);
- }
-
- void File::remove(bool recursive)
- {
- if (recursive && !isLink() && isDirectory())
- {
- std::vector<File> files;
- list(files);
- for (std::vector<File>::iterator it = files.begin(); it != files.end(); ++it)
- {
- it->remove(true);
- }
- }
- removeImpl();
- }
- bool File::createFile()
- {
- return createFileImpl();
- }
- bool File::createDirectory()
- {
- return createDirectoryImpl();
- }
- void File::createDirectories()
- {
- if (!exists())
- {
- Path p(getPathImpl());
- p.makeDirectory();
- if (p.depth() > 1)
- {
- p.makeParent();
- File f(p);
- f.createDirectories();
- }
- createDirectoryImpl();
- }
- }
- void File::list(std::vector<std::string>& files) const
- {
- files.clear();
- DirectoryIterator it(*this);
- DirectoryIterator end;
- while (it != end)
- {
- files.push_back(it.name());
- ++it;
- }
- }
- void File::list(std::vector<File>& files) const
- {
- files.clear();
- DirectoryIterator it(*this);
- DirectoryIterator end;
- while (it != end)
- {
- files.push_back(*it);
- ++it;
- }
- }
- void File::handleLastError(const std::string& path)
- {
- handleLastErrorImpl(path);
- }
- } // namespace Poco
|