TSDuck Version 3.32-2820 (TSDuck - The MPEG Transport Stream Toolkit)
ts::TextParser Class Reference

A support class for applications which parse various text formats. More...

Classes

class  Position
 A class which describes a position in the document. More...
 

Public Member Functions

 TextParser (const UString &text, Report &report)
 Constructor. More...
 
 TextParser (const UStringList &lines, Report &report)
 Constructor. More...
 
 TextParser (Report &report)
 Constructor. More...
 
virtual ~TextParser ()
 Destructor.
 
virtual void clear ()
 Clear the document in the parser.
 
bool eof () const
 Check end of file. More...
 
bool eol () const
 Check end of line. More...
 
virtual bool isAtNumberStart () const
 Check if the parser is at the start of a number (digit or minus sign). More...
 
virtual bool isAtXMLNameStart () const
 Check if the parser is at the start of an XML name. More...
 
virtual bool isXMLNameChar (UChar c) const
 Check if a character is suitable in the middle of an XML name. More...
 
virtual bool isXMLNameStartChar (UChar c) const
 Check if a character is suitable for starting an XML name. More...
 
size_t lineNumber () const
 Get the current line number. More...
 
void loadDocument (const UString &text)
 Load the document to parse. More...
 
void loadDocument (const UStringList &lines)
 Load the document to parse from a list of lines. More...
 
bool loadFile (const UString &fileName)
 Load the document to parse from a text file. More...
 
bool loadStream (std::istream &strm)
 Load the document to parse from a text stream. More...
 
bool match (const UString &str, bool skipIfMatch, CaseSensitivity cs=CASE_SENSITIVE)
 Check if the current position in the document matches a string. More...
 
virtual bool parseJSONStringLiteral (UString &str)
 Parse a JSON string literal. More...
 
virtual bool parseNumericLiteral (UString &str, bool allowHexa=false, bool allowFloat=false)
 Parse a numeric literal. More...
 
virtual bool parseStringLiteral (UString &str, UChar requiredQuote=CHAR_NULL)
 Parse a string literal. More...
 
virtual bool parseText (UString &result, const UString endToken, bool skipIfMatch, bool translateEntities)
 Parse text up to a given token. More...
 
virtual bool parseXMLName (UString &name)
 Parse an XML name. More...
 
Position position () const
 Save the position in the document. More...
 
void rewind ()
 Rewind to start of document.
 
bool saveFile (const UString &fileName)
 Save the document to parse to a text file. More...
 
bool saveStream (std::ostream &strm)
 Save the document to parse to a text stream. More...
 
bool seek (const Position &pos)
 Restore a previous position in the document. More...
 
bool skipLine ()
 Skip to next line. More...
 
bool skipWhiteSpace ()
 Skip all whitespaces, including end of lines. More...
 

Detailed Description

A support class for applications which parse various text formats.

Constructor & Destructor Documentation

◆ TextParser() [1/3]

ts::TextParser::TextParser ( Report report)

Constructor.

The document to parse is empty.

Parameters
[in,out]reportWhere to report errors.

◆ TextParser() [2/3]

ts::TextParser::TextParser ( const UStringList lines,
Report report 
)

Constructor.

Parameters
[in]linesReference to a list of text lines forming the document. The lifetime of the referenced list must equals or exceeds the lifetime of the parser.
[in,out]reportWhere to report errors.

◆ TextParser() [3/3]

ts::TextParser::TextParser ( const UString text,
Report report 
)

Constructor.

Parameters
[in]textDocument text to parse with embedded new-line characters.
[in,out]reportWhere to report errors.

Member Function Documentation

◆ loadDocument() [1/2]

void ts::TextParser::loadDocument ( const UStringList lines)

Load the document to parse from a list of lines.

Parameters
[in]linesReference to a list of text lines forming the document. The lifetime of the referenced list must equals or exceeds the lifetime of the parser.

◆ loadDocument() [2/2]

void ts::TextParser::loadDocument ( const UString text)

Load the document to parse.

Parameters
[in]textDocument text to parse with embedded new-line characters.

◆ loadFile()

bool ts::TextParser::loadFile ( const UString fileName)

Load the document to parse from a text file.

Parameters
[in]fileNameName of the file to load.
Returns
True on success, false on failure.

◆ loadStream()

bool ts::TextParser::loadStream ( std::istream &  strm)

Load the document to parse from a text stream.

Parameters
[in,out]strmA standard text stream in input mode.
Returns
True on success, false on error.

◆ saveFile()

bool ts::TextParser::saveFile ( const UString fileName)

Save the document to parse to a text file.

Parameters
[in]fileNameName of the file to save.
Returns
True on success, false on failure.

◆ saveStream()

bool ts::TextParser::saveStream ( std::ostream &  strm)

