blob: 7de3a2ec228dd344aa4d32589fff547092b5a47b [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
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 Cownie3b81ce62014-08-05 09:32:28 +000031# define strdup _strdup
32# define snprintf _snprintf
Jim Cownie5e8470a2013-09-27 10:38:44 +000033#endif
34
35/* some macros to replace ctype.h functions */
36#define TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) + 'a' - 'A') : (c))
37
38struct 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
44typedef 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
48void __kmp_str_buf_clear( kmp_str_buf_t * buffer );
49void __kmp_str_buf_reserve( kmp_str_buf_t * buffer, int size );
50void __kmp_str_buf_detach( kmp_str_buf_t * buffer );
51void __kmp_str_buf_free( kmp_str_buf_t * buffer );
52void __kmp_str_buf_cat( kmp_str_buf_t * buffer, char const * str, int len );
53void __kmp_str_buf_vprint( kmp_str_buf_t * buffer, char const * format, va_list args );
54void __kmp_str_buf_print( kmp_str_buf_t * buffer, char const * format, ... );
55void __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*/
66struct kmp_str_fname {
67 char * path;
68 char * dir;
69 char * base;
70}; // struct kmp_str_fname
71typedef struct kmp_str_fname kmp_str_fname_t;
72void __kmp_str_fname_init( kmp_str_fname_t * fname, char const * path );
73void __kmp_str_fname_free( kmp_str_fname_t * fname );
74// Compares file name with specified patern. If pattern is NULL, any fname matched.
75int __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*/
89struct 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
97typedef struct kmp_str_loc kmp_str_loc_t;
98kmp_str_loc_t __kmp_str_loc_init( char const * psource, int init_fname );
99void __kmp_str_loc_free( kmp_str_loc_t * loc );
100
101int __kmp_str_eqf( char const * lhs, char const * rhs );
102char * __kmp_str_format( char const * format, ... );
103void __kmp_str_free( char const * * str );
104int __kmp_str_match( char const * target, int len, char const * data );
105int __kmp_str_match_false( char const * data );
106int __kmp_str_match_true( char const * data );
107void __kmp_str_replace( char * str, char search_for, char replace_with );
108void __kmp_str_split( char * str, char delim, char ** head, char ** tail );
109char * __kmp_str_token( char * str, char const * delim, char ** buf );
110int __kmp_str_to_int( char const * str, char sentinel );
111
112void __kmp_str_to_size( char const * str, size_t * out, size_t dfactor, char const * * error );
113void __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