@@ -13,14 +13,15 @@ SPDX-License-Identifier: MIT
13
13
#include < fstream>
14
14
#include < iostream>
15
15
#include < locale>
16
+ #include < string>
16
17
#include < vector>
17
18
18
19
#include " fatal.hpp"
19
20
#include " system.hpp"
20
21
21
22
22
23
static inline void readBinaryStream (
23
- const char * streamName,
24
+ const std::string & streamName,
24
25
std::istream &is,
25
26
std::vector<unsigned char > &bin)
26
27
{
@@ -33,7 +34,8 @@ static inline void readBinaryStream(
33
34
}
34
35
bin.push_back ((char )chr);
35
36
}
36
- fatalExitWithMessage (streamName, " : error reading " );
37
+ fatalExitWithMessage (
38
+ streamName, " : error reading (" , iga::LastErrorString (), " )" );
37
39
}
38
40
39
41
#define IGA_STDIN_FILENAME std::string (" std::cin" )
@@ -47,84 +49,91 @@ static inline std::vector<unsigned char> readBinaryStreamStdin()
47
49
}
48
50
49
51
static inline void readBinaryFile (
50
- const char * fileName, std::vector<unsigned char > &bin)
52
+ const std::string & fileName, std::vector<unsigned char > &bin)
51
53
{
52
54
std::ifstream is (fileName, std::ios::binary);
53
- if (!is.is_open ()) {
54
- fatalExitWithMessage (fileName, " : failed to open file" );
55
+ if (!is.good ()) {
56
+ fatalExitWithMessage (
57
+ fileName, " : failed to open file (" , iga::LastErrorString (), " )" );
55
58
}
56
59
readBinaryStream (fileName, is, bin);
57
60
}
58
61
59
62
static inline std::string readTextStream (
60
- const char * streamName,
63
+ const std::string & streamName,
61
64
std::istream &is)
62
65
{
63
66
std::string s;
64
67
is.clear ();
65
68
s.append (std::istreambuf_iterator<char >(is),
66
69
std::istreambuf_iterator<char >());
67
70
if (!is.good ()) {
68
- fatalExitWithMessage (streamName, " : error reading" );
71
+ fatalExitWithMessage (
72
+ streamName, " : error reading (" , iga::LastErrorString (), " )" );
69
73
}
70
74
return s;
71
75
}
72
76
73
- static inline std::string readTextFile (
74
- const char *fileName)
77
+ static inline std::string readTextFile (const std::string &fileName)
75
78
{
76
79
std::ifstream file (fileName);
77
80
if (!file.good ()) {
78
- fatalExitWithMessage (fileName, " : failed to open file" );
81
+ fatalExitWithMessage (
82
+ fileName, " : failed to open file (" , iga::LastErrorString (), " )" );
79
83
}
80
- return readTextStream (fileName,file);
84
+ return readTextStream (fileName, file);
81
85
}
82
86
83
87
static inline void writeTextStream (
84
- const char * streamName, std::ostream &os, const char *output)
88
+ const std::string & streamName, std::ostream &os, const char *output)
85
89
{
86
90
os.clear ();
87
91
os << output;
88
92
if (!os.good ()) {
89
- fatalExitWithMessage (streamName, " : error writing" );
93
+ fatalExitWithMessage (
94
+ streamName, " : error writing (" , iga::LastErrorString (), " )" );
90
95
}
91
96
}
92
97
93
- static inline void writeTextFile (const char *fileName, const char *output)
98
+ static inline void writeTextFile (
99
+ const std::string &fileName, const char *output)
94
100
{
95
101
std::ofstream file (fileName);
96
102
if (!file.good ()) {
97
- fatalExitWithMessage (fileName, " : failed to open file" );
103
+ fatalExitWithMessage (
104
+ fileName, " : failed to open file (" , iga::LastErrorString (), " )" );
98
105
}
99
106
writeTextStream (fileName, file, output);
100
107
}
101
108
102
109
static inline void writeBinaryStream (
103
- const char * streamName,
110
+ const std::string & streamName,
104
111
std::ostream &os,
105
112
const void *bits,
106
113
size_t bitsLen)
107
114
{
108
115
os.clear ();
109
116
os.write ((const char *)bits, bitsLen);
110
117
if (!os.good ()) {
111
- fatalExitWithMessage (streamName, " : error writing stream" );
118
+ fatalExitWithMessage (
119
+ streamName, " : error writing stream (" , iga::LastErrorString (), " )" );
112
120
}
113
121
}
114
122
115
123
static inline void writeBinaryFile (
116
- const char * fileName,
117
- const void *bits,
118
- size_t bitsLen)
124
+ const std::string & fileName,
125
+ const void *bits,
126
+ size_t bitsLen)
119
127
{
120
- std::ofstream file (fileName,std::ios::binary);
128
+ std::ofstream file (fileName, std::ios::binary);
121
129
if (!file.good ()) {
122
- fatalExitWithMessage (fileName, " : failed to open file" );
130
+ fatalExitWithMessage (
131
+ fileName, " : failed to open file (" , iga::LastErrorString (), " )" );
123
132
}
124
- writeBinaryStream (fileName,file,bits,bitsLen);
133
+ writeBinaryStream (fileName, file, bits, bitsLen);
125
134
}
126
135
127
- static inline bool doesFileExist (const char * fileName) {
136
+ static inline bool doesFileExist (const std::string & fileName) {
128
137
return iga::DoesFileExist (fileName);
129
138
}
130
139
0 commit comments