![]() |
O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
|
Buffered file reader. More...
#include "o2s/input_stream.h"
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
Go to the source code of this file.
Data Structures | |
struct | ifstream_t |
File Input Stream. More... |
Macros | |
#define | FileInputStream |
Use the RAII idiom with a file input stream. |
Functions | |
void | file_default_signal_handler (int, siginfo_t *, void *) |
Default signal handler. | |
Contructors and destructor | |
ifstream_t | file_open (const char *file_name, int flags) |
Construct a file input stream : Open the file and allocate the buffer. | |
ifstream_t | file_from_descriptor (int descriptor) |
Construct a file input stream from an externally managed file descriptor. | |
void | file_close (ifstream_t *file) |
Release resources to the system. | |
Reading | |
ssize_t | file_single_read (ifstream_t *file) |
Read as much as possible. | |
bool | file_accumulate (ifstream_t *file, size_t count) |
If possible, accumulate count bytes before returning. | |
bool | file_accumulate_infinite (ifstream_t *file, size_t count) |
If possible, accumulate count bytes before returning. | |
void | file_stop_reading (void) |
Prevent the accumulate function from calling read in this thread. | |
void | file_resume_reading (void) |
Resume calling read in this thread. |
Buffered file reader.
Definition in file file_input_stream.h.
#define FileInputStream |
Use the RAII idiom with a file input stream.
In a situation where one wants to declare an ifstream variable on the stack in the local scope, this "typedef" can be used for the file to be closed automatically when the variable goes out of scope.
It means this "typedef" can only be used like this :
Definition at line 68 of file file_input_stream.h.
bool file_accumulate | ( | ifstream_t * | file, |
size_t | count ) |
bool file_accumulate_infinite | ( | ifstream_t * | file, |
size_t | count ) |
If possible, accumulate count
bytes before returning.
This alternative keeps trying upon reaching the end of the file, waiting for new data to be written. It is suitable when reading pipes, serial ports and such infinite streams.
To set a timeout, create a timer that emits a signal with file_stop_reading as a signal handler, for the current read call to be interrupted and to break out of the loop.
void file_close | ( | ifstream_t * | file | ) |
ssize_t file_single_read | ( | ifstream_t * | file | ) |
void file_stop_reading | ( | void | ) |
Prevent the accumulate function from calling read in this thread.
This allows setting a thread specific timeout, that interrupts the current read, and exits the accumulating loop. The effect is thread_local, so multiple files can have their own timeouts in parallel.