| 1 | /* Easel's foundation. |
| 2 | * |
| 3 | * Core functionality of easel: errors, memory allocations, constants, |
| 4 | * and configuration for portability. |
| 5 | * |
| 6 | * Contents: |
| 7 | * 1. Macros implementing Easel error handling conventions |
| 8 | * 2. Macros implementing Easel memory allocation conventions |
| 9 | * 3. Macros implementing Easel function argument conventions |
| 10 | * 4. Macros implementing Easel debugging output conventions |
| 11 | * 5. Defined constants |
| 12 | * 6. Basic support for Easel digitized biosequences |
| 13 | * 7. Using optional compiler attributes |
| 14 | * 8. Miscellaneous |
| 15 | * 9. API declarations of easel.c |
| 16 | */ |
| 17 | #ifndef eslEASEL_INCLUDED |
| 18 | #define eslEASEL_INCLUDED |
| 19 | #include <esl_config.h> |
| 20 | |
| 21 | #include <stdlib.h> |
| 22 | #include <stdio.h> /* for FILE */ |
| 23 | #include <stdarg.h> /* for va_list */ |
| 24 | #include <math.h> /* for HUGE_VAL */ |
| 25 | #ifdef HAVE_STDINT_H |
| 26 | #include <stdint.h> /* for uint32_t and the like (C99) */ |
| 27 | #endif |
| 28 | #ifdef HAVE_INTTYPES_H |
| 29 | #include <inttypes.h> /* some systems allegedly put uints here */ |
| 30 | #endif |
| 31 | |
| 32 | /***************************************************************** |
| 33 | * 1. Macros implementing Easel's error handling conventions |
| 34 | *****************************************************************/ |
| 35 | /* Many objects contain a fixed length "errbuf" for failure |
| 36 | * diagnostics: ESL_FAIL() and ESL_XFAIL() fill this buffer. |
| 37 | */ |
| 38 | #define eslERRBUFSIZE 128 |
| 39 | |
| 40 | /* ESL_FAIL() - return an error message, without cleanup. |
| 41 | * ESL_XFAIL() - return an error message, with cleanup. |
| 42 | * ESL_EXCEPTION() - throwing an exception, without cleanup. |
| 43 | * ESL_XEXCEPTION() - throwing an exception, with cleanup. |
| 44 | * |
| 45 | * The X versions (with cleanup) require the caller to have an |
| 46 | * <int status> variable and a <ERROR:> goto target in scope, |
| 47 | * which, yes, is a little hacky. |
| 48 | * |
| 49 | * Wrapping these macros in <while(0)> loops allows a statement: |
| 50 | * if (something) ESL_XEXCEPTION(code,mesg); |
| 51 | * without the trailing semicolon becoming a null statement after |
| 52 | * macro expansion. |
| 53 | * |
| 54 | * All esl_fail() does is vsnprintf() to the <errbuf>; the reason to |
| 55 | * have ESL_FAIL and ESL_XFAIL call esl_fail() is to enable us to set |
| 56 | * a debugging breakpoint in esl_fail(), so we can break execution at |
| 57 | * a normal failure. |
| 58 | */ |
| 59 | /*::cexcerpt::error_macros::begin::*/ |
| 60 | #define ESL_FAIL(code, errbuf, ...) do { \ |
| 61 | esl_fail(errbuf, __VA_ARGS__); \ |
| 62 | return code; } \ |
| 63 | while (0) |
| 64 | |
| 65 | #define ESL_XFAIL(code, errbuf, ...) do { \ |
| 66 | status = code; \ |
| 67 | esl_fail(errbuf, __VA_ARGS__); \ |
| 68 | goto ERROR; } \ |
| 69 | while (0) |
| 70 | |
| 71 | #define ESL_EXCEPTION(code, ...) do { \ |
| 72 | esl_exception(code, FALSE, __FILE__, __LINE__, __VA_ARGS__); \ |
| 73 | return code; } \ |
| 74 | while (0) |
| 75 | |
| 76 | #define ESL_XEXCEPTION(code, ...) do { \ |
| 77 | status = code; \ |
| 78 | esl_exception(code, FALSE, __FILE__, __LINE__, __VA_ARGS__); \ |
| 79 | goto ERROR; } \ |
| 80 | while (0) |
| 81 | |
| 82 | #define ESL_EXCEPTION_SYS(code, ...) do { \ |
| 83 | esl_exception(code, TRUE, __FILE__, __LINE__, __VA_ARGS__); \ |
| 84 | return code; } \ |
| 85 | while (0) |
| 86 | |
| 87 | #define ESL_XEXCEPTION_SYS(code, ...) do { \ |
| 88 | status = code; \ |
| 89 | esl_exception(code, TRUE, __FILE__, __LINE__, __VA_ARGS__); \ |
| 90 | goto ERROR; } \ |
| 91 | while (0) |
| 92 | /*::cexcerpt::error_macros::end::*/ |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | /* Return codes for error handler |
| 98 | */ |
| 99 | /*::cexcerpt::statuscodes::begin::*/ |
| 100 | #define eslOK 0 /* no error/success */ |
| 101 | #define eslFAIL 1 /* failure */ |
| 102 | #define eslEOL 2 /* end-of-line (often normal) */ |
| 103 | #define eslEOF 3 /* end-of-file (often normal) */ |
| 104 | #define eslEOD 4 /* end-of-data (often normal) */ |
| 105 | #define eslEMEM 5 /* malloc or realloc failed */ |
| 106 | #define eslENOTFOUND 6 /* file or key not found */ |
| 107 | #define eslEFORMAT 7 /* file format not correct */ |
| 108 | #define eslEAMBIGUOUS 8 /* an ambiguity of some sort */ |
| 109 | #define eslEDIVZERO 9 /* attempted div by zero */ |
| 110 | #define eslEINCOMPAT 10 /* incompatible parameters */ |
| 111 | #define eslEINVAL 11 /* invalid argument/parameter */ |
| 112 | #define eslESYS 12 /* generic system call failure */ |
| 113 | #define eslECORRUPT 13 /* unexpected data corruption */ |
| 114 | #define eslEINCONCEIVABLE 14 /* "can't happen" error */ |
| 115 | #define eslESYNTAX 15 /* invalid user input syntax */ |
| 116 | #define eslERANGE 16 /* value out of allowed range */ |
| 117 | #define eslEDUP 17 /* saw a duplicate of something */ |
| 118 | #define eslENOHALT 18 /* a failure to converge */ |
| 119 | #define eslENORESULT 19 /* no result was obtained */ |
| 120 | #define eslENODATA 20 /* no data provided, file empty */ |
| 121 | #define eslETYPE 21 /* invalid type of argument */ |
| 122 | #define eslEOVERWRITE 22 /* attempted to overwrite data */ |
| 123 | #define eslENOSPACE 23 /* ran out of some resource */ |
| 124 | #define eslEUNIMPLEMENTED 24 /* feature is unimplemented */ |
| 125 | #define eslENOFORMAT 25 /* couldn't guess file format */ |
| 126 | #define eslENOALPHABET 26 /* couldn't guess seq alphabet */ |
| 127 | #define eslEWRITE 27 /* write failed (fprintf, etc) */ |
| 128 | #define eslEINACCURATE 28 /* return val may be inaccurate */ |
| 129 | /*::cexcerpt::statuscodes::end::*/ |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | /***************************************************************** |
| 137 | * 2. Macros implementing Easel's memory allocation conventions |
| 138 | *****************************************************************/ |
| 139 | /* ESL_ALLOC(), ESL_RALLOC(): |
| 140 | * |
| 141 | * Allocation and reallocation wrappers. |
| 142 | * Both require <int status> in scope, and <ERROR:> goto target. |
| 143 | * ESL_RALLOC() also requires <void *> ptr to be provided as <tmp>. |
| 144 | * |
| 145 | * ESL_REALLOC() is a newer version of ESL_RALLOC() which doesn't |
| 146 | * need a tmp ptr. All ESL_RALLOC() calls can be safely converted |
| 147 | * to ESL_REALLOC() calls. |
| 148 | * |
| 149 | * We don't use zero allocations. The result of malloc(0) is |
| 150 | * implementation-defined (either NULL or a ptr that may not be |
| 151 | * dereferenced), a bit of a hole in the C standard. In Easel, we want |
| 152 | * to avoid having NULL as a valid non-error result of malloc(), |
| 153 | * because it confuses static analysis tools when they see |
| 154 | * dereferences of possibly NULL pointers. We therefore treat |
| 155 | * malloc(0) as an eslEMEM error. |
| 156 | * |
| 157 | * The `size` argument is >= 0. It can be either signed or unsigned, |
| 158 | * but beware of mixed constructs like `(sizeof(foo) * n)`. `sizeof()` |
| 159 | * returns unsigned; (unsigned * signed) first converts the signed |
| 160 | * operand to unsigned; if the signed operand is negative, the |
| 161 | * conversion adds (UINT_MAX+1) modulo (UINT_MAX+1), and a small |
| 162 | * negative signed number becomes a ridiculously large unsigned |
| 163 | * one. Even when you know n is positive, a -Walloc-size-larger-than |
| 164 | * warning in some gcc versions is very aggressively looking for |
| 165 | * problems of this sort, where it may assume that your n could have |
| 166 | * any value from INT_MIN to -1, generating a false positive compiler |
| 167 | * warning. To suppress this warning we typically use a signed cast, |
| 168 | * `(ptrdiff_t) sizeof(foo) * n`. |
| 169 | * [2023/0804-h3-gcc-malloc-warning] |
| 170 | */ |
| 171 | /*::cexcerpt::alloc_macros::begin::*/ |
| 172 | #define ESL_ALLOC(p, size) do {\ |
| 173 | if ( size <= 0 ) { \ |
| 174 | p = NULL; \ |
| 175 | status = eslEMEM; \ |
| 176 | esl_exception(status, FALSE, __FILE__, __LINE__, "zero malloc disallowed"); \ |
| 177 | goto ERROR;\ |
| 178 | }\ |
| 179 | if ( ((p) = malloc(size)) == NULL) { \ |
| 180 | status = eslEMEM;\ |
| 181 | esl_exception(status, FALSE, __FILE__, __LINE__, "malloc of size %d failed", size); \ |
| 182 | goto ERROR;\ |
| 183 | }} while (0) |
| 184 | |
| 185 | #define ESL_RALLOC(p, tmp, newsize) do {\ |
| 186 | if ((p) == NULL) { (tmp) = malloc(newsize); }\ |
| 187 | else { (tmp) = realloc((p), (newsize)); }\ |
| 188 | if ((tmp) != NULL) (p) = (tmp);\ |
| 189 | else {\ |
| 190 | status = eslEMEM;\ |
| 191 | esl_exception(status, FALSE, __FILE__, __LINE__, "realloc for size %d failed", newsize); \ |
| 192 | goto ERROR;\ |
| 193 | }} while (0) |
| 194 | |
| 195 | #define ESL_REALLOC(p, newsize) do {\ |
| 196 | void *esltmpp;\ |
| 197 | if ((p) == NULL) { (esltmpp) = malloc(newsize); }\ |
| 198 | else { (esltmpp) = realloc((p), (newsize)); }\ |
| 199 | if ((esltmpp) != NULL) (p) = (esltmpp);\ |
| 200 | else {\ |
| 201 | status = eslEMEM;\ |
| 202 | esl_exception(status, FALSE, __FILE__, __LINE__, "realloc for size %d failed", newsize); \ |
| 203 | goto ERROR;\ |
| 204 | }} while (0) |
| 205 | /*::cexcerpt::alloc_macros::end::*/ |
| 206 | |
| 207 | /* Convert MiB,GiB,TiB to bytes, using binary definitions (2^20, 2^30, 2^40): |
| 208 | * Pedantically speaking, that's: mebibytes (MiB), gibibytes (GiB), tebibytes (TiB). |
| 209 | * 1 TB = 10^12 bytes; 1 TiB = 2^40 bytes. |
| 210 | */ |
| 211 | #define ESL_MBYTES(x) ((x) * 1048576) |
| 212 | #define ESL_GBYTES(x) ((x) * 1024 * 1048576) |
| 213 | #define ESL_TBYTES(x) ((x) * 1024 * 1024 * 1048576) |
| 214 | |
| 215 | /* Round integer <n> up to the nearest multiple of <m>. |
| 216 | * Particularly useful when dealing w/ memory alignment issues. |
| 217 | */ |
| 218 | #define ESL_UPROUND(n, m) ( ((n) + (m)-1) / (m) * (m)) |
| 219 | |
| 220 | |
| 221 | /***************************************************************** |
| 222 | * 3. Macros implementing Easel's function argument conventions |
| 223 | *****************************************************************/ |
| 224 | |
| 225 | #define esl_byp_IsInternal(p) ((p) == NULL) |
| 226 | #define esl_byp_IsReturned(p) ((p) != NULL && (*p) == NULL) |
| 227 | #define esl_byp_IsProvided(p) ((p) != NULL && (*p) != NULL) |
| 228 | |
| 229 | /* Sometimes a shared function API dictates arguments that a function |
| 230 | * doesn't use, and we want to silence compiler warnings about this. |
| 231 | * Putting ESL_UNUSED(x) in the function, for an unused argument <x>, |
| 232 | * satisfies the compiler while generating a no-op. |
| 233 | */ |
| 234 | #define ESL_UNUSED(x) (void)(sizeof((x))) |
| 235 | |
| 236 | /* Sometimes we need a macro's value as a string constant |
| 237 | * instead of a number. For example we might have |
| 238 | * #define eslFOO_DEFAULT 42 |
| 239 | * and we want to use that default value in an esl_getopts OPTIONS |
| 240 | * array, where the default needs to be specified as a string constant |
| 241 | * "42" not a number 42. We can use a C preprocessor trick for this, |
| 242 | * using the stringize `#` preprocessor operator. It requires two |
| 243 | * steps: one to expand the macro, another to convert the expanded |
| 244 | * value to a string constant. You call ESL_STR(eslFOO_DEFAULT). The |
| 245 | * ESL_XSTR() macro is just to make the trick work. |
| 246 | */ |
| 247 | #define ESL_XSTR(x) #x |
| 248 | #define ESL_STR(x) ESL_XSTR(x) |
| 249 | |
| 250 | |
| 251 | /***************************************************************** |
| 252 | * 4. Macros implementing Easel's debugging output conventions |
| 253 | *****************************************************************/ |
| 254 | /* Debugging hooks, w/ three levels (1-3). |
| 255 | */ |
| 256 | #if eslDEBUGLEVEL >= 1 /* for ESL_DASSERT() macros */ |
| 257 | #include <assert.h> |
| 258 | #endif |
| 259 | |
| 260 | #if (eslDEBUGLEVEL >= 1) |
| 261 | #define ESL_DPRINTF1(x) printf x |
| 262 | #define ESL_DASSERT1(x) assert x |
| 263 | #else |
| 264 | #define ESL_DPRINTF1(x) |
| 265 | #define ESL_DASSERT1(x) |
| 266 | #endif |
| 267 | #if (eslDEBUGLEVEL >= 2) |
| 268 | #define ESL_DPRINTF2(x) printf x |
| 269 | #define ESL_DASSERT2(x) assert x |
| 270 | #else |
| 271 | #define ESL_DPRINTF2(x) |
| 272 | #define ESL_DASSERT2(x) |
| 273 | #endif |
| 274 | #if (eslDEBUGLEVEL >= 3) |
| 275 | #define ESL_DPRINTF3(x) printf x |
| 276 | #define ESL_DASSERT3(x) assert x |
| 277 | #else |
| 278 | #define ESL_DPRINTF3(x) |
| 279 | #define ESL_DASSERT3(x) |
| 280 | #endif |
| 281 | |
| 282 | |
| 283 | /***************************************************************** |
| 284 | * 5. Defined constants |
| 285 | *****************************************************************/ |
| 286 | |
| 287 | /* Making sure TRUE/FALSE are defined, for convenience */ |
| 288 | #ifndef TRUE |
| 289 | #define TRUE 1 |
| 290 | #endif |
| 291 | #ifndef FALSE |
| 292 | #define FALSE 0 |
| 293 | #endif |
| 294 | |
| 295 | /* Some basic mathematical constants. |
| 296 | * Assuming IEEE754 math with 64-bit doubles (53-bit mantissas), we |
| 297 | * want 17 significant decimal digits in our constants. More is |
| 298 | * a waste (but we do it for some anyway). |
| 299 | */ |
| 300 | #define eslCONST_E 2.71828182845904523536028747135 |
| 301 | #define eslCONST_PI 3.14159265358979323846264338328 |
| 302 | #define eslCONST_EULER 0.57721566490153286060651209008 |
| 303 | #define eslCONST_GOLD 1.61803398874989484820458683437 |
| 304 | #define eslCONST_LOG2 0.69314718055994529 |
| 305 | #define eslCONST_LOG2R 1.44269504088896341 |
| 306 | |
| 307 | /* Define <eslINFINITY>, <eslNaN> portably. |
| 308 | * ANSI C99 makes it easy (at last!). The failovers to other pre-C99 |
| 309 | * methods are legacy now that we require a C99 compiler - but no harm |
| 310 | * leaving them in Just In Case. |
| 311 | */ |
| 312 | #if defined (INFINITY) |
| 313 | #define eslINFINITY INFINITY /* C99 */ |
| 314 | #elif defined (HUGE_VAL) |
| 315 | #define eslINFINITY HUGE_VAL /* assume IEEE754 HUGE_VAL = infinity. ok? */ |
| 316 | #else |
| 317 | #define eslINFINITY (1.0/0.0) /* portable? */ |
| 318 | #endif |
| 319 | |
| 320 | #if defined (NAN) |
| 321 | #define eslNaN NAN /* C99 */ |
| 322 | #else |
| 323 | #define eslNaN (eslINFINITY/eslINFINITY) /* portably make a IEEE754 NaN */ |
| 324 | #endif |
| 325 | |
| 326 | /* Define crossovers for numerical approximations. |
| 327 | */ |
| 328 | /* log(1+x) ~ x and 1-e^x = -x approximation. |
| 329 | * Same threshold appears to be optimal for float or double x. xref STL9/138. |
| 330 | */ |
| 331 | #define eslSMALLX1 5e-9 |
| 332 | |
| 333 | |
| 334 | /***************************************************************** |
| 335 | * 6. Basic support for Easel's digitized biosequences. |
| 336 | *****************************************************************/ |
| 337 | |
| 338 | /* Most of this support is in the alphabet module, but we externalize |
| 339 | * some into the easel foundation because ESL_INMAP is used in |
| 340 | * sqio, msa modules. |
| 341 | * |
| 342 | * A digital sequence residue (ESL_DSQ) is an unsigned 8-bit type |
| 343 | * (0..255). A valid digital residue has a value in the range 0..127 |
| 344 | * (Easel can represent alphabets of up to 128 different characters). |
| 345 | * Values 128..255 are reserved for flags. |
| 346 | * |
| 347 | * An "inmap" is ESL_DSQ[128], or *ESL_DSQ allocated for 128 values; |
| 348 | * it is a many-to-one construct for mapping 7-bit ASCII chars (in |
| 349 | * range 0..127) either to new ASCII chars (in the case of raw |
| 350 | * sequence input in sqio, msa) or to digital codes (in the alphabet |
| 351 | * module). Valid mapped values are 0..127; any value in range |
| 352 | * 128..255 is some kind of flag. |
| 353 | */ |
| 354 | typedef uint8_t ESL_DSQ; |
| 355 | #define eslDSQ_SENTINEL 255 /* sentinel bytes 0,L+1 in a dsq */ |
| 356 | #define eslDSQ_ILLEGAL 254 /* input symbol is unmapped and unexpected */ |
| 357 | #define eslDSQ_IGNORED 253 /* input symbol is unmapped and ignored */ |
| 358 | #define eslDSQ_EOL 252 /* input symbol marks end of a line */ |
| 359 | #define eslDSQ_EOD 251 /* input symbol marks end of a seq record */ |
| 360 | |
| 361 | /* If you try to test sym > 0 && sym <= 127 below, instead of isascii(sym), |
| 362 | * you'll get a compiler warning for an always-successful test regardless |
| 363 | * of whether a char is signed or unsigned. So we trust that isascii() is |
| 364 | * doing the Right Thing. |
| 365 | */ |
| 366 | #define esl_inmap_IsValid(inmap, sym) (isascii(sym) && (inmap)[(int)sym] <= 127) |
| 367 | |
| 368 | |
| 369 | /***************************************************************** |
| 370 | * 7. Using optional compiler attributes |
| 371 | *****************************************************************/ |
| 372 | /* It's convenient to be able to use some optional features of |
| 373 | * gcc-like compilers, especially when developing, but we have to be |
| 374 | * able to make these features vanish when the compiler doesn't |
| 375 | * support them. |
| 376 | */ |
| 377 | |
| 378 | /* ESL_ATTRIBUTE_NORETURN |
| 379 | * Static analyzers (like clang's) may need to be clued in when |
| 380 | * a function cannot return: fatal error handlers, for example. |
| 381 | * gcc-like compilers support the __attribute__((__noreturn__)) |
| 382 | * extension on function declarations. Functions that don't return |
| 383 | * are declared like: |
| 384 | * extern void fatal(char *msg, ...) ESL_ATTRIBUTE_NORETURN; |
| 385 | */ |
| 386 | #ifdef HAVE_FUNC_ATTRIBUTE_NORETURN |
| 387 | #define ESL_ATTRIBUTE_NORETURN __attribute__((__noreturn__)) |
| 388 | #else |
| 389 | #define ESL_ATTRIBUTE_NORETURN |
| 390 | #endif |
| 391 | |
| 392 | /* ESL_ATTRIBUTE_FORMAT |
| 393 | * For printf()-like functions with a variable number of arguments |
| 394 | * corresponding to a format string, a compiler is generally unable |
| 395 | * to check that the args match the format. gcc-like compilers |
| 396 | * allow declaring a "format" attribute to enable typechecking of |
| 397 | * printf-like functions. The arguments are printf, <string_idx>, |
| 398 | * <first-to-check>. For example: |
| 399 | * extern void my_printf(FILE *fp, char *s, ...) ESL_ATTRIBUTE_FORMAT(printf, 2, 3); |
| 400 | */ |
| 401 | #ifdef HAVE_FUNC_ATTRIBUTE_FORMAT |
| 402 | #define ESL_ATTRIBUTE_FORMAT(type,idx,first) __attribute__((format(type,idx,first))) |
| 403 | #else |
| 404 | #define ESL_ATTRIBUTE_FORMAT(type,idx,first) |
| 405 | #endif |
| 406 | |
| 407 | |
| 408 | |
| 409 | /***************************************************************** |
| 410 | * 8. Miscellaneous. |
| 411 | *****************************************************************/ |
| 412 | /* A placeholder for helping w/ portability of filenames/paths. |
| 413 | * I think, but have not tested, that: |
| 414 | * VMS: #define DIRSLASH ']' |
| 415 | * ancient MacOS: #define DIRSLASH ':' |
| 416 | * DOS: #define DIRSLASH '\\' |
| 417 | * Setting DIRSLASH correctly is probably not the only thing |
| 418 | * that would need to be done to port to other OS's, but it's |
| 419 | * probably a start. |
| 420 | * |
| 421 | * The code assumes that '.' is used for file name extensions, |
| 422 | * such as "foo.bar". |
| 423 | * |
| 424 | * This gets used in easel.c's *_File*() functions. |
| 425 | */ |
| 426 | #define eslDIRSLASH '/' /* UNIX directory paths have /foo/bar */ |
| 427 | |
| 428 | /* Some generic macros for swapping, min, and max. |
| 429 | */ |
| 430 | #define ESL_SWAP(x, y, type) do { type esltmpxyz = (x); (x) = (y); (y) = esltmpxyz; } while (0) |
| 431 | #define ESL_MIN(a,b) (((a)<(b))?(a):(b)) |
| 432 | #define ESL_MAX(a,b) (((a)>(b))?(a):(b)) |
| 433 | |
| 434 | static inline float esl_log (double x) { return (x == 0.0 ? -eslINFINITY : log(x)); } /* avoid fp exceptions; log(0) = -inf is fine */ |
| 435 | static inline float esl_log2 (double x) { return (x == 0.0 ? -eslINFINITY : log2(x)); } |
| 436 | static inline float esl_logf (float x) { return (x == 0.0 ? -eslINFINITY : logf(x)); } |
| 437 | static inline float esl_log2f(float x) { return (x == 0.0 ? -eslINFINITY : log2f(x)); } |
| 438 | |
| 439 | /* Typedef: <esl_pos_t> |
| 440 | * |
| 441 | * <esl_pos_t> is a signed integer type suitable for safe casting |
| 442 | * to EITHER an <off_t> or <size_t> on this system (i.e. as a position |
| 443 | * in memory or in a file on disk), where we may use a -1 as a flag |
| 444 | * (or even other negative numbers). |
| 445 | * |
| 446 | * <esl_pos_t> is for use for anything having to do with positions in |
| 447 | * large buffers, strings, or files, where we want strict control |
| 448 | * of integer range limits. |
| 449 | * |
| 450 | * Note that POSIX requires size_t to be unsigned, and off_t to be |
| 451 | * signed. |
| 452 | */ |
| 453 | typedef int64_t esl_pos_t; |
| 454 | |
| 455 | |
| 456 | |
| 457 | /***************************************************************** |
| 458 | * 9. The API declarations for easel.c |
| 459 | *****************************************************************/ |
| 460 | |
| 461 | /* 1. Error handling. */ |
| 462 | typedef void (*esl_exception_handler_f)(int errcode, int use_errno, char *sourcefile, int sourceline, char *format, va_list argp); |
| 463 | extern void esl_fail(char *errbuf, const char *format, ...); |
| 464 | extern void esl_exception(int errcode, int use_errno, char *sourcefile, int sourceline, char *format, ...); |
| 465 | extern void esl_exception_SetHandler(esl_exception_handler_f); |
| 466 | extern void esl_exception_ResetDefaultHandler(void); |
| 467 | extern void esl_nonfatal_handler(int errcode, int use_errno, char *sourcefile, int sourceline, char *format, va_list argp); |
| 468 | extern void esl_fatal(const char *format, ...) ESL_ATTRIBUTE_NORETURN; |
| 469 | |
| 470 | /* 2. Memory allocation/deallocation conventions. */ |
| 471 | extern int esl_resize(int n, int a, int r); |
| 472 | extern void esl_free(void *p); |
| 473 | extern void esl_Free2D(void **p, int dim1); |
| 474 | extern void esl_Free3D(void ***p, int dim1, int dim2); |
| 475 | |
| 476 | /* 3. Standard banner for Easel miniapplications. */ |
| 477 | extern int esl_banner (FILE *fp, const char *progname, char *banner); |
| 478 | extern int esl_usage (FILE *fp, const char *progname, char *usage); |
| 479 | extern int esl_dataheader(FILE *fp, ...); |
| 480 | |
| 481 | /* 4. Improved replacements for some C library functions */ |
| 482 | extern int esl_fgets(char **buf, int *n, FILE *fp); |
| 483 | extern int esl_fprintf(FILE *fp, const char *format, ...); |
| 484 | extern int esl_printf(const char *format, ...); |
| 485 | extern int esl_strdup(const char *s, int64_t n, char **ret_dup); |
| 486 | extern int esl_strcat(char **dest, int64_t ldest, const char *src, int64_t lsrc); |
| 487 | extern int esl_strmapcat (const ESL_DSQ *inmap, char **dest, int64_t *ldest, const char *src, esl_pos_t lsrc); |
| 488 | extern int esl_strmapcat_noalloc(const ESL_DSQ *inmap, char *dest, int64_t *ldest, const char *src, esl_pos_t lsrc); |
| 489 | extern int esl_strtok (char **s, char *delim, char **ret_tok); |
| 490 | extern int esl_strtok_adv(char **s, char *delim, char **ret_tok, int *opt_toklen, char *opt_endchar); |
| 491 | extern int esl_sprintf (char **ret_s, const char *format, ...); |
| 492 | extern int esl_vsprintf(char **ret_s, const char *format, va_list *ap); |
| 493 | extern int esl_strcmp(const char *s1, const char *s2); |
| 494 | |
| 495 | /* 5. Portable drop-in replacements for non-standard C functions */ |
| 496 | #ifndef HAVE_STRCASECMP |
| 497 | #ifdef _MSC_VER |
| 498 | #define strcasecmp stricmp |
| 499 | #else |
| 500 | extern int esl_strcasecmp(const char *s1, const char *s2); |
| 501 | #define strcasecmp esl_strcasecmp |
| 502 | #endif |
| 503 | #endif |
| 504 | #ifndef HAVE_STRSEP |
| 505 | extern char *esl_strsep(char **stringp, const char *delim); |
| 506 | #define strsep esl_strsep |
| 507 | #endif |
| 508 | |
| 509 | /* 6. Additional string functions, esl_str*() */ |
| 510 | extern int esl_strchop(char *s, int64_t n); |
| 511 | extern int esl_strdealign(char *s, const char *aseq, const char *gapchars, int64_t *opt_rlen); |
| 512 | extern int esl_str_IsBlank(char *s); |
| 513 | extern int esl_str_IsInteger(char *s); |
| 514 | extern int esl_str_IsReal(char *s); |
| 515 | extern int64_t esl_str_GetMaxWidth(char **s, int n); |
| 516 | |
| 517 | /* 7. File path/name manipulation functions, including tmpfiles */ |
| 518 | extern int esl_FileExists(const char *filename); |
| 519 | extern int esl_FileTail(const char *path, int nosuffix, char **ret_file); |
| 520 | extern int esl_file_Extension(char *filename, esl_pos_t n_ignore, char **ret_sfx, esl_pos_t *ret_n); |
| 521 | extern int esl_FileConcat(const char *dir, const char *file, char **ret_path); |
| 522 | extern int esl_FileNewSuffix(const char *filename, const char *sfx, char **ret_newpath); |
| 523 | extern int esl_FileEnvOpen(const char *fname, const char *env, |
| 524 | FILE **ret_fp, char **ret_path); |
| 525 | extern int esl_tmpfile(char *basename6X, FILE **ret_fp); |
| 526 | extern int esl_tmpfile_named(char *basename6X, FILE **ret_fp); |
| 527 | extern int esl_getcwd(char **ret_cwd); |
| 528 | |
| 529 | /* 8. Typed comparison routines. */ |
| 530 | extern int esl_DCompare(double x0, double x, double r_tol, double a_tol); |
| 531 | extern int esl_FCompare(float x0, float x, float r_tol, float a_tol); |
| 532 | extern int esl_CCompare(char *s1, char *s2); |
| 533 | extern int esl_DCompare_old(double a, double b, double tol); |
| 534 | extern int esl_FCompare_old(float a, float b, float tol); |
| 535 | |
| 536 | /* 9. Other miscellaneous functions */ |
| 537 | extern uint32_t esl_mix3(uint32_t a, uint32_t b, uint32_t c); |
| 538 | |
| 539 | #endif /*eslEASEL_INCLUDED*/ |
| 540 | |
| 541 |