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