Commit a96a3886 authored by Davis King's avatar Davis King

Changed the file and directory objects to not be reference counted. This

is so they are safer to use in threaded programs.
parent 12dfdde3
...@@ -30,8 +30,6 @@ namespace dlib ...@@ -30,8 +30,6 @@ namespace dlib
) )
{ {
using namespace std; using namespace std;
state = new data;
state->count = 1;
char buf[3000]; char buf[3000];
...@@ -41,21 +39,21 @@ namespace dlib ...@@ -41,21 +39,21 @@ namespace dlib
// the file was not found // the file was not found
throw file_not_found("Unable to find file " + name); throw file_not_found("Unable to find file " + name);
} }
state->full_name = buf; state.full_name = buf;
string::size_type pos = state->full_name.find_last_of(directory::get_separator()); string::size_type pos = state.full_name.find_last_of(directory::get_separator());
if (pos == string::npos) if (pos == string::npos)
{ {
// no valid full path has no separator characters. // no valid full path has no separator characters.
throw file_not_found("Unable to find file " + name); throw file_not_found("Unable to find file " + name);
} }
state->name = state->full_name.substr(pos+1); state.name = state.full_name.substr(pos+1);
// now find the size of this file // now find the size of this file
WIN32_FIND_DATAA data; WIN32_FIND_DATAA data;
HANDLE ffind = FindFirstFileA(state->full_name.c_str(), &data); HANDLE ffind = FindFirstFileA(state.full_name.c_str(), &data);
if (ffind == INVALID_HANDLE_VALUE || if (ffind == INVALID_HANDLE_VALUE ||
(data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) != 0) (data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) != 0)
{ {
...@@ -66,7 +64,7 @@ namespace dlib ...@@ -66,7 +64,7 @@ namespace dlib
uint64 temp = data.nFileSizeHigh; uint64 temp = data.nFileSizeHigh;
temp <<= 32; temp <<= 32;
temp |= data.nFileSizeLow; temp |= data.nFileSizeLow;
state->file_size = temp; state.file_size = temp;
FindClose(ffind); FindClose(ffind);
} }
...@@ -81,12 +79,12 @@ namespace dlib ...@@ -81,12 +79,12 @@ namespace dlib
{ {
using namespace std; using namespace std;
if (state->full_name.size() != rhs.state->full_name.size()) if (state.full_name.size() != rhs.state.full_name.size())
return false; return false;
// compare the strings but ignore the case because file names // compare the strings but ignore the case because file names
// are not case sensitive on windows // are not case sensitive on windows
return tolower(state->full_name) == tolower(rhs.state->full_name); return tolower(state.full_name) == tolower(rhs.state.full_name);
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
...@@ -102,8 +100,6 @@ namespace dlib ...@@ -102,8 +100,6 @@ namespace dlib
{ {
using namespace std; using namespace std;
state = new data;
state->count = 1;
char buf[3000]; char buf[3000];
char* str; char* str;
...@@ -112,30 +108,30 @@ namespace dlib ...@@ -112,30 +108,30 @@ namespace dlib
// the directory was not found // the directory was not found
throw dir_not_found("Unable to find directory " + name); throw dir_not_found("Unable to find directory " + name);
} }
state->full_name = buf; state.full_name = buf;
const char sep = get_separator(); const char sep = get_separator();
if (is_root_path(state->full_name) == false) if (is_root_path(state.full_name) == false)
{ {
// ensure that thre is not a trialing separator // ensure that thre is not a trialing separator
if (state->full_name[state->full_name.size()-1] == sep) if (state.full_name[state.full_name.size()-1] == sep)
state->full_name.erase(state->full_name.size()-1); state.full_name.erase(state.full_name.size()-1);
// pick out the directory name // pick out the directory name
string::size_type pos = state->full_name.find_last_of(sep); string::size_type pos = state.full_name.find_last_of(sep);
state->name = state->full_name.substr(pos+1); state.name = state.full_name.substr(pos+1);
} }
else else
{ {
// ensure that there is a trailing separator // ensure that there is a trailing separator
if (state->full_name[state->full_name.size()-1] != sep) if (state.full_name[state.full_name.size()-1] != sep)
state->full_name += sep; state.full_name += sep;
} }
// now check that this is actually a valid directory // now check that this is actually a valid directory
DWORD attribs = GetFileAttributesA(state->full_name.c_str()); DWORD attribs = GetFileAttributesA(state.full_name.c_str());
if (attribs == INVALID_FILE_ATTRIBUTES || if (attribs == INVALID_FILE_ATTRIBUTES ||
(attribs&FILE_ATTRIBUTE_DIRECTORY) == 0) (attribs&FILE_ATTRIBUTE_DIRECTORY) == 0)
{ {
...@@ -163,12 +159,12 @@ namespace dlib ...@@ -163,12 +159,12 @@ namespace dlib
{ {
using namespace std; using namespace std;
if (state->full_name.size() != rhs.state->full_name.size()) if (state.full_name.size() != rhs.state.full_name.size())
return false; return false;
// compare the strings but ignore the case because file names // compare the strings but ignore the case because file names
// are not case sensitive on windows // are not case sensitive on windows
return tolower(state->full_name) == tolower(rhs.state->full_name); return tolower(state.full_name) == tolower(rhs.state.full_name);
} }
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
...@@ -189,23 +185,23 @@ namespace dlib ...@@ -189,23 +185,23 @@ namespace dlib
const char sep = get_separator(); const char sep = get_separator();
string::size_type pos = state->full_name.find_last_of(sep); string::size_type pos = state.full_name.find_last_of(sep);
temp.state->full_name = state->full_name.substr(0,pos); temp.state.full_name = state.full_name.substr(0,pos);
if ( is_root_path(temp.state->full_name)) if ( is_root_path(temp.state.full_name))
{ {
temp.state->full_name += sep; temp.state.full_name += sep;
} }
else else
{ {
pos = temp.state->full_name.find_last_of(sep); pos = temp.state.full_name.find_last_of(sep);
if (pos != string::npos) if (pos != string::npos)
{ {
temp.state->name = temp.state->full_name.substr(pos+1); temp.state.name = temp.state.full_name.substr(pos+1);
} }
else else
{ {
temp.state->full_name += sep; temp.state.full_name += sep;
} }
} }
return temp; return temp;
......
...@@ -36,15 +36,14 @@ namespace dlib ...@@ -36,15 +36,14 @@ namespace dlib
{ {
/*! /*!
INITIAL VALUES INITIAL VALUES
state->name == name() state.name == name()
state->full_name == full_name() state.full_name == full_name()
state->file_size == size() state.file_size == size()
CONVENTION CONVENTION
state->name == name() state.name == name()
state->full_name == full_name() state.full_name == full_name()
state->file_size == size() state.file_size == size()
state->count == the number of file objects that point to state
!*/ !*/
...@@ -55,7 +54,6 @@ namespace dlib ...@@ -55,7 +54,6 @@ namespace dlib
uint64 file_size; uint64 file_size;
std::string name; std::string name;
std::string full_name; std::string full_name;
unsigned long count;
}; };
...@@ -71,11 +69,9 @@ namespace dlib ...@@ -71,11 +69,9 @@ namespace dlib
private_constructor private_constructor
) )
{ {
state = new data; state.file_size = file_size;
state->count = 1; state.name = name;
state->file_size = file_size; state.full_name = full_name;
state->name = name;
state->full_name = full_name;
} }
...@@ -88,9 +84,7 @@ namespace dlib ...@@ -88,9 +84,7 @@ namespace dlib
inline file ( inline file (
) )
{ {
state = new data; state.file_size = 0;
state->count = 1;
state->file_size = 0;
} }
file ( file (
...@@ -101,48 +95,14 @@ namespace dlib ...@@ -101,48 +95,14 @@ namespace dlib
const char* name const char* name
) { init(name); } ) { init(name); }
inline file (
const file& item
)
{
state = item.state;
state->count += 1;
}
inline ~file (
)
{
if (state->count == 1)
delete state;
else
state->count -= 1;
}
inline const std::string& name ( inline const std::string& name (
) const { return state->name; } ) const { return state.name; }
inline const std::string& full_name ( inline const std::string& full_name (
) const { return state->full_name; } ) const { return state.full_name; }
inline uint64 size ( inline uint64 size (
) const { return state->file_size; } ) const { return state.file_size; }
inline file& operator= (
const file& rhs
)
{
if (&rhs == this)
return *this;
if (state->count == 1)
delete state;
else
state->count -= 1;
state = rhs.state;
state->count += 1;
return *this;
}
bool operator == ( bool operator == (
const file& rhs const file& rhs
...@@ -165,8 +125,7 @@ namespace dlib ...@@ -165,8 +125,7 @@ namespace dlib
private: private:
// member data data state;
data* state;
}; };
...@@ -180,14 +139,13 @@ namespace dlib ...@@ -180,14 +139,13 @@ namespace dlib
{ {
/*! /*!
INITIAL VALUES INITIAL VALUES
state->name == name() state.name == name()
state->full_name == full_name() state.full_name == full_name()
CONVENTION CONVENTION
state->name == name() state.name == name()
state->full_name == full_name() state.full_name == full_name()
state->count == the number of directory objects that point to state is_root() == state.name.size() == 0
is_root() == state->name.size() == 0
!*/ !*/
...@@ -199,7 +157,6 @@ namespace dlib ...@@ -199,7 +157,6 @@ namespace dlib
{ {
std::string name; std::string name;
std::string full_name; std::string full_name;
unsigned long count;
}; };
...@@ -218,10 +175,8 @@ namespace dlib ...@@ -218,10 +175,8 @@ namespace dlib
private_constructor private_constructor
) )
{ {
state = new data; state.name = name;
state->count = 1; state.full_name = full_name;
state->name = name;
state->full_name = full_name;
} }
...@@ -235,8 +190,6 @@ namespace dlib ...@@ -235,8 +190,6 @@ namespace dlib
inline directory ( inline directory (
) )
{ {
state = new data;
state->count = 1;
} }
directory ( directory (
...@@ -247,22 +200,6 @@ namespace dlib ...@@ -247,22 +200,6 @@ namespace dlib
const char* name const char* name
) { init(name); } ) { init(name); }
inline directory (
const directory& item
)
{
state = item.state;
state->count += 1;
}
inline ~directory (
)
{
if (state->count == 1)
delete state;
else
state->count -= 1;
}
static char get_separator ( static char get_separator (
); );
...@@ -286,30 +223,13 @@ namespace dlib ...@@ -286,30 +223,13 @@ namespace dlib
) const; ) const;
inline bool is_root ( inline bool is_root (
) const { return state->name.size() == 0; } ) const { return state.name.size() == 0; }
inline const std::string& name ( inline const std::string& name (
) const { return state->name; } ) const { return state.name; }
inline const std::string& full_name ( inline const std::string& full_name (
) const { return state->full_name; } ) const { return state.full_name; }
directory& operator= (
const directory& rhs
)
{
if (&rhs == this)
return *this;
if (state->count == 1)
delete state;
else
state->count -= 1;
state = rhs.state;
state->count += 1;
return *this;
}
bool operator == ( bool operator == (
const directory& rhs const directory& rhs
...@@ -333,7 +253,7 @@ namespace dlib ...@@ -333,7 +253,7 @@ namespace dlib
private: private:
// member data // member data
data* state; data state;
bool is_root_path ( bool is_root_path (
const std::string& path const std::string& path
...@@ -423,7 +343,7 @@ namespace dlib ...@@ -423,7 +343,7 @@ namespace dlib
> >
typename disable_if<is_std_vector<queue_of_files>,void>::type typename disable_if<is_std_vector<queue_of_files>,void>::type
directory_helper_get_files ( directory_helper_get_files (
const directory::data* state, const directory::data& state,
queue_of_files& files queue_of_files& files
) )
{ {
...@@ -432,14 +352,14 @@ namespace dlib ...@@ -432,14 +352,14 @@ namespace dlib
typedef file::private_constructor private_constructor; typedef file::private_constructor private_constructor;
files.clear(); files.clear();
if (state->full_name.size() == 0) if (state.full_name.size() == 0)
throw listing_error("This directory object currently doesn't represent any directory."); throw listing_error("This directory object currently doesn't represent any directory.");
HANDLE ffind = INVALID_HANDLE_VALUE; HANDLE ffind = INVALID_HANDLE_VALUE;
try try
{ {
WIN32_FIND_DATAA data; WIN32_FIND_DATAA data;
string path = state->full_name; string path = state.full_name;
// ensure that the path ends with a separator // ensure that the path ends with a separator
if (path[path.size()-1] != directory::get_separator()) if (path[path.size()-1] != directory::get_separator())
path += directory::get_separator(); path += directory::get_separator();
...@@ -447,7 +367,7 @@ namespace dlib ...@@ -447,7 +367,7 @@ namespace dlib
ffind = FindFirstFileA((path+"*").c_str(), &data); ffind = FindFirstFileA((path+"*").c_str(), &data);
if (ffind == INVALID_HANDLE_VALUE) if (ffind == INVALID_HANDLE_VALUE)
{ {
throw listing_error("Unable to list the contents of " + state->full_name); throw listing_error("Unable to list the contents of " + state.full_name);
} }
...@@ -475,7 +395,7 @@ namespace dlib ...@@ -475,7 +395,7 @@ namespace dlib
else else
{ {
// there was an error // there was an error
throw listing_error("Unable to list the contents of " + state->full_name); throw listing_error("Unable to list the contents of " + state.full_name);
} }
} }
} while (no_more_files == false); } while (no_more_files == false);
...@@ -499,7 +419,7 @@ namespace dlib ...@@ -499,7 +419,7 @@ namespace dlib
> >
typename enable_if<is_std_vector<queue_of_files>,void>::type typename enable_if<is_std_vector<queue_of_files>,void>::type
directory_helper_get_files ( directory_helper_get_files (
const directory::data* state, const directory::data& state,
queue_of_files& files queue_of_files& files
) )
{ {
...@@ -540,7 +460,7 @@ namespace dlib ...@@ -540,7 +460,7 @@ namespace dlib
> >
typename disable_if<is_std_vector<queue_of_dirs>,void>::type typename disable_if<is_std_vector<queue_of_dirs>,void>::type
directory_helper_get_dirs ( directory_helper_get_dirs (
const directory::data* state, const directory::data& state,
queue_of_dirs& dirs queue_of_dirs& dirs
) )
{ {
...@@ -549,14 +469,14 @@ namespace dlib ...@@ -549,14 +469,14 @@ namespace dlib
typedef directory::private_constructor private_constructor; typedef directory::private_constructor private_constructor;
dirs.clear(); dirs.clear();
if (state->full_name.size() == 0) if (state.full_name.size() == 0)
throw listing_error("This directory object currently doesn't represent any directory."); throw listing_error("This directory object currently doesn't represent any directory.");
HANDLE dfind = INVALID_HANDLE_VALUE; HANDLE dfind = INVALID_HANDLE_VALUE;
try try
{ {
WIN32_FIND_DATAA data; WIN32_FIND_DATAA data;
string path = state->full_name; string path = state.full_name;
// ensure that the path ends with a separator // ensure that the path ends with a separator
if (path[path.size()-1] != directory::get_separator()) if (path[path.size()-1] != directory::get_separator())
path += directory::get_separator(); path += directory::get_separator();
...@@ -564,7 +484,7 @@ namespace dlib ...@@ -564,7 +484,7 @@ namespace dlib
dfind = FindFirstFileA((path+"*").c_str(), &data); dfind = FindFirstFileA((path+"*").c_str(), &data);
if (dfind == INVALID_HANDLE_VALUE) if (dfind == INVALID_HANDLE_VALUE)
{ {
throw listing_error("Unable to list the contents of " + state->full_name); throw listing_error("Unable to list the contents of " + state.full_name);
} }
...@@ -592,7 +512,7 @@ namespace dlib ...@@ -592,7 +512,7 @@ namespace dlib
else else
{ {
// there was an error // there was an error
throw listing_error("Unable to list the contents of " + state->full_name); throw listing_error("Unable to list the contents of " + state.full_name);
} }
} }
} while (no_more_files == false); } while (no_more_files == false);
...@@ -617,7 +537,7 @@ namespace dlib ...@@ -617,7 +537,7 @@ namespace dlib
> >
typename enable_if<is_std_vector<queue_of_dirs>,void>::type typename enable_if<is_std_vector<queue_of_dirs>,void>::type
directory_helper_get_dirs ( directory_helper_get_dirs (
const directory::data* state, const directory::data& state,
queue_of_dirs& dirs queue_of_dirs& dirs
) )
{ {
......
...@@ -29,8 +29,6 @@ namespace dlib ...@@ -29,8 +29,6 @@ namespace dlib
) )
{ {
using namespace std; using namespace std;
state = new data;
state->count = 1;
...@@ -40,21 +38,21 @@ namespace dlib ...@@ -40,21 +38,21 @@ namespace dlib
// the file was not found // the file was not found
throw file_not_found("Unable to find file " + name); throw file_not_found("Unable to find file " + name);
} }
state->full_name = buf; state.full_name = buf;
string::size_type pos = state->full_name.find_last_of(directory::get_separator()); string::size_type pos = state.full_name.find_last_of(directory::get_separator());
if (pos == string::npos) if (pos == string::npos)
{ {
// no valid full path has no separtor characters. // no valid full path has no separtor characters.
throw file_not_found("Unable to find file " + name); throw file_not_found("Unable to find file " + name);
} }
state->name = state->full_name.substr(pos+1); state.name = state.full_name.substr(pos+1);
// now find the size of this file // now find the size of this file
struct stat64 buffer; struct stat64 buffer;
if (::stat64(state->full_name.c_str(), &buffer) || if (::stat64(state.full_name.c_str(), &buffer) ||
S_ISDIR(buffer.st_mode)) S_ISDIR(buffer.st_mode))
{ {
// there was an error during the call to stat64 or // there was an error during the call to stat64 or
...@@ -63,7 +61,7 @@ namespace dlib ...@@ -63,7 +61,7 @@ namespace dlib
} }
else else
{ {
state->file_size = static_cast<uint64>(buffer.st_size); state.file_size = static_cast<uint64>(buffer.st_size);
} }
} }
...@@ -76,18 +74,18 @@ namespace dlib ...@@ -76,18 +74,18 @@ namespace dlib
) const ) const
{ {
using namespace std; using namespace std;
if (state->full_name.size() == 0 && rhs.state->full_name.size() == 0) if (state.full_name.size() == 0 && rhs.state.full_name.size() == 0)
return true; return true;
// These files might have different names but actually represent the same // These files might have different names but actually represent the same
// file due to the presence of symbolic links. // file due to the presence of symbolic links.
char buf[PATH_MAX]; char buf[PATH_MAX];
string left, right; string left, right;
if (realpath(state->full_name.c_str(),buf) == 0) if (realpath(state.full_name.c_str(),buf) == 0)
return false; return false;
left = buf; left = buf;
if (realpath(rhs.state->full_name.c_str(),buf) == 0) if (realpath(rhs.state.full_name.c_str(),buf) == 0)
return false; return false;
right = buf; right = buf;
...@@ -107,8 +105,6 @@ namespace dlib ...@@ -107,8 +105,6 @@ namespace dlib
{ {
using namespace std; using namespace std;
state = new data;
state->count = 1;
char buf[PATH_MAX]; char buf[PATH_MAX];
if (realpath(name.c_str(),buf) == 0) if (realpath(name.c_str(),buf) == 0)
...@@ -116,31 +112,31 @@ namespace dlib ...@@ -116,31 +112,31 @@ namespace dlib
// the directory was not found // the directory was not found
throw dir_not_found("Unable to find directory " + name); throw dir_not_found("Unable to find directory " + name);
} }
state->full_name = buf; state.full_name = buf;
const char sep = get_separator(); const char sep = get_separator();
if (is_root_path(state->full_name) == false) if (is_root_path(state.full_name) == false)
{ {
// ensure that thre is not a trialing separator // ensure that thre is not a trialing separator
if (state->full_name[state->full_name.size()-1] == sep) if (state.full_name[state.full_name.size()-1] == sep)
state->full_name.erase(state->full_name.size()-1); state.full_name.erase(state.full_name.size()-1);
// pick out the directory name // pick out the directory name
string::size_type pos = state->full_name.find_last_of(sep); string::size_type pos = state.full_name.find_last_of(sep);
state->name = state->full_name.substr(pos+1); state.name = state.full_name.substr(pos+1);
} }
else else
{ {
// ensure that there is a trailing separator // ensure that there is a trailing separator
if (state->full_name[state->full_name.size()-1] != sep) if (state.full_name[state.full_name.size()-1] != sep)
state->full_name += sep; state.full_name += sep;
} }
struct stat64 buffer; struct stat64 buffer;
// now check that this is actually a valid directory // now check that this is actually a valid directory
if (::stat64(state->full_name.c_str(),&buffer)) if (::stat64(state.full_name.c_str(),&buffer))
{ {
// the directory was not found // the directory was not found
throw dir_not_found("Unable to find directory " + name); throw dir_not_found("Unable to find directory " + name);
...@@ -169,18 +165,18 @@ namespace dlib ...@@ -169,18 +165,18 @@ namespace dlib
) const ) const
{ {
using namespace std; using namespace std;
if (state->full_name.size() == 0 && rhs.state->full_name.size() == 0) if (state.full_name.size() == 0 && rhs.state.full_name.size() == 0)
return true; return true;
// These directories might have different names but actually represent the same // These directories might have different names but actually represent the same
// directory due to the presence of symbolic links. // directory due to the presence of symbolic links.
char buf[PATH_MAX]; char buf[PATH_MAX];
string left, right; string left, right;
if (realpath(state->full_name.c_str(),buf) == 0) if (realpath(state.full_name.c_str(),buf) == 0)
return false; return false;
left = buf; left = buf;
if (realpath(rhs.state->full_name.c_str(),buf) == 0) if (realpath(rhs.state.full_name.c_str(),buf) == 0)
return false; return false;
right = buf; right = buf;
...@@ -205,23 +201,23 @@ namespace dlib ...@@ -205,23 +201,23 @@ namespace dlib
const char sep = get_separator(); const char sep = get_separator();
string::size_type pos = state->full_name.find_last_of(sep); string::size_type pos = state.full_name.find_last_of(sep);
temp.state->full_name = state->full_name.substr(0,pos); temp.state.full_name = state.full_name.substr(0,pos);
if ( is_root_path(temp.state->full_name)) if ( is_root_path(temp.state.full_name))
{ {
temp.state->full_name += sep; temp.state.full_name += sep;
} }
else else
{ {
pos = temp.state->full_name.find_last_of(sep); pos = temp.state.full_name.find_last_of(sep);
if (pos != string::npos) if (pos != string::npos)
{ {
temp.state->name = temp.state->full_name.substr(pos+1); temp.state.name = temp.state.full_name.substr(pos+1);
} }
else else
{ {
temp.state->full_name += sep; temp.state.full_name += sep;
} }
} }
return temp; return temp;
......
This diff is collapsed.
...@@ -56,10 +56,6 @@ namespace dlib ...@@ -56,10 +56,6 @@ namespace dlib
object is constructed. Thus if a file changes sizes after its object is constructed. Thus if a file changes sizes after its
file object has been created its file object's size() method file object has been created its file object's size() method
will not reflect the new file size. will not reflect the new file size.
THREAD SAFETY
This object is reference counted so use with caution in a threaded
environment.
!*/ !*/
public: public:
...@@ -208,10 +204,6 @@ namespace dlib ...@@ -208,10 +204,6 @@ namespace dlib
the ability to traverse a directory tree. the ability to traverse a directory tree.
Note that the directories . and .. are not returned by get_dirs() Note that the directories . and .. are not returned by get_dirs()
THREAD SAFETY
This object is reference counted so use with caution in a threaded
environment.
!*/ !*/
public: public:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment