MonkOS  v0.1
A simple 64-bit operating system (x86_64)
string.h
Go to the documentation of this file.
1 //============================================================================
2 /// @file string.h
3 /// @brief String and memory operations.
4 //
5 // Copyright 2016 Brett Vickers.
6 // Use of this source code is governed by a BSD-style license that can
7 // be found in the MonkOS LICENSE file.
8 //============================================================================
9 
10 #pragma once
11 
12 #include <core.h>
13 
14 //----------------------------------------------------------------------------
15 // @function strlen
16 /// @brief Return the length of a null-terminated string.
17 /// @details Count the number of characters in the null-terminated string
18 /// and return the count.
19 /// @param[in] str Pointer to a null-terminated string.
20 /// @returns The number of characters preceding the null
21 /// terminator.
22 //----------------------------------------------------------------------------
23 size_t
24 strlen(const char *str);
25 
26 //----------------------------------------------------------------------------
27 // @function strlcpy
28 /// @brief Copy the source string to the destination buffer.
29 /// @details Appends string src to the end of dst. It will copy at most
30 /// dstsize - 1 characters. A null terminator is added to the end
31 /// of the destination string unless dstsize was 0.
32 /// @param[in] dst Pointer to the destination buffer.
33 /// @param[in] src Pointer to the source string.
34 /// @param[in] dstsize Maximum number of characters in the dst buffer after
35 /// copying, including the null terminator.
36 /// @returns The length of the copied string after truncation.
37 //----------------------------------------------------------------------------
38 size_t
39 strlcpy(char *dst, const char *src, size_t dstsize);
40 
41 //----------------------------------------------------------------------------
42 // @function strlcat
43 /// @brief Append the source string to the end of the destination string.
44 /// @details The function will append at most dstsize - strlen(dst) - 1
45 /// characters. A null terminator is added to the end of the
46 /// concatenated string unless dstsize was 0.
47 /// @param[in] dst Pointer to the destination string.
48 /// @param[in] src Pointer to the source string.
49 /// @param[in] dstsize Maximum number of characters in the dst buffer after
50 /// concatenation, including the null terminator.
51 /// @returns The length of the concatenated string after truncation.
52 //----------------------------------------------------------------------------
53 size_t
54 strlcat(char *dst, const char *src, size_t dstsize);
55 
56 //----------------------------------------------------------------------------
57 // @function strcmp
58 /// @brief Compare two strings and return a value indicating their
59 /// lexicographical order.
60 /// @details String comparison continues until a null terminator is reached
61 /// in one of the strings.
62 /// @param[in] str1 Pointer to the first string.
63 /// @param[in] str2 Pointer to the second string.
64 /// @returns < 0 if the first character in str1 that doesn't match a
65 /// character in str2 has a lower value.
66 /// = 0 if the two strings are identical.
67 /// > 0 otherwise.
68 //----------------------------------------------------------------------------
69 int
70 strcmp(const char *str1, const char *str2);
71 
72 //----------------------------------------------------------------------------
73 // @function memcpy
74 /// @brief Copy bytes from one memory region to another.
75 /// @details If the memory regions overlap, this function's behavior
76 /// is undefined, and you should use memmove instead.
77 /// @param[in] dst Address of the destination memory area.
78 /// @param[in] src Address of the source memory area.
79 /// @param[in] num Number of bytes to copy.
80 /// @returns Destination address.
81 //----------------------------------------------------------------------------
82 void *
83 memcpy(void *dst, const void *src, size_t num);
84 
85 //----------------------------------------------------------------------------
86 // @function memmove
87 /// @brief Move bytes from one memory region to another, even if the
88 /// regions overlap.
89 /// @param[in] dst Address of the destination memory area.
90 /// @param[in] src Address of the source memory area.
91 /// @param[in] num Number of bytes to copy.
92 /// @returns Destination address.
93 //----------------------------------------------------------------------------
94 void *
95 memmove(void *dst, const void *src, size_t num);
96 
97 //----------------------------------------------------------------------------
98 // @function memset
99 /// @brief Fill a region of memory with a single byte value.
100 /// @param[in] dst Address of the destination memory area.
101 /// @param[in] b Value of the byte used to fill memory.
102 /// @param[in] num Number of bytes to set.
103 /// @returns Destination address.
104 //----------------------------------------------------------------------------
105 void *
106 memset(void *dst, int b, size_t num);
107 
108 //----------------------------------------------------------------------------
109 // @function memsetw
110 /// @brief Fill a region of memory with a single 16-bit word value.
111 /// @param[in] dst Address of the destination memory area.
112 /// @param[in] w Value of the word used to fill memory.
113 /// @param[in] num Number of words to set.
114 /// @returns Destination address.
115 //----------------------------------------------------------------------------
116 void *
117 memsetw(void *dst, int w, size_t num);
118 
119 //----------------------------------------------------------------------------
120 // @function memsetd
121 /// @brief Fill a region of memory with a single 32-bit dword value.
122 /// @param[in] dst Address of the destination memory area.
123 /// @param[in] d Value of the dword used to fill memory.
124 /// @param[in] num Number of dwords to set.
125 /// @returns Destination address.
126 //----------------------------------------------------------------------------
127 void *
128 memsetd(void *dst, uint32_t d, size_t num);
129 
130 //----------------------------------------------------------------------------
131 // @function memzero
132 /// @brief Fill a region of memory with zeroes.
133 /// @param[in] dst Address of the destination memory area.
134 /// @param[in] num Number of bytes to set to zero.
135 /// @returns Destination address.
136 //----------------------------------------------------------------------------
137 void *
138 memzero(void *dst, size_t num);
int strcmp(const char *str1, const char *str2)
Compare two strings and return a value indicating their lexicographical order.
size_t strlcat(char *dst, const char *src, size_t dstsize)
Append the source string to the end of the destination string.
size_t strlcpy(char *dst, const char *src, size_t dstsize)
Copy the source string to the destination buffer.
Core include file.
void * memsetd(void *dst, uint32_t d, size_t num)
Fill a region of memory with a single 32-bit dword value.
void * memmove(void *dst, const void *src, size_t num)
Move bytes from one memory region to another, even if the regions overlap.
size_t strlen(const char *str)
Return the length of a null-terminated string.
void * memset(void *dst, int b, size_t num)
Fill a region of memory with a single byte value.
void * memzero(void *dst, size_t num)
Fill a region of memory with zeroes.
void * memsetw(void *dst, int w, size_t num)
Fill a region of memory with a single 16-bit word value.
void * memcpy(void *dst, const void *src, size_t num)
Copy bytes from one memory region to another.