@@ -13,14 +13,15 @@ SPDX-License-Identifier: MIT
1313#include < fstream>
1414#include < iostream>
1515#include < locale>
16+ #include < string>
1617#include < vector>
1718
1819#include " fatal.hpp"
1920#include " system.hpp"
2021
2122
2223static inline void readBinaryStream (
23- const char * streamName,
24+ const std::string & streamName,
2425 std::istream &is,
2526 std::vector<unsigned char > &bin)
2627{
@@ -33,7 +34,8 @@ static inline void readBinaryStream(
3334 }
3435 bin.push_back ((char )chr);
3536 }
36- fatalExitWithMessage (streamName, " : error reading " );
37+ fatalExitWithMessage (
38+ streamName, " : error reading (" , iga::LastErrorString (), " )" );
3739}
3840
3941#define IGA_STDIN_FILENAME std::string (" std::cin" )
@@ -47,84 +49,91 @@ static inline std::vector<unsigned char> readBinaryStreamStdin()
4749}
4850
4951static inline void readBinaryFile (
50- const char * fileName, std::vector<unsigned char > &bin)
52+ const std::string & fileName, std::vector<unsigned char > &bin)
5153{
5254 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 (), " )" );
5558 }
5659 readBinaryStream (fileName, is, bin);
5760}
5861
5962static inline std::string readTextStream (
60- const char * streamName,
63+ const std::string & streamName,
6164 std::istream &is)
6265{
6366 std::string s;
6467 is.clear ();
6568 s.append (std::istreambuf_iterator<char >(is),
6669 std::istreambuf_iterator<char >());
6770 if (!is.good ()) {
68- fatalExitWithMessage (streamName, " : error reading" );
71+ fatalExitWithMessage (
72+ streamName, " : error reading (" , iga::LastErrorString (), " )" );
6973 }
7074 return s;
7175}
7276
73- static inline std::string readTextFile (
74- const char *fileName)
77+ static inline std::string readTextFile (const std::string &fileName)
7578{
7679 std::ifstream file (fileName);
7780 if (!file.good ()) {
78- fatalExitWithMessage (fileName, " : failed to open file" );
81+ fatalExitWithMessage (
82+ fileName, " : failed to open file (" , iga::LastErrorString (), " )" );
7983 }
80- return readTextStream (fileName,file);
84+ return readTextStream (fileName, file);
8185}
8286
8387static inline void writeTextStream (
84- const char * streamName, std::ostream &os, const char *output)
88+ const std::string & streamName, std::ostream &os, const char *output)
8589{
8690 os.clear ();
8791 os << output;
8892 if (!os.good ()) {
89- fatalExitWithMessage (streamName, " : error writing" );
93+ fatalExitWithMessage (
94+ streamName, " : error writing (" , iga::LastErrorString (), " )" );
9095 }
9196}
9297
93- static inline void writeTextFile (const char *fileName, const char *output)
98+ static inline void writeTextFile (
99+ const std::string &fileName, const char *output)
94100{
95101 std::ofstream file (fileName);
96102 if (!file.good ()) {
97- fatalExitWithMessage (fileName, " : failed to open file" );
103+ fatalExitWithMessage (
104+ fileName, " : failed to open file (" , iga::LastErrorString (), " )" );
98105 }
99106 writeTextStream (fileName, file, output);
100107}
101108
102109static inline void writeBinaryStream (
103- const char * streamName,
110+ const std::string & streamName,
104111 std::ostream &os,
105112 const void *bits,
106113 size_t bitsLen)
107114{
108115 os.clear ();
109116 os.write ((const char *)bits, bitsLen);
110117 if (!os.good ()) {
111- fatalExitWithMessage (streamName, " : error writing stream" );
118+ fatalExitWithMessage (
119+ streamName, " : error writing stream (" , iga::LastErrorString (), " )" );
112120 }
113121}
114122
115123static 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)
119127{
120- std::ofstream file (fileName,std::ios::binary);
128+ std::ofstream file (fileName, std::ios::binary);
121129 if (!file.good ()) {
122- fatalExitWithMessage (fileName, " : failed to open file" );
130+ fatalExitWithMessage (
131+ fileName, " : failed to open file (" , iga::LastErrorString (), " )" );
123132 }
124- writeBinaryStream (fileName,file,bits,bitsLen);
133+ writeBinaryStream (fileName, file, bits, bitsLen);
125134}
126135
127- static inline bool doesFileExist (const char * fileName) {
136+ static inline bool doesFileExist (const std::string & fileName) {
128137 return iga::DoesFileExist (fileName);
129138}
130139
0 commit comments