|
|
|
|
|
#ifndef DLIB_DIR_NAV_KERNEL_2_CPp_ |
|
#define DLIB_DIR_NAV_KERNEL_2_CPp_ |
|
|
|
#include "../platform.h" |
|
|
|
#ifdef DLIB_POSIX |
|
|
|
|
|
#include "dir_nav_kernel_2.h" |
|
|
|
|
|
|
|
|
|
|
|
namespace dlib |
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void file:: |
|
init ( |
|
const std::string& name |
|
) |
|
{ |
|
using namespace std; |
|
|
|
|
|
|
|
char buf[PATH_MAX]; |
|
if (realpath(name.c_str(),buf) == 0) |
|
{ |
|
|
|
throw file_not_found("Unable to find file " + name); |
|
} |
|
state.full_name = buf; |
|
|
|
|
|
string::size_type pos = state.full_name.find_last_of(directory::get_separator()); |
|
if (pos == string::npos) |
|
{ |
|
|
|
throw file_not_found("Unable to find file " + name); |
|
} |
|
state.name = state.full_name.substr(pos+1); |
|
|
|
|
|
|
|
struct stat64 buffer; |
|
if (::stat64(state.full_name.c_str(), &buffer) || |
|
S_ISDIR(buffer.st_mode)) |
|
{ |
|
|
|
|
|
throw file_not_found("Unable to find file " + name); |
|
} |
|
else |
|
{ |
|
state.file_size = static_cast<uint64>(buffer.st_size); |
|
|
|
|
|
state.last_modified = std::chrono::system_clock::from_time_t(buffer.st_mtime); |
|
#ifdef _BSD_SOURCE |
|
state.last_modified += std::chrono::duration_cast<std::chrono::system_clock::duration>(std::chrono::nanoseconds(buffer.st_atim.tv_nsec)); |
|
#endif |
|
} |
|
|
|
} |
|
|
|
|
|
|
|
bool file:: |
|
operator == ( |
|
const file& rhs |
|
) const |
|
{ |
|
using namespace std; |
|
if (state.full_name.size() == 0 && rhs.state.full_name.size() == 0) |
|
return true; |
|
|
|
|
|
|
|
char buf[PATH_MAX]; |
|
string left, right; |
|
if (realpath(state.full_name.c_str(),buf) == 0) |
|
return false; |
|
left = buf; |
|
|
|
if (realpath(rhs.state.full_name.c_str(),buf) == 0) |
|
return false; |
|
right = buf; |
|
|
|
return (left == right); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void directory:: |
|
init ( |
|
const std::string& name |
|
) |
|
{ |
|
using namespace std; |
|
|
|
|
|
char buf[PATH_MAX]; |
|
if (realpath(name.c_str(),buf) == 0) |
|
{ |
|
|
|
throw dir_not_found("Unable to find directory " + name); |
|
} |
|
state.full_name = buf; |
|
|
|
|
|
const char sep = get_separator(); |
|
if (is_root_path(state.full_name) == false) |
|
{ |
|
|
|
if (state.full_name[state.full_name.size()-1] == sep) |
|
state.full_name.erase(state.full_name.size()-1); |
|
|
|
|
|
string::size_type pos = state.full_name.find_last_of(sep); |
|
state.name = state.full_name.substr(pos+1); |
|
} |
|
else |
|
{ |
|
|
|
if (state.full_name[state.full_name.size()-1] != sep) |
|
state.full_name += sep; |
|
} |
|
|
|
|
|
struct stat64 buffer; |
|
|
|
if (::stat64(state.full_name.c_str(),&buffer)) |
|
{ |
|
|
|
throw dir_not_found("Unable to find directory " + name); |
|
} |
|
else if (S_ISDIR(buffer.st_mode) == 0) |
|
{ |
|
|
|
throw dir_not_found("Unable to find directory " + name); |
|
} |
|
} |
|
|
|
|
|
|
|
char directory:: |
|
get_separator ( |
|
) |
|
{ |
|
return '/'; |
|
} |
|
|
|
|
|
|
|
bool directory:: |
|
operator == ( |
|
const directory& rhs |
|
) const |
|
{ |
|
using namespace std; |
|
if (state.full_name.size() == 0 && rhs.state.full_name.size() == 0) |
|
return true; |
|
|
|
|
|
|
|
char buf[PATH_MAX]; |
|
string left, right; |
|
if (realpath(state.full_name.c_str(),buf) == 0) |
|
return false; |
|
left = buf; |
|
|
|
if (realpath(rhs.state.full_name.c_str(),buf) == 0) |
|
return false; |
|
right = buf; |
|
|
|
return (left == right); |
|
} |
|
|
|
|
|
|
|
const directory directory:: |
|
get_parent ( |
|
) const |
|
{ |
|
using namespace std; |
|
|
|
if (is_root()) |
|
{ |
|
return *this; |
|
} |
|
else |
|
{ |
|
directory temp; |
|
|
|
const char sep = get_separator(); |
|
|
|
string::size_type pos = state.full_name.find_last_of(sep); |
|
temp.state.full_name = state.full_name.substr(0,pos); |
|
|
|
if ( is_root_path(temp.state.full_name)) |
|
{ |
|
temp.state.full_name += sep; |
|
} |
|
else |
|
{ |
|
pos = temp.state.full_name.find_last_of(sep); |
|
if (pos != string::npos) |
|
{ |
|
temp.state.name = temp.state.full_name.substr(pos+1); |
|
} |
|
else |
|
{ |
|
temp.state.full_name += sep; |
|
} |
|
} |
|
return temp; |
|
} |
|
} |
|
|
|
|
|
|
|
bool directory:: |
|
is_root_path ( |
|
const std::string& path |
|
) const |
|
{ |
|
const char sep = get_separator(); |
|
if (path.size() == 1 && path[0] == sep) |
|
return true; |
|
else |
|
return false; |
|
} |
|
|
|
|
|
|
|
} |
|
|
|
#endif |
|
|
|
#endif |
|
|
|
|