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/**
34 * @brief A shortcut macro to create a new @ref NvdDynamicString out of a simple string.
35 * @since v0.10
36 * @ingroup String
37 */
38#define NVD_STRING(s) nvd_string_new(s)
39/**
40 * @brief A shortcut macro to get a C string out of the @ref NvdDynamicString given.
41 * @warning The returned string is only valid as long as @ref s is. See @ref nvd_string_to_cstr for details.
42 * @since v0.10
43 * @ingroup String
44 */
45#define NVD_CSTR(s) nvd_string_to_cstr(s)
46
47/**
48 * @brief A string type that can be resized, manipulated, converted and read from.
49 *
50 * `NvdDynamicString` is a recent addition to the library that allows various kinds of strings
51 * across various operating systems to be abstracted and represented by a single type. Whereas `const char*` is
52 * widely used throughout the library, an `NvdDynamicString` is useful when strings are returned - Simple character literals cannot
53 * be returned since they are allocated on the stack. It is also simple to use and convert from and to `char` buffers, making it
54 * compatible with almost all existing APIs.
55 * @note Do not pass this to any parameters that require a `char*` without explicit conversion (See `nvd_string_to_cstr()`).
56 * @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.
57 * @since v0.10.0
58 * @ingroup String
59*/
60typedef struct _NvdDynamicString NvdDynamicString;
61
62/**
63 * @brief Creates a new @ref NvdDynamicString and returns it, optionally with the data given in `contents`.
64 * @param data A C-style string to convert into a dynamic string, optional. Pass NULL to create an empty `NvdDynamicString`.
65 * @returns A new `NvdDynamicString` on success, otherwise `NULL` and the internal error code is set.
66 * @since v0.10.0
67 * @ingroup String
68*/
70
71/**
72 * @brief Sets the actual string inside the given @ref NvdDynamicString.
73 * @param string The @ref NvdDynamicString to modify.
74 * @param data The string to set as the contents of the `NvdDynamicString`.
75 * @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
76 * @since v0.10.0
77 * @ingroup String
78*/
79
80NVD_API void nvd_string_set_data(NvdDynamicString *string, const char *data);
81
82/**
83 * @brief Duplicates the given @ref NvdDynamicString without modifying it.
84 *
85 * You might find this useful when you wish to give the duplicated string in a function that needs to temporarily modify it,
86 * or simply modify the string yourself without losing the original contents.
87 * In most cases, reusing the same string is enough.
88 * @param other The string to copy from.
89 * @note The returned object is completely unique from @ref other and any modifications in one won't impact the other.
90 * @since v0.10.0
91 * @ingroup String
92 */
94
95/**
96 * @brief Appends the contents of `new_data` to the end of `string`.
97 * @param string The `NvdDynamicString` to append to. Must not be `NULL`.
98 * @param new_data The string whose contents will be appended. Must not be `NULL`.
99 * @note This function automatically resizes `string` if needed.
100 * @since v0.10.0
101 * @ingroup String
102*/
104
105/**
106 * @brief Returns a `const char*` representation of the given `NvdDynamicString`.
107 * @param string The `NvdDynamicString` to convert. Must not be `NULL`.
108 * @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.
109 * @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.
110 * @since v0.10.0
111 * @ingroup String
112*/
114
115/**
116 * @brief Preallocates memory for at least `bytes` inside the @ref string 's data. No data will be lost.
117 * @param string The string to increase the capacity of.
118 * @param bytes The minimum number of bytes to reserve.
119 * @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.
120 * @since v0.10.0
121 * @ingroup String
122*/
123NVD_API void nvd_string_reserve(NvdDynamicString *string, size_t bytes);
124
125/**
126 * @brief Returns the length of the given `NvdDynamicString`.
127 * @param string The string whose length will be measured. Must not be `NULL`.
128 * @returns The number of characters in the string, excluding the null-terminator.
129 * @since v0.10.0
130 * @ingroup String
131*/
133
134/**
135 * @brief Clears the contents of the given `NvdDynamicString`.
136 * @param string The string to clear. Must not be `NULL`.
137 * @note Any data inside will be lost, but no additional (re)allocations will be performed.
138 * @since v0.10.0
139 * @ingroup String
140*/
142/**
143 * @brief Deletes the string from memory and casts it invalid to use.
144 * @note For security and safety reasons, all the data of the pointer will be zeroed-out first.
145 * @param string The string to delete. This function does nothing if @ref string is `NULL`
146 * @since v0.10.0
147 * @ingroup String
148*/
150
151#endif /* __nvdialog_string_h__ */
#define NVD_API
Definition nvdialog.h:113
struct _NvdDynamicString NvdDynamicString
A string type that can be resized, manipulated, converted and read from.
Definition nvdialog_string.h:60
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.