Save the document to parse to a text stream.

Parameters
[in,out]strmA standard text stream in output mode.
Returns
True on success, false on error.

◆ eof()

bool ts::TextParser::eof ( ) const

Check end of file.

Returns
True if the parser reached the end of file.

◆ eol()

bool ts::TextParser::eol ( ) const

Check end of line.

Returns
True if the parser reached the end of line.

◆ position()

Position ts::TextParser::position ( ) const
inline

Save the position in the document.

Returns
The current position in the document.

◆ seek()

bool ts::TextParser::seek ( const Position pos)

Restore a previous position in the document.

Parameters
[in]posPrevious position to restore.
Returns
True on success, false on error.

◆ lineNumber()

size_t ts::TextParser::lineNumber ( ) const
inline

Get the current line number.

Returns
The current line number.

◆ skipWhiteSpace()

bool ts::TextParser::skipWhiteSpace ( )

Skip all whitespaces, including end of lines.

Note that the optional BOM at start of an UTF-8 file has already been removed by the UTF-16 conversion.

Returns
Always true.

◆ skipLine()

bool ts::TextParser::skipLine ( )

Skip to next line.

Returns
Always true.

◆ match()

bool ts::TextParser::match ( const UString str,
bool  skipIfMatch,
CaseSensitivity  cs = CASE_SENSITIVE 
)

Check if the current position in the document matches a string.

Parameters
[in]strA string to check at the current position in the document.
[in]skipIfMatchIf true and str matches the current position, skip it in the document.
[in]csCase sensitivity of the comparision.
Returns
True if str matches the current position in the document.

◆ parseText()

virtual bool ts::TextParser::parseText ( UString result,
const UString  endToken,
bool  skipIfMatch,
bool  translateEntities 
)
virtual

Parse text up to a given token.

Parameters
[out]resultReturned parsed text.
[in]endTokenStop when this token is found. Do not include endToken in returned string.
[in]skipIfMatchIf true, skip endToken in the parser.
[in]translateEntitiesIf true, translate HTML entities in the text.
Returns
True on success, false if endToken was not found.

◆ isXMLNameStartChar()

virtual bool ts::TextParser::isXMLNameStartChar ( UChar  c) const
virtual

Check if a character is suitable for starting an XML name.

An XML name starts with a letter, underscore or colon.

Parameters
[in]cThe character to check.
Returns
True if c is suitable for starting an XML name.

◆ isXMLNameChar()

virtual bool ts::TextParser::isXMLNameChar ( UChar  c) const
virtual

Check if a character is suitable in the middle of an XML name.

An XML name contains letters, digits, underscores, colons, dots and dashes.

Parameters
[in]cThe character to check.
Returns
True if c is suitable in the middle of an XML name.

◆ isAtXMLNameStart()

virtual bool ts::TextParser::isAtXMLNameStart ( ) const
virtual

Check if the parser is at the start of an XML name.

Returns
True if the parser is at the start of a name.

◆ parseXMLName()

virtual bool ts::TextParser::parseXMLName ( UString name)
virtual

Parse an XML name.

Parameters
[out]nameReturned parsed name.
Returns
True on success, false if no name was not found.

◆ isAtNumberStart()

virtual bool ts::TextParser::isAtNumberStart ( ) const
virtual

Check if the parser is at the start of a number (digit or minus sign).

Returns
True if the parser is at the start of a number.

◆ parseNumericLiteral()

virtual bool ts::TextParser::parseNumericLiteral ( UString str,
bool  allowHexa = false,
bool  allowFloat = false 
)
virtual

Parse a numeric literal.

Parameters
[out]strReturned numeric literal
[in]allowHexaIf true, hexadecimal integers "0x..." are allowed.
[in]allowFloatIt true, floating point values "[-]digits[.digits][e|E[+|-]digits]" are allowed.
Returns
True on success, false if no string literal was not found.

◆ parseStringLiteral()

virtual bool ts::TextParser::parseStringLiteral ( UString str,
UChar  requiredQuote = CHAR_NULL 
)
virtual

Parse a string literal.

A string literal is enclosed in simple or double quotes. Any similar quotation mark is considered part of the string when it is preceded by a backslash character.

Parameters
[out]strReturned string literal, including the first and last quote. When the method returns true (success), it is consequently guaranteed that the length of the returned string is greater than or equal to 2.
[in]requiredQuoteIf defined as single or double quote, accept only that type of quote. Otherwise, accept any of the two.
Returns
True on success, false if no string literal was not found.

◆ parseJSONStringLiteral()

virtual bool ts::TextParser::parseJSONStringLiteral ( UString str)
virtual

Parse a JSON string literal.

Parameters
[out]strReturned decoded value of the string literal.
Returns
True on success, false if no string literal was not found.

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