1 |
|
/****************************************************************************** |
2 |
|
* Top contributors (to current version): |
3 |
|
* Andres Noetzli |
4 |
|
* |
5 |
|
* This file is part of the cvc5 project. |
6 |
|
* |
7 |
|
* Copyright (c) 2009-2021 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 { |
25 |
|
|
26 |
|
std::unique_ptr<std::fstream> openTmpFile(std::string* pattern) |
27 |
|
{ |
28 |
|
char* tmpDir = getenv("TMPDIR"); |
29 |
|
if (tmpDir != nullptr) |
30 |
|
{ |
31 |
|
*pattern = std::string(tmpDir) + "/" + *pattern; |
32 |
|
} |
33 |
|
else |
34 |
|
{ |
35 |
|
*pattern = "/tmp/" + *pattern; |
36 |
|
} |
37 |
|
|
38 |
|
// Note: With C++17, we can avoid creating a copy using std::string::data(). |
39 |
|
char* tmpName = new char[pattern->size() + 1]; |
40 |
|
pattern->copy(tmpName, pattern->size()); |
41 |
|
tmpName[pattern->size()] = '\0'; |
42 |
|
int r = mkstemp(tmpName); |
43 |
|
if (r == -1) |
44 |
|
{ |
45 |
|
CVC5_FATAL() << "Could not create temporary file " << *pattern; |
46 |
|
} |
47 |
|
std::unique_ptr<std::fstream> tmpStream(new std::fstream(tmpName)); |
48 |
|
close(r); |
49 |
|
*pattern = std::string(tmpName); |
50 |
|
delete[] tmpName; |
51 |
|
return tmpStream; |
52 |
|
} |
53 |
|
|
54 |
|
} // namespace cvc5 |