Branch data Line data Source code
1 : : /****************************************************************************** 2 : : * Top contributors (to current version): 3 : : * Andres Noetzli, Aina Niemetz 4 : : * 5 : : * This file is part of the cvc5 project. 6 : : * 7 : : * Copyright (c) 2009-2025 by the authors listed in the file AUTHORS 8 : : * in the top-level source directory and their institutional affiliations. 9 : : * All rights reserved. See the file COPYING in the top-level source 10 : : * directory for licensing information. 11 : : * **************************************************************************** 12 : : * 13 : : * Some standard STL-related utility functions for cvc5. 14 : : */ 15 : : 16 : : #include "util/utility.h" 17 : : 18 : : #include <unistd.h> 19 : : 20 : : #include <cstdlib> 21 : : 22 : : #include "base/check.h" 23 : : 24 : : namespace cvc5::internal { 25 : : 26 : 0 : std::unique_ptr<std::fstream> openTmpFile(std::string* pattern) 27 : : { 28 : 0 : char* tmpDir = getenv("TMPDIR"); 29 [ - - ]: 0 : if (tmpDir != nullptr) 30 : : { 31 : 0 : *pattern = std::string(tmpDir) + "/" + *pattern; 32 : : } 33 : : else 34 : : { 35 : 0 : *pattern = "/tmp/" + *pattern; 36 : : } 37 : : 38 : : // Note: With C++17, we can avoid creating a copy using std::string::data(). 39 : 0 : char* tmpName = new char[pattern->size() + 1]; 40 : 0 : pattern->copy(tmpName, pattern->size()); 41 : 0 : tmpName[pattern->size()] = '\0'; 42 : 0 : int r = mkstemp(tmpName); 43 [ - - ]: 0 : if (r == -1) 44 : : { 45 : 0 : CVC5_FATAL() << "Could not create temporary file " << *pattern; 46 : : } 47 : 0 : std::unique_ptr<std::fstream> tmpStream(new std::fstream(tmpName)); 48 : 0 : close(r); 49 : 0 : *pattern = std::string(tmpName); 50 [ - - ]: 0 : delete[] tmpName; 51 : 0 : return tmpStream; 52 : : } 53 : : 54 : : } // namespace cvc5::internal