| 1 | #ifndef _DASM_UTIL_H |
| 2 | #define _DASM_UTIL_H |
| 3 | |
| 4 | /* |
| 5 | the DASM macro assembler (aka small systems cross assembler) |
| 6 | |
| 7 | Copyright (c) 1988-2002 by Matthew Dillon. |
| 8 | Copyright (c) 1995 by Olaf "Rhialto" Seibert. |
| 9 | Copyright (c) 2003-2008 by Andrew Davie. |
| 10 | Copyright (c) 2008 by Peter H. Froehlich. |
| 11 | |
| 12 | This program is free software; you can redistribute it and/or modify |
| 13 | it under the terms of the GNU General Public License as published by |
| 14 | the Free Software Foundation; either version 2 of the License, or |
| 15 | (at your option) any later version. |
| 16 | |
| 17 | This program is distributed in the hope that it will be useful, |
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | GNU General Public License for more details. |
| 21 | |
| 22 | You should have received a copy of the GNU General Public License along |
| 23 | with this program; if not, write to the Free Software Foundation, Inc., |
| 24 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 25 | */ |
| 26 | |
| 27 | /** |
| 28 | * @file |
| 29 | * |
| 30 | * @brief Utility functions for string manipulation and memory allocation. |
| 31 | */ |
| 32 | |
| 33 | #include <stdbool.h> |
| 34 | #include <string.h> |
| 35 | |
| 36 | /** |
| 37 | * @brief |
| 38 | * Wrapper for malloc(3) that terminates DASM with panic() if no |
| 39 | * memory is available. |
| 40 | * |
| 41 | * @pre |
| 42 | * bytes > 0 |
| 43 | */ |
| 44 | |
| 45 | /*@out@*/ |
| 46 | void *checked_malloc(size_t bytes); |
| 47 | |
| 48 | /** |
| 49 | * @brief |
| 50 | * Wrapper for checked_malloc() that zero's the allocated memory |
| 51 | * using memset(3). |
| 52 | * |
| 53 | * @pre |
| 54 | * bytes > 0 |
| 55 | * |
| 56 | * @warning |
| 57 | * memset(3) may not initialize pointers or floats/doubles |
| 58 | * correctly to NULL or 0.0 on some (very strange) machines. |
| 59 | */ |
| 60 | |
| 61 | void *zero_malloc(size_t bytes); |
| 62 | |
| 63 | /** |
| 64 | * @brief |
| 65 | * Efficiently allocate small amounts of memory. |
| 66 | * |
| 67 | * @pre |
| 68 | * bytes > 0 |
| 69 | * |
| 70 | * @warning |
| 71 | * You can only request *small* amounts of memory from this |
| 72 | * function, less than 1024 bytes at a time is a good rule of |
| 73 | * thumb. |
| 74 | * You *cannot* free(3) the pointer returned by small_alloc(), |
| 75 | * truly bad things will happen if you try. |
| 76 | * You can *only* free *all* the memory ever allocated through |
| 77 | * small_alloc() using small_free_all() below. |
| 78 | */ |
| 79 | |
| 80 | void *small_alloc(size_t bytes); |
| 81 | |
| 82 | /** |
| 83 | * @brief |
| 84 | * Free *all* memory allocated by small_alloc() so far. |
| 85 | * |
| 86 | * @warning |
| 87 | * This function should only be called if your process is |
| 88 | * about to exit(3) or if you *really* know what you're doing; |
| 89 | * otherwise you'll end up with lots of dangling pointers. |
| 90 | */ |
| 91 | |
| 92 | void small_free_all(void); |
| 93 | |
| 94 | /** |
| 95 | * @brief |
| 96 | * An excellent hash function for strings. |
| 97 | * |
| 98 | * @pre |
| 99 | * string != NULL && length > 0 && length <= strlen(string) |
| 100 | */ |
| 101 | |
| 102 | unsigned int hash_string(const char *string, size_t length); |
| 103 | |
| 104 | /** |
| 105 | * @brief |
| 106 | * Return a fresh copy of the given string. Memory is |
| 107 | * allocated with checked_malloc() so it won't return |
| 108 | * NULL on failure like the regular strdup() does. |
| 109 | * |
| 110 | * @pre |
| 111 | * s != NULL && strlen(s) > 0 |
| 112 | */ |
| 113 | |
| 114 | char *checked_strdup(const char *s); |
| 115 | |
| 116 | /** |
| 117 | * @brief |
| 118 | * Convert string to lower case. |
| 119 | * |
| 120 | * @pre |
| 121 | * dst != NULL && src != NULL && size > 0 |
| 122 | */ |
| 123 | |
| 124 | size_t strlower(/*@out@*/ char *dst, const char *src, size_t size); |
| 125 | |
| 126 | /** |
| 127 | * @brief |
| 128 | * Convert string to upper case. |
| 129 | * |
| 130 | * @pre |
| 131 | * dst != NULL && src != NULL && size > 0 |
| 132 | */ |
| 133 | |
| 134 | size_t strupper(/*@out@*/ char *dst, const char *src, size_t size); |
| 135 | |
| 136 | /** |
| 137 | * @brief |
| 138 | * Remove all whitespace from a string. |
| 139 | * |
| 140 | * @pre |
| 141 | * dst != NULL && src != NULL && size > 0 |
| 142 | */ |
| 143 | |
| 144 | size_t strip_whitespace(/*@out@*/ char *dst, const char *src, size_t size); |
| 145 | |
| 146 | /** |
| 147 | * @brief |
| 148 | * True if strcmp(string, strlower(either)) == 0 or |
| 149 | * strcmp(string, strupper(either)) == 0, so mixed |
| 150 | * capitalization is not allowed. |
| 151 | * |
| 152 | * @pre |
| 153 | * string != NULL && either != NULL |
| 154 | */ |
| 155 | bool match_either_case(const char *string, const char *either); |
| 156 | |
| 157 | #if !defined(__APPLE__) && !defined(__BSD__) |
| 158 | |
| 159 | /** |
| 160 | * @brief |
| 161 | * Append src to string dst of size siz. |
| 162 | * |
| 163 | * Unlike in strncat(3), siz is the full size of dst, not space left. |
| 164 | * At most siz-1 characters will be copied. |
| 165 | * Always NUL terminates (unless siz <= strlen(dst)). |
| 166 | * Returns strlen(src) + MIN(siz, strlen(initial dst)). |
| 167 | * If retval >= siz, truncation occurred. |
| 168 | * |
| 169 | * @pre |
| 170 | * dst != NULL && src != NULL && siz > 0 |
| 171 | * |
| 172 | * @note |
| 173 | * On BSD (including OS X) this function is defined in the C library! |
| 174 | */ |
| 175 | |
| 176 | size_t strlcat(/*@in@*/ /*@out@*/ char *dst, const char *src, size_t siz); |
| 177 | |
| 178 | /** |
| 179 | * @brief |
| 180 | * Copy src to string dst of size siz. |
| 181 | * |
| 182 | * At most siz-1 characters will be copied. |
| 183 | * Always NUL terminates (unless siz == 0). |
| 184 | * Returns strlen(src); if retval >= siz, truncation occurred. |
| 185 | * |
| 186 | * @pre |
| 187 | * dst != NULL && src != NULL && siz > 0 |
| 188 | * |
| 189 | * @note |
| 190 | * On BSD (including OS X) this function is defined in the C library! |
| 191 | */ |
| 192 | |
| 193 | size_t strlcpy(/*@out@*/ char *dst, const char *src, size_t siz); |
| 194 | |
| 195 | /** |
| 196 | * @brief |
| 197 | * Name of current program. |
| 198 | * @warning |
| 199 | * If name not set, a string to that effect is returned. |
| 200 | * @note |
| 201 | * On BSD (including OS X) this function is defined in the C library! |
| 202 | */ |
| 203 | |
| 204 | /*@temp@*/ |
| 205 | const char *getprogname(void); |
| 206 | |
| 207 | /** |
| 208 | * @brief |
| 209 | * Sets name of current program sets to last component of given path. |
| 210 | * @pre |
| 211 | * name != NULL |
| 212 | * @warning |
| 213 | * Just pass argv[0] if you want, however the parsing is simplistic so |
| 214 | * you should be careful if your environment is the least bit exotic. |
| 215 | * @note |
| 216 | * On BSD (including OS X) this function is defined in the C library! |
| 217 | */ |
| 218 | |
| 219 | void setprogname(const char *name); |
| 220 | |
| 221 | #endif /* !defined(__APPLE__) && !defined(__BSD__) */ |
| 222 | |
| 223 | #endif /* _DASM_UTIL_H */ |
| 224 | |
| 225 | /* vim: set tabstop=4 softtabstop=4 expandtab shiftwidth=4 autoindent: */ |