1
2
3
4
5
6 #include <soulng/util/FilePtr.hpp>
7 #include <soulng/util/TextUtils.hpp>
8 #include <soulng/util/FileLocking.hpp>
9 #include <stdexcept>
10 #include <stdio.h>
11 #include <share.h>
12
13 namespace soulng { namespace util {
14
15 FILE* OpenRead(const char* fileName)
16 {
17 LockFile(fileName, LockKind::read);
18 #if defined(_WIN32) && !defined(__MINGW32__)
19
20 #else
21 FILE* file = std::fopen(fileName, "rb");
22 #endif
23 if (!file)
24 {
25 throw std::runtime_error("could not open '" + std::string(fileName) + "' for reading: " + soulng::util::PlatformStringToUtf8(std::strerror(errno)));
26 }
27 return file;
28 }
29
30 FILE* OpenWrite(const char* fileName)
31 {
32 LockFile(fileName, LockKind::write);
33 #if defined(_WIN32) && !defined(__MINGW32__)
34
35 #else
36 FILE* file = std::fopen(fileName, "wb");
37 #endif
38 if (!file)
39 {
40 throw std::runtime_error("could not open '" + std::string(fileName) + "' for writing: " + soulng::util::PlatformStringToUtf8(std::strerror(errno)));
41 }
42 return file;
43 }
44
45 } }