File: FileConnection.h

package info (click to toggle)
r-cran-sourcetools 0.1.7-1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 308 kB
  • sloc: cpp: 1,985; ansic: 505; sh: 10; makefile: 2
file content (50 lines) | stat: -rw-r--r-- 870 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef SOURCETOOLS_READ_WINDOWS_FILE_CONNECTION_H
#define SOURCETOOLS_READ_WINDOWS_FILE_CONNECTION_H

#undef Realloc
#undef Free
#include <windows.h>

namespace sourcetools {
namespace detail {

class FileConnection
{
public:
  typedef HANDLE FileDescriptor;

  FileConnection(const char* path, int flags = GENERIC_READ)
  {
    handle_ = ::CreateFile(path, flags, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
  }

  ~FileConnection()
  {
    if (open())
      ::CloseHandle(handle_);
  }

  bool open()
  {
    return handle_ != INVALID_HANDLE_VALUE;
  }

  bool size(std::size_t* pSize)
  {
    *pSize = ::GetFileSize(handle_, NULL);
    return true;
  }

  operator FileDescriptor() const
  {
    return handle_;
  }

private:
  FileDescriptor handle_;
};

} // namespace detail
} // namespace sourcetools

#endif /* SOURCETOOLS_READ_WINDOWS_FILE_CONNECTION_H */