CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Line Parser

Modules

 Line Parser Flags
 
 Pull Mode
 
 Push Mode
 

Functions

void lparseDestroy (LineParser **lp)
 

Detailed Description

Stream buffer consumer that parses line-by-line input.

The line parser reads a stream buffer one line at a time. It handles the different line ending conventions (LF, CRLF, or mixed) and works in both stream buffer modes.

Common Use Cases:

Pull Mode: Create the parser with lparseCreatePull(), then call lparseLine() for each line.

Push Mode: Create the parser with lparseCreatePush() and give it a callback that runs for each line as data arrives.

Either way, destroy the parser with lparseDestroy() when done. A push parser is registered as the stream buffer's consumer and detaches when it is destroyed.

Example (pull mode):

VFSFile *file = vfsOpen(vfs, _SL("config.txt"), FS_Read);
StreamBuffer *sb = sbufCreate(4096);
sbufFilePRegisterPull(sb, file, true);
LineParser *lp = lparseCreatePull(sb, LPARSE_Auto);
string line = 0;
while (lparseLine(lp, &line)) {
// process line
}
strDestroy(&line);
@ FS_Read
Open for reading.
Definition fs.h:321
VFSFile * vfsOpen(VFS *vfs, strref path, flags_t flags)
@ LPARSE_Auto
Auto-detect line ending style from first line found.
Definition lineparse.h:78
bool lparseLine(LineParser *lp, string *out)
#define lparseCreatePull(sb,...)
Definition lineparse.h:143
void lparseDestroy(LineParser **lp)
#define sbufCreate(targetsz,...)
Definition streambuf.h:275
void sbufFinish(StreamBuffer **sb)
void strDestroy(strhandle ps)
#define _SL(s)
Inline ASCII string literal with compile-time embedded length (STR_LEN8). Content must be < 200 bytes...
Definition strliteral.h:207

Example (push mode):

bool processLine(stvlist *cvars, strref line) {
// process each line
return true; // continue parsing
}
VFSFile *file = vfsOpen(vfs, _SL("data.txt"), FS_Read);
StreamBuffer *sb = sbufCreate(4096);
LineParser *lp = lparseCreatePush(sb, closureCreateAs(lparseLineCB, processLine, stvNone),
sbufFileIn(sb, file, true); // calls processLine for each line
sbufClose(sb); // flushes a last line with no EOL
#define closureCreateAs(sigtype, func,...)
Definition closure.h:168
@ LPARSE_LF
Expect LF (Unix-style) line endings.
Definition lineparse.h:82
#define lparseCreatePush(sb, pline,...)
Definition lineparse.h:210
bool(* lparseLineCB)(stvlist *cvars, strref line)
Definition lineparse.h:171
void sbufClose(StreamBuffer *sb)
void sbufRelease(StreamBuffer **sb)
#define stvNone
Definition stvar.h:178

Function Documentation

◆ lparseDestroy()

void lparseDestroy ( LineParser **  lp)

Destroys a line parser.

A push parser detaches from its stream buffer here, which runs the cleanup callback it was created with. Does not end the stream, and does not release the caller's reference to it.

Parameters
lpPointer to the line parser