UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
UpnpString.cpp
Go to the documentation of this file.
1// Copyright (C) 2021+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
2// Redistribution only with this Copyright remark. Last modified: 2024-08-17
3// Also Copyright by other contributor who haven't made a note.
4// Last compare with pupnp original source file on 2023-04-26, ver 1.14.15
23#include <UpnpString.hpp>
24#ifndef COMPA_UPNPSTRING_HPP
25#error "Wrong UpnpString.hpp header file included."
26#endif
27#include <UPnPsdk/port.hpp>
28
29#include <umock/stdlib.hpp>
30#include <umock/stringh.hpp>
31
32#ifndef UPNP_USE_MSVCPP
33#ifdef UPNP_USE_BCBPP
34static size_t strnlen(const char* s, size_t n) { return strnlen_s(s, n); }
35#else
36/* VC has strnlen which is already included but with
37 * (potentially) different linkage */
38#if !HAVE_STRNLEN || defined(DOXYGEN_RUN)
40static size_t strnlen(const char* s, size_t n) {
41 const char* p = (const char*)memchr(s, 0, n);
42 return p ? p - s : n;
43}
44#endif /* !HAVE_STRNLEN */
45#endif /* UPNP_USE_BCBPP */
46#endif /* _WIN32 */
47
48/* strndup() is a GNU extension. */
49// For mocking we need the identical function strndup() from upnplib port.cpp
50// to have a successful program linking. --Ingo
51// #if !HAVE_STRNDUP || defined(_WIN32)
52// static char* __strndup(const char* __string, size_t __n) {
53// size_t strsize = strnlen(__string, __n);
54// char* newstr = (char*)malloc(strsize + 1);
55// if (newstr == NULL)
56// return NULL;
57//
58// strncpy(newstr, __string, strsize);
59// newstr[strsize] = 0;
60//
61// return newstr;
62// }
63// #endif /* HAVE_STRNDUP && !defined(_WIN32) */
64
70// This structure is addressed with 'UpnpString' by
71// 'typedef struct s_UpnpString UpnpString;' in the header file. The typedef
72// must be the same as in pupnp otherwise we cannot switch between pupnp gtest
73// and compa gtest.
76 size_t m_length;
79 char* m_string;
80};
81
83 /* All bytes are zero, and so is the length of the string. */
84 UpnpString* p =
85 (UpnpString*)umock::stdlib_h.calloc((size_t)1, sizeof(UpnpString));
86 if (p == NULL) {
87 goto error_handler1;
88 }
89#if 0
90 p->m_length = 0;
91#endif
92
93 /* This byte is zero, calloc does initialize it. */
94 p->m_string = (char*)umock::stdlib_h.calloc((size_t)1, (size_t)1);
95 if (p->m_string == NULL) {
96 goto error_handler2;
97 }
98
99 return (UpnpString*)p;
100
101 /*free(p->m_string); */
102error_handler2:
103 umock::stdlib_h.free(p);
104error_handler1:
105 return NULL;
106}
107
109 UpnpString* q = (UpnpString*)p;
110
111 if (!q)
112 return;
113
114 q->m_length = (size_t)0;
115
116 umock::stdlib_h.free(q->m_string);
117 q->m_string = NULL;
118
119 umock::stdlib_h.free(p);
120}
121
123 UpnpString* q =
124 (UpnpString*)umock::stdlib_h.calloc((size_t)1, sizeof(UpnpString));
125 if (q == NULL) {
126 goto error_handler1;
127 }
128 q->m_length = ((UpnpString*)p)->m_length;
129 q->m_string = umock::string_h.strdup(((UpnpString*)p)->m_string);
130 if (q->m_string == NULL) {
131 goto error_handler2;
132 }
133
134 return (UpnpString*)q;
135
136 /*free(q->m_string); */
137error_handler2:
138 umock::stdlib_h.free(q);
139error_handler1:
140 return NULL;
141}
142
143[[maybe_unused]] void UpnpString_assign(UpnpString* p, const UpnpString* q) {
144 if (p != q) {
146 }
147}
148
150 if (!p)
151 return 0;
152 return ((UpnpString*)p)->m_length;
153}
154
156 if (((UpnpString*)p)->m_length > n) {
157 ((UpnpString*)p)->m_length = n;
158 /* No need to realloc now, will do later when needed. */
159 ((UpnpString*)p)->m_string[n] = 0;
160 }
161}
162
164const char* UpnpString_get_String(const UpnpString* p) {
165 if (!p)
166 return nullptr;
167 return ((UpnpString*)p)->m_string;
168}
169
171int UpnpString_set_String(UpnpString* p, const char* s) {
172 if (!p || !s)
173 return 0;
174 char* q = umock::string_h.strdup(s);
175 if (!q)
176 goto error_handler1;
177 umock::stdlib_h.free(((UpnpString*)p)->m_string);
178 ((UpnpString*)p)->m_length = strlen(q);
179 ((UpnpString*)p)->m_string = q;
180
181error_handler1:
182 return q != NULL;
183}
184
185int UpnpString_set_StringN(UpnpString* p, const char* s, size_t n) {
186 if (!p || !s)
187 return 0;
188 char* q = umock::string_h.strndup(s, n);
189 if (!q)
190 goto error_handler1;
191 umock::stdlib_h.free(((UpnpString*)p)->m_string);
192 ((UpnpString*)p)->m_length = strlen(q);
193 ((UpnpString*)p)->m_string = q;
194
195error_handler1:
196 return q != NULL;
197}
198
200 if (!p)
201 return;
202 ((UpnpString*)p)->m_length = (size_t)0;
203 /* No need to realloc now, will do later when needed. */
204 ((UpnpString*)p)->m_string[0] = 0;
205}
206
208 const char* cp = UpnpString_get_String(p);
209 const char* cq = UpnpString_get_String(q);
210
211 return strcmp(cp, cq);
212}
213
215 const char* cp = UpnpString_get_String(p);
216 const char* cq = UpnpString_get_String(q);
217
218 return strcasecmp(cp, cq);
219}
220
UpnpString object declaration.
char * m_string
Pointer to a dynamically allocated area that holds the NULL terminated string.
size_t m_length
Length of the string excluding terminating null byte ('\0').
Internal implementation of the class UpnpString.
PUPNP_Api UpnpString * UpnpString_dup(const UpnpString *p)
Copy Constructor.
PUPNP_Api int UpnpString_set_StringN(UpnpString *p, const char *s, size_t n)
Sets the string from a pointer to char using a maximum of N chars.
PUPNP_Api int UpnpString_set_String(UpnpString *p, const char *s)
Sets the string from a pointer to char.
PUPNP_Api int UpnpString_casecmp(UpnpString *p, UpnpString *q)
Compares two strings for equality. Case does not matter.
PUPNP_Api size_t UpnpString_get_Length(const UpnpString *p)
Returns the length of the string.
PUPNP_Api UpnpString * UpnpString_new(void)
Constructor.
PUPNP_Api const char * UpnpString_get_String(const UpnpString *p)
Returns the pointer to char.
static size_t strnlen(const char *s, size_t n)
strnlen() is a GNU extension and here provided for portability.
PUPNP_Api void UpnpString_delete(UpnpString *p)
Destructor.
PUPNP_Api int UpnpString_cmp(UpnpString *p, UpnpString *q)
Compares two strings for equality. Case matters.
PUPNP_Api void UpnpString_set_Length(UpnpString *p, size_t n)
Truncates the string to the specified lenght, or does nothing if the current lenght is less than or e...
struct s_UpnpString UpnpString
Type of the string objects inside libupnp.
PUPNP_Api void UpnpString_assign(UpnpString *p, const UpnpString *q)
Assignment operator.
PUPNP_Api void UpnpString_clear(UpnpString *p)
Clears the string, sets its size to zero.
Specifications to be portable between different platforms.