xref: /freebsd/contrib/llvm-project/openmp/runtime/src/kmp_str.h (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
10b57cec5SDimitry Andric /*
20b57cec5SDimitry Andric  * kmp_str.h -- String manipulation routines.
30b57cec5SDimitry Andric  */
40b57cec5SDimitry Andric 
50b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
80b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
90b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #ifndef KMP_STR_H
140b57cec5SDimitry Andric #define KMP_STR_H
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #include <stdarg.h>
170b57cec5SDimitry Andric #include <string.h>
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric #include "kmp_os.h"
200b57cec5SDimitry Andric 
210b57cec5SDimitry Andric #ifdef __cplusplus
220b57cec5SDimitry Andric extern "C" {
230b57cec5SDimitry Andric #endif // __cplusplus
240b57cec5SDimitry Andric 
250b57cec5SDimitry Andric #if KMP_OS_WINDOWS
260b57cec5SDimitry Andric #define strdup _strdup
270b57cec5SDimitry Andric #endif
280b57cec5SDimitry Andric 
290b57cec5SDimitry Andric /*  some macros to replace ctype.h functions  */
300b57cec5SDimitry Andric #define TOLOWER(c) ((((c) >= 'A') && ((c) <= 'Z')) ? ((c) + 'a' - 'A') : (c))
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric struct kmp_str_buf {
330b57cec5SDimitry Andric   char *str; // Pointer to buffer content, read only.
340b57cec5SDimitry Andric   unsigned int size; // Do not change this field!
350b57cec5SDimitry Andric   int used; // Number of characters printed to buffer, read only.
360b57cec5SDimitry Andric   char bulk[512]; // Do not use this field!
370b57cec5SDimitry Andric }; // struct kmp_str_buf
380b57cec5SDimitry Andric typedef struct kmp_str_buf kmp_str_buf_t;
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric #define __kmp_str_buf_init(b)                                                  \
410b57cec5SDimitry Andric   {                                                                            \
420b57cec5SDimitry Andric     (b)->str = (b)->bulk;                                                      \
430b57cec5SDimitry Andric     (b)->size = sizeof((b)->bulk);                                             \
440b57cec5SDimitry Andric     (b)->used = 0;                                                             \
450b57cec5SDimitry Andric     (b)->bulk[0] = 0;                                                          \
460b57cec5SDimitry Andric   }
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric void __kmp_str_buf_clear(kmp_str_buf_t *buffer);
49e8d8bef9SDimitry Andric void __kmp_str_buf_reserve(kmp_str_buf_t *buffer, size_t size);
500b57cec5SDimitry Andric void __kmp_str_buf_detach(kmp_str_buf_t *buffer);
510b57cec5SDimitry Andric void __kmp_str_buf_free(kmp_str_buf_t *buffer);
52e8d8bef9SDimitry Andric void __kmp_str_buf_cat(kmp_str_buf_t *buffer, char const *str, size_t len);
530b57cec5SDimitry Andric void __kmp_str_buf_catbuf(kmp_str_buf_t *dest, const kmp_str_buf_t *src);
540b57cec5SDimitry Andric int __kmp_str_buf_vprint(kmp_str_buf_t *buffer, char const *format,
550b57cec5SDimitry Andric                          va_list args);
560b57cec5SDimitry Andric int __kmp_str_buf_print(kmp_str_buf_t *buffer, char const *format, ...);
570b57cec5SDimitry Andric void __kmp_str_buf_print_size(kmp_str_buf_t *buffer, size_t size);
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric /* File name parser.
600b57cec5SDimitry Andric    Usage:
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric    kmp_str_fname_t fname = __kmp_str_fname_init( path );
630b57cec5SDimitry Andric    // Use fname.path (copy of original path ), fname.dir, fname.base.
640b57cec5SDimitry Andric    // Note fname.dir concatenated with fname.base gives exact copy of path.
650b57cec5SDimitry Andric    __kmp_str_fname_free( & fname );
660b57cec5SDimitry Andric */
670b57cec5SDimitry Andric struct kmp_str_fname {
680b57cec5SDimitry Andric   char *path;
690b57cec5SDimitry Andric   char *dir;
700b57cec5SDimitry Andric   char *base;
710b57cec5SDimitry Andric }; // struct kmp_str_fname
720b57cec5SDimitry Andric typedef struct kmp_str_fname kmp_str_fname_t;
730b57cec5SDimitry Andric void __kmp_str_fname_init(kmp_str_fname_t *fname, char const *path);
740b57cec5SDimitry Andric void __kmp_str_fname_free(kmp_str_fname_t *fname);
755ffd83dbSDimitry Andric // Compares file name with specified pattern. If pattern is NULL, any fname
760b57cec5SDimitry Andric // matched.
770b57cec5SDimitry Andric int __kmp_str_fname_match(kmp_str_fname_t const *fname, char const *pattern);
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric /* The compiler provides source locations in string form
805ffd83dbSDimitry Andric    ";file;func;line;col;;". It is not convenient for manipulation. This
810b57cec5SDimitry Andric    structure keeps source location in more convenient form.
820b57cec5SDimitry Andric    Usage:
830b57cec5SDimitry Andric 
84e8d8bef9SDimitry Andric    kmp_str_loc_t loc = __kmp_str_loc_init(ident->psource, false);
850b57cec5SDimitry Andric    // use loc.file, loc.func, loc.line, loc.col.
860b57cec5SDimitry Andric    // loc.fname is available if second argument of __kmp_str_loc_init is true.
870b57cec5SDimitry Andric    __kmp_str_loc_free( & loc );
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric    If psource is NULL or does not follow format above, file and/or func may be
900b57cec5SDimitry Andric    NULL pointers.
910b57cec5SDimitry Andric */
920b57cec5SDimitry Andric struct kmp_str_loc {
930b57cec5SDimitry Andric   char *_bulk; // Do not use thid field.
940b57cec5SDimitry Andric   kmp_str_fname_t fname; // Will be initialized if init_fname is true.
950b57cec5SDimitry Andric   char *file;
960b57cec5SDimitry Andric   char *func;
970b57cec5SDimitry Andric   int line;
980b57cec5SDimitry Andric   int col;
990b57cec5SDimitry Andric }; // struct kmp_str_loc
1000b57cec5SDimitry Andric typedef struct kmp_str_loc kmp_str_loc_t;
101e8d8bef9SDimitry Andric kmp_str_loc_t __kmp_str_loc_init(char const *psource, bool init_fname);
102e8d8bef9SDimitry Andric void __kmp_str_loc_numbers(char const *Psource, int *Line, int *Col);
1030b57cec5SDimitry Andric void __kmp_str_loc_free(kmp_str_loc_t *loc);
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric int __kmp_str_eqf(char const *lhs, char const *rhs);
1060b57cec5SDimitry Andric char *__kmp_str_format(char const *format, ...);
1070b57cec5SDimitry Andric void __kmp_str_free(char **str);
1080b57cec5SDimitry Andric int __kmp_str_match(char const *target, int len, char const *data);
109349cc55cSDimitry Andric bool __kmp_str_contains(char const *target, int len, char const *data);
1100b57cec5SDimitry Andric int __kmp_str_match_false(char const *data);
1110b57cec5SDimitry Andric int __kmp_str_match_true(char const *data);
1120b57cec5SDimitry Andric void __kmp_str_replace(char *str, char search_for, char replace_with);
1130b57cec5SDimitry Andric void __kmp_str_split(char *str, char delim, char **head, char **tail);
1140b57cec5SDimitry Andric char *__kmp_str_token(char *str, char const *delim, char **buf);
115*5f757f3fSDimitry Andric int __kmp_basic_str_to_int(char const *str);
1160b57cec5SDimitry Andric int __kmp_str_to_int(char const *str, char sentinel);
1170b57cec5SDimitry Andric 
1180b57cec5SDimitry Andric void __kmp_str_to_size(char const *str, size_t *out, size_t dfactor,
1190b57cec5SDimitry Andric                        char const **error);
1200b57cec5SDimitry Andric void __kmp_str_to_uint(char const *str, kmp_uint64 *out, char const **error);
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric #ifdef __cplusplus
1230b57cec5SDimitry Andric } // extern "C"
1240b57cec5SDimitry Andric #endif // __cplusplus
1250b57cec5SDimitry Andric 
1260b57cec5SDimitry Andric #endif // KMP_STR_H
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric // end of file //
129