Skip to content

nmeasim::core::track::xml

namespace nmeasim::core::track::xml · Library internals

Helpers on top of pugixml shared by the GPX and KML readers of nmeasim::core::track; private to the core library.

Elements are matched on their local name, ignoring any namespace prefix, so that files written with or without a prefix (gpx:trkpt, trkpt) parse the same way. Numbers are parsed independently of the process locale.

Summary

Functions

Name Description
trim() Removes leading and trailing whitespace.
child_text() Returns the text of the first child element with a given local name.
parse_number() Parses a decimal number independently of the process locale.
load_document() Parses XML text into a document and formats a one-line reason when it is not well formed.
local_name() Returns an element name without its namespace prefix.
is_named() Tells whether a node has a given local name, whatever its namespace prefix.
child() Finds the first child element with a given local name.

Functions

trim()

std::string_view trim(std::string_view text) noexcept

Removes leading and trailing whitespace.

Parameters

Name Type Description
text std::string_view The text to trim.

Returns std::string_view: A view into text without the whitespace, as classified by std::isspace in the current C locale, at either end; valid as long as the viewed characters are.

Declared in src/core/src/track/xml_helpers.hpp:74 · defined in src/core/src/track/xml_helpers.cpp:14

child_text()

std::string child_text(const pugi::xml_node& parent, const char* local)

Returns the text of the first child element with a given local name.

Parameters

Name Type Description
parent const pugi::xml_node& The element whose children are searched.
local const char* The local name of the child.

Returns std::string: The child's first text or CDATA node with surrounding whitespace removed, as pugixml's child_value returns it; empty when the child is absent or has no text.

Preconditions

  • local is not null.

Declared in src/core/src/track/xml_helpers.hpp:67 · defined in src/core/src/track/xml_helpers.cpp:24

parse_number()

std::optional<double> parse_number(std::string_view text)

Parses a decimal number independently of the process locale.

The number is read with the classic locale, so the decimal separator is always .; a sign and an exponent (1.5e3) are accepted.

Parameters

Name Type Description
text std::string_view The text to parse; surrounding whitespace is ignored.

Returns std::optional<double>: The value, or std::nullopt when text is empty, is not a number, has anything after the number, or is out of the range of double.

Declared in src/core/src/track/xml_helpers.hpp:84 · defined in src/core/src/track/xml_helpers.cpp:32

load_document()

bool load_document(pugi::xml_document& document, std::string_view xml, std::string* error)

Parses XML text into a document and formats a one-line reason when it is not well formed.

Parameters

Name Type Description
document (out) pugi::xml_document& Receives the parsed tree; its previous content is discarded.
xml std::string_view The XML text. pugixml copies it, so it need not outlive the call; the encoding is detected automatically.
error std::string* Receives Invalid XML at offset N: DESCRIPTION on failure, where N is the byte offset of the error in xml and DESCRIPTION pugixml's text; left unchanged on success. May be null.

Returns bool: True when the document was parsed, false when it is not well-formed XML, including empty input, which has no document element.

Declared in src/core/src/track/xml_helpers.hpp:96 · defined in src/core/src/track/xml_helpers.cpp:48

local_name()

const char* local_name(const char* qualified) noexcept

Returns an element name without its namespace prefix.

Parameters

Name Type Description
qualified const char* A qualified name such as gx:Track.

Returns const char*: A pointer into qualified just after its first :, or qualified itself when it has none. It is valid as long as qualified is.

Preconditions

  • qualified is not null.

Declared in src/core/src/track/xml_helpers.hpp:27

is_named()

bool is_named(const pugi::xml_node& node, const char* local) noexcept

Tells whether a node has a given local name, whatever its namespace prefix.

Parameters

Name Type Description
node const pugi::xml_node& The node to test; nodes that are not elements have an empty name.
local const char* The local name to compare with, case-sensitively.

Returns bool: True when the local name of node equals local.

Preconditions

  • local is not null.

Declared in src/core/src/track/xml_helpers.hpp:38

child()

pugi::xml_node child(const pugi::xml_node& parent, const char* local) noexcept

Finds the first child element with a given local name.

Only direct children are searched.

Parameters

Name Type Description
parent const pugi::xml_node& The element whose children are searched.
local const char* The local name to look for.

Returns pugi::xml_node: The first child element of parent whose local name is local, or an empty (null) node when there is none. It refers into the document that owns parent.

Preconditions

  • local is not null.

Declared in src/core/src/track/xml_helpers.hpp:51