summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorAndres Noetzli <andres.noetzli@gmail.com>2019-06-21 10:55:03 -0700
committerGitHub <noreply@github.com>2019-06-21 10:55:03 -0700
commitcf9dfb9be23b4f802989fecd18756ed62aecc8e4 (patch)
tree3f1f45759bf85b451b458aab98cf62892f7aef17 /src/util
parent073335156ff7644364d12a91d4d41af776cfb91b (diff)
Use TMPDIR environment variable for temp files (#2849)
Previously, we were just writing temporary files to `/tmp/` but this commit allows the user to use the `TMPDIR` environment variable to determine which directory the temporary file should be written to. The commit adds a helper function for this and also includes some minor cleanup of existing code.
Diffstat (limited to 'src/util')
-rw-r--r--src/util/CMakeLists.txt1
-rw-r--r--src/util/utility.cpp56
-rw-r--r--src/util/utility.h16
3 files changed, 72 insertions, 1 deletions
diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt
index a17f7c510..f107ad95f 100644
--- a/src/util/CMakeLists.txt
+++ b/src/util/CMakeLists.txt
@@ -47,6 +47,7 @@ libcvc4_add_sources(
statistics_registry.h
tuple.h
unsafe_interrupt_exception.h
+ utility.cpp
utility.h
)
diff --git a/src/util/utility.cpp b/src/util/utility.cpp
new file mode 100644
index 000000000..9936504d2
--- /dev/null
+++ b/src/util/utility.cpp
@@ -0,0 +1,56 @@
+/********************* */
+/*! \file utility.cpp
+ ** \verbatim
+ ** Top contributors (to current version):
+ ** Andres Noetzli
+ ** This file is part of the CVC4 project.
+ ** Copyright (c) 2009-2019 by the authors listed in the file AUTHORS
+ ** in the top-level source directory) and their institutional affiliations.
+ ** All rights reserved. See the file COPYING in the top-level source
+ ** directory for licensing information.\endverbatim
+ **
+ ** \brief Some standard STL-related utility functions for CVC4
+ **
+ ** Some standard STL-related utility functions for CVC4.
+ **/
+
+#include "util/utility.h"
+
+#include <unistd.h>
+
+#include <cstdlib>
+#include <iostream>
+
+#include "base/cvc4_check.h"
+
+namespace CVC4 {
+
+std::fstream openTmpFile(std::string* pattern)
+{
+ char* tmpDir = getenv("TMPDIR");
+ if (tmpDir != nullptr)
+ {
+ *pattern = std::string(tmpDir) + "/" + *pattern;
+ }
+ else
+ {
+ *pattern = "/tmp/" + *pattern;
+ }
+
+ // Note: With C++17, we can avoid creating a copy using std::string::data().
+ char* tmpName = new char[pattern->size() + 1];
+ pattern->copy(tmpName, pattern->size());
+ tmpName[pattern->size()] = '\0';
+ int r = mkstemp(tmpName);
+ if (r == -1)
+ {
+ CVC4_FATAL() << "Could not create temporary file " << *pattern;
+ }
+ std::fstream tmpStream(tmpName);
+ close(r);
+ *pattern = std::string(tmpName);
+ delete[] tmpName;
+ return tmpStream;
+}
+
+} // namespace CVC4
diff --git a/src/util/utility.h b/src/util/utility.h
index 275efd9d0..a606648bd 100644
--- a/src/util/utility.h
+++ b/src/util/utility.h
@@ -20,8 +20,10 @@
#define CVC4__UTILITY_H
#include <algorithm>
-#include <utility>
+#include <fstream>
#include <functional>
+#include <string>
+#include <utility>
namespace CVC4 {
@@ -85,6 +87,18 @@ void container_to_stream(std::ostream& out,
out << postfix;
}
+/**
+ * Opens a new temporary file with a given filename pattern and returns an
+ * fstream to it. The directory that the file is created in is either TMPDIR or
+ * /tmp/ if TMPDIR is not set.
+ *
+ * @param pattern The filename pattern. This string is modified to contain the
+ * name of the temporary file.
+ *
+ * @return A filestream for the temporary file.
+ */
+std::fstream openTmpFile(std::string* pattern);
+
}/* CVC4 namespace */
#endif /* CVC4__UTILITY_H */
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback