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
)
{
using namespace std;
state = new data;
state->count = 1;
char buf[3000];
......@@ -41,21 +39,21 @@ namespace dlib
// the file was not found
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)
{
// no valid full path has no separator characters.
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
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 ||
(data.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) != 0)
{
......@@ -66,7 +64,7 @@ namespace dlib
uint64 temp = data.nFileSizeHigh;
temp <<= 32;
temp |= data.nFileSizeLow;
state->file_size = temp;
state.file_size = temp;
FindClose(ffind);
}
......@@ -81,12 +79,12 @@ namespace dlib
{
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;
// compare the strings but ignore the case because file names
// 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
{
using namespace std;
state = new data;
state->count = 1;
char buf[3000];
char* str;
......@@ -112,30 +108,30 @@ namespace dlib
// the directory was not found
throw dir_not_found("Unable to find directory " + name);
}
state->full_name = buf;
state.full_name = buf;
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
if (state->full_name[state->full_name.size()-1] == sep)
state->full_name.erase(state->full_name.size()-1);
if (state.full_name[state.full_name.size()-1] == sep)
state.full_name.erase(state.full_name.size()-1);
// pick out the directory name
string::size_type pos = state->full_name.find_last_of(sep);
state->name = state->full_name.substr(pos+1);
string::size_type pos = state.full_name.find_last_of(sep);
state.name = state.full_name.substr(pos+1);
}
else
{
// ensure that there is a trailing separator
if (state->full_name[state->full_name.size()-1] != sep)
state->full_name += sep;
if (state.full_name[state.full_name.size()-1] != sep)
state.full_name += sep;
}
// 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 ||
(attribs&FILE_ATTRIBUTE_DIRECTORY) == 0)
{
......@@ -163,12 +159,12 @@ namespace dlib
{
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;
// compare the strings but ignore the case because file names
// 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
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);
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))
if ( is_root_path(temp.state.full_name))
{
temp.state->full_name += sep;
temp.state.full_name += sep;
}
else
{
pos = temp.state->full_name.find_last_of(sep);
pos = temp.state.full_name.find_last_of(sep);
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
{
temp.state->full_name += sep;
temp.state.full_name += sep;
}
}
return temp;
......
......@@ -36,15 +36,14 @@ namespace dlib
{
/*!
INITIAL VALUES
state->name == name()
state->full_name == full_name()
state->file_size == size()
state.name == name()
state.full_name == full_name()
state.file_size == size()
CONVENTION
state->name == name()
state->full_name == full_name()
state->file_size == size()
state->count == the number of file objects that point to state
state.name == name()
state.full_name == full_name()
state.file_size == size()
!*/
......@@ -55,7 +54,6 @@ namespace dlib
uint64 file_size;
std::string name;
std::string full_name;
unsigned long count;
};
......@@ -71,11 +69,9 @@ namespace dlib
private_constructor
)
{
state = new data;
state->count = 1;
state->file_size = file_size;
state->name = name;
state->full_name = full_name;
state.file_size = file_size;
state.name = name;
state.full_name = full_name;
}
......@@ -88,9 +84,7 @@ namespace dlib
inline file (
)
{
state = new data;
state->count = 1;
state->file_size = 0;
state.file_size = 0;
}
file (
......@@ -101,48 +95,14 @@ namespace dlib
const char* 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 (
) const { return state->name; }
) const { return state.name; }
inline const std::string& full_name (
) const { return state->full_name; }
) const { return state.full_name; }
inline uint64 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;
}
) const { return state.file_size; }
bool operator == (
const file& rhs
......@@ -165,8 +125,7 @@ namespace dlib
private:
// member data
data* state;
data state;
};
......@@ -180,14 +139,13 @@ namespace dlib
{
/*!
INITIAL VALUES
state->name == name()
state->full_name == full_name()
state.name == name()
state.full_name == full_name()
CONVENTION
state->name == name()
state->full_name == full_name()
state->count == the number of directory objects that point to state
is_root() == state->name.size() == 0
state.name == name()
state.full_name == full_name()
is_root() == state.name.size() == 0
!*/
......@@ -199,7 +157,6 @@ namespace dlib
{
std::string name;
std::string full_name;
unsigned long count;
};
......@@ -218,10 +175,8 @@ namespace dlib
private_constructor
)
{
state = new data;
state->count = 1;
state->name = name;
state->full_name = full_name;
state.name = name;
state.full_name = full_name;
}
......@@ -235,8 +190,6 @@ namespace dlib
inline directory (
)
{
state = new data;
state->count = 1;
}
directory (
......@@ -247,22 +200,6 @@ namespace dlib
const char* 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 (
);
......@@ -286,30 +223,13 @@ namespace dlib
) const;
inline bool is_root (
) const { return state->name.size() == 0; }
) const { return state.name.size() == 0; }
inline const std::string& name (
) const { return state->name; }
) const { return state.name; }
inline const std::string& 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;
}
) const { return state.full_name; }
bool operator == (
const directory& rhs
......@@ -333,7 +253,7 @@ namespace dlib
private:
// member data
data* state;
data state;
bool is_root_path (
const std::string& path
......@@ -423,7 +343,7 @@ namespace dlib
>
typename disable_if<is_std_vector<queue_of_files>,void>::type
directory_helper_get_files (
const directory::data* state,
const directory::data& state,
queue_of_files& files
)
{
......@@ -432,14 +352,14 @@ namespace dlib
typedef file::private_constructor private_constructor;
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.");
HANDLE ffind = INVALID_HANDLE_VALUE;
try
{
WIN32_FIND_DATAA data;
string path = state->full_name;
string path = state.full_name;
// ensure that the path ends with a separator
if (path[path.size()-1] != directory::get_separator())
path += directory::get_separator();
......@@ -447,7 +367,7 @@ namespace dlib
ffind = FindFirstFileA((path+"*").c_str(), &data);
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
else
{
// 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);
......@@ -499,7 +419,7 @@ namespace dlib
>
typename enable_if<is_std_vector<queue_of_files>,void>::type
directory_helper_get_files (
const directory::data* state,
const directory::data& state,
queue_of_files& files
)
{
......@@ -540,7 +460,7 @@ namespace dlib
>
typename disable_if<is_std_vector<queue_of_dirs>,void>::type
directory_helper_get_dirs (
const directory::data* state,
const directory::data& state,
queue_of_dirs& dirs
)
{
......@@ -549,14 +469,14 @@ namespace dlib
typedef directory::private_constructor private_constructor;
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.");
HANDLE dfind = INVALID_HANDLE_VALUE;
try
{
WIN32_FIND_DATAA data;
string path = state->full_name;
string path = state.full_name;
// ensure that the path ends with a separator
if (path[path.size()-1] != directory::get_separator())
path += directory::get_separator();
......@@ -564,7 +484,7 @@ namespace dlib
dfind = FindFirstFileA((path+"*").c_str(), &data);
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
else
{
// 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);
......@@ -617,7 +537,7 @@ namespace dlib
>
typename enable_if<is_std_vector<queue_of_dirs>,void>::type
directory_helper_get_dirs (
const directory::data* state,
const directory::data& state,
queue_of_dirs& dirs
)
{
......
......@@ -29,8 +29,6 @@ namespace dlib
)
{
using namespace std;
state = new data;
state->count = 1;
......@@ -40,21 +38,21 @@ namespace dlib
// the file was not found
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)
{
// no valid full path has no separtor characters.
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
struct stat64 buffer;
if (::stat64(state->full_name.c_str(), &buffer) ||
if (::stat64(state.full_name.c_str(), &buffer) ||
S_ISDIR(buffer.st_mode))
{
// there was an error during the call to stat64 or
......@@ -63,7 +61,7 @@ namespace dlib
}
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
) const
{
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;
// These files might have different names but actually represent the same
// file due to the presence of symbolic links.
char buf[PATH_MAX];
string left, right;
if (realpath(state->full_name.c_str(),buf) == 0)
if (realpath(state.full_name.c_str(),buf) == 0)
return false;
left = buf;
if (realpath(rhs.state->full_name.c_str(),buf) == 0)
if (realpath(rhs.state.full_name.c_str(),buf) == 0)
return false;
right = buf;
......@@ -107,8 +105,6 @@ namespace dlib
{
using namespace std;
state = new data;
state->count = 1;
char buf[PATH_MAX];
if (realpath(name.c_str(),buf) == 0)
......@@ -116,31 +112,31 @@ namespace dlib
// the directory was not found
throw dir_not_found("Unable to find directory " + name);
}
state->full_name = buf;
state.full_name = buf;
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
if (state->full_name[state->full_name.size()-1] == sep)
state->full_name.erase(state->full_name.size()-1);
if (state.full_name[state.full_name.size()-1] == sep)
state.full_name.erase(state.full_name.size()-1);
// pick out the directory name
string::size_type pos = state->full_name.find_last_of(sep);
state->name = state->full_name.substr(pos+1);
string::size_type pos = state.full_name.find_last_of(sep);
state.name = state.full_name.substr(pos+1);
}
else
{
// ensure that there is a trailing separator
if (state->full_name[state->full_name.size()-1] != sep)
state->full_name += sep;
if (state.full_name[state.full_name.size()-1] != sep)
state.full_name += sep;
}
struct stat64 buffer;
// 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
throw dir_not_found("Unable to find directory " + name);
......@@ -169,18 +165,18 @@ namespace dlib
) const
{
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;
// These directories might have different names but actually represent the same
// directory due to the presence of symbolic links.
char buf[PATH_MAX];
string left, right;
if (realpath(state->full_name.c_str(),buf) == 0)
if (realpath(state.full_name.c_str(),buf) == 0)
return false;
left = buf;
if (realpath(rhs.state->full_name.c_str(),buf) == 0)
if (realpath(rhs.state.full_name.c_str(),buf) == 0)
return false;
right = buf;
......@@ -205,23 +201,23 @@ namespace dlib
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);
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))
if ( is_root_path(temp.state.full_name))
{
temp.state->full_name += sep;
temp.state.full_name += sep;
}
else
{
pos = temp.state->full_name.find_last_of(sep);
pos = temp.state.full_name.find_last_of(sep);
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
{
temp.state->full_name += sep;
temp.state.full_name += sep;
}
}
return temp;
......
This diff is collapsed.
......@@ -56,10 +56,6 @@ namespace dlib
object is constructed. Thus if a file changes sizes after its
file object has been created its file object's size() method
will not reflect the new file size.
THREAD SAFETY
This object is reference counted so use with caution in a threaded
environment.
!*/
public:
......@@ -208,10 +204,6 @@ namespace dlib
the ability to traverse a directory tree.
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:
......
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