mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-26 10:32:56 +01:00
initial import
This commit is contained in:
295
Foundation/src/File.cpp
Normal file
295
Foundation/src/File.cpp
Normal file
@@ -0,0 +1,295 @@
|
||||
//
|
||||
// File.cpp
|
||||
//
|
||||
// $Id: //poco/1.1.0/Foundation/src/File.cpp#2 $
|
||||
//
|
||||
// Library: Foundation
|
||||
// Package: Filesystem
|
||||
// Module: File
|
||||
//
|
||||
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
|
||||
// and Contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
//
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
|
||||
#include "Foundation/File.h"
|
||||
#include "Foundation/Path.h"
|
||||
#include "Foundation/DirectoryIterator.h"
|
||||
|
||||
|
||||
#if defined(POCO_OS_FAMILY_WINDOWS) && defined(POCO_WIN32_UTF8)
|
||||
#include "File_WIN32U.cpp"
|
||||
#elif defined(POCO_OS_FAMILY_WINDOWS)
|
||||
#include "File_WIN32.cpp"
|
||||
#elif defined(POCO_OS_FAMILY_UNIX)
|
||||
#include "File_UNIX.cpp"
|
||||
#else
|
||||
#include "File_VMS.cpp"
|
||||
#endif
|
||||
|
||||
|
||||
Foundation_BEGIN
|
||||
|
||||
|
||||
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::isFile() const
|
||||
{
|
||||
return isFileImpl();
|
||||
}
|
||||
|
||||
|
||||
bool File::isDirectory() const
|
||||
{
|
||||
return isDirectoryImpl();
|
||||
}
|
||||
|
||||
|
||||
Timestamp File::created() const
|
||||
{
|
||||
return createdImpl();
|
||||
}
|
||||
|
||||
|
||||
Timestamp File::getLastModified() const
|
||||
{
|
||||
return getLastModifiedImpl();
|
||||
}
|
||||
|
||||
|
||||
void File::setLastModified(const Timestamp& ts)
|
||||
{
|
||||
setLastModifiedImpl(ts);
|
||||
}
|
||||
|
||||
|
||||
File::FileSize File::getSize() const
|
||||
{
|
||||
return getSizeImpl();
|
||||
}
|
||||
|
||||
|
||||
void File::setSize(FileSizeImpl size)
|
||||
{
|
||||
setSizeImpl(size);
|
||||
}
|
||||
|
||||
|
||||
void File::setWriteable(bool flag)
|
||||
{
|
||||
setWriteableImpl(flag);
|
||||
}
|
||||
|
||||
|
||||
void File::setReadOnly(bool flag)
|
||||
{
|
||||
setWriteableImpl(!flag);
|
||||
}
|
||||
|
||||
|
||||
void File::copyTo(const std::string& path) const
|
||||
{
|
||||
Path src(getPathImpl());
|
||||
File srcFile(src);
|
||||
if (srcFile.isDirectory())
|
||||
throw OpenFileException("cannot copy directory", src.toString());
|
||||
|
||||
Path dest(path);
|
||||
if (dest.isDirectory())
|
||||
{
|
||||
dest.makeDirectory();
|
||||
dest.setFileName(src.getFileName());
|
||||
}
|
||||
copyToImpl(dest.toString());
|
||||
}
|
||||
|
||||
|
||||
void File::moveTo(const std::string& path)
|
||||
{
|
||||
copyToImpl(path);
|
||||
removeImpl();
|
||||
setPathImpl(path);
|
||||
}
|
||||
|
||||
|
||||
void File::renameTo(const std::string& path)
|
||||
{
|
||||
renameToImpl(path);
|
||||
setPathImpl(path);
|
||||
}
|
||||
|
||||
|
||||
void File::remove(bool recursive)
|
||||
{
|
||||
if (recursive && 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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Foundation_END
|
||||
Reference in New Issue
Block a user