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 |
|
std::string cvc5_errno_failreason() |
33 |
|
{ |
34 |
|
#if HAVE_STRERROR_R |
35 |
|
#if STRERROR_R_CHAR_P |
36 |
|
if (errno != 0) |
37 |
|
{ |
38 |
|
// GNU version of strerror_r: *might* use the given buffer, |
39 |
|
// or might not. It returns a pointer to buf, or not. |
40 |
|
char buf[80]; |
41 |
|
return std::string(strerror_r(errno, buf, sizeof buf)); |
42 |
|
} |
43 |
|
else |
44 |
|
{ |
45 |
|
return "unknown reason"; |
46 |
|
} |
47 |
|
#else /* STRERROR_R_CHAR_P */ |
48 |
|
if (errno != 0) |
49 |
|
{ |
50 |
|
// XSI version of strerror_r: always uses the given buffer. |
51 |
|
// Returns an error code. |
52 |
|
char buf[80]; |
53 |
|
if (strerror_r(errno, buf, sizeof buf) == 0) |
54 |
|
{ |
55 |
|
return std::string(buf); |
56 |
|
} |
57 |
|
else |
58 |
|
{ |
59 |
|
// some error occurred while getting the error string |
60 |
|
return "unknown reason"; |
61 |
|
} |
62 |
|
} |
63 |
|
else |
64 |
|
{ |
65 |
|
return "unknown reason"; |
66 |
|
} |
67 |
|
#endif /* STRERROR_R_CHAR_P */ |
68 |
|
#else /* HAVE_STRERROR_R */ |
69 |
|
return "unknown reason"; |
70 |
|
#endif /* HAVE_STRERROR_R */ |
71 |
|
} |
72 |
|
|
73 |
|
namespace detail { |
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 |
12304 |
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 |
312086 |
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 |
29574 |
} // namespace cvc5 |