[453] | 1 | // Copyright (c) Microsoft. All rights reserved.
|
---|
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for full license information.
|
---|
| 3 |
|
---|
| 4 | #ifndef STRING_TOKEN_H
|
---|
| 5 | #define STRING_TOKEN_H
|
---|
| 6 |
|
---|
| 7 | #ifdef __cplusplus
|
---|
| 8 | #include <cstddef>
|
---|
| 9 | #else
|
---|
| 10 | #include <stddef.h>
|
---|
| 11 | #include <stdbool.h>
|
---|
| 12 | #endif
|
---|
| 13 |
|
---|
| 14 | #include "umock_c/umock_c_prod.h"
|
---|
| 15 |
|
---|
| 16 | #ifdef __cplusplus
|
---|
| 17 | extern "C"
|
---|
| 18 | {
|
---|
| 19 | #endif
|
---|
| 20 |
|
---|
| 21 | typedef struct STRING_TOKEN_TAG* STRING_TOKEN_HANDLE;
|
---|
| 22 |
|
---|
| 23 | /*
|
---|
| 24 | * @brief Tries to identify the first token defined in source up to its length using the delimiters provided.
|
---|
| 25 | * @Remark If no delimiter is found, the entire source string becomes the resulting token. Empty tokens (when delimiters occur side-by-side) can also detected.
|
---|
| 26 | * @param source The initial string to be tokenized.
|
---|
| 27 | * @param length The length of the source string, not including the null-terminator.
|
---|
| 28 | * @param delimiters Array with null-terminated strings to be used as token delimiters.
|
---|
| 29 | * @param n_delims Number of elements in delimiters array.
|
---|
| 30 | * @return A STRING_TOKEN_HANDLE if no failures occur (arguments, memory allocation), or NULL otherwise.
|
---|
| 31 | */
|
---|
| 32 | MOCKABLE_FUNCTION(, STRING_TOKEN_HANDLE, StringToken_GetFirst, const char*, source, size_t, length, const char**, delimiters, size_t, n_delims);
|
---|
| 33 |
|
---|
| 34 | /*
|
---|
| 35 | * @brief Tries to identify the next token defined in source up to its length using the delimiters provided.
|
---|
| 36 | * @Remark After StringToken_GetFirst is initially called, StringToken_GetNext shall be called for obtaining the next tokens.
|
---|
| 37 | * If no delimiter is found, the entire source string becomes the resulting token.
|
---|
| 38 | * Empty tokens (when delimiters occur side-by-side) can also detected.
|
---|
| 39 | * @param token The handle returned by StringToken_GetFirst.
|
---|
| 40 | * @param delimiters Array with null-terminated strings to be used as token delimiters.
|
---|
| 41 | * @param n_delims Number of elements in delimiters array.
|
---|
| 42 | * @return True if a token could be identified, or false if the tokenizer already reached the end of the source string an no more tokens are available.
|
---|
| 43 | */
|
---|
| 44 | MOCKABLE_FUNCTION(, bool, StringToken_GetNext, STRING_TOKEN_HANDLE, token, const char**, delimiters, size_t, n_delims);
|
---|
| 45 |
|
---|
| 46 | /*
|
---|
| 47 | * @brief Gets the pointer to the beginning of the current token identified in the source string.
|
---|
| 48 | * @param token The handle returned by StringToken_GetFirst.
|
---|
| 49 | * @return The pointer to the token identified in the last successfull call to StringToken_GetFirst or StringToken_GetNext.
|
---|
| 50 | * If the token string is empty (e.g., between two delimiters side-by-side or if a delimiter occurs at the end of the source string),
|
---|
| 51 | * NULL is returned instead of an empty string.
|
---|
| 52 | */
|
---|
| 53 | MOCKABLE_FUNCTION(, const char*, StringToken_GetValue, STRING_TOKEN_HANDLE, token);
|
---|
| 54 |
|
---|
| 55 | /*
|
---|
| 56 | * @brief Gets the length of the current token identified in the source string.
|
---|
| 57 | * @param token The handle returned by StringToken_GetFirst.
|
---|
| 58 | * @return A non-zero value if the current token is not empty, zero otherwise.
|
---|
| 59 | */
|
---|
| 60 | MOCKABLE_FUNCTION(, size_t, StringToken_GetLength, STRING_TOKEN_HANDLE, token);
|
---|
| 61 |
|
---|
| 62 | /*
|
---|
| 63 | * @brief Gets a pointer to last delimiter identified (which defined the current token).
|
---|
| 64 | * @param token The handle returned by StringToken_GetFirst.
|
---|
| 65 | * @return If a delimiter was identified in the previous search,
|
---|
| 66 | * the pointer returned is to the element in the delimiters array provided in the previous call to StringToken_GetFirst or StringToken_GetNext;
|
---|
| 67 | * it returns NULL if no delimiter was found in the previous call to those functions.
|
---|
| 68 | */
|
---|
| 69 | MOCKABLE_FUNCTION(, const char*, StringToken_GetDelimiter, STRING_TOKEN_HANDLE, token);
|
---|
| 70 |
|
---|
| 71 | /*
|
---|
| 72 | * @brief Splits a string into an array with all the tokens identified using the delimiters provided.
|
---|
| 73 | * @param source The string to be tokenized.
|
---|
| 74 | * @param length The length of the source string, not including the null-terminator.
|
---|
| 75 | * @param delimiters Array with null-terminated strings to be used as token delimiters.
|
---|
| 76 | * @param n_delims Number of elements in delimiters array.
|
---|
| 77 | * @param include_empty Indicates if empty strings shall be included (as NULL values) in the resulting array.
|
---|
| 78 | * @param tokens If no failures occur, the resulting array with the split tokens (dynamically allocated).
|
---|
| 79 | * @param token_count The number of elements in the tokens array.
|
---|
| 80 | * @return Zero if no failures occur, or a non-zero value otherwise.
|
---|
| 81 | */
|
---|
| 82 | MOCKABLE_FUNCTION(, int, StringToken_Split, const char*, source, size_t, length, const char**, delimiters, size_t, n_delims, bool, include_empty, char***, tokens, size_t*, token_count);
|
---|
| 83 |
|
---|
| 84 | /*
|
---|
| 85 | * @brief Destroys the handle created when calling StringToken_GetFirst.
|
---|
| 86 | * @param token The handle returned by StringToken_GetFirst.
|
---|
| 87 | * @return Nothing
|
---|
| 88 | */
|
---|
| 89 | MOCKABLE_FUNCTION(, void, StringToken_Destroy, STRING_TOKEN_HANDLE, token);
|
---|
| 90 |
|
---|
| 91 | #ifdef __cplusplus
|
---|
| 92 | }
|
---|
| 93 | #endif
|
---|
| 94 |
|
---|
| 95 | #endif /*STRING_TOKEN_H*/
|
---|