sendfile_defs_default.hpp
Go to the documentation of this file.
/*
restinio
*/
#pragma once
#include <cstdio>
namespace restinio
{
using file_descriptor_t = std::FILE*;
using file_offset_t = std::int64_t;
using file_size_t = std::uint64_t;
constexpr file_descriptor_t null_file_descriptor(){ return nullptr; }
inline file_descriptor_t
open_file( const char * file_path )
{
file_descriptor_t file_descriptor = std::fopen( file_path, "rb" );
if( null_file_descriptor() == file_descriptor )
{
throw exception_t{ fmt::format( "std::fopen failed: '{}'", file_path ) };
}
return file_descriptor;
}
template < typename META >
META
get_file_meta( file_descriptor_t fd )
{
file_size_t fsize = 0;
if( null_file_descriptor() != fd )
{
// Obtain file size:
if( 0 == std::fseek( fd , 0 , SEEK_END ) )
{
const auto end_pos = std::ftell( fd );
if( -1 != end_pos )
{
fsize = static_cast< file_size_t >( end_pos );
std::rewind( fd );
}
else
{
throw exception_t{ "std::ftell failed" };
}
}
else
{
throw exception_t{ "std::fseek failed" };
}
}
// No way to get last modification,
// Use current time instead.
return META{ fsize, std::chrono::system_clock::now() };
}
inline void
close_file( file_descriptor_t fd )
{
std::fclose( fd );
}
} /* namespace restinio */
restinio::exception_t
Exception class for all exceptions thrown by RESTinio.
Definition: exception.hpp:26
restinio
Definition: asio_include.hpp:21
restinio::null_file_descriptor
constexpr file_descriptor_t null_file_descriptor()
Get file descriptor which stands for null.
Definition: sendfile_defs_default.hpp:36
restinio::file_size_t
std::uint64_t file_size_t
Definition: sendfile_defs_default.hpp:23
restinio::close_file
void close_file(file_descriptor_t fd)
Close file by its descriptor.
Definition: sendfile_defs_default.hpp:89
restinio::file_offset_t
std::int64_t file_offset_t
Definition: sendfile_defs_default.hpp:22
restinio::file_descriptor_t
std::FILE * file_descriptor_t
Definition: sendfile_defs_default.hpp:21
restinio::open_file
file_descriptor_t open_file(const char *file_path)
Open file.
Definition: sendfile_defs_default.hpp:40
restinio::get_file_meta
META get_file_meta(file_descriptor_t fd)
Get file size.
Definition: sendfile_defs_default.hpp:55
SEEK_END
#define SEEK_END
Definition: zconf.h:500