RESTinio
Loading...
Searching...
No Matches
sendfile_defs_default.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
11#pragma once
12
13#include <cstdio>
14
15namespace restinio
16{
17
21using file_descriptor_t = std::FILE*;
22using file_offset_t = std::int64_t;
23using file_size_t = std::uint64_t;
25
26
34
36constexpr file_descriptor_t null_file_descriptor(){ return nullptr; }
37
40open_file( const char * file_path )
41{
42 file_descriptor_t file_descriptor = std::fopen( file_path, "rb" );
43
44 if( null_file_descriptor() == file_descriptor )
45 {
46 throw exception_t{
47 fmt::format(
48 RESTINIO_FMT_FORMAT_STRING( "std::fopen failed: '{}'" ),
49 file_path )
50 };
51 }
52
53 return file_descriptor;
54}
55
57template < typename META >
58META
60{
61 file_size_t fsize = 0;
62
63 if( null_file_descriptor() != fd )
64 {
65 // Obtain file size:
66 if( 0 == std::fseek( fd , 0 , SEEK_END ) )
67 {
68 const auto end_pos = std::ftell( fd );
69
70 if( -1 != end_pos )
71 {
72 fsize = static_cast< file_size_t >( end_pos );
73 std::rewind( fd );
74 }
75 else
76 {
77 throw exception_t{ "std::ftell failed" };
78 }
79 }
80 else
81 {
82 throw exception_t{ "std::fseek failed" };
83 }
84 }
85
86 // No way to get last modification,
87 // Use current time instead.
88 return META{ fsize, std::chrono::system_clock::now() };
89}
90
92inline void
94{
95 std::fclose( fd );
96}
98
99} /* namespace restinio */
Exception class for all exceptions thrown by RESTinio.
Definition: exception.hpp:26
#define RESTINIO_FMT_FORMAT_STRING(s)
constexpr file_descriptor_t null_file_descriptor()
Get file descriptor which stands for null.
std::uint64_t file_size_t
void close_file(file_descriptor_t fd)
Close file by its descriptor.
std::int64_t file_offset_t
std::FILE * file_descriptor_t
file_descriptor_t open_file(const char *file_path)
Open file.
META get_file_meta(file_descriptor_t fd)
Get file size.
#define SEEK_END
Definition: zconf.h:500