GCC Code Coverage Report
Directory: . Exec Total Coverage
File: src/options/managed_streams.cpp Lines: 3 37 8.1 %
Date: 2021-09-07 Branches: 2 72 2.8 %

Line Exec Source
1
/******************************************************************************
2
 * Top contributors (to current version):
3
 *   Gereon Kremer
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
 * Wrappers to handle memory management of streams.
14
 *
15
 * This file contains wrappers to handle special cases of managing memory
16
 * related to streams stored in options.
17
 */
18
19
#include "options/managed_streams.h"
20
21
#include <string.h>
22
23
#include <cerrno>
24
#include <fstream>
25
#include <iostream>
26
#include <sstream>
27
28
#include "options/option_exception.h"
29
30
namespace cvc5 {
31
32
namespace detail {
33
34
std::string cvc5_errno_failreason()
35
{
36
#if HAVE_STRERROR_R
37
#if STRERROR_R_CHAR_P
38
  if (errno != 0)
39
  {
40
    // GNU version of strerror_r: *might* use the given buffer,
41
    // or might not.  It returns a pointer to buf, or not.
42
    char buf[80];
43
    return std::string(strerror_r(errno, buf, sizeof buf));
44
  }
45
  else
46
  {
47
    return "unknown reason";
48
  }
49
#else  /* STRERROR_R_CHAR_P */
50
  if (errno != 0)
51
  {
52
    // XSI version of strerror_r: always uses the given buffer.
53
    // Returns an error code.
54
    char buf[80];
55
    if (strerror_r(errno, buf, sizeof buf) == 0)
56
    {
57
      return std::string(buf);
58
    }
59
    else
60
    {
61
      // some error occurred while getting the error string
62
      return "unknown reason";
63
    }
64
  }
65
  else
66
  {
67
    return "unknown reason";
68
  }
69
#endif /* STRERROR_R_CHAR_P */
70
#else  /* HAVE_STRERROR_R */
71
  return "unknown reason";
72
#endif /* HAVE_STRERROR_R */
73
}
74
75
std::ostream* openOStream(const std::string& filename)
76
{
77
  errno = 0;
78
  std::ostream* res;
79
  res = new std::ofstream(filename);
80
  if (res == nullptr || !*res)
81
  {
82
    std::stringstream ss;
83
    ss << "Cannot open file: `" << filename << "': " << cvc5_errno_failreason();
84
    throw OptionException(ss.str());
85
  }
86
  return res;
87
}
88
std::istream* openIStream(const std::string& filename)
89
{
90
  errno = 0;
91
  std::istream* res;
92
  res = new std::ifstream(filename);
93
  if (res == nullptr || !*res)
94
  {
95
    std::stringstream ss;
96
    ss << "Cannot open file: `" << filename << "': " << cvc5_errno_failreason();
97
    throw OptionException(ss.str());
98
  }
99
  return res;
100
}
101
}  // namespace detail
102
103
12280
std::ostream* ManagedErr::defaultValue() const { return &std::cerr; }
104
bool ManagedErr::specialCases(const std::string& value)
105
{
106
  if (value == "stderr" || value == "--")
107
  {
108
    d_stream.reset();
109
    return true;
110
  }
111
  return false;
112
}
113
114
std::istream* ManagedIn::defaultValue() const { return &std::cin; }
115
bool ManagedIn::specialCases(const std::string& value)
116
{
117
  if (value == "stdin" || value == "--")
118
  {
119
    d_stream.reset();
120
    return true;
121
  }
122
  return false;
123
}
124
125
311945
std::ostream* ManagedOut::defaultValue() const { return &std::cout; }
126
bool ManagedOut::specialCases(const std::string& value)
127
{
128
  if (value == "stdout" || value == "--")
129
  {
130
    d_stream.reset();
131
    return true;
132
  }
133
  return false;
134
}
135
136
29502
}  // namespace cvc5