From 0f305ab9fdb8b6caaea6d3866ebbeab112293178 Mon Sep 17 00:00:00 2001 From: Rick Barenthin Date: Tue, 26 Jul 2022 23:47:46 +0200 Subject: [PATCH] feat(str): add a string data structure for easy use --- CMakeLists.txt | 1 + library/str/CMakeLists.txt | 20 +++++++ library/str/include/waitui/str.h | 91 ++++++++++++++++++++++++++++++++ library/str/project-meta-info.in | 3 ++ library/str/src/str.c | 8 +++ 5 files changed, 123 insertions(+) create mode 100644 library/str/CMakeLists.txt create mode 100644 library/str/include/waitui/str.h create mode 100644 library/str/project-meta-info.in create mode 100644 library/str/src/str.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 5023cf5..eb5f848 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ endif () add_subdirectory(app) add_subdirectory(library/list) add_subdirectory(library/log) +add_subdirectory(library/str) if ((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR MODERN_CMAKE_BUILD_TESTING) AND BUILD_TESTING) diff --git a/library/str/CMakeLists.txt b/library/str/CMakeLists.txt new file mode 100644 index 0000000..0e54641 --- /dev/null +++ b/library/str/CMakeLists.txt @@ -0,0 +1,20 @@ +cmake_minimum_required(VERSION 3.21 FATAL_ERROR) + +include("project-meta-info.in") + +project(waitui-str + VERSION ${project_version} + DESCRIPTION ${project_description} + HOMEPAGE_URL ${project_homepage} + LANGUAGES C) + +add_library(str OBJECT) + +target_sources(str + PRIVATE + "src/str.c" + PUBLIC + "include/waitui/str.h" + ) + +target_include_directories(str PUBLIC "include") diff --git a/library/str/include/waitui/str.h b/library/str/include/waitui/str.h new file mode 100644 index 0000000..5f93dfe --- /dev/null +++ b/library/str/include/waitui/str.h @@ -0,0 +1,91 @@ +/** +* @file str.h +* @author rick +* @date 19.02.20 +* @brief File for the String implementation +*/ + +#ifndef WAITUI_STR_H +#define WAITUI_STR_H + +#include +#include + + +// ----------------------------------------------------------------------------- +// Public types +// ----------------------------------------------------------------------------- + +/** +* @brief Type for strings that do not need to be '\0' terminated +*/ +typedef struct str { + char *s; + unsigned long int len; +} str; + + +// ----------------------------------------------------------------------------- +// Public defines +// ----------------------------------------------------------------------------- + +#define STR_NULL_INIT \ + { NULL, 0 } + +#define STR_STATIC_INIT(_str_) \ + { (_str_), sizeof((_str_)) - 1 } + +#define STR_FMT(_pstr_) \ + (((_pstr_) != (str *) 0) ? (int) (_pstr_)->len : 0), \ + (((_pstr_) != (str *) 0) ? (_pstr_)->s : "") + +#define STR_STATIC_SET(_pstr_, _str_) \ + do { \ + if ((_pstr_)) { \ + (_pstr_)->s = (_str_); \ + (_pstr_)->len = sizeof((_str_)) - 1; \ + } \ + } while (0) + +#define STR_STATIC_COPY(_dstr_, _str_) \ + do { \ + if ((_dstr_)) { \ + char *tmp = (_str_); \ + (_dstr_)->len = sizeof((_str_)) - 1; \ + (_dstr_)->s = calloc((_dstr_)->len, sizeof(*(_dstr_)->s)); \ + if ((_dstr_)->s) { memcpy((_dstr_)->s, tmp, (_dstr_)->len); } \ + } \ + } while (0) + +#define STR_COPY(_dstr_, _pstr_) \ + do { \ + if ((_pstr_) && (_dstr_)) { \ + (_dstr_)->len = (_pstr_)->len; \ + (_dstr_)->s = calloc((_pstr_)->len, sizeof(*(_pstr_)->s)); \ + if ((_dstr_)->s) { \ + memcpy((_dstr_)->s, (_pstr_)->s, (_dstr_)->len); \ + } \ + } \ + } while (0) + +#define STR_COPY_WITH_NUL(_dstr_, _pstr_) \ + do { \ + if ((_pstr_) && (_dstr_)) { \ + (_dstr_)->len = (_pstr_)->len; \ + (_dstr_)->s = calloc((_pstr_)->len + 1, sizeof(*(_pstr_)->s)); \ + if ((_dstr_)->s) { \ + memcpy((_dstr_)->s, (_pstr_)->s, (_dstr_)->len); \ + } \ + } \ + } while (0) + +#define STR_FREE(_pstr_) \ + do { \ + if ((_pstr_)) { \ + if ((_pstr_)->s) { free((_pstr_)->s); }; \ + (_pstr_)->len = 0; \ + (_pstr_)->s = NULL; \ + } \ + } while (0) + +#endif//WAITUI_STR_H diff --git a/library/str/project-meta-info.in b/library/str/project-meta-info.in new file mode 100644 index 0000000..90a4459 --- /dev/null +++ b/library/str/project-meta-info.in @@ -0,0 +1,3 @@ +set(project_version 0.0.1) +set(project_description "waitui str library") +set(project_homepage "http://example.com") \ No newline at end of file diff --git a/library/str/src/str.c b/library/str/src/str.c new file mode 100644 index 0000000..4e9cf2f --- /dev/null +++ b/library/str/src/str.c @@ -0,0 +1,8 @@ +/** +* @file str.c +* @author rick +* @date 19.02.20 +* @brief File for the String implementation +*/ + +#include "waitui/str.h"