NvDialog 0.10.1
A cross-platform modal dialogs library for C/C++ that uses the native OS theme.
Loading...
Searching...
No Matches
nvdialog_string.h
Go to the documentation of this file.
1/*
2 * The MIT License (MIT)
3 *
4 * Copyright (c) 2025 Aggelos Tselios
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25#pragma once
26
27#ifndef __nvdialog_string_h__
28#define __nvdialog_string_h__ 1
29
30#include <stddef.h>
31#include "nvdialog_platform.h"
32
33#ifdef __cplusplus
34extern "C" {
35#endif /* __cplusplus */
36
37/**
38 * @brief A shortcut macro to create a new @ref NvdDynamicString out of a simple string.
39 * @since v0.10
40 * @ingroup String
41 */
42#define NVD_STRING(s) nvd_string_new(s)
43/**
44 * @brief A shortcut macro to get a C string out of the @ref NvdDynamicString given.
45 * @warning The returned string is only valid as long as @ref s is. See @ref nvd_string_to_cstr for details.
46 * @since v0.10
47 * @ingroup String
48 */
49#define NVD_CSTR(s) nvd_string_to_cstr(s)
50
51/**
52 * @brief A string type that can be resized, manipulated, converted and read from.
53 *
54 * `NvdDynamicString` is a recent addition to the library that allows various kinds of strings
55 * across various operating systems to be abstracted and represented by a single type. Whereas `const char*` is
56 * widely used throughout the library, an `NvdDynamicString` is useful when strings are returned - Simple character literals cannot
57 * be returned since they are allocated on the stack. It is also simple to use and convert from and to `char` buffers, making it
58 * compatible with almost all existing APIs.
59 * @note Do not pass this to any parameters that require a `char*` without explicit conversion (See `nvd_string_to_cstr()`).
60 * @note Internally, this dynamic string maintains a capacity variable, that may allocate more memory than needed to store the string. This is done to avoid multiple allocations.
61 * @since v0.10.0
62 * @ingroup String
63*/
64typedef struct _NvdDynamicString NvdDynamicString;
65
66/**
67 * @brief Creates a new @ref NvdDynamicString and returns it, optionally with
68 * the data given in `contents`.
69 * @param data A C-style string to convert into a dynamic string, optional. Pass
70 * NULL to create an empty `NvdDynamicString`.
71 * @returns A new `NvdDynamicString` on success, otherwise `NULL` and the
72 * internal error code is set.
73 * @since v0.10.0
74 * @note This function duplicates the string given internally, so if your string
75 * is allocated on the heap, make sure to free it!
76 * @ingroup String
77 */
79
80/**
81 * @brief Sets the actual string inside the given @ref NvdDynamicString.
82 * @param string The @ref NvdDynamicString to modify.
83 * @param data The string to set as the contents of the `NvdDynamicString`.
84 * @note This will discard any existing data in the string. If you wish to simply append @ref data to the string, see @ref nvd_append_to_string
85 * @since v0.10.0
86 * @ingroup String
87*/
88
89NVD_API void nvd_string_set_data(NvdDynamicString *string, const char *data);
90
91/**
92 * @brief Duplicates the given @ref NvdDynamicString without modifying it.
93 *
94 * You might find this useful when you wish to give the duplicated string in a function that needs to temporarily modify it,
95 * or simply modify the string yourself without losing the original contents.
96 * In most cases, reusing the same string is enough.
97 * @param other The string to copy from.
98 * @note The returned object is completely unique from @ref other and any modifications in one won't impact the other.
99 * @since v0.10.0
100 * @ingroup String
101 */
103
104/**
105 * @brief Appends the contents of `new_data` to the end of `string`.
106 * @param string The `NvdDynamicString` to append to. Must not be `NULL`.
107 * @param new_data The string whose contents will be appended. Must not be `NULL`.
108 * @note This function automatically resizes `string` if needed.
109 * @since v0.10.0
110 * @ingroup String
111*/
113
114/**
115 * @brief Returns a `const char*` representation of the given `NvdDynamicString`.
116 * @param string The `NvdDynamicString` to convert. Must not be `NULL`.
117 * @returns A pointer to a null-terminated C string. The pointer is valid as long as the dynamic string exists and is not modified. Do NOT modify the returned buffer.
118 * @note Do not modify or free the returned pointer, as it may be used elsewhere. Use `nvd_duplicate_string()` if a mutable copy is needed.
119 * @since v0.10.0
120 * @ingroup String
121*/
123
124/**
125 * @brief Preallocates memory for at least `bytes` inside the @ref string 's data. No data will be lost.
126 * @param string The string to increase the capacity of.
127 * @param bytes The minimum number of bytes to reserve.
128 * @note While optional and implicitly done internally when extra space is needed, this can help with performance if you know the size of the data that must be appended.
129 * @since v0.10.0
130 * @ingroup String
131*/
132NVD_API void nvd_string_reserve(NvdDynamicString *string, size_t bytes);
133
134/**
135 * @brief Returns the length of the given `NvdDynamicString`.
136 * @param string The string whose length will be measured. Must not be `NULL`.
137 * @returns The number of characters in the string, excluding the null-terminator.
138 * @since v0.10.0
139 * @ingroup String
140*/
142
143/**
144 * @brief Clears the contents of the given `NvdDynamicString`.
145 * @param string The string to clear. Must not be `NULL`.
146 * @note Any data inside will be lost, but no additional (re)allocations will be performed.
147 * @since v0.10.0
148 * @ingroup String
149*/
151/**
152 * @brief Deletes the string from memory and casts it invalid to use.
153 * @note For security and safety reasons, all the data of the pointer will be zeroed-out first.
154 * @param string The string to delete. This function does nothing if @ref string is `NULL`
155 * @since v0.10.0
156 * @ingroup String
157*/
159
160#ifdef __cplusplus
161}
162#endif /* __cplusplus */
163
164#endif /* __nvdialog_string_h__ */
#define NVD_API
Definition: nvdialog_platform.h:57
struct _NvdDynamicString NvdDynamicString
A string type that can be resized, manipulated, converted and read from.
Definition: nvdialog_string.h:64
NVD_API void nvd_string_clear(NvdDynamicString *string)
Clears the contents of the given NvdDynamicString.
NVD_API void nvd_string_reserve(NvdDynamicString *string, size_t bytes)
Preallocates memory for at least bytes inside the string 's data. No data will be lost.
NVD_API NvdDynamicString * nvd_duplicate_string(NvdDynamicString *other)
Duplicates the given NvdDynamicString without modifying it.
NVD_API NvdDynamicString * nvd_string_new(const char *data)
Creates a new NvdDynamicString and returns it, optionally with the data given in contents.
NVD_API void nvd_string_set_data(NvdDynamicString *string, const char *data)
Sets the actual string inside the given NvdDynamicString.
NVD_API size_t nvd_strlen(NvdDynamicString *string)
Returns the length of the given NvdDynamicString.
NVD_API void nvd_append_to_string(NvdDynamicString *string, NvdDynamicString *new_data)
Appends the contents of new_data to the end of string.
NVD_API void nvd_delete_string(NvdDynamicString *string)
Deletes the string from memory and casts it invalid to use.
NVD_API const char * nvd_string_to_cstr(NvdDynamicString *string)
Returns a const char* representation of the given NvdDynamicString.