Skip to content

Commit

Permalink
tempname.cpp: Introduce gen_temp(), shared implementation of tempname…
Browse files Browse the repository at this point in the history
…::tpie_name and tempname::tpie_dir_name
  • Loading branch information
Mortal committed Aug 2, 2012
1 parent b6f704b commit fcc2863
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 48 deletions.
67 changes: 26 additions & 41 deletions tpie/tempname.cpp
Expand Up @@ -42,9 +42,14 @@ using namespace tpie;

boost::rand48 prng(42);

std::string tempname::default_path;
std::string tempname::default_base_name;
std::string tempname::default_extension;
namespace {

std::string default_path;
std::string default_base_name;
std::string default_extension;
std::string tpie_mktemp();

}

std::string tempname::get_system_path() {
#ifdef WIN32
Expand All @@ -62,62 +67,40 @@ std::string tempname::get_system_path() {
#endif
}

std::string tempname::tpie_name(const std::string& post_base, const std::string& dir, const std::string& ext)
{ std::string extension;
namespace {
std::string gen_temp(const std::string& post_base, const std::string& dir, const std::string& suffix) {
std::string base_name;
boost::filesystem::path base_dir;

extension = ext;
if(extension.empty()) {
extension = default_extension;
if(extension.empty())
extension = "tpie";
}


base_name = default_base_name;
if(base_name.empty())
base_name = "TPIE";

if(!dir.empty())
base_dir = dir;
else
else
base_dir = tempname::get_actual_path();

boost::filesystem::path p;
for(int i=0; i < 42; ++i) {
if(post_base.empty())
p = base_dir / (base_name + "_" + tpie_mktemp() + "." + extension);
else
p = base_dir / (base_name + "_" + post_base + "_" + tpie_mktemp() + "." + extension);
p = base_dir / (base_name + "_" + tpie_mktemp() + suffix);
else
p = base_dir / (base_name + "_" + post_base + "_" + tpie_mktemp() + suffix);
if ( !boost::filesystem::exists(p) )
return p.file_string();
}
throw tempfile_error("Unable to find free name for temporary file");
}
}

std::string tempname::tpie_dir_name(const std::string& post_base, const std::string& dir)
{ std::string base_name;
boost::filesystem::path base_dir;

base_name = default_base_name;
if(base_name.empty())
base_name = "TPIE";

if(!dir.empty())
base_dir = dir;
else
base_dir = tempname::get_actual_path();
std::string tempname::tpie_name(const std::string& post_base, const std::string& dir, const std::string& ext) {
if (ext.empty()) return gen_temp(post_base, dir, ".tpie");
else return gen_temp(post_base, dir, "." + ext);
}

boost::filesystem::path p;
for(int i=0; i < 42; ++i) {
if(post_base.empty())
p = base_dir / (base_name + "_" + tpie_mktemp());
else
p = base_dir / (base_name + "_" + post_base + "_" + tpie_mktemp());
if ( !boost::filesystem::exists(p) )
return p.file_string();
}
throw tempfile_error("Unable to find free name for temporary file");
std::string tempname::tpie_dir_name(const std::string& post_base, const std::string& dir) {
return gen_temp(post_base, dir, "");
}

std::string tempname::get_actual_path() {
Expand All @@ -135,7 +118,8 @@ std::string tempname::get_actual_path() {
return dir;
}

std::string tempname::tpie_mktemp()
namespace {
std::string tpie_mktemp()
{
const std::string chars[] =
{ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
Expand All @@ -161,6 +145,7 @@ std::string tempname::tpie_mktemp()

return result;
}
}


void tempname::set_default_path(const std::string& path, const std::string& subdir) {
Expand Down
7 changes: 0 additions & 7 deletions tpie/tempname.h
Expand Up @@ -142,13 +142,6 @@ namespace tpie {
/// \return A string containing the path.
///////////////////////////////////////////////////////////////////////
static std::string get_actual_path();

private:
static std::string default_path;
static std::string default_base_name;
static std::string default_extension;

static std::string tpie_mktemp();
};

///////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit fcc2863

Please sign in to comment.