Skip to content

Commit 1c78643

Browse files
author
Meher C
committed
fix: rename cbor.c/h to lcbor.c/h, update CMakeLists.txt and migrate to current API
- Rename src/cbor.c -> src/lcbor.c and src/cbor.h -> src/lcbor.h to avoid collision with system libcbor headers - Update CMakeLists.txt references to use new filenames - Update #include directives in parser_cbor.c and printer_cbor.c - Migrate printer_cbor.c to current libyang API (LYD_PRINT_SIBLINGS, plugins_internal.h, simplified anydata handling) - Address PR review feedback in parser_cbor.c
1 parent 93d4684 commit 1c78643

File tree

10 files changed

+606
-783
lines changed

10 files changed

+606
-783
lines changed

CMakeLists.txt

Lines changed: 17 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ option(ENABLE_YANGLINT_INTERACTIVE "Enable interactive CLI yanglint" ON)
257257
option(ENABLE_TOOLS "Build binary tools 'yanglint' and 'yangre'" ON)
258258
option(ENABLE_COMMON_TARGETS "Define common custom target names such as 'doc' or 'uninstall', may cause conflicts when using add_subdirectory() to build this project" ON)
259259
option(BUILD_SHARED_LIBS "By default, shared libs are enabled. Turn off for a static build." ON)
260-
option(ENABLE_CBOR_SUPPORT "Enable CBOR support with libcbor" ON)
261260
set(YANG_MODULE_DIR "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_DATADIR}/yang/modules/libyang" CACHE STRING "Directory where to copy the YANG modules to")
262261

263262
if(ENABLE_INTERNAL_DOCS)
@@ -325,40 +324,14 @@ if(ENABLE_COVERAGE)
325324
gen_coverage_enable(${ENABLE_TESTS})
326325
endif()
327326

328-
if(ENABLE_CBOR_SUPPORT)
329-
find_package(PkgConfig)
330-
if(PKG_CONFIG_FOUND)
331-
pkg_check_modules(LIBCBOR REQUIRED libcbor)
332-
if(LIBCBOR_FOUND)
333-
message(STATUS "libcbor found, enabling CBOR support")
334-
add_definitions(-DENABLE_CBOR_SUPPORT)
335-
include_directories(${LIBCBOR_INCLUDE_DIRS})
336-
# Add CBOR parser files to the library sources
337-
list(APPEND libsrc src/parser_cbor.c src/lcbor.c src/printer_cbor.c)
338-
list(APPEND headers src/lcbor.h)
339-
# Add CBOR files to format sources
340-
list(APPEND format_sources src/parser_cbor.c src/lcbor.h src/lcbor.c src/printer_cbor.c)
341-
else()
342-
message(FATAL_ERROR "libcbor not found! Please install libcbor development package or disable CBOR support with -DENABLE_CBOR_SUPPORT=OFF")
343-
endif()
344-
else()
345-
# Fallback to find_path and find_library if pkg-config is not available
346-
find_path(LIBCBOR_INCLUDE_DIR cbor.h)
347-
find_library(LIBCBOR_LIBRARY cbor)
348-
if(LIBCBOR_INCLUDE_DIR AND LIBCBOR_LIBRARY)
349-
message(STATUS "libcbor found via find_path/find_library, enabling CBOR support")
350-
add_definitions(-DENABLE_CBOR_SUPPORT)
351-
include_directories(${LIBCBOR_INCLUDE_DIR})
352-
set(LIBCBOR_LIBRARIES ${LIBCBOR_LIBRARY})
353-
# Add CBOR parser files to the library sources
354-
list(APPEND libsrc src/parser_cbor.c src/lcbor.c src/printer_cbor.c)
355-
list(APPEND headers src/lcbor.h)
356-
# Add CBOR files to format sources
357-
list(APPEND format_sources src/parser_cbor.c src/lcbor.h src/lcbor.c src/printer_cbor.c)
358-
else()
359-
message(FATAL_ERROR "libcbor not found! Please install libcbor development package or disable CBOR support with -DENABLE_CBOR_SUPPORT=OFF")
360-
endif()
361-
endif()
327+
find_package(CBOR)
328+
if(CBOR_FOUND)
329+
set(CBOR_SUPPORT ON)
330+
list(APPEND libsrc src/parser_cbor.c src/lcbor.c src/printer_cbor.c)
331+
list(APPEND headers src/lcbor.h)
332+
list(APPEND format_sources src/parser_cbor.c src/lcbor.h src/lcbor.c src/printer_cbor.c)
333+
else()
334+
message(STATUS "libcbor not found, CBOR support disabled")
362335
endif()
363336

364337
if ("${BUILD_TYPE_UPPER}" STREQUAL "DEBUG")
@@ -465,9 +438,15 @@ find_package(PCRE2 10.21 REQUIRED)
465438
include_directories(${PCRE2_INCLUDE_DIRS})
466439
target_link_libraries(yang ${PCRE2_LIBRARIES})
467440

468-
# link libcbor if CBOR support is enabled
469-
if(ENABLE_CBOR_SUPPORT)
470-
target_link_libraries(yang ${LIBCBOR_LIBRARIES})
441+
# link libcbor if found
442+
if(CBOR_FOUND)
443+
if(TARGET yangobj)
444+
target_compile_definitions(yangobj PRIVATE ENABLE_CBOR_SUPPORT)
445+
target_include_directories(yangobj PRIVATE ${CBOR_INCLUDE_DIR})
446+
endif()
447+
target_compile_definitions(yang PRIVATE ENABLE_CBOR_SUPPORT)
448+
target_include_directories(yang PRIVATE ${CBOR_INCLUDE_DIR})
449+
target_link_libraries(yang ${CBOR_LIBRARY})
471450
endif()
472451

473452
# XXHash include and library

CMakeModules/FindCBOR.cmake

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Try to find libcbor
2+
# Once done this will define
3+
#
4+
# Read-Only variables:
5+
# CBOR_FOUND - system has libcbor
6+
# CBOR_INCLUDE_DIR - the libcbor include directory
7+
# CBOR_LIBRARY - Link these to use libcbor
8+
9+
find_path(CBOR_INCLUDE_DIR
10+
NAMES
11+
cbor.h
12+
PATHS
13+
/usr/include
14+
/usr/local/include
15+
/opt/local/include
16+
/sw/include
17+
${CMAKE_INCLUDE_PATH}
18+
${CMAKE_INSTALL_PREFIX}/include
19+
)
20+
21+
find_library(CBOR_LIBRARY
22+
NAMES
23+
cbor
24+
libcbor
25+
PATHS
26+
/usr/lib
27+
/usr/lib64
28+
/usr/local/lib
29+
/usr/local/lib64
30+
/opt/local/lib
31+
/sw/lib
32+
${CMAKE_LIBRARY_PATH}
33+
${CMAKE_INSTALL_PREFIX}/lib
34+
)
35+
36+
include(FindPackageHandleStandardArgs)
37+
find_package_handle_standard_args(CBOR FOUND_VAR CBOR_FOUND REQUIRED_VARS CBOR_INCLUDE_DIR CBOR_LIBRARY)

src/lcbor.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @file lcbor.h
2+
* @file lcbor.c
33
* @author MeherRushi <meherrrushi2@gmail.com>
44
* @brief CBOR data parser for libyang (abstraction over libcbor)
55
*
@@ -12,8 +12,6 @@
1212
* https://opensource.org/licenses/BSD-3-Clause
1313
*/
1414

15-
#ifdef ENABLE_CBOR_SUPPORT
16-
1715
#include <assert.h>
1816
#include <ctype.h>
1917
#include <errno.h>
@@ -55,8 +53,10 @@ lycbor_token2str(enum cbor_type cbortype)
5553
*/
5654
void lycbor_ctx_free(struct lycbor_ctx *cborctx)
5755
{
58-
if (cborctx)
59-
{
56+
if (cborctx) {
57+
if (cborctx->cbor_data) {
58+
cbor_decref(&cborctx->cbor_data);
59+
}
6060
free(cborctx);
6161
}
6262
}
@@ -108,24 +108,22 @@ lycbor_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lycbor_ctx **c
108108
cborctx->format = format;
109109

110110
/* load and parse CBOR data */
111-
cborctx->cbor_data = cbor_load(in->current, in->length, &result);
111+
cborctx->cbor_data = cbor_load((cbor_data)in->current, in->length, &result);
112112
if (!cborctx->cbor_data) {
113-
LOGVAL(ctx, LYVE_SYNTAX, "Failed to parse CBOR data.");
113+
LOGVAL(ctx, NULL, LYVE_SYNTAX, "Failed to parse CBOR data.");
114114
free(cborctx);
115115
return LY_EVALID;
116116
}
117117
if (result.error.code != CBOR_ERR_NONE) {
118-
LOGVAL(ctx, LYVE_SYNTAX, "CBOR parsing error (code %d).", result.error.code);
118+
LOGVAL(ctx, NULL, LYVE_SYNTAX, "CBOR parsing error (code %d).", result.error.code);
119119
cbor_decref(&cborctx->cbor_data);
120120
free(cborctx);
121121
return LY_EVALID;
122122
}
123123

124124
/* input line logging */
125-
ly_log_location(NULL, NULL, NULL, in);
125+
ly_log_location(NULL, NULL, in);
126126

127127
*cborctx_p = cborctx;
128128
return ret;
129129
}
130-
131-
#endif /* ENABLE_CBOR_SUPPORT */

src/lcbor.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@
1212
* https://opensource.org/licenses/BSD-3-Clause
1313
*/
1414

15-
#ifndef LY_LCBOR_H_
16-
#define LY_LCBOR_H_
17-
18-
#ifdef ENABLE_CBOR_SUPPORT
15+
#ifndef LY_CBOR_H_
16+
#define LY_CBOR_H_
1917

2018
#include <stddef.h>
2119
#include <stdint.h>
@@ -53,6 +51,14 @@ struct lycbor_ctx {
5351
} backup;
5452
};
5553

54+
/**
55+
* @brief Get a human-readable name for a CBOR type.
56+
*
57+
* @param[in] cbortype CBOR type.
58+
* @return String representation of the CBOR type.
59+
*/
60+
const char *lycbor_token2str(enum cbor_type cbortype);
61+
5662
/**
5763
* @brief Create new CBOR context for parsing.
5864
*
@@ -72,6 +78,4 @@ lycbor_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lycbor_ctx **c
7278
void
7379
lycbor_ctx_free(struct lycbor_ctx *cborctx);
7480

75-
#endif /* ENABLE_CBOR_SUPPORT */
76-
77-
#endif /* LY_LCBOR_H_ */
81+
#endif /* LY_CBOR_H_ */

0 commit comments

Comments
 (0)