1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 #pragma once
20 
21 /** \file
22  * \ingroup bli
23  */
24 
25 #include "BLI_compiler_attrs.h"
26 #include "BLI_utildefines.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 void BLI_setenv(const char *env, const char *val) ATTR_NONNULL(1);
33 void BLI_setenv_if_new(const char *env, const char *val) ATTR_NONNULL(1);
34 const char *BLI_getenv(const char *env) ATTR_NONNULL(1);
35 
36 void BLI_make_file_string(const char *relabase, char *string, const char *dir, const char *file);
37 bool BLI_make_existing_file(const char *name);
38 void BLI_split_dirfile(
39     const char *string, char *dir, char *file, const size_t dirlen, const size_t filelen);
40 void BLI_split_dir_part(const char *string, char *dir, const size_t dirlen);
41 void BLI_split_file_part(const char *string, char *file, const size_t filelen);
42 const char *BLI_path_extension(const char *filepath) ATTR_NONNULL();
43 
44 void BLI_path_append(char *__restrict dst, const size_t maxlen, const char *__restrict file)
45     ATTR_NONNULL();
46 void BLI_join_dirfile(char *__restrict dst,
47                       const size_t maxlen,
48                       const char *__restrict dir,
49                       const char *__restrict file) ATTR_NONNULL();
50 size_t BLI_path_join(char *__restrict dst, const size_t dst_len, const char *path_first, ...)
51     ATTR_NONNULL(1, 3) ATTR_SENTINEL(0);
52 const char *BLI_path_basename(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT;
53 bool BLI_path_name_at_index(const char *__restrict path,
54                             const int index,
55                             int *__restrict r_offset,
56                             int *__restrict r_len) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT;
57 
58 const char *BLI_path_slash_rfind(const char *string) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT;
59 int BLI_path_slash_ensure(char *string) ATTR_NONNULL();
60 void BLI_path_slash_rstrip(char *string) ATTR_NONNULL();
61 const char *BLI_path_slash_find(const char *string) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT;
62 void BLI_path_slash_native(char *path) ATTR_NONNULL();
63 
64 #ifdef _WIN32
65 bool BLI_path_program_extensions_add_win32(char *name, const size_t maxlen);
66 #endif
67 bool BLI_path_program_search(char *fullname, const size_t maxlen, const char *name);
68 
69 bool BLI_path_extension_check(const char *str, const char *ext)
70     ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT;
71 bool BLI_path_extension_check_n(const char *str, ...) ATTR_NONNULL(1) ATTR_SENTINEL(0);
72 bool BLI_path_extension_check_array(const char *str, const char **ext_array)
73     ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT;
74 bool BLI_path_extension_check_glob(const char *str, const char *ext_fnmatch)
75     ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT;
76 bool BLI_path_extension_glob_validate(char *ext_fnmatch) ATTR_NONNULL();
77 bool BLI_path_extension_replace(char *path, size_t maxlen, const char *ext) ATTR_NONNULL();
78 bool BLI_path_extension_ensure(char *path, size_t maxlen, const char *ext) ATTR_NONNULL();
79 bool BLI_path_filename_ensure(char *filepath, size_t maxlen, const char *filename) ATTR_NONNULL();
80 int BLI_path_sequence_decode(const char *string,
81                              char *head,
82                              char *tail,
83                              unsigned short *r_num_len);
84 void BLI_path_sequence_encode(
85     char *string, const char *head, const char *tail, unsigned short numlen, int pic);
86 
87 void BLI_path_normalize(const char *relabase, char *path) ATTR_NONNULL(2);
88 /* Same as above but adds a trailing slash. */
89 void BLI_path_normalize_dir(const char *relabase, char *dir) ATTR_NONNULL(2);
90 
91 bool BLI_filename_make_safe(char *fname) ATTR_NONNULL(1);
92 bool BLI_path_make_safe(char *path) ATTR_NONNULL(1);
93 
94 /* Go back one directory. */
95 bool BLI_path_parent_dir(char *path) ATTR_NONNULL();
96 /* Go back until the directory is found. */
97 bool BLI_path_parent_dir_until_exists(char *path) ATTR_NONNULL();
98 
99 bool BLI_path_abs(char *path, const char *basepath) ATTR_NONNULL();
100 bool BLI_path_frame(char *path, int frame, int digits) ATTR_NONNULL();
101 bool BLI_path_frame_range(char *path, int sta, int end, int digits) ATTR_NONNULL();
102 bool BLI_path_frame_get(char *path, int *r_frame, int *numdigits) ATTR_NONNULL();
103 void BLI_path_frame_strip(char *path, char *ext) ATTR_NONNULL();
104 bool BLI_path_frame_check_chars(const char *path) ATTR_NONNULL();
105 bool BLI_path_abs_from_cwd(char *path, const size_t maxlen) ATTR_NONNULL();
106 void BLI_path_rel(char *file, const char *relfile) ATTR_NONNULL();
107 
108 bool BLI_path_is_rel(const char *path) ATTR_NONNULL() ATTR_WARN_UNUSED_RESULT;
109 bool BLI_path_is_unc(const char *path);
110 
111 void BLI_path_to_display_name(char *display_name, int maxlen, const char *name) ATTR_NONNULL();
112 
113 #if defined(WIN32)
114 void BLI_path_normalize_unc_16(wchar_t *path_16);
115 void BLI_path_normalize_unc(char *path_16, int maxlen);
116 #endif
117 
118 bool BLI_path_suffix(char *string, size_t maxlen, const char *suffix, const char *sep)
119     ATTR_NONNULL();
120 
121 /* path string comparisons: case-insensitive for Windows, case-sensitive otherwise */
122 #if defined(WIN32)
123 #  define BLI_path_cmp BLI_strcasecmp
124 #  define BLI_path_ncmp BLI_strncasecmp
125 #else
126 #  define BLI_path_cmp strcmp
127 #  define BLI_path_ncmp strncmp
128 #endif
129 
130 /* these values need to be hardcoded in structs, dna does not recognize defines */
131 /* also defined in DNA_space_types.h */
132 #ifndef FILE_MAXDIR
133 #  define FILE_MAXDIR 768
134 #  define FILE_MAXFILE 256
135 #  define FILE_MAX 1024
136 #endif
137 
138 #ifdef WIN32
139 #  define SEP '\\'
140 #  define ALTSEP '/'
141 #  define SEP_STR "\\"
142 #  define ALTSEP_STR "/"
143 #else
144 #  define SEP '/'
145 #  define ALTSEP '\\'
146 #  define SEP_STR "/"
147 #  define ALTSEP_STR "\\"
148 #endif
149 
150 /* Parent and current dir helpers. */
151 #define FILENAME_PARENT ".."
152 #define FILENAME_CURRENT "."
153 
154 /* Avoid calling strcmp on one or two chars! */
155 #define FILENAME_IS_PARENT(_n) (((_n)[0] == '.') && ((_n)[1] == '.') && ((_n)[2] == '\0'))
156 #define FILENAME_IS_CURRENT(_n) (((_n)[0] == '.') && ((_n)[1] == '\0'))
157 #define FILENAME_IS_CURRPAR(_n) \
158   (((_n)[0] == '.') && (((_n)[1] == '\0') || (((_n)[1] == '.') && ((_n)[2] == '\0'))))
159 
160 #ifdef __cplusplus
161 }
162 #endif
163