From 9433628a0ee0aab3de599074166bbe9fa8689235 Mon Sep 17 00:00:00 2001 From: David Rose Date: Sun, 13 May 2007 15:59:29 +0000 Subject: [PATCH] add Filename::get_temp_directory() --- dtool/src/dtoolutil/filename.cxx | 40 ++++++++++++++++++++++++++++++++ dtool/src/dtoolutil/filename.h | 5 ++++ 2 files changed, 45 insertions(+) diff --git a/dtool/src/dtoolutil/filename.cxx b/dtool/src/dtoolutil/filename.cxx index af32b98986..017a8e5eac 100644 --- a/dtool/src/dtoolutil/filename.cxx +++ b/dtool/src/dtoolutil/filename.cxx @@ -51,6 +51,8 @@ #include #endif +bool Filename::_got_temp_directory; +Filename Filename::_temp_directory; TypeHandle Filename::_type_handle; #ifdef WIN32 @@ -436,6 +438,44 @@ temporary(const string &dirname, const string &prefix, const string &suffix, return result; } +//////////////////////////////////////////////////////////////////// +// Function: Filename::get_temp_directory +// Access: Published +// Description: Returns a path to a system-defined temporary +// directory. +//////////////////////////////////////////////////////////////////// +const Filename &Filename:: +get_temp_directory() { + if (!_got_temp_directory) { +#ifdef WIN32 + static const size_t buffer_size = 4096; + char buffer[buffer_size]; + if (GetTempPath(buffer_size, buffer) != 0) { + _temp_directory = from_os_specific(buffer); + if (_temp_directory.is_directory()) { + if (_temp_directory.make_canonical()) { + _got_temp_directory = true; + } + } + } + + if (!_got_temp_directory) { + // if $TMP or $TEMP contain bogus strings, GetTempPath() may + // return a bogus string. + _temp_directory = ExecutionEnvironment::get_cwd(); + _got_temp_directory = true; + } + +#else + // Posix case. + _temp_directory = "/tmp"; + _got_temp_directory = true; +#endif // WIN32 + } + + return _temp_directory; +} + //////////////////////////////////////////////////////////////////// // Function: Filename::set_fullpath // Access: Published diff --git a/dtool/src/dtoolutil/filename.h b/dtool/src/dtoolutil/filename.h index 7852b5ee72..fb5177a382 100644 --- a/dtool/src/dtoolutil/filename.h +++ b/dtool/src/dtoolutil/filename.h @@ -87,6 +87,8 @@ PUBLISHED: const string &suffix = string(), Type type = T_general); + static const Filename &get_temp_directory(); + // Assignment is via the = operator. INLINE Filename &operator = (const string &filename); INLINE Filename &operator = (const char *filename); @@ -218,6 +220,9 @@ protected: int _flags; + static bool _got_temp_directory; + static Filename _temp_directory; + public: static TypeHandle get_class_type() { return _type_handle;