GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/util/utility.cpp Lines: 0 16 0.0 %
Date: 2021-03-22 Branches: 0 26 0.0 %

Line Exec Source
1
/*********************                                                        */
2
/*! \file utility.cpp
3
 ** \verbatim
4
 ** Top contributors (to current version):
5
 **   Andres Noetzli
6
 ** This file is part of the CVC4 project.
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.\endverbatim
11
 **
12
 ** \brief Some standard STL-related utility functions for CVC4
13
 **
14
 ** Some standard STL-related utility functions for CVC4.
15
 **/
16
17
#include "util/utility.h"
18
19
#include <unistd.h>
20
21
#include <cstdlib>
22
23
#include "base/check.h"
24
25
namespace CVC4 {
26
27
std::unique_ptr<std::fstream> openTmpFile(std::string* pattern)
28
{
29
  char* tmpDir = getenv("TMPDIR");
30
  if (tmpDir != nullptr)
31
  {
32
    *pattern = std::string(tmpDir) + "/" + *pattern;
33
  }
34
  else
35
  {
36
    *pattern = "/tmp/" + *pattern;
37
  }
38
39
  // Note: With C++17, we can avoid creating a copy using std::string::data().
40
  char* tmpName = new char[pattern->size() + 1];
41
  pattern->copy(tmpName, pattern->size());
42
  tmpName[pattern->size()] = '\0';
43
  int r = mkstemp(tmpName);
44
  if (r == -1)
45
  {
46
    CVC4_FATAL() << "Could not create temporary file " << *pattern;
47
  }
48
  std::unique_ptr<std::fstream> tmpStream(new std::fstream(tmpName));
49
  close(r);
50
  *pattern = std::string(tmpName);
51
  delete[] tmpName;
52
  return tmpStream;
53
}
54
55
}  // namespace CVC4