Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ add_subdirectory(base64)
add_subdirectory(bfdev)
add_subdirectory(bloom)
add_subdirectory(btree)
add_subdirectory(buff)
add_subdirectory(cache)
add_subdirectory(circle)
add_subdirectory(crc)
Expand Down
2 changes: 2 additions & 0 deletions examples/buff/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# SPDX-License-Identifier: GPL-2.0-or-later
/buff-string
22 changes: 22 additions & 0 deletions examples/buff/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Copyright(c) 2025 ffashion <helloworldffashion@gmail.com>
#

add_executable(buff-string string.c)
target_link_libraries(buff-string bfdev)
add_test(buff-string buff-string)

if(${CMAKE_PROJECT_NAME} STREQUAL "bfdev")
install(FILES
string.c
DESTINATION
${CMAKE_INSTALL_DOCDIR}/examples/buff
)

install(TARGETS
buff-string
DESTINATION
${CMAKE_INSTALL_DOCDIR}/bin
)
endif()
18 changes: 18 additions & 0 deletions examples/buff/string.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright(c) 2025 John Sanpe <sanpeqf@gmail.com>
*/

#define MODULE_NAME "string-simple"
#define bfdev_log_fmt(fmt) MODULE_NAME ": " fmt

#include <stdio.h>
#include <bfdev/buff.h>

int
main(int argc, const char *argv[])
{
BFDEV_DEFINE_STRING(test, "Hello World!\n");
fwrite(test.data, test.len, 1, stdout);
return 0;
}
61 changes: 61 additions & 0 deletions include/bfdev/buff.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
* Copyright(c) 2024 John Sanpe <sanpeqf@gmail.com>
*/

#ifndef _BFDEV_BUFF_H_
#define _BFDEV_BUFF_H_

#include <bfdev/config.h>
#include <bfdev/types.h>
#include <bfdev/string.h>

BFDEV_BEGIN_DECLS

typedef struct bfdev_buff bfdev_buff_t;

struct bfdev_buff {
bfdev_size_t len;
void *data;
};

#define BFDEV_BUFF_STATIC(LEN, DATA) { \
.len = (LEN), .data = (void *)(DATA), \
}

#define BFDEV_BUFF_INIT(len, data) \
(bfdev_buff_t) BFDEV_BUFF_STATIC(len, data)

#define BFDEV_DEFINE_BUFF(name, len, data) \
bfdev_buff_t name = BFDEV_BUFF_INIT(len, data)

#define BFDEV_DEFINE_STRING(name, str) \
BFDEV_DEFINE_BUFF(name, sizeof(str) - 1, str)

static inline void
bfdev_buff_init(bfdev_buff_t *buff, bfdev_size_t len, void *data)
{
*buff = BFDEV_BUFF_INIT(len, data);
}

static inline void
bfdev_string_init(bfdev_buff_t *buff, const char *str)
{
*buff = BFDEV_BUFF_INIT(bfdev_strlen(str), str);
}

static inline void *
bfdev_buff_data(bfdev_buff_t *buff)
{
return buff->data;
}

static inline bfdev_size_t
bfdev_buff_len(bfdev_buff_t *buff)
{
return buff->len;
}

BFDEV_END_DECLS

#endif /* _BFDEV_BUFF_H_ */
11 changes: 11 additions & 0 deletions include/bfdev/template/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <bfdev/config.h>
#include <bfdev/array.h>
#include <bfdev/buff.h>

BFDEV_BEGIN_DECLS

Expand All @@ -32,6 +33,16 @@ bfdev_array_append(bfdev_array_t *array, const void *data, unsigned long num);
extern int
bfdev_array_append_array(bfdev_array_t *array, const bfdev_array_t *append);

/**
* bfdev_array_append_buff() - append buff into the array.
* @array: the array object.
* @append: the buff to append.
*
* Return 0 on success or a negative error code on failure.
*/
extern int
bfdev_array_append_buff(bfdev_array_t *array, bfdev_buff_t *append);

/**
* bfdev_array_append_cstr() - append cstr into the array.
* @array: the array object.
Expand Down
15 changes: 15 additions & 0 deletions template/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ bfdev_array_append_array(bfdev_array_t *array, const bfdev_array_t *append)
return -BFDEV_ENOERR;
}

export int
bfdev_array_append_buff(bfdev_array_t *array, bfdev_buff_t *append)
{
int retval;

if (array->cells != BFDEV_BYTES_PER_U8)
return -BFDEV_EPROTO;

retval = bfdev_array_append(array, append->data, append->len);
if (bfdev_unlikely(retval))
return retval;

return -BFDEV_ENOERR;
}

export int
bfdev_array_append_cstr(bfdev_array_t *array, const char *append)
{
Expand Down
Loading