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