libyang 6.4.3
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
Loading...
Searching...
No Matches
utils.h File Reference

Utilities and generic types of libyang. More...

#include <stdint.h>
#include <time.h>
Include dependency graph for utils.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define LY_DATA_TYPE_COUNT   20
 
#define LY_LIST_FOR(START, ELEM)
 Macro to iterate via all sibling elements without affecting the list itself.
 
#define LY_LIST_FOR_SAFE(START, NEXT, ELEM)
 Macro to iterate via all sibling elements allowing to modify the list itself (e.g. removing elements)
 

Enumerations

enum  LY_DATA_TYPE {
  LY_TYPE_UNKNOWN = 0 , LY_TYPE_BINARY , LY_TYPE_UINT8 , LY_TYPE_UINT16 ,
  LY_TYPE_UINT32 , LY_TYPE_UINT64 , LY_TYPE_STRING , LY_TYPE_BITS ,
  LY_TYPE_BOOL , LY_TYPE_DEC64 , LY_TYPE_EMPTY , LY_TYPE_ENUM ,
  LY_TYPE_IDENT , LY_TYPE_INST , LY_TYPE_LEAFREF , LY_TYPE_UNION ,
  LY_TYPE_INT8 , LY_TYPE_INT16 , LY_TYPE_INT32 , LY_TYPE_INT64
}
 YANG built-in types. More...
 
enum  LY_VALUE_FORMAT {
  LY_VALUE_CANON , LY_VALUE_SCHEMA , LY_VALUE_SCHEMA_RESOLVED , LY_VALUE_XML ,
  LY_VALUE_JSON , LY_VALUE_CBOR , LY_VALUE_LYB , LY_VALUE_STR_NS
}
 All kinds of supported value formats and prefix mappings to modules. More...
 

Functions

LIBYANG_API_DECL LY_ERR ly_pattern_compile (const struct ly_ctx *ctx, const char *pattern, void **pat_comp)
 Compile an XML Schema regex pattern prior to matching.
 
LIBYANG_API_DECL void ly_pattern_free (void *pat_comp)
 Free a compiled XML Schema regex pattern.
 
LIBYANG_API_DECL LY_ERR ly_pattern_match (const struct ly_ctx *ctx, const char *pattern, const char *string, uint32_t str_len, void **pat_comp)
 Check a string matches an XML Schema regex used in YANG.
 
LIBYANG_API_DECL LY_ERR ly_time_str2time (const char *value, time_t *time, char **fractions_s)
 Convert date-and-time from string to UNIX timestamp and fractions of a second.
 
LIBYANG_API_DECL LY_ERR ly_time_str2ts (const char *value, struct timespec *ts)
 Convert date-and-time from string to timespec.
 
LIBYANG_API_DECL LY_ERR ly_time_time2str (time_t time, const char *fractions_s, char **str)
 Convert UNIX timestamp and fractions of a second into canonical date-and-time string value.
 
LIBYANG_API_DECL LY_ERR ly_time_ts2str (const struct timespec *ts, char **str)
 Convert timespec into date-and-time string value.
 
LIBYANG_API_DECL int ly_time_tz_offset (void)
 Get current timezone (including DST setting) UTC (GMT) time offset in seconds.
 
LIBYANG_API_DECL int ly_time_tz_offset_at (time_t time)
 Get UTC (GMT) timezone offset in seconds at a specific timestamp (including DST setting).
 

Variables

const char * ly_data_type2str [20]
 Stringfield YANG built-in data types.
 

Detailed Description

Utilities and generic types of libyang.

Author
Michal Vasko mvask.nosp@m.o@ce.nosp@m.snet..nosp@m.cz

Copyright (c) 2026 CESNET, z.s.p.o.

This source code is licensed under BSD 3-Clause License (the "License"). You may not use this file except in compliance with the License. You may obtain a copy of the License at

https://opensource.org/licenses/BSD-3-Clause

Definition in file utils.h.

Macro Definition Documentation

◆ LY_DATA_TYPE_COUNT

#define LY_DATA_TYPE_COUNT   20

Number of different types

Definition at line 83 of file utils.h.

◆ LY_LIST_FOR

#define LY_LIST_FOR (   START,
  ELEM 
)
Value:
for ((ELEM) = (START); \
(ELEM); \
(ELEM) = (ELEM)->next)

Macro to iterate via all sibling elements without affecting the list itself.

Works for all types of nodes despite it is data or schema tree, but all the parameters must be pointers to the same type.

Use with opening curly bracket ({). All parameters must be of the same type.

Parameters
STARTPointer to the starting element.
ELEMIterator.

Definition at line 39 of file utils.h.

◆ LY_LIST_FOR_SAFE

#define LY_LIST_FOR_SAFE (   START,
  NEXT,
  ELEM 
)
Value:
for ((ELEM) = (START); \
(ELEM) ? (NEXT = (ELEM)->next, 1) : 0; \
(ELEM) = (NEXT))

Macro to iterate via all sibling elements allowing to modify the list itself (e.g. removing elements)

Use with opening curly bracket ({). All parameters must be of the same type.

Parameters
STARTPointer to the starting element.
NEXTTemporary storage to allow removing of the current iterator content.
ELEMIterator.

Definition at line 53 of file utils.h.

Enumeration Type Documentation

◆ LY_DATA_TYPE

YANG built-in types.

Enumerator
LY_TYPE_UNKNOWN 

Unknown type

LY_TYPE_BINARY 

Any binary data (RFC 6020 sec 9.8)

LY_TYPE_UINT8 

8-bit unsigned integer (RFC 6020 sec 9.2)

LY_TYPE_UINT16 

16-bit unsigned integer (RFC 6020 sec 9.2)

LY_TYPE_UINT32 

32-bit unsigned integer (RFC 6020 sec 9.2)

LY_TYPE_UINT64 

64-bit unsigned integer (RFC 6020 sec 9.2)

LY_TYPE_STRING 

Human-readable string (RFC 6020 sec 9.4)

LY_TYPE_BITS 

A set of bits or flags (RFC 6020 sec 9.7)

LY_TYPE_BOOL 

"true" or "false" (RFC 6020 sec 9.5)

LY_TYPE_DEC64 

64-bit signed decimal number (RFC 6020 sec 9.3)

LY_TYPE_EMPTY 

A leaf that does not have any value (RFC 6020 sec 9.11)

LY_TYPE_ENUM 

Enumerated strings (RFC 6020 sec 9.6)

LY_TYPE_IDENT 

A reference to an abstract identity (RFC 6020 sec 9.10)

LY_TYPE_INST 

References a data tree node (RFC 6020 sec 9.13)

LY_TYPE_LEAFREF 

A reference to a leaf instance (RFC 6020 sec 9.9)

LY_TYPE_UNION 

Choice of member types (RFC 6020 sec 9.12)

LY_TYPE_INT8 

8-bit signed integer (RFC 6020 sec 9.2)

LY_TYPE_INT16 

16-bit signed integer (RFC 6020 sec 9.2)

LY_TYPE_INT32 

32-bit signed integer (RFC 6020 sec 9.2)

LY_TYPE_INT64 

64-bit signed integer (RFC 6020 sec 9.2)

Definition at line 61 of file utils.h.

◆ LY_VALUE_FORMAT

All kinds of supported value formats and prefix mappings to modules.

Enumerator
LY_VALUE_CANON 

canonical value, prefix mapping is type-specific

LY_VALUE_SCHEMA 

YANG schema value, prefixes map to YANG import prefixes

LY_VALUE_SCHEMA_RESOLVED 

resolved YANG schema value, prefixes map to module structures directly

LY_VALUE_XML 

XML data value, prefixes map to XML namespace prefixes

LY_VALUE_JSON 

JSON data value, prefixes map to module names

LY_VALUE_CBOR 

CBOR data value, prefixes map to module names (same as JSON)

LY_VALUE_LYB 

LYB data binary value, prefix mapping is type-specific (but usually like JSON)

LY_VALUE_STR_NS 

any data format value, prefixes map to XML namespace prefixes

Definition at line 93 of file utils.h.

Function Documentation

◆ ly_pattern_compile()

LIBYANG_API_DECL LY_ERR ly_pattern_compile ( const struct ly_ctx ctx,
const char *  pattern,
void **  pat_comp 
)

Compile an XML Schema regex pattern prior to matching.

Parameters
[in]ctxOptional context for storing errors.
[in]patternRegular expression pattern to use.
[out]pat_compCompiled pattern to be used by ly_pattern_match(). Free it using ly_pattern_free().
Returns
LY_SUCCESS on success;
LY_ERR on error.

◆ ly_pattern_free()

LIBYANG_API_DECL void ly_pattern_free ( void *  pat_comp)

Free a compiled XML Schema regex pattern.

Parameters
[in]pat_compCompiled pattern to free.

◆ ly_pattern_match()

LIBYANG_API_DECL LY_ERR ly_pattern_match ( const struct ly_ctx ctx,
const char *  pattern,
const char *  string,
uint32_t  str_len,
void **  pat_comp 
)

Check a string matches an XML Schema regex used in YANG.

Parameters
[in]ctxOptional context for storing errors.
[in]patternRegular expression pattern to use.
[in]stringString to match.
[in]str_lenLength of string, may be 0 if string is 0-terminated.
[in,out]pat_compOptional pointer to pattern code. If set and NULL, it is returned. If set and non-NULL, it is used directly for matching instead of compiling pattern. Free it using ly_pattern_free().
Returns
LY_SUCCESS on a match;
LY_ENOT if the string does not match;
LY_ERR on error.

◆ ly_time_str2time()

LIBYANG_API_DECL LY_ERR ly_time_str2time ( const char *  value,
time_t *  time,
char **  fractions_s 
)

Convert date-and-time from string to UNIX timestamp and fractions of a second.

Parameters
[in]valueValid string date-and-time value, the string may continue after the value (be longer).
[out]timeUNIX timestamp.
[out]fractions_sOptional fractions of a second, set to NULL if none.
Returns
LY_ERR value.

◆ ly_time_str2ts()

LIBYANG_API_DECL LY_ERR ly_time_str2ts ( const char *  value,
struct timespec *  ts 
)

Convert date-and-time from string to timespec.

Parameters
[in]valueValid string date-and-time value, the string may continue after the value (be longer).
[out]tsTimespec.
Returns
LY_ERR value.

◆ ly_time_time2str()

LIBYANG_API_DECL LY_ERR ly_time_time2str ( time_t  time,
const char *  fractions_s,
char **  str 
)

Convert UNIX timestamp and fractions of a second into canonical date-and-time string value.

Parameters
[in]timeUNIX timestamp.
[in]fractions_sFractions of a second, if any.
[out]strString date-and-time value in the local timezone.
Returns
LY_ERR value.

◆ ly_time_ts2str()

LIBYANG_API_DECL LY_ERR ly_time_ts2str ( const struct timespec *  ts,
char **  str 
)

Convert timespec into date-and-time string value.

Parameters
[in]tsTimespec.
[out]strString date-and-time value in the local timezone.
Returns
LY_ERR value.

◆ ly_time_tz_offset()

LIBYANG_API_DECL int ly_time_tz_offset ( void  )

Get current timezone (including DST setting) UTC (GMT) time offset in seconds.

Returns
Timezone shift in seconds.

◆ ly_time_tz_offset_at()

LIBYANG_API_DECL int ly_time_tz_offset_at ( time_t  time)

Get UTC (GMT) timezone offset in seconds at a specific timestamp (including DST setting).

Parameters
[in]timeTimestamp to get the offset at.
Returns
Timezone shift in seconds.

Variable Documentation

◆ ly_data_type2str

const char* ly_data_type2str[20]
extern

Stringfield YANG built-in data types.