remove filesystem::file_attrib() function

This commit is contained in:
Jinhao
2015-12-30 01:11:07 +08:00
parent 48254b6555
commit 0a396c12c2
7 changed files with 55 additions and 242 deletions

View File

@@ -281,13 +281,39 @@ namespace nana { namespace experimental {
#endif
}//end namespace detail
bool not_found_error(int errval)
{
#if defined(NANA_WINDOWS)
switch (errval)
{
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
case ERROR_INVALID_NAME:
case ERROR_INVALID_DRIVE:
case ERROR_NOT_READY:
case ERROR_INVALID_PARAMETER:
case ERROR_BAD_PATHNAME:
case ERROR_BAD_NETPATH:
return true;
}
return false;
#elif defined(NANA_POSIX)
return (errval == ENOENT || errval == ENOTDIR);
#else
static_assert(false, "Only Windows and Unix are supported now (Mac OS is experimental)");
#endif
}
file_status status(const path& p)
{
#if defined(NANA_WINDOWS)
auto attr = ::GetFileAttributesW(p.c_str());
if(INVALID_FILE_ATTRIBUTES == attr)
return file_status{file_type::unknown};
if (INVALID_FILE_ATTRIBUTES == attr)
{
if (not_found_error(static_cast<int>(::GetLastError())))
return file_status{file_type::not_found};
return file_status{ file_type::unknown };
}
return file_status{(FILE_ATTRIBUTE_DIRECTORY & attr) ? file_type::directory : file_type::regular, perms::all};
#elif defined(NANA_POSIX)
struct stat path_stat;
@@ -326,33 +352,6 @@ namespace nana { namespace experimental {
#endif
}
bool file_attrib(const nana::string& file, attribute& attr)
{
#if defined(NANA_WINDOWS)
WIN32_FILE_ATTRIBUTE_DATA fad;
if (::GetFileAttributesEx(file.c_str(), GetFileExInfoStandard, &fad))
{
LARGE_INTEGER li;
li.u.LowPart = fad.nFileSizeLow;
li.u.HighPart = fad.nFileSizeHigh;
attr.size = li.QuadPart;
attr.directory = (0 != (fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
detail::filetime_to_c_tm(fad.ftLastWriteTime, attr.modified);
return true;
}
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
struct stat fst;
if (0 == ::stat(static_cast<std::string>(nana::charset(file)).c_str(), &fst))
{
attr.size = fst.st_size;
attr.directory = (0 != (040000 & fst.st_mode));
attr.modified = *(::localtime(&fst.st_ctime));
return true;
}
#endif
return false;
}
bool is_directory(const path& p)
{
return (status(p).type() == file_type::directory);
@@ -396,6 +395,7 @@ namespace nana { namespace experimental {
#endif
}
bool modified_file_time(const std::wstring& file, struct tm& t)
{
#if defined(NANA_WINDOWS)
@@ -440,59 +440,6 @@ namespace nana { namespace experimental {
#endif
}
/*
bool create_directory(const std::wstring& path, bool & if_exist) //deprecated
{
if_exist = false;
if (path.size() == 0) return false;
std::wstring root;
#if defined(NANA_WINDOWS)
if (path.size() > 3 && path[1] == L':')
root = path.substr(0, 3);
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
if (path[0] == L'/')
root = '/';
#endif
bool mkstat = false;
std::size_t beg = root.size();
while (true)
{
beg = path.find_first_not_of(L"/\\", beg);
if (beg == path.npos)
break;
std::size_t pos = path.find_first_of(L"/\\", beg + 1);
if (pos != path.npos)
{
root += path.substr(beg, pos - beg);
mkstat = detail::mkdir_helper(root, if_exist);
if (mkstat == false && if_exist == false)
return false;
#if defined(NANA_WINDOWS)
root += L'\\';
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
root += L'/';
#endif
}
else
{
if (beg + 1 < path.size())
{
root += path.substr(beg);
mkstat = detail::mkdir_helper(root, if_exist);
}
break;
}
beg = pos + 1;
}
return mkstat;
}
*/
bool rmfile(const path& p)
{
if(p.empty())

View File

@@ -192,73 +192,6 @@ namespace filesystem
#endif
}//end namespace detail
bool file_attrib(const std::string& file, attribute& attr)
{
throw_not_utf8(file);
#if defined(NANA_WINDOWS)
WIN32_FILE_ATTRIBUTE_DATA fad;
if(::GetFileAttributesEx(utf8_cast(file).c_str(), GetFileExInfoStandard, &fad))
{
LARGE_INTEGER li;
li.u.LowPart = fad.nFileSizeLow;
li.u.HighPart = fad.nFileSizeHigh;
attr.bytes = li.QuadPart;
attr.is_directory = (0 != (fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
detail::filetime_to_c_tm(fad.ftLastWriteTime, attr.modified);
return true;
}
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
struct stat fst;
if(0 == ::stat(file.c_str(), &fst))
{
attr.bytes = fst.st_size;
attr.is_directory = (0 != (040000 & fst.st_mode));
attr.modified = *(::localtime(&fst.st_ctime));
return true;
}
#endif
return false;
}
/*
long long filesize(const std::string& file) //deprecated
{
#if defined(NANA_WINDOWS)
//Some compilation environment may fail to link to GetFileSizeEx
typedef BOOL (__stdcall *GetFileSizeEx_fptr_t)(HANDLE, PLARGE_INTEGER);
GetFileSizeEx_fptr_t get_file_size_ex = reinterpret_cast<GetFileSizeEx_fptr_t>(::GetProcAddress(::GetModuleHandleA("Kernel32.DLL"), "GetFileSizeEx"));
if(get_file_size_ex)
{
HANDLE handle = ::CreateFile(utf8_cast(file).c_str(), GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(INVALID_HANDLE_VALUE != handle)
{
LARGE_INTEGER li;
if(!get_file_size_ex(handle, &li))
li.QuadPart = 0;
::CloseHandle(handle);
return li.QuadPart;
}
}
return 0;
#else
auto stream = ::fopen(file.c_str(), "rb");
long long size = 0;
if(stream)
{
#if defined(NANA_LINUX)
fseeko64(stream, 0, SEEK_END);
size = ftello64(stream);
#elif defined(NANA_MACOS)
fseeko(stream, 0, SEEK_END);
size = ftello(stream);
#endif
fclose(stream);
}
return size;
#endif
}
*/
bool modified_file_time(const ::std::string& file, struct tm& t)
{
@@ -294,59 +227,6 @@ namespace filesystem
return false;
}
/*
bool mkdir(const std::string& path, bool & if_exist) //deprecated
{
if_exist = false;
if(path.size() == 0) return false;
std::string root;
#if defined(NANA_WINDOWS)
if(path.size() > 3 && path[1] == ':')
root = path.substr(0, 3);
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
if(path[0] == '/')
root = '/';
#endif
bool mkstat = false;
std::size_t beg = root.size();
while(true)
{
beg = path.find_first_not_of("/\\", beg);
if(beg == path.npos)
break;
std::size_t pos = path.find_first_of("/\\", beg + 1);
if(pos != path.npos)
{
root += path.substr(beg, pos - beg);
mkstat = detail::mkdir_helper(root, if_exist);
if(mkstat == false && if_exist == false)
return false;
#if defined(NANA_WINDOWS)
root += L'\\';
#elif defined(NANA_LINUX) || defined(NANA_MACOS)
root += L'/';
#endif
}
else
{
if(beg + 1 < path.size())
{
root += path.substr(beg);
mkstat = detail::mkdir_helper(root, if_exist);
}
break;
}
beg = pos + 1;
}
return mkstat;
}
*/
bool rmfile(const char* file)
{
#if defined(NANA_WINDOWS)