%% options copyright owner = Dirk Krause copyright year = 2015-xxxx SPDX-License-Identifier: BSD-3-Clause %% header /** @file dk4strx.h String functions for wxChar. */ #ifndef DK4CONF_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4conf.h" #else #include #endif #endif #ifndef DK4TYPES_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4types.h" #else #include #endif #endif #ifndef DK4CONST_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4const.h" #else #include #endif #endif #ifndef DK4ERROR_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4error.h" #else #include #endif #endif #ifndef DK4WXCS_H_INCLUDED #if DK4_BUILDING_DKTOOLS4 #include "dk4wxcs.h" #else #include #endif #endif #ifndef WX_WXPREC_H_INCLUDED #include #define WX_WXPREC_H_INCLUDED 1 #endif #ifdef __BORLANDC__ #pragma hdrstop #endif #ifndef WX_PRECOMP #if DK4_HAVE_WX_CHARTYPE_H #ifndef WX_CHARTYPE_H_INCLUDED #include #define WX_CHARTYPE_H_INCLUDED 1 #endif #else #if DK4_HAVE_WX_WXCHAR_H #ifndef WX_WXCHAR_H_INCLUDED #include #define WX_WXCHAR_H_INCLUDED 1 #endif #else #ifndef WX_WX_H_INCLUDED #include #define WX_WX_H_INCLUDED #endif #endif #endif #endif #if DK4_ON_WINDOWS || DK4_HAVE_BACKSLASH /** File name separator. */ #define DK4_WX_CHAR_SEP wxT('\\') #else /** File name separator. */ #define DK4_WX_CHAR_SEP wxT('/') #endif #ifdef __cplusplus extern "C" { #endif /** Copy string, check destination buffer size. @param dst Destination buffer address. @param sz Destination buffer size. @param src Source string. @param erp Error report, may be NULL. @return 1 on success, 0 on error. Error codes: - DK4_E_INVALID_ARGUMENTS
if dst or src is NULL or sz is 0, - DK4_E_BUFFER_TOO_SMALL
if the dst buffer is too small. */ int dk4strx_cpy_s(wxChar *dst, size_t sz, const wxChar *src, dk4_er_t *erp); /** Concatenate strings, check destination buffer size. @param dst Destination buffer address. @param sz Destination buffer size. @param src Source string. @param erp Error report, may be NULL. @return 1 on success, 0 on error. Error codes: - DK4_E_INVALID_ARGUMENTS
if dst or src is NULL or sz is 0, - DK4_E_MATH_OVERFLOW
if a mathematical overflow occured in size calculation, - DK4_E_BUFFER_TOO_SMALL
if the concatenated string doest not fit into the buffer. */ int dk4strx_cat_s(wxChar *dst, size_t sz, const wxChar *src, dk4_er_t *erp); /** Copy characters within a string from right to left. This copy operation is intended for moving characters within one string from right to left, making the string shorter as is. This function does not check buffer size or report any error. For normal string copy operations, use the dk4strx_cpy_s() function instead. @param dst Destination buffer address. @param src Source string. */ void dk4strx_cpy_to_left(wxChar *dst, const wxChar *src); /** Find string length. @param src Source string. @return String length. */ size_t dk4strx_len(const wxChar *src); /** Compare two strings. @param s1 Left string, may be NULL. @param s2 Right string, may be NULL. @return 1 if s1>s2, 0 if s1==s2, -1 if s1s2, 0 if s1==s2, -1 if s1s2, 0 if s1==s2, -1 if s1 for invalid function arguments, - DK4_E_BUFFER_TOO_SMALL
with dt.mem.nelem set to the number of tokens in string if the dpp array is too short. */ size_t dk4strx_tokenize( wxChar **dpp, size_t szdpp, wxChar *src, const wxChar *delim, dk4_er_t *erp ); /** Normalize a string (remove leading and trailing delimiters, replace delimiter sequences by just one delimiter). @param src Buffer containing the string to normalize. @param delim String containing delimiter characters, may be NULL to use the default delimiter set. */ void dk4strx_normalize(wxChar *src, const wxChar *delim); /** Find index of a string in an array of strings. @param arr Array of strings. @param str String to find in array. @param cs Flag: Case-sensitive search (0=no, other=yes). @return Non-negative index value on success, -1 on error. */ int dk4strx_array_index(const wxChar * const *arr, const wxChar *str, int cs); /** Find index of a string in an array of strings. @param arr Array of strings allowing abbreviation. @param spec Special character indicating start of optional text. @param str String to find in array. @param cs Flag: Case-sensitive search (0=no, other=yes). @return Non-negative index value on success, -1 on error. */ int dk4strx_abbr_index( const wxChar * const *arr, wxChar spec, const wxChar *str, int cs ); /** Check whether a text matches a pattern, the text may be abbreviated. @param str Text to check. @param pattern Pattern for comparison. @param spec Special character marking the abbreviation in the pattern. @param cs Flag: Case sensitive (1) or not (0). @return 1 for a match, 0 otherwise. */ int dk4strx_is_abbr(const wxChar *str, const wxChar *pattern, wxChar spec, int cs); /** Check whether a string represents a boolean value. @param str String to check. @return 1 if str represents a boolean value, 0 otherwise. */ int dk4strx_is_bool(const wxChar *str); /** Check whether a string represents the boolean value TRUE. @param str String to check. @return 1 if str represents the boolean value TRUE, 0 otherwise. */ int dk4strx_is_on(const wxChar *str); #if (defined(_WIN32) && DK4_USE_WINDOWS_LOCAL_ALLOC) \ || (DK4_HAVE_MALLOC && DK4_HAVE_FREE) /** Duplicate string in dynamically allocated memory. @param src String to duplicate. @param erp Error report, may be NULL. @return Pointer to string in new memory on succes, NULL on error. Use dk4mem_free() to release the memory after usage. Error codes: - DK4_E_INVALID_ARGUMENTS
if src is a NULL pointer, - DK4_E_MATH_OVERFLOW
on mathematical overflow in size calculation, - DK4_E_MEMORY
if no memory is available. */ wxChar * dk4strx_dup(const wxChar *src, dk4_er_t *erp); #endif /* if (defined(_WIN32) ... */ /** Remove trailing white spaces. @param str String to remove white spaces from. @param whsp White spaces set. */ void dk4strx_rtwh(wxChar *str, const wxChar *whsp); /** Remove trailing newline from line. @param lptr Line pointer. */ void dk4strx_delnl(wxChar *lptr); /** Find file name suffix. @param filename File name to find suffix from. @param erp Error report, may be NULL. @return Valid pointer on success, NULL on error. */ wxChar const * dk4strx_get_path_suffix(wxChar const *filename, dk4_er_t *erp); /** Check whether a file name is an absolute path. CRT on Windows: Not used. @param path Path name to check. @return 1 for absolute path, 0 for other path. */ int dk4strx_path_is_absolute(const wxChar *path); /** Check whether a file name is a relative path. CRT on Windows: Not used. @param path Path name to check. @return 1 for absolute path, 0 for other path. */ int dk4strx_path_is_relative(const wxChar *path); /** Append path filename to path name already stored in buffer. Resolve . and .. for current directory and parent directory. CRT on Windows: Optional, disabling CRT degrades performance. @param buffer Buffer already containing a path. @param sz Buffer size. @param filename Relative file name to append to buffer. @param erp Error report, may be NULL. @return 1 on success, 0 on error. Error codes: - DK4_E_INVALID_ARGUMENTS
if buffer or filename is NULL or sz is 0, - DK4_E_MATH_OVERFLOW
if filename is too long, - DK4_E_MEMORY
if allocation of a filename copy fails, - DK4_E_SYNTAX
if too many .. in filename, - DK4_E_BUFFER_TOO_SMALL
if buffer size is too small. */ int dk4strx_path_append( wxChar *buffer, size_t sz, const wxChar *filename, dk4_er_t *erp ); /** Find pointer to suffix. CRT on Windows: Not used. @param filename File name to find suffix for. @param erp Error report, may be NULL. @return Pointer to suffix dot if found, NULL otherwise. Error codes: - DK4_E_INVALID_ARGUMENTS
if filename is NULL, - DK4_E_NOT_FOUND
if the file name does not contain a suffix. */ wxChar * dk4strx_path_get_suffix(const wxChar *filename, dk4_er_t *erp); /** Correct file name separators from slash to backslash on Windows, vice versa on other systems. CRT on Windows: Not used. @param filename File name to correct. */ void dk4strx_path_correct_sep(wxChar *filename); /** Check whether file name needs expansion on Windows. CRT on Windows: Not used. @param filename File name to check. @return 1 if expansion is necessary, 0 otherwise. */ int dk4strx_path_must_expand(const wxChar *filename); /** Create file name with specified suffix. @param pdst Destination buffer. @param szdst Size of pdst (number of wxChar). @param srcname Source file name. @param newsu New file suffix. @param erp Error report, may be NULL. @return 1 on success, 0 on error. Error codes: - DK4_E_INVALID_ARGUMENTS
if pdst or srcname or newsu is NULL or szdst is 0, - DK4_E_BUFFER_TOO_SMALL
if the dst buffer is too small. - DK4_E_MATH_OVERFLOW
if a mathematical overflow occured in size calculation, */ int dk4strx_path_set_suffix( wxChar *pdst, size_t szdst, wxChar const *srcname, wxChar const *newsu, dk4_er_t *erp ); /** Create a dynamic copy of a file name with changed suffix. @param srcname Old file name. @param newsu New suffix. @param erp Error report, may be NULL. @return Valid pointer to changed file name on success, NULL on error. On success call dk4mem_free() on the pointer when no longer needed to release the memory. Error codes: - DK4_E_INVALID_ARGUMENTS
if srcname or newsu is NULL, - DK4_E_BUG
if a bug occured, - DK4_E_MATH_OVERFLOW
if a mathematical overflow occured in size calculation, - DK4_E_MEMORY_ALLOCATION_FAILED
with mem.elsize and mem.nelem set if there is not enough memory available. */ wxChar * dk4strx_path_dup_change_suffix( wxChar const *srcname, wxChar const *newsu, dk4_er_t *erp ); /** Calculate buffer size required to concatenate a directory name and a file name. @param dirname Directory name. @param filename File name. @param erp Error report, may be NULL. @return Buffer size for concatenated names including the finalizer byte on success, 0 on error. Error codes: - DK4_E_INVALID_ARGUMENTS
if dirname or filename is NULL, - DK4_E_MATH_OVERFLOW
if the calculation results in a numeric overflow. */ size_t dk4strx_path_concatenate_size( wxChar const *dirname, wxChar const *filename, dk4_er_t *erp ); /** Concatenate a directory name and a file name into an existing buffer. This function simply concatenates directory name and file name, . and .. for current and parent directory are not resolved. @param buffer Result buffer for combined file name. @param szbuf Buffer size (number of wxChar). @param dirn Directory name. @param filen File name. @param erp Error report, may be NULL. @return 1 on success, 0 on error. Error codes: - DK4_E_INVALID_ARGUMENTS
if buffer, dirn or filen is NULL or szbuf is 0, - DK4_E_BUFFER_TOO_SMALL
if the buffer is too small. */ int dk4strx_path_concatenate_buffer( wxChar *buffer, size_t szbuf, wxChar const *dirn, wxChar const *filen, dk4_er_t *erp ); /** Concatenate a directory name and a file name into newly allocated memory. This function simply concatenates directory name and file name, . and .. for current and parent directory are not resolved. @param dirn Directory name. @param filen File name. @param erp Error report, may be NULL. @return Valid pointer to newly allocated memory containing the concatenation on success, NULL on error. On success use dk4mem_free() to release the memory when done with it. Error codes: - DK4_E_INVALID_ARGUMENTS
if dirn or filen is NULL, - DK4_E_MATH_OVERFLOW
if the size calculation results in a numeric overflow. - DK4_E_MEMORY_ALLOCATION_FAILED
if the memory allocation failed, - DK4_E_BUFFER_TOO_SMALL
if the buffer is too small. */ wxChar * dk4strx_path_concatenate_new( wxChar const *dirn, wxChar const *filen, dk4_er_t *erp ); /** Check whether a file name is in specified directory. There is no check for the presence of the file, just a name check. @param filename File name to check. @param parent Directory the file should be in. @param erp Error report, may be NULL. @return Non-zero value if file is in directory, 0 otherwise. */ int dk4strx_path_is_in_subdir( const wxChar *filename, const wxChar *parent, dk4_er_t *erp ); #ifdef __cplusplus } #endif /* vim: set ai sw=4 ts=4 : */ %% module #include "dk4conf.h" #ifndef DK4_SIZEOF_WXCHAR #ifndef DK4WXCS_H_INCLUDED #include "dk4wxcs.h" #endif #endif #include "dk4strx.h" #include "dk4mem.h" #include "dk4mpl.h" #if DK4_SIZEOF_WXCHAR > 1 #include "dk4strw.h" #include "dk4pathw.h" #else #include "dk4str8.h" #include "dk4path8.h" #endif $!trace-include int dk4strx_cpy_s(wxChar *dst, size_t sz, const wxChar *src, dk4_er_t *erp) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_cpy_s(dst, sz, src, erp)); #else return (dk4str8_cpy_s(dst, sz, src, erp)); #endif } int dk4strx_cat_s(wxChar *dst, size_t sz, const wxChar *src, dk4_er_t *erp) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_cat_s(dst, sz, src, erp)); #else return (dk4str8_cat_s(dst, sz, src, erp)); #endif } void dk4strx_cpy_to_left(wxChar *dst, const wxChar *src) { #if DK4_SIZEOF_WXCHAR > 1 dk4strw_cpy_to_left(dst, src); #else dk4str8_cpy_to_left(dst, src); #endif } size_t dk4strx_len(const wxChar *src) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_len(src)); #else return (dk4str8_len(src)); #endif } int dk4strx_cmp(const wxChar *s1, const wxChar *s2) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_cmp(s1, s2)); #else return (dk4str8_cmp(s1, s2)); #endif } int dk4strx_casecmp(const wxChar *s1, const wxChar *s2) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_casecmp(s1, s2)); #else return (dk4str8_casecmp(s1, s2)); #endif } int dk4strx_pathcmp(const wxChar *s1, const wxChar *s2) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_pathcmp(s1, s2)); #else return (dk4str8_pathcmp(s1, s2)); #endif } wxChar * dk4strx_chr(const wxChar *s, wxChar c) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_chr(s, c)); #else return (dk4str8_chr(s, c)); #endif } wxChar * dk4strx_rchr(const wxChar *s, wxChar c) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_rchr(s, c)); #else return (dk4str8_rchr(s, c)); #endif } wxChar * dk4strx_sep(wxChar **stringp, const wxChar *delim) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_sep(stringp, delim)); #else return (dk4str8_sep(stringp, delim)); #endif } wxChar * dk4strx_start(const wxChar *src, const wxChar *delim) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_start(src, delim)); #else return (dk4str8_start(src, delim)); #endif } wxChar * dk4strx_next(wxChar *src, const wxChar *delim) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_next(src, delim)); #else return (dk4str8_next(src, delim)); #endif } size_t dk4strx_tokenize( wxChar **dpp, size_t szdpp, wxChar *src, const wxChar *delim, dk4_er_t *erp ) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_tokenize(dpp, szdpp, src, delim, erp)); #else return (dk4str8_tokenize(dpp, szdpp, src, delim, erp)); #endif } void dk4strx_normalize(wxChar *src, const wxChar *delim) { #if DK4_SIZEOF_WXCHAR > 1 dk4strw_normalize(src, delim); #else dk4str8_normalize(src, delim); #endif } int dk4strx_array_index(const wxChar * const *arr, const wxChar *str, int cs) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_array_index(arr, str, cs)); #else return (dk4str8_array_index(arr, str, cs)); #endif } int dk4strx_abbr_index( const wxChar * const *arr, wxChar spec, const wxChar *str, int cs ) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_abbr_index(arr, spec, str, cs)); #else return (dk4str8_abbr_index(arr, spec, str, cs)); #endif } int dk4strx_is_abbr(const wxChar *str, const wxChar *pattern, wxChar spec, int cs) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_is_abbr(str, pattern, spec, cs)); #else return (dk4str8_is_abbr(str, pattern, spec, cs)); #endif } int dk4strx_is_bool(const wxChar *str) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_is_bool(str)); #else return (dk4str8_is_bool(str)); #endif } int dk4strx_is_on(const wxChar *str) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_is_on(str)); #else return (dk4str8_is_on(str)); #endif } #if (defined(_WIN32) && DK4_USE_WINDOWS_LOCAL_ALLOC) \ || (DK4_HAVE_MALLOC && DK4_HAVE_FREE) wxChar * dk4strx_dup(const wxChar *src, dk4_er_t *erp) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4strw_dup(src, erp)); #else return (dk4str8_dup(src, erp)); #endif } #endif /* if (defined(_WIN32) ... */ void dk4strx_rtwh(wxChar *str, const wxChar *whsp) { #if DK4_SIZEOF_WXCHAR > 1 dk4strw_rtwh(str, whsp); #else dk4str8_rtwh(str, whsp); #endif } void dk4strx_delnl(wxChar *lptr) { #if DK4_SIZEOF_WXCHAR > 1 dk4strw_delnl(lptr); #else dk4str8_delnl(lptr); #endif } wxChar const * dk4strx_get_path_suffix(wxChar const *filename, dk4_er_t *erp) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4pathw_get_suffix(filename, erp)); #else return (dk4path8_get_suffix(filename, erp)); #endif } int dk4strx_path_is_absolute(const wxChar *path) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4pathw_is_absolute(path)); #else return (dk4path8_is_absolute(path)); #endif } int dk4strx_path_is_relative(const wxChar *path) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4pathw_is_relative(path)); #else return (dk4path8_is_relative(path)); #endif } int dk4strx_path_append( wxChar *buffer, size_t sz, const wxChar *filename, dk4_er_t *erp ) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4pathw_append(buffer, sz, filename, erp)); #else return (dk4path8_append(buffer, sz, filename, erp)); #endif } wxChar * dk4strx_path_get_suffix(const wxChar *filename, dk4_er_t *erp) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4pathw_get_suffix(filename, erp)); #else return (dk4path8_get_suffix(filename, erp)); #endif } void dk4strx_path_correct_sep(wxChar *filename) { #if DK4_SIZEOF_WXCHAR > 1 dk4pathw_correct_sep(filename); #else dk4path8_correct_sep(filename); #endif } int dk4strx_path_must_expand(const wxChar *filename) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4pathw_must_expand(filename)); #else return (dk4path8_must_expand(filename)); #endif } int dk4strx_path_set_suffix( wxChar *pdst, size_t szdst, wxChar const *srcname, wxChar const *newsu, dk4_er_t *erp ) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4pathw_set_suffix(pdst, szdst, srcname, newsu, erp)); #else return (dk4path8_set_suffix(pdst, szdst, srcname, newsu, erp)); #endif } wxChar * dk4strx_path_dup_change_suffix( wxChar const *srcname, wxChar const *newsu, dk4_er_t *erp ) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4pathw_dup_change_suffix(srcname, newsu, erp)); #else return (dk4path8_dup_change_suffix(srcname, newsu, erp)); #endif } size_t dk4strx_path_concatenate_size( wxChar const *dirname, wxChar const *filename, dk4_er_t *erp ) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4pathw_concatenate_size(dirname, filename, erp)); #else return (dk4path8_concatenate_size(dirname, filename, erp)); #endif } int dk4strx_path_concatenate_buffer( wxChar *buffer, size_t szbuf, wxChar const *dirn, wxChar const *filen, dk4_er_t *erp ) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4pathw_concatenate_buffer(buffer, szbuf, dirn, filen, erp)); #else return (dk4path8_concatenate_buffer(buffer, szbuf, dirn, filen, erp)); #endif } wxChar * dk4strx_path_concatenate_new( wxChar const *dirn, wxChar const *filen, dk4_er_t *erp ) { #if DK4_SIZEOF_WXCHAR > 1 return (dk4pathw_concatenate_new(dirn, filen, erp)); #else return (dk4path8_concatenate_new(dirn, filen, erp)); #endif } int dk4strx_path_is_in_subdir( const wxChar *filename,const wxChar *parent,dk4_er_t *erp ) { wxChar buf[DK4_MAX_PATH]; size_t szf; size_t szp; int back = 0; $? "+ dk4path_is_in_subdir f=\"%!ws\" p=\"%!ws\"", filename, parent if ((NULL != filename) && (NULL != parent)) { if (0 != dk4strx_cpy_s(buf, DK4_SIZEOF(buf,wxChar), filename, erp)) { szf = dk4strx_len(buf); szp = dk4strx_len(parent); if (szf > szp) { $? ". filename larger than parent" #if DK4_ON_WINDOWS || DK4_HAVE_BACKSLASH_AS_SEP if (buf[szp] == wxT('\\')) #else if (buf[szp] == wxT('/')) #endif { $? ". separator at right place" buf[szp] = wxT('\0'); if (0 == dk4strx_pathcmp(buf, parent)) { back = 1; $? ". match" } #if DK4_ON_WINDOWS || DK4_HAVE_BACKSLASH_AS_SEP buf[szp] = wxT('\\'); #else buf[szp] = wxT('/'); #endif } } else { if (szf == szp) { $? ". equal lengths" if (0 == dk4strx_pathcmp(buf, parent)) { back = 1; $? ". match" } } } } } else { dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS); } $? "- dk4path_is_in_subdir %d", back return back; } /* vim: set ai sw=4 ts=4 : */