Rename unzip tool.
[SquirrelJME.git] / nanocoat / include / sjme / util.h
blobac20b776ebd4aa6d5dfee33b0242258f5ac3fe1a
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 /**
11 * Utilities.
13 * @since 2023/07/26
16 #ifndef SQUIRRELJME_UTIL_H
17 #define SQUIRRELJME_UTIL_H
19 #include "sjme/nvm.h"
21 /* Anti-C++. */
22 #ifdef __cplusplus
23 #ifndef SJME_CXX_IS_EXTERNED
24 #define SJME_CXX_IS_EXTERNED
25 #define SJME_CXX_SQUIRRELJME_UTIL_H
26 extern "C" {
27 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
28 #endif /* #ifdef __cplusplus */
30 /*--------------------------------------------------------------------------*/
32 /**
33 * Function for returning the number of entries within a tree.
35 * @param in The tree to get the size of.
36 * @return The number of items in the tree.
37 * @since 2023/07/26
39 typedef sjme_jint (*sjme_tree_findCount)(sjme_pointer tree);
41 /**
42 * Function for returning the hash of the search item.
44 * @param what What to get the hash of.
45 * @return The hash of the given search item.
46 * @since 2023/07/26
48 typedef sjme_jint (*sjme_tree_findHash)(void* what);
50 /**
51 * Compares an entry in the tree at the given index with the given hash and
52 * item.
54 * @param tree The tree to search in.
55 * @param what What to being searched for in the tree.
56 * @param hash The hash generated from @c sjme_tree_findHash .
57 * @param withIndex Compare @c hash and @c what against the given tree.
58 * @return A negative value if lower, zero if equal, or a positive value if
59 * greater.
60 * @since 2023/07/26
62 typedef sjme_jint (*sjme_tree_findCompare)(void* tree, void* what,
63 sjme_jint hash, sjme_jint withIndex);
65 /**
66 * Random number state.
68 * @since 2023/12/02
70 typedef struct sjme_random
72 /** The current seed value. */
73 sjme_jlong seed;
74 } sjme_random;
76 /**
77 * Tree finding functions, used with @c sjme_tree_find to determine how to
78 * search through a given tree.
80 * @since 2023/07/26
82 typedef struct sjme_tree_findFunc
84 /** Count function. */
85 sjme_tree_findCount count;
87 /** Hash function. */
88 sjme_tree_findHash hash;
90 /** Compare function. */
91 sjme_tree_findCompare compare;
92 } sjme_tree_findFunc;
94 /**
95 * Compares two null values, nulls are placed before non-nulls.
97 * @param a The first value.
98 * @param b The second value.
99 * @return The resultant comparison.
100 * @since 2024/02/14
102 sjme_jint sjme_compare_null(
103 sjme_attrInNullable sjme_cpointer a,
104 sjme_attrInNullable sjme_cpointer b);
107 * Initializes the random number generator.
109 * @param outRandom The random state to initialize.
110 * @param seedHi The high seed value.
111 * @param seedLo The low seed value.
112 * @return Returns @c SJME_JNI_TRUE on success.
113 * @since 2023/12/02
115 sjme_errorCode sjme_randomInit(
116 sjme_attrInOutNotNull sjme_random* outRandom,
117 sjme_attrInValue sjme_jint seedHi,
118 sjme_attrInValue sjme_jint seedLo);
121 * Initializes the random number generator.
123 * @param outRandom The random state to initialize.
124 * @param seed The seed value.
125 * @return Returns @c SJME_JNI_TRUE on success.
126 * @since 2023/12/02
128 sjme_errorCode sjme_randomInitL(
129 sjme_attrInOutNotNull sjme_random* outRandom,
130 sjme_attrInValue sjme_jlong seed);
133 * Returns the next random value.
135 * @param random The random state.
136 * @param outValue The output value.
137 * @return Returns @c SJME_JNI_TRUE on success.
138 * @since 2023/12/02
140 sjme_errorCode sjme_randomNextInt(
141 sjme_attrInOutNotNull sjme_random* random,
142 sjme_attrOutNotNull sjme_jint* outValue);
145 * Returns the next random value within the given range.
147 * @param random The random state.
148 * @param outValue The output value.
149 * @param maxValue The maximum exclusive value.
150 * @return Returns @c SJME_JNI_TRUE on success.
151 * @since 2023/12/02
153 sjme_errorCode sjme_randomNextIntMax(
154 sjme_attrInOutNotNull sjme_random* random,
155 sjme_attrOutNotNull sjme_jint* outValue,
156 sjme_attrInPositiveNonZero sjme_jint maxValue);
159 * Returns the character at the given index.
161 * @param string The string to get from.
162 * @param index The index within the string.
163 * @return The given character or @c -1 if not valid.
164 * @since 2023/12/16
166 sjme_jint sjme_string_charAt(sjme_lpcstr string, sjme_jint index);
169 * Compares two strings up to the given number of characters each, nulls are
170 * in the same order as @c sjme_compare_null() .
172 * @param aString A string.
173 * @param aLen A length.
174 * @param bString B string.
175 * @param bLen B length.
176 * @return The comparison between the two.
177 * @since 2024/02/22
179 sjme_jint sjme_string_compareN(sjme_lpcstr aString, sjme_jint aLen,
180 sjme_lpcstr bString, sjme_jint bLen);
183 * Decodes the given UTF-8 character.
185 * @param at The character sequence to decode.
186 * @param stringP Adjustable pointer to the string, when the character is
187 * decoded then this will increment accordingly.
188 * @return The decoded character or @c -1 if
189 * it is not valid.
190 * @since 2023/07/27
192 sjme_jint sjme_string_decodeChar(sjme_lpcstr at, sjme_lpcstr* stringP);
195 * Hashes the given string in accordance to @c String::hashCode() .
197 * @param string The string to hash.
198 * @return The hashcode of the given string.
199 * @since 2023/07/26
201 sjme_jint sjme_string_hash(sjme_lpcstr string);
204 * Hashes the given string in accordance to @c String::hashCode() .
206 * @param string The string to hash.
207 * @param limit The string limit.
208 * @return The hashcode of the given string.
209 * @since 2024/02/20
211 sjme_jint sjme_string_hashN(sjme_lpcstr string, sjme_jint limit);
214 * Returns the length of the string in accordance to @c String::length() .
216 * @param string The string to get the length of.
217 * @return The string length or @c -1 if it is not valid.
218 * @since 2023/07/29
220 sjme_jint sjme_string_length(sjme_lpcstr string);
223 * Returns the length of the string in accordance to @c String::length() .
225 * @param string The string to get the length of.
226 * @param limit The length limit of the C string.
227 * @return The string length or @c -1 if it is not valid.
228 * @since 2024/02/20
230 sjme_jint sjme_string_lengthN(sjme_lpcstr string, sjme_jint limit);
233 * Swaps an unsigned integer value.
235 * @param in The input value.
236 * @return The swapped value.
237 * @since 2024/01/05
239 static sjme_inline sjme_attrArtificial sjme_juint sjme_swap_uint(
240 sjme_juint in)
242 // 0xAABBCCDD -> 0xBBAADDCC
243 in = (((in & 0xFF00FF00) >> 8) | ((in & 0x00FF00FF) << 8));
245 // 0xBBAADDCC -> 0xDDCCBBAA
246 return (in >> 16) | (in << 16);
250 * Swaps an integer value.
252 * @param in The input value.
253 * @return The swapped value.
254 * @since 2024/01/05
256 static sjme_inline sjme_attrArtificial sjme_jint sjme_swap_int(
257 sjme_jint in)
259 return (sjme_jint)sjme_swap_uint((sjme_juint)in);
263 * Swaps a long value.
265 * @param in The input value.
266 * @return The swapped value.
267 * @since 2024/01/05
269 static sjme_inline sjme_attrArtificial sjme_jlong sjme_swap_long(
270 sjme_jlong in)
272 sjme_juint temp;
274 /* Swap high and low first. */
275 temp = in.part.hi;
276 in.part.hi = (sjme_jint)in.part.lo;
277 in.part.lo = temp;
279 /* Then finish swap each side. */
280 in.part.hi = sjme_swap_int(in.part.hi);
281 in.part.lo = sjme_swap_uint(in.part.lo);
283 /* Return the result. */
284 return in;
288 * Swaps an unsigned short value.
290 * @param in The input value.
291 * @return The swapped value.
292 * @since 2024/01/05
294 static sjme_inline sjme_attrArtificial sjme_jchar sjme_swap_ushort(
295 sjme_jchar in)
297 return ((in >> 8) | (in << 8));
301 * Swaps a short value.
303 * @param in The input value.
304 * @return The swapped value.
305 * @since 2024/01/05
307 static sjme_inline sjme_attrArtificial sjme_jshort sjme_swap_short(
308 sjme_jshort in)
310 return (sjme_jchar)sjme_swap_ushort((sjme_jchar)in);
314 * Performs @c memmove() followed by shifting up by 8 the destination buffer,
315 * then following a byte swap.
317 * @param dest The destination.
318 * @param src The source.
319 * @param n The number of bytes to copy.
320 * @return Any resultant error, if any.
321 * @since 2024/07/10
323 sjme_errorCode sjme_swap_shu8_uint_memmove(
324 sjme_attrInNotNull void* dest,
325 sjme_attrInNotNull void* src,
326 sjme_attrInPositiveNonZero sjme_jint n);
329 * Performs @c memmove() followed by swapping the destination buffer.
331 * @param dest The destination.
332 * @param src The source.
333 * @param n The number of bytes to copy.
334 * @return Any resultant error, if any.
335 * @since 2024/07/10
337 sjme_errorCode sjme_swap_uint_memmove(
338 sjme_attrInNotNull void* dest,
339 sjme_attrInNotNull void* src,
340 sjme_attrInPositiveNonZero sjme_jint n);
343 * Locates an item within a tree.
345 * @param tree The tree to search in.
346 * @param what What is being searched for.
347 * @param functions Functions used for the tree search logic.
348 * @return The index where the item was found.
349 * @since 2023/07/26
351 sjme_jint sjme_tree_find(void* tree, void* what,
352 const sjme_tree_findFunc* functions);
354 /*--------------------------------------------------------------------------*/
356 /* Anti-C++. */
357 #ifdef __cplusplus
358 #ifdef SJME_CXX_SQUIRRELJME_UTIL_H
360 #undef SJME_CXX_SQUIRRELJME_UTIL_H
361 #undef SJME_CXX_IS_EXTERNED
362 #endif /* #ifdef SJME_CXX_SQUIRRELJME_UTIL_H */
363 #endif /* #ifdef __cplusplus */
365 #endif /* SQUIRRELJME_UTIL_H */