Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * kmp_str.h -- String manipulation routines. |
| 3 | * $Revision: 42613 $ |
| 4 | * $Date: 2013-08-23 13:29:50 -0500 (Fri, 23 Aug 2013) $ |
| 5 | */ |
| 6 | |
| 7 | |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // The LLVM Compiler Infrastructure |
| 11 | // |
| 12 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 13 | // Source Licenses. See LICENSE.txt for details. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| 17 | |
| 18 | #ifndef KMP_STR_H |
| 19 | #define KMP_STR_H |
| 20 | |
| 21 | #include <string.h> |
| 22 | #include <stdarg.h> |
| 23 | |
| 24 | #include "kmp_os.h" |
| 25 | |
| 26 | #ifdef __cplusplus |
| 27 | extern "C" { |
| 28 | #endif // __cplusplus |
| 29 | |
| 30 | #if KMP_OS_WINDOWS |
Jim Cownie | 3b81ce6 | 2014-08-05 09:32:28 +0000 | [diff] [blame] | 31 | # define strdup _strdup |
| 32 | # define snprintf _snprintf |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | /* some macros to replace ctype.h functions */ |
| 36 | #define TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) + 'a' - 'A') : (c)) |
| 37 | |
| 38 | struct kmp_str_buf { |
| 39 | char * str; // Pointer to buffer content, read only. |
| 40 | unsigned int size; // Do not change this field! |
| 41 | int used; // Number of characters printed to buffer, read only. |
| 42 | char bulk[ 512 ]; // Do not use this field! |
| 43 | }; // struct kmp_str_buf |
| 44 | typedef struct kmp_str_buf kmp_str_buf_t; |
| 45 | |
| 46 | #define __kmp_str_buf_init( b ) { (b)->str = (b)->bulk; (b)->size = sizeof( (b)->bulk ); (b)->used = 0; (b)->bulk[ 0 ] = 0; } |
| 47 | |
| 48 | void __kmp_str_buf_clear( kmp_str_buf_t * buffer ); |
| 49 | void __kmp_str_buf_reserve( kmp_str_buf_t * buffer, int size ); |
| 50 | void __kmp_str_buf_detach( kmp_str_buf_t * buffer ); |
| 51 | void __kmp_str_buf_free( kmp_str_buf_t * buffer ); |
| 52 | void __kmp_str_buf_cat( kmp_str_buf_t * buffer, char const * str, int len ); |
| 53 | void __kmp_str_buf_vprint( kmp_str_buf_t * buffer, char const * format, va_list args ); |
| 54 | void __kmp_str_buf_print( kmp_str_buf_t * buffer, char const * format, ... ); |
| 55 | void __kmp_str_buf_print_size( kmp_str_buf_t * buffer, size_t size ); |
| 56 | |
| 57 | /* |
| 58 | File name parser. Usage: |
| 59 | |
| 60 | kmp_str_fname_t fname = __kmp_str_fname_init( path ); |
| 61 | // Use fname.path (copy of original path ), fname.dir, fname.base. |
| 62 | // Note fname.dir concatenated with fname.base gives exact copy of path. |
| 63 | __kmp_str_fname_free( & fname ); |
| 64 | |
| 65 | */ |
| 66 | struct kmp_str_fname { |
| 67 | char * path; |
| 68 | char * dir; |
| 69 | char * base; |
| 70 | }; // struct kmp_str_fname |
| 71 | typedef struct kmp_str_fname kmp_str_fname_t; |
| 72 | void __kmp_str_fname_init( kmp_str_fname_t * fname, char const * path ); |
| 73 | void __kmp_str_fname_free( kmp_str_fname_t * fname ); |
| 74 | // Compares file name with specified patern. If pattern is NULL, any fname matched. |
| 75 | int __kmp_str_fname_match( kmp_str_fname_t const * fname, char const * pattern ); |
| 76 | |
| 77 | /* |
| 78 | The compiler provides source locations in string form ";file;func;line;col;;". It not not |
| 79 | convenient for manupulation. These structure keeps source location in more convenient form. |
| 80 | Usage: |
| 81 | |
| 82 | kmp_str_loc_t loc = __kmp_str_loc_init( ident->psource, 0 ); |
| 83 | // use loc.file, loc.func, loc.line, loc.col. |
| 84 | // loc.fname is available if the second argument of __kmp_str_loc_init is true. |
| 85 | __kmp_str_loc_free( & loc ); |
| 86 | |
| 87 | If psource is NULL or does not follow format above, file and/or func may be NULL pointers. |
| 88 | */ |
| 89 | struct kmp_str_loc { |
| 90 | char * _bulk; // Do not use thid field. |
| 91 | kmp_str_fname_t fname; // Will be initialized if init_fname is true. |
| 92 | char * file; |
| 93 | char * func; |
| 94 | int line; |
| 95 | int col; |
| 96 | }; // struct kmp_str_loc |
| 97 | typedef struct kmp_str_loc kmp_str_loc_t; |
| 98 | kmp_str_loc_t __kmp_str_loc_init( char const * psource, int init_fname ); |
| 99 | void __kmp_str_loc_free( kmp_str_loc_t * loc ); |
| 100 | |
| 101 | int __kmp_str_eqf( char const * lhs, char const * rhs ); |
| 102 | char * __kmp_str_format( char const * format, ... ); |
| 103 | void __kmp_str_free( char const * * str ); |
| 104 | int __kmp_str_match( char const * target, int len, char const * data ); |
| 105 | int __kmp_str_match_false( char const * data ); |
| 106 | int __kmp_str_match_true( char const * data ); |
| 107 | void __kmp_str_replace( char * str, char search_for, char replace_with ); |
| 108 | void __kmp_str_split( char * str, char delim, char ** head, char ** tail ); |
| 109 | char * __kmp_str_token( char * str, char const * delim, char ** buf ); |
| 110 | int __kmp_str_to_int( char const * str, char sentinel ); |
| 111 | |
| 112 | void __kmp_str_to_size( char const * str, size_t * out, size_t dfactor, char const * * error ); |
| 113 | void __kmp_str_to_uint( char const * str, kmp_uint64 * out, char const * * error ); |
| 114 | |
| 115 | #ifdef __cplusplus |
| 116 | } // extern "C" |
| 117 | #endif // __cplusplus |
| 118 | |
| 119 | #endif // KMP_STR_H |
| 120 | |
| 121 | // end of file // |
| 122 | |