Ver Fonte

add Filename::get_temp_directory()

David Rose há 18 anos atrás
pai
commit
9433628a0e
2 ficheiros alterados com 45 adições e 0 exclusões
  1. 40 0
      dtool/src/dtoolutil/filename.cxx
  2. 5 0
      dtool/src/dtoolutil/filename.h

+ 40 - 0
dtool/src/dtoolutil/filename.cxx

@@ -51,6 +51,8 @@
 #include <unistd.h>
 #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

+ 5 - 0
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;