1
2
3
4
5
6 #ifndef SOULNG_FILE_PTR_INCLUDED
7 #define SOULNG_FILE_PTR_INCLUDED
8 #include <soulng/util/FileLocking.hpp>
9 #include <cstdio>
10 #include <cstring>
11 #include <string>
12
13 namespace soulng { namespace util {
14
15 class FilePtr
16 {
17 public:
18 FilePtr(FILE* fp_, const std::string& fileName_, LockKind lockKind_) : fp(fp_), fileName(fileName_), lockKind(lockKind_)
19 {
20 }
21 ~FilePtr()
22 {
23 if (fp != nullptr)
24 {
25 std::fclose(fp);
26 UnlockFile(fileName, lockKind);
27 }
28 }
29 operator FILE*() const { return fp; }
30 private:
31 FILE* fp;
32 std::string fileName;
33 LockKind lockKind;
34 };
35
36 FILE* OpenRead(const char* fileName);
37 FILE* OpenWrite(const char* fileName);
38
39 } }
40
41 #endif // SOULNG_FILE_PTR_INCLUDED