1 /* 2 * Copyright (c) 2014-2017 Pavel Kalvoda <me@pavelkalvoda.com> 3 * 4 * libcbor is free software; you can redistribute it and/or modify 5 * it under the terms of the MIT license. See LICENSE for details. 6 */ 7 8 #ifndef LIBCBOR_STRINGS_H 9 #define LIBCBOR_STRINGS_H 10 11 #include "cbor/common.h" 12 13 #ifdef __cplusplus 14 extern "C" { 15 #endif 16 17 /* 18 * ============================================================================ 19 * String manipulation 20 * ============================================================================ 21 */ 22 23 /** Returns the length of the underlying string 24 * 25 * For definite strings only 26 * 27 * @param item[borrow] a definite string 28 * @return length of the string. Zero if no chunk has been attached yet 29 */ 30 size_t cbor_string_length(const cbor_item_t *item); 31 32 /** The number of codepoints in this string 33 * 34 * Might differ from length if there are multibyte ones 35 * 36 * @param item[borrow] A string 37 * @return The number of codepoints in this string 38 */ 39 size_t cbor_string_codepoint_count(const cbor_item_t *item); 40 41 /** Is the string definite? 42 * 43 * @param item[borrow] a string 44 * @return Is the string definite? 45 */ 46 bool cbor_string_is_definite(const cbor_item_t *item); 47 48 /** Is the string indefinite? 49 * 50 * @param item[borrow] a string 51 * @return Is the string indefinite? 52 */ 53 bool cbor_string_is_indefinite(const cbor_item_t *item); 54 55 /** Get the handle to the underlying string 56 * 57 * Definite items only. Modifying the data is allowed. In that case, the caller takes 58 * responsibility for the effect on items this item might be a part of 59 * 60 * @param item[borrow] A definite string 61 * @return The address of the underlying string. `NULL` if no data have been assigned yet. 62 */ 63 cbor_mutable_data cbor_string_handle(const cbor_item_t *item); 64 65 /** Set the handle to the underlying string 66 * 67 * 68 * \rst 69 * .. warning:: Using a pointer to a stack allocated constant is a common mistake. Lifetime of the string will expire when it goes out of scope and the CBOR item will be left inconsistent. 70 * \endrst 71 * 72 * @param item[borrow] A definite string 73 * @param data The memory block. The caller gives up the ownership of the block. libcbor will deallocate it when appropriate using its free function 74 * @param length Length of the data block 75 */ 76 void cbor_string_set_handle(cbor_item_t *item, cbor_mutable_data CBOR_RESTRICT_POINTER data, size_t length); 77 78 /** Get the handle to the array of chunks 79 * 80 * Manipulations with the memory block (e.g. sorting it) are allowed, but the validity and the number of chunks must be retained. 81 * 82 * @param item[borrow] A indefinite string 83 * @return array of #cbor_string_chunk_count definite strings 84 */ 85 cbor_item_t **cbor_string_chunks_handle(const cbor_item_t *item); 86 87 /** Get the number of chunks this string consist of 88 * 89 * @param item[borrow] A indefinite string 90 * @return The chunk count. 0 for freshly created items. 91 */ 92 size_t cbor_string_chunk_count(const cbor_item_t *item); 93 94 /** Appends a chunk to the string 95 * 96 * Indefinite strings only. 97 * 98 * May realloc the chunk storage. 99 * 100 * @param item[borrow] An indefinite string 101 * @param item[incref] A definite string 102 * @return true on success. false on realloc failure. In that case, the refcount of `chunk` is not increased and the `item` is left intact. 103 */ 104 bool cbor_string_add_chunk(cbor_item_t *item, cbor_item_t *chunk); 105 106 /** Creates a new definite string 107 * 108 * The handle is initialized to `NULL` and length to 0 109 * 110 * @return **new** definite string. `NULL` on malloc failure. 111 */ 112 cbor_item_t *cbor_new_definite_string(); 113 114 /** Creates a new indefinite string 115 * 116 * The chunks array is initialized to `NULL` and chunkcount to 0 117 * 118 * @return **new** indefinite string. `NULL` on malloc failure. 119 */ 120 cbor_item_t *cbor_new_indefinite_string(); 121 122 /** Creates a new string and initializes it 123 * 124 * The `val` will be copied to a newly allocated block 125 * 126 * @param val A null-terminated UTF-8 string 127 * @return A **new** string with content `handle`. `NULL` on malloc failure. 128 */ 129 cbor_item_t *cbor_build_string(const char *val); 130 131 /** Creates a new string and initializes it 132 * 133 * The `handle` will be copied to a newly allocated block 134 * 135 * @param val A UTF-8 string, at least \p length long (excluding the null byte) 136 * @return A **new** string with content `handle`. `NULL` on malloc failure. 137 */ 138 cbor_item_t *cbor_build_stringn(const char *val, size_t length); 139 140 #ifdef __cplusplus 141 } 142 #endif 143 144 #endif //LIBCBOR_STRINGS_H 145