|
| 1 | +/* |
| 2 | + * Copyright (c) 2020 The University of Tennessee and The University |
| 3 | + * of Tennessee Research Foundation. All rights |
| 4 | + * reserved. |
| 5 | + * Copyright (c) 2020 High Performance Computing Center Stuttgart, |
| 6 | + * University of Stuttgart. All rights reserved. |
| 7 | + * $COPYRIGHT$ |
| 8 | + * |
| 9 | + * Additional copyrights may follow |
| 10 | + * |
| 11 | + * $HEADER$ |
| 12 | + */ |
| 13 | + |
| 14 | +/** |
| 15 | + * @file |
| 16 | + * |
| 17 | + * Implementation of a reference-counted immutable string object. |
| 18 | + * The string object is created using either \c opal_cstring_create(string) or |
| 19 | + * \c opal_cstring_create_l(string, len) with the latter accepting the number |
| 20 | + * of characters to take from the input string. |
| 21 | + * |
| 22 | + * The reference counting is done using opal's \c OBJ_RETAIN / \c OBJ_RELEASE mechanism |
| 23 | + * and it is the user's responsibility to ensure that the string is eventually |
| 24 | + * free'd by decrementing the reference counter using OBJ_RETAIN. |
| 25 | + * |
| 26 | + * The structure contains two relevant members: |
| 27 | + * |
| 28 | + * - \c length: the length of the string, i.e., the number of characters in \c string *not* |
| 29 | + * including the null-terminator. |
| 30 | + * - \c string: the array of characters that make up the string. |
| 31 | + * |
| 32 | + * Both fields are \c const and should not be altered by the user. If the string |
| 33 | + * contained in an \c opal_cstring_t object should be modified it has to be copied |
| 34 | + * to a different buffer (e.g., using strdup). |
| 35 | + * |
| 36 | + * The \c string is always guaranteed to be null-terminated, even if the |
| 37 | + * \c opal_cstring_t object was created using \c OBJ_NEW (which results in an |
| 38 | + * empty string with the \c length field set to zero and the \c string field |
| 39 | + * pointing to the null-terminator). |
| 40 | + * |
| 41 | + */ |
| 42 | + |
| 43 | +#ifndef OPAL_STRING_H |
| 44 | +#define OPAL_STRING_H |
| 45 | + |
| 46 | +#include "opal_config.h" |
| 47 | +#include "opal/class/opal_object.h" |
| 48 | +#include "opal/mca/base/mca_base_var_enum.h" |
| 49 | + |
| 50 | +#include <string.h> |
| 51 | + |
| 52 | +/** |
| 53 | + * Reference-counted immutable string object. |
| 54 | + * |
| 55 | + * The two relevant members are: |
| 56 | + * |
| 57 | + * - \c length: the length of the string, i.e., the number of characters in \c string *not* |
| 58 | + * including the null-terminator. |
| 59 | + * - \c string: the array of characters that make up the string. |
| 60 | + * |
| 61 | + * The string is eventually free'd by calling \c OBJ_RELEASE on it. |
| 62 | + * |
| 63 | + * If allocated using \c OBJ_NEW the object will contain an empty string. |
| 64 | + * The member field \c _ignored is used to force the existance of padding bytes |
| 65 | + * that can be used to write the null-terminator even if no additional memory |
| 66 | + * was allocated succeeding the object itself and is ignored. |
| 67 | + */ |
| 68 | +struct opal_cstring_t { |
| 69 | + opal_object_t super; |
| 70 | + const size_t length; //< the number of characters not including the null-terminator |
| 71 | + char _ignored; //< single char forcing additional padding to always ensure null-termination |
| 72 | + const char string[]; //< FMA containing the string, making use of padding bytes |
| 73 | +}; |
| 74 | + |
| 75 | +typedef struct opal_cstring_t opal_cstring_t; |
| 76 | + |
| 77 | + |
| 78 | +BEGIN_C_DECLS |
| 79 | + |
| 80 | +/** |
| 81 | + * \internal |
| 82 | + * |
| 83 | + * The class for string objects. |
| 84 | + */ |
| 85 | +OPAL_DECLSPEC OBJ_CLASS_DECLARATION(opal_cstring_t); |
| 86 | + |
| 87 | +/** |
| 88 | + * Create a new instance of a reference-counted immutable string object |
| 89 | + * (\ref opal_cstring_t) containing the characters of \c string. |
| 90 | + * |
| 91 | + * @param string Value of the new string |
| 92 | + * @return An object representing the null-terminated string with value \c string |
| 93 | + * |
| 94 | + * If \c string is \c NULL then the resulting string will be empty. |
| 95 | + * |
| 96 | + */ |
| 97 | +OPAL_DECLSPEC |
| 98 | +opal_cstring_t* opal_cstring_create(const char *string) __opal_attribute_malloc__; |
| 99 | + |
| 100 | +/** |
| 101 | + * Create a new instance of a reference-counted immutable string object |
| 102 | + * (\ref opal_cstring_t) containing the first \c length characters of \c string. |
| 103 | + * |
| 104 | + * @param string Value of the new string |
| 105 | + * @return An object representing null-terminated string with the first |
| 106 | + * \c length characters of \c string |
| 107 | + * |
| 108 | + * If \c string is \c NULL or \c length is zero the resulting string will be empty. |
| 109 | + */ |
| 110 | +OPAL_DECLSPEC |
| 111 | +opal_cstring_t* opal_cstring_create_l(const char *string, size_t length) __opal_attribute_malloc__; |
| 112 | + |
| 113 | +/** |
| 114 | + * Convert string to integer |
| 115 | + * |
| 116 | + * Convert \c string into an integer, adhering to the |
| 117 | + * interpretation rules specified in MPI-4 Chapter 10. |
| 118 | + * All others will return \c OPAL_ERR_BAD_PARAM |
| 119 | + * |
| 120 | + * @param string Value string to interpret |
| 121 | + * @param interp returned interpretation of the value key |
| 122 | + * |
| 123 | + * @retval OPAL_SUCCESS string was successfully interpreted |
| 124 | + * @retval OPAL_ERR_BAD_PARAM string could not be interpreted |
| 125 | + * |
| 126 | + */ |
| 127 | +OPAL_DECLSPEC |
| 128 | +int opal_cstring_to_int(opal_cstring_t *string, int *interp); |
| 129 | + |
| 130 | + |
| 131 | +/** |
| 132 | + * Convert string to boolean |
| 133 | + * |
| 134 | + * Convert \c string into a boolean, adhering to the |
| 135 | + * interpretation rules specified in MPI-4 Chapter 10. |
| 136 | + * |
| 137 | + * @param value Value string to interpret |
| 138 | + * @param interp returned interpretation of the value key |
| 139 | + * |
| 140 | + * @retval OPAL_SUCCESS string was successfully interpreted |
| 141 | + * @retval OPAL_ERR_BAD_PARAM string was not able to be interpreted |
| 142 | + * |
| 143 | + * The string value will be cast to the boolen output in |
| 144 | + * the following manner: |
| 145 | + * |
| 146 | + * - If the string value is digits, the return value is "(bool) |
| 147 | + * atoi(value)" |
| 148 | + * - If the string value is (case-insensitive) "yes" or "true", the |
| 149 | + * result is true |
| 150 | + * - If the string value is (case-insensitive) "no" or "false", the |
| 151 | + * result is false |
| 152 | + * - All other values will lead to a return value of OPAL_ERR_BAD_PARAM and |
| 153 | + * \c interp will be set to false. |
| 154 | + */ |
| 155 | +OPAL_DECLSPEC |
| 156 | +int opal_cstring_to_bool(opal_cstring_t *string, bool *interp); |
| 157 | + |
| 158 | + |
| 159 | +OPAL_DECLSPEC |
| 160 | +bool opal_str_to_bool(const char *string); |
| 161 | + |
| 162 | +END_C_DECLS |
| 163 | + |
| 164 | +#endif // OPAL_STRING_H |
0 commit comments