|
@@ -1,33 +1,7 @@
|
1
|
1
|
#ifndef CSTL_CSTRING_H
|
2
|
2
|
#define CSTL_CSTRING_H
|
3
|
3
|
|
4
|
|
-#include <string.h>
|
5
|
|
-
|
6
|
|
-/**
|
7
|
|
- * Guarantees copying lim-1 chars to the destination string, reserving at least
|
8
|
|
- * one for a null character ('\0'). Strings copied are thus guaranteed to be
|
9
|
|
- * null-terminated, and provides safety by truncating silently.
|
10
|
|
- */
|
11
|
|
-void safestrcpy(char* dest, const char* src, size_t lim);
|
12
|
|
-
|
13
|
|
-/**
|
14
|
|
- * Guarantees concatenating as many characters as possible from src to dest.
|
15
|
|
- * This is intended to act as a convenience macro, to mimic safestrcpy's usage.
|
16
|
|
- */
|
17
|
|
-#define safestrcat(dest, src, dest_lim) strncat(dest, src, dest_lim - strlen(dest) - 1)
|
18
|
|
-
|
19
|
|
-/**
|
20
|
|
- * fstrcat, as in, "fast" strcat.
|
21
|
|
- * Returns a pointer to the last element, to try to alleviate future O(n) calls.
|
22
|
|
- */
|
23
|
|
-char* fstrcat(char* dest, const char* src);
|
24
|
|
-
|
25
|
|
-/**
|
26
|
|
- * sfstrcat, as in, "safe-fast" strcat.
|
27
|
|
- * Guarantees concatenating as many characters as possible from src to dest.
|
28
|
|
- * Returns a pointer to the last element, to try to alleviate future O(n) calls.
|
29
|
|
- */
|
30
|
|
-char* sfstrcat(char* dest, const char* src, size_t lim);
|
|
4
|
+#include <stdlib.h>
|
31
|
5
|
|
32
|
6
|
/**
|
33
|
7
|
* String OO-like functions: cstring
|
|
@@ -42,28 +16,17 @@ char* sfstrcat(char* dest, const char* src, size_t lim);
|
42
|
16
|
* NULL, and reserve deinit/destroy for the actual calling of cstring_destroy.
|
43
|
17
|
*/
|
44
|
18
|
|
45
|
|
-/**
|
46
|
|
- * Options for supplanting a user-defined malloc/realloc equivalent for vectors.
|
47
|
|
- */
|
48
|
|
-#ifndef CSTL_MALLOC
|
49
|
|
-#define CSTL_MALLOC malloc
|
50
|
|
-#include <stdlib.h>
|
51
|
|
-#endif
|
52
|
|
-
|
53
|
|
-#ifndef CSTL_REALLOC
|
54
|
|
-#define CSTL_REALLOC realloc
|
55
|
|
-#include <stdlib.h>
|
56
|
|
-#endif
|
57
|
|
-
|
58
|
|
-#ifndef CSTL_FREE
|
59
|
|
-#define CSTL_FREE free
|
60
|
|
-#include <stdlib.h>
|
61
|
|
-#endif
|
62
|
|
-
|
63
|
19
|
/**
|
64
|
20
|
* Initialize a c-string to a valid state, or, NULL.
|
|
21
|
+ *
|
|
22
|
+ * NOTE: The true interface for this function is:
|
|
23
|
+ *
|
|
24
|
+ * void cstring_init(char* s)
|
|
25
|
+ *
|
|
26
|
+ * Macros have simply been use to unify the interface.
|
65
|
27
|
*/
|
66
|
|
-void cstring_init(char** s);
|
|
28
|
+#define cstring_init(s) cstring_init_(&s)
|
|
29
|
+void cstring_init_(char** s);
|
67
|
30
|
|
68
|
31
|
/**
|
69
|
32
|
* Create a new c-string, and fill it with the contents of c-string s.
|
|
@@ -77,12 +40,49 @@ char* cstring_create(const char* s);
|
77
|
40
|
* This will cause a deallocation, if s1 already has been filled.
|
78
|
41
|
* As NULL is a valid state for a char*, this is checked for; if s2 is NULL,
|
79
|
42
|
* then s1 will simply be freed and will stay as NULL.
|
|
43
|
+ *
|
|
44
|
+ * NOTE: The true interface for this function is:
|
|
45
|
+ *
|
|
46
|
+ * void cstring_asn(char* s1, const char* s2)
|
80
|
47
|
*/
|
81
|
|
-void cstring_asn(char** s1, const char* s2);
|
|
48
|
+#define cstring_asn(s1, s2) cstring_asn_(&s1, s2)
|
|
49
|
+void cstring_asn_(char** s1, const char* s2);
|
82
|
50
|
|
83
|
51
|
/**
|
84
|
52
|
* Destroys and releases the memory for a c-string.
|
85
|
53
|
*/
|
86
|
54
|
void cstring_destroy(char* s);
|
87
|
55
|
|
|
56
|
+
|
|
57
|
+/**
|
|
58
|
+ * Utility functions for handling c-strings
|
|
59
|
+ */
|
|
60
|
+
|
|
61
|
+/**
|
|
62
|
+ * Guarantees copying lim-1 chars to the destination string, reserving at least
|
|
63
|
+ * one for a null character ('\0'). Strings copied are thus guaranteed to be
|
|
64
|
+ * null-terminated, and provides safety by truncating silently.
|
|
65
|
+ */
|
|
66
|
+void safestrcpy(char* dest, const char* src, size_t lim);
|
|
67
|
+
|
|
68
|
+/**
|
|
69
|
+ * Guarantees concatenating as many characters as possible from src to dest.
|
|
70
|
+ * This is intended to act as a convenience macro, to mimic safestrcpy's usage.
|
|
71
|
+ */
|
|
72
|
+#define safestrcat(dest, src, dest_lim) strncat(dest, src, dest_lim - strlen(dest) - 1)
|
|
73
|
+
|
|
74
|
+/**
|
|
75
|
+ * fstrcat, as in, "fast" strcat.
|
|
76
|
+ * Returns a pointer to the last element, to try to alleviate future O(n) calls.
|
|
77
|
+ */
|
|
78
|
+char* fstrcat(char* dest, const char* src);
|
|
79
|
+
|
|
80
|
+/**
|
|
81
|
+ * sfstrcat, as in, "safe-fast" strcat.
|
|
82
|
+ * Guarantees concatenating as many characters as possible from src to dest.
|
|
83
|
+ * Returns a pointer to the last element, to try to alleviate future O(n) calls.
|
|
84
|
+ */
|
|
85
|
+char* sfstrcat(char* dest, const char* src, size_t lim);
|
|
86
|
+
|
|
87
|
+
|
88
|
88
|
#endif
|