CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
File Producers

Functions

bool _sbufFileIn (StreamBuffer *sb, File *file, bool close)
 
bool _sbufFilePRegisterPull (StreamBuffer *sb, File *file, bool close)
 

Detailed Description

Functions for using files as stream buffer data sources.

Function Documentation

◆ _sbufFileIn()

bool _sbufFileIn ( StreamBuffer *  sb,
File file,
bool  close 
)

bool sbufFileIn(StreamBuffer *sb, File *file, bool close)

Reads a file and pushes its entire contents into a stream buffer.

Automatically chunks the data based on the stream buffer's target size for efficient operation. The stream buffer is automatically finished after all data is read.

If flow control is active (see sbufSetWatermark()), this waits for the consumer to make room rather than dropping data, so the consumer has to be draining the buffer from another thread.

Parameters
sbThe stream buffer
fileFile to read from (optionally closed based on close parameter)
closeIf true, the file is closed after reading
Returns
true on success, false on error

Example:

VFSFile *file = vfsOpen(vfs, _SL("data.txt"), FS_Read);
StreamBuffer *sb = sbufCreate(4096);
sbufStrCRegisterPush(sb, &output);
sbufFileIn(sb, file, true); // file is closed automatically
@ FS_Read
Open for reading.
Definition fs.h:321
VFSFile * vfsOpen(VFS *vfs, strref path, flags_t flags)
#define sbufCreate(targetsz,...)
Definition streambuf.h:275
bool sbufStrCRegisterPush(StreamBuffer *sb, string *strout)
#define _SL(s)
Inline ASCII string literal with compile-time embedded length (STR_LEN8). Content must be < 200 bytes...
Definition strliteral.h:207

◆ _sbufFilePRegisterPull()

bool _sbufFilePRegisterPull ( StreamBuffer *  sb,
File file,
bool  close 
)

bool sbufFilePRegisterPull(StreamBuffer *sb, File *file, bool close)

Registers a file as a producer with the stream buffer in pull mode.

In pull mode, the consumer pulls data as needed, and the file is read in chunks on demand. Use this instead of sbufFileIn() when you need finer control over when data is read.

The registration holds its own reference to the file until the producer slot is handed back, so the caller may release its own at any time after this returns.

Parameters
sbThe stream buffer
fileFile to read from. With close set, the caller's reference is handed over with it and the handle must not be used again
closeIf true, the file is closed when the producer finishes
Returns
true on success, false if registration failed