/
dstrings.h
154 lines (143 loc) · 4.85 KB
/
dstrings.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2020-2024 The Defold Foundation
// Copyright 2014-2020 King
// Copyright 2009-2014 Ragnar Svensson, Christian Murray
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
// specific language governing permissions and limitations under the License.
#ifndef DMSDK_DSTRINGS_H
#define DMSDK_DSTRINGS_H
#include <stdio.h>
/*# String functions.
*
* SDK Defold String Utils API documentation
*
* @document
* @namespace dmStringFunc
* @name DStrings
* @path engine/dlib/src/dmsdk/dlib/dstrings.h
*/
/*# Size-bounded string formating.
*
* Size-bounded string formating. Resulting string is guaranteed to be 0-terminated.
*
* @name dmSnPrintf
* @param buffer Buffer to write to
* @param count Size of the buffer
* @param format String format
* @return Size of the resulting string (excl terminating 0) if it fits, -1 otherwise
* @examples
*
* ```cpp
* char path[64];
* dmSnPrintf(path, 64, PATH_FORMAT, filename);
* ```
*/
#ifdef __GNUC__
int dmSnPrintf(char *buffer, size_t count, const char *format, ...) __attribute__ ((format (printf, 3, 4)));
#else
int dmSnPrintf(char *buffer, size_t count, const char *format, ...);
#endif
/*# Tokenize strings.
*
* Tokenize strings. Equivalent to BSD strsep_r. Thread-save version of strtok.
*
* @name dmStrTok
* @param string Pointer to string. For the first call string is the string to tokenize. Subsequent should pass NULL.
* @param delim Delimiter string
* @param lasts Internal state pointer
* @return Each call to dmStrTok() returns a pointer to a null-terminated string containing the next token. This string does not include the delimiting byte. If no more tokens are found, dmStrTok() returns NULL
* @examples
*
* ```cpp
* char* string = strdup("a b c");
* char* s, *last;
* s = dmStrTok(string, " ", &last); // a
* s = dmStrTok(0, " ", &last); // b
* s = dmStrTok(0, " ", &last); // c
* ```
*/
char* dmStrTok(char *string, const char *delim, char **lasts);
/*# Size-bounded string copying.
*
* Size-bounded string copying. Same as OpenBSD 2.4 [strlcpy](http://www.manpagez.com/man/3/strlcpy/).
* Copy src to string dst of size siz. At most siz-1 characters will be copied.
* Always NUL terminates (unless siz == 0).Returns strlen(src); if retval >= siz, truncation occurred.
*
* @name dmStrlCpy
* @param dst Destination string
* @param src Source string
* @param size Max size
* @return Total length of the created string
* @examples
*
* ```cpp
* const char* src = "foo";
* char dst[3];
* dmStrlCpy(dst, src, sizeof(dst)); // dst = "fo"
* ```
*/
size_t dmStrlCpy(char *dst, const char *src, size_t size);
/*# Size-bounded string concatenation.
*
* Size-bounded string concatenation. Same as OpenBSD 2.4 [strlcat](http://www.manpagez.com/man/3/strlcat).
* Appends src to string dst of size siz (unlike strncat, siz is the full size of dst, not space left).
* At most siz-1 characters will be copied. Always NUL terminates (unless siz == 0).
* Returns strlen(src); if retval >= siz, truncation occurred.
*
* @name dmStrlCat
* @param dst Destination string
* @param src Source string
* @param size Max size
* @return Total length of the created string
* @examples
*
* ```cpp
* const char* src = "foo";
* char dst[3] = { 0 };
* dmStrlCat(dst, src, sizeof(dst)); // dst = "fo"
* ```
*/
size_t dmStrlCat(char *dst, const char *src, size_t size);
/*# Case-insensitive string comparison
*
* Case-insensitive string comparison
*
* @name dmStrCaseCmp
* @param s1 First string to compare
* @param s2 Second string to compare
* @return an integer greater than, equal to, or less than 0 after lexicographically comparison of s1 and s2
* @examples
*
* ```cpp
* dmStrCaseCmp("a", "b"); // <0
* dmStrCaseCmp("b", "a"); // >0
* dmStrCaseCmp("a", "a"); // 0
* ```
*/
int dmStrCaseCmp(const char *s1, const char *s2);
/*# Error code to string representation
*
* Error code to string representation. Wrapper for thread-safe strerror_s/r variants.
* If the size of the buffer is too small, the message will be truncated to fit the buffer.
* If the buffer is null, or if size is zero, nothing will happen.
*
* @name dmStrError
* @param dst Destination string that carries the error message
* @param size Max size of destination string in bytes
* @return a null-terminated error message
* @examples
*
* ```cpp
* char buf[128];
* dmStrError(buf, sizeof(buf), ENOENT); // buf => "No such file or directory"
* ```
*/
void dmStrError(char* dst, size_t size, int err);
#endif //DMSDK_DSTRINGS_H