UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
ufo::FileHandler Class Reference

RAII wrapper around a C std::FILE* handle. More...

#include <lib/io/include/ufo/io/file_handler.hpp>

Public Member Functions

 FileHandler ()=default
 Constructs an empty (closed) file handler.
 
 FileHandler (FileHandler &&other) noexcept
 Move-constructs by transferring ownership of the file handle.
 
 FileHandler (FileHandler const &)=delete
 Copying a FILE* handle has no meaningful semantics.
 
 FileHandler (std::filesystem::path const &file, std::string_view modes)
 Opens file in the given modes.
 
 ~FileHandler ()
 Closes the managed file handle (if open).
 
void close () noexcept
 Closes the managed file handle and resets the pointer to null.
 
std::FILE * get () const noexcept
 Returns the underlying FILE*.
 
void open (std::filesystem::path const &file, std::string_view modes)
 Closes any currently open file, then opens file in modes.
 
 operator bool () const noexcept
 Returns true if the file is currently open (handle is non-null).
 
FileHandleroperator= (FileHandler &&other) noexcept
 Move-assigns by closing any currently open file, then taking ownership.
 
FileHandleroperator= (FileHandler const &)=delete
 
char * readline ()
 Reads one line from the file into the internal buffer.
 

Static Public Attributes

static constexpr std::size_t buffer_size = 1024
 Maximum number of characters (including the null terminator) read by readline().
 

Detailed Description

RAII wrapper around a C std::FILE* handle.

FileHandler manages the lifetime of a C file pointer obtained via std::fopen, ensuring the file is properly closed when the handler goes out of scope or is reassigned. It is move-only — copying a FILE* has no meaningful semantics.

A fixed-size internal buffer supports line-by-line reading via readline().

Example

ufo::FileHandler fh("data.txt", "r");
if (!fh) throw std::runtime_error("failed to open");
while (char* line = fh.readline()) {
// process line
}
RAII wrapper around a C std::FILE* handle.

Definition at line 72 of file file_handler.hpp.

Constructor & Destructor Documentation

◆ FileHandler() [1/2]

ufo::FileHandler::FileHandler ( std::filesystem::path const &  file,
std::string_view  modes 
)

Opens file in the given modes.

Parameters
filePath to the file to open.
modesNull-terminated fopen mode string (e.g. "r", "wb").

Definition at line 51 of file file_handler.cpp.

◆ FileHandler() [2/2]

ufo::FileHandler::FileHandler ( FileHandler &&  other)
noexcept

Move-constructs by transferring ownership of the file handle.

Postcondition
other is left in the closed (null) state.

Definition at line 56 of file file_handler.cpp.

◆ ~FileHandler()

ufo::FileHandler::~FileHandler ( )

Closes the managed file handle (if open).

Definition at line 70 of file file_handler.cpp.

Member Function Documentation

◆ close()

void ufo::FileHandler::close ( )
noexcept

Closes the managed file handle and resets the pointer to null.

No-op if the handle is already closed.

Definition at line 78 of file file_handler.cpp.

◆ get()

std::FILE * ufo::FileHandler::get ( ) const
noexcept

Returns the underlying FILE*.

Returns
The managed file pointer, or nullptr if not open.

Definition at line 86 of file file_handler.cpp.

◆ open()

void ufo::FileHandler::open ( std::filesystem::path const &  file,
std::string_view  modes 
)

Closes any currently open file, then opens file in modes.

Parameters
filePath to the file to open.
modesNull-terminated fopen mode string (e.g. "r", "wb").

Definition at line 72 of file file_handler.cpp.

◆ operator bool()

ufo::FileHandler::operator bool ( ) const
explicitnoexcept

Returns true if the file is currently open (handle is non-null).

Definition at line 93 of file file_handler.cpp.

◆ operator=()

FileHandler & ufo::FileHandler::operator= ( FileHandler &&  other)
noexcept

Move-assigns by closing any currently open file, then taking ownership.

Self-assignment is handled safely.

Postcondition
other is left in the closed (null) state.

Definition at line 61 of file file_handler.cpp.

◆ readline()

char * ufo::FileHandler::readline ( )

Reads one line from the file into the internal buffer.

Wraps std::fgets. Reads at most buffer_size - 1 characters (plus a null terminator). The newline character, if present, is retained in the buffer.

Returns
Pointer to the internal buffer on success, or nullptr at EOF or on error. The returned pointer is valid until the next call to readline() or until the handler is destroyed / closed.

Definition at line 88 of file file_handler.cpp.

Member Data Documentation

◆ buffer_size

constexpr std::size_t ufo::FileHandler::buffer_size = 1024
staticconstexpr

Maximum number of characters (including the null terminator) read by readline().

Definition at line 76 of file file_handler.hpp.


The documentation for this class was generated from the following files: