Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
FileIO.hpp
Go to the documentation of this file.
1#pragma once
3// Portable wrappers for the few file calls used to serve file responses.
4// These are functions rather than `#define close _close`-style macros, which
5// also rewrote every member named close()/open() in the including file.
6#include <cstddef>
7#ifdef _WIN32
8#include <io.h>
9#include <fcntl.h>
10#include <stdio.h>
11#else
12#include <fcntl.h>
13#include <unistd.h>
14#endif
15
17
19inline int open_read_only(const char* path) {
20#ifdef _WIN32
21 return ::_open(path, _O_RDONLY | _O_BINARY);
22#else
23 return ::open(path, O_RDONLY | O_CLOEXEC);
24#endif
25}
26
27inline int close(int fd) {
28#ifdef _WIN32
29 return ::_close(fd);
30#else
31 return ::close(fd);
32#endif
33}
34
36inline long long pread(int fd, void* buf, size_t count, long long offset) {
37#ifdef _WIN32
38 // Each file response owns its descriptor, so moving its position is safe.
39 if (::_lseeki64(fd, offset, SEEK_SET) < 0) return -1;
40 return ::_read(fd, buf, static_cast<unsigned int>(count));
41#else
42 return ::pread(fd, buf, count, static_cast<off_t>(offset));
43#endif
44}
45
46} // namespace utils::file
Definition FileIO.hpp:16
int open_read_only(const char *path)
Opens a file for reading (binary, close-on-exec); -1 on failure.
Definition FileIO.hpp:19
int close(int fd)
Definition FileIO.hpp:27
long long pread(int fd, void *buf, size_t count, long long offset)
Reads up to count bytes at offset (64-bit on every platform); -1 on error.
Definition FileIO.hpp:36