diff options
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/util/utility.cpp | 56 | ||||
-rw-r--r-- | src/util/utility.h | 16 |
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 */ |