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 (58 lines) | stat: -rw-r--r-- 857 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
51
52
53
54
55
56
57
58
#ifndef SOURCETOOLS_READ_POSIX_FILE_CONNECTION_H
#define SOURCETOOLS_READ_POSIX_FILE_CONNECTION_H

#include <cstddef>

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

namespace sourcetools {
namespace detail {

class FileConnection
{
public:

  typedef int FileDescriptor;

  FileConnection(const char* path, int flags = O_RDONLY)
  {
    fd_ = ::open(path, flags);
  }

  ~FileConnection()
  {
    if (open())
      ::close(fd_);
  }

  bool open()
  {
    return fd_ != -1;
  }

  bool size(std::size_t* pSize)
  {
    struct stat info;
    if (::fstat(fd_, &info) == -1)
      return false;

    *pSize = info.st_size;
    return true;
  }

  operator FileDescriptor() const
  {
    return fd_;
  }

private:
  FileDescriptor fd_;
};


} // namespace detail
} // namespace sourcetools

#endif /* SOURCETOOLS_READ_POSIX_FILE_CONNECTION_H */