O2S C Library 1.8.2
Provide high-level data-structures and other fundamental tools for C projects
Loading...
Searching...
No Matches
file_input_stream.h File Reference

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.

Detailed Description

Buffered file reader.

Author
Antoine GAGNIERE

Definition in file file_input_stream.h.

Macro Definition Documentation

◆ FileInputStream

#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 :

{
...
} // <- The file will be closed at that point
ifstream_t file_open(const char *file_name, int flags)
Construct a file input stream : Open the file and allocate the buffer.
Definition open.c:35
#define FileInputStream
Use the RAII idiom with a file input stream.

Definition at line 68 of file file_input_stream.h.

Function Documentation

◆ file_accumulate()

bool file_accumulate ( ifstream_t * file,
size_t count )

If possible, accumulate count bytes before returning.

This alternative exits upon reaching the end of the file. It is suitable when reading regular files

Returns
a boolean
  • true if n bytes were successfully accumulated
  • false otherwise

Definition at line 87 of file read.c.

◆ file_accumulate_infinite()

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.

Returns
a boolean
  • true if n bytes were successfully accumulated
  • false otherwise

Definition at line 69 of file read.c.

◆ file_close()

void file_close ( ifstream_t * file)

Release resources to the system.

In case of error when calling close, there is absolutely nothing interesting that can be done beyond reporting it, so it is not propagated.

Definition at line 30 of file close.c.

◆ file_single_read()

ssize_t file_single_read ( ifstream_t * file)

Read as much as possible.

Attempt to fill the buffer

Definition at line 38 of file read.c.

◆ file_stop_reading()

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.

Definition at line 100 of file read.c.