Skip to content

Commit

Permalink
Adding utility functions for parsing query params relevant to advance…
Browse files Browse the repository at this point in the history
…d pub/sub.
  • Loading branch information
gmartin82 committed Feb 12, 2025
1 parent 25b8499 commit d78bb69
Show file tree
Hide file tree
Showing 10 changed files with 696 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,7 @@ if(UNIX OR MSVC)
add_executable(z_api_encoding_test ${PROJECT_SOURCE_DIR}/tests/z_api_encoding_test.c)
add_executable(z_refcount_test ${PROJECT_SOURCE_DIR}/tests/z_refcount_test.c)
add_executable(z_lru_cache_test ${PROJECT_SOURCE_DIR}/tests/z_lru_cache_test.c)
add_executable(z_utils_test ${PROJECT_SOURCE_DIR}/tests/z_utils_test.c)

target_link_libraries(z_data_struct_test zenohpico::lib)
target_link_libraries(z_channels_test zenohpico::lib)
Expand All @@ -529,6 +530,7 @@ if(UNIX OR MSVC)
target_link_libraries(z_api_encoding_test zenohpico::lib)
target_link_libraries(z_refcount_test zenohpico::lib)
target_link_libraries(z_lru_cache_test zenohpico::lib)
target_link_libraries(z_utils_test zenohpico::lib)

configure_file(${PROJECT_SOURCE_DIR}/tests/modularity.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/modularity.py COPYONLY)
configure_file(${PROJECT_SOURCE_DIR}/tests/raweth.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/raweth.py COPYONLY)
Expand All @@ -554,6 +556,7 @@ if(UNIX OR MSVC)
add_test(z_api_encoding_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_encoding_test)
add_test(z_refcount_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_refcount_test)
add_test(z_lru_cache_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_lru_cache_test)
add_test(z_utils_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_utils_test)
endif()

if(BUILD_MULTICAST)
Expand Down
25 changes: 24 additions & 1 deletion include/zenoh-pico/collections/ring.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ typedef struct {
size_t _w_idx;
} _z_ring_t;

/**
* An iterator of a ring buffer.
*/
typedef struct {
void *_val;

const _z_ring_t *_ring;
size_t _r_idx;
size_t _w_idx;
} _z_ring_iterator_t;

z_result_t _z_ring_init(_z_ring_t *ring, size_t capacity);
_z_ring_t _z_ring_make(size_t capacity);

Expand All @@ -50,8 +61,13 @@ _z_ring_t *_z_ring_clone(const _z_ring_t *xs, z_element_clone_f d_f);
void _z_ring_clear(_z_ring_t *v, z_element_free_f f);
void _z_ring_free(_z_ring_t **xs, z_element_free_f f_f);

_z_ring_iterator_t _z_ring_iterator_make(const _z_ring_t *ring);
bool _z_ring_iterator_next(_z_ring_iterator_t *iter);
void *_z_ring_iterator_value(const _z_ring_iterator_t *iter);

#define _Z_RING_DEFINE(name, type) \
typedef _z_ring_t name##_ring_t; \
typedef _z_ring_iterator_t name##_ring_iterator_t; \
static inline z_result_t name##_ring_init(name##_ring_t *ring, size_t capacity) { \
return _z_ring_init(ring, capacity); \
} \
Expand All @@ -67,7 +83,14 @@ void _z_ring_free(_z_ring_t **xs, z_element_free_f f_f);
} \
static inline type *name##_ring_pull(name##_ring_t *r) { return (type *)_z_ring_pull(r); } \
static inline void name##_ring_clear(name##_ring_t *r) { _z_ring_clear(r, name##_elem_free); } \
static inline void name##_ring_free(name##_ring_t **r) { _z_ring_free(r, name##_elem_free); }
static inline void name##_ring_free(name##_ring_t **r) { _z_ring_free(r, name##_elem_free); } \
static inline name##_ring_iterator_t name##_ring_iterator_make(const name##_ring_t *ring) { \
return _z_ring_iterator_make(ring); \
} \
static inline bool name##_ring_iterator_next(name##_ring_iterator_t *iter) { return _z_ring_iterator_next(iter); } \
static inline type *name##_ring_iterator_value(const name##_ring_iterator_t *iter) { \
return (type *)_z_ring_iterator_value(iter); \
}

#ifdef __cplusplus
}
Expand Down
43 changes: 43 additions & 0 deletions include/zenoh-pico/utils/query_params.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// Copyright (c) 2025 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//

#ifndef ZENOH_PICO_UTILS_QUERY_PARAMS_H
#define ZENOH_PICO_UTILS_QUERY_PARAMS_H

#include "zenoh-pico/utils/string.h"

#ifdef __cplusplus
extern "C" {
#endif

extern const char *_Z_QUERY_PARAMS_KEY_TIME;

typedef struct {
_z_str_se_t key;
_z_str_se_t value;
} _z_query_param_t;

/**
* Extracts the next query parameter from a `_z_str_se_t` string.
*
* Returns a `_z_query_param_t` with positions of the next key/value in the string if present.
* After invocation `str` will point to the remainder of the string.
*/
_z_query_param_t _z_query_params_next(_z_str_se_t *str);

#ifdef __cplusplus
}
#endif

#endif /* ZENOH_PICO_UTILS_QUERY_PARAMS_H */
5 changes: 5 additions & 0 deletions include/zenoh-pico/utils/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ typedef struct {
char const *delimiter;
} _z_splitstr_t;

/**
* Creates a `_z_str_se_t` from a null-terminated C string.
*/
_z_str_se_t _z_bstrnew(const char *start);

/**
* The reverse equivalent of libc's `strstr`.
*
Expand Down
47 changes: 47 additions & 0 deletions include/zenoh-pico/utils/time_range.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Copyright (c) 2025 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//

#ifndef ZENOH_PICO_UTILS_TIME_RANGE_H
#define ZENOH_PICO_UTILS_TIME_RANGE_H

#include "zenoh-pico/system/platform.h"
#include "zenoh-pico/utils/string.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
enum { _Z_TIME_BOUND_INCLUSIVE, _Z_TIME_BOUND_EXCLUSIVE, _Z_TIME_BOUND_UNBOUNDED } bound;
double now_offset;
} _z_time_bound_t;

typedef struct {
_z_time_bound_t start;
_z_time_bound_t end;
} _z_time_range_t;

/**
* Parse a time range from a string.
*
* Returns true if the string contained a valid time range, false otherwise.
* If valid range will contain the result.
*/
bool _z_time_range_from_str(const char *str, size_t len, _z_time_range_t *range);

#ifdef __cplusplus
}
#endif

#endif /* ZENOH_PICO_UTILS_TIME_RANGE_H */
21 changes: 21 additions & 0 deletions src/collections/ring.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,24 @@ void _z_ring_free(_z_ring_t **r, z_element_free_f free_f) {
*r = NULL;
}
}

_z_ring_iterator_t _z_ring_iterator_make(const _z_ring_t *ring) {
_z_ring_iterator_t iter = {0};

iter._ring = ring;
iter._r_idx = ring->_r_idx;
iter._w_idx = ring->_w_idx;

return iter;
}

bool _z_ring_iterator_next(_z_ring_iterator_t *iter) {
if (iter->_r_idx != iter->_w_idx) {
iter->_val = iter->_ring->_val[iter->_r_idx];
iter->_r_idx = (iter->_r_idx + (size_t)1) % iter->_ring->_capacity;
return true;
}
return false;
}

void *_z_ring_iterator_value(const _z_ring_iterator_t *iter) { return iter->_val; }
49 changes: 49 additions & 0 deletions src/utils/query_params.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//
// Copyright (c) 2025 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//

#include "zenoh-pico/utils/query_params.h"

#include "zenoh-pico/utils/pointers.h"

const char *_Z_QUERY_PARAMS_KEY_TIME = "_time";

static const char *_Z_QUERY_PARAMS_LIST_SEPARATOR = ";";
static const char *_Z_QUERY_PARAMS_FIELD_SEPARATOR = "=";

_z_query_param_t _z_query_params_next(_z_str_se_t *str) {
_z_query_param_t result = {0};

_z_splitstr_t params = {.s = *str, .delimiter = _Z_QUERY_PARAMS_LIST_SEPARATOR};
_z_str_se_t param = _z_splitstr_next(&params);
str->start = params.s.start;
str->end = params.s.end;

if (param.start != NULL) {
_z_splitstr_t kvpair = {.s = param, .delimiter = _Z_QUERY_PARAMS_FIELD_SEPARATOR};
_z_str_se_t key = _z_splitstr_next(&kvpair);

// Set key if length > 0
if (_z_ptr_char_diff(key.end, key.start) > 0) {
result.key.start = key.start;
result.key.end = key.end;

// Set value if length > 0
if (_z_ptr_char_diff(kvpair.s.end, kvpair.s.start) > 0) {
result.value.start = kvpair.s.start;
result.value.end = kvpair.s.end;
}
}
}
return result;
}
Loading

0 comments on commit d78bb69

Please sign in to comment.