Debian Codegraph @

melder/STRVEC.cpp

raw symbols SCIP
1 /* STRVEC.cpp
2 *
3 * Copyright (C) 2006,2007,2009,2011,2012,2015-2026 Paul Boersma
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #if defined (_WIN32)
20 #include "winport_on.h"
21 #include <windows.h>
22 #include "winport_off.h"
23 #else
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <dirent.h>
27 #endif
28
29 #include "melder.h"cxx . . $ `<file>/melder/melder.h`/
30
31 static autoSTRVECcxx . . $ autoSTRVEC# fileOrFolderNames_STRVECcxx . . $ fileOrFolderNames_STRVEC(51211524670ca482). (conststring32cxx . . $ conststring32# path /* cattable */, bool wantDirectories, bool caseSensitive) {
32 /*
33 Resolve home-relative paths.
34 */
35 if (const bool pathIsHomeRelative = ( path [0] == U'~' &&
36 (path [1] == U'/' || path [1] == Melder_DIRECTORY_SEPARATORcxx . . $ `melder/melder_files.h:24:10`! || path [1] == U'\0') )
37 ) {
38 char32cxx . . $ char32# absolutePath [kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1];
39 Melder_sprintcxx . . $ Melder_sprint(2b992b0fb5123bdf). (absolutePath,kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1, Melder_peek8to32_u (getenvcxx . . $ getenv(c63359b659779399). ("HOME")), & path [1]);
40 return fileOrFolderNames_STRVECcxx . . $ fileOrFolderNames_STRVEC(51211524670ca482). (absolutePath, wantDirectories, caseSensitive);
41 }
42 #if defined (_WIN32)
43 try {
44 /*
45 Parse the path.
46 This can be either a directory name such as "/Users/paul/sounds"
47 or a wildcarded path such as "/Users/paul/sounds/h*.wav".
48 Example: in "/Users/paul/sounds/h*llo.*av",
49 the search directory is "/Users/paul/sounds",
50 the left environment is "h", the middle environment is "llo.", and the right environment is "av".
51 */
52 autoMelderString searchDirectory, left, middle, right, filePath;
53 MelderString_copy (& searchDirectory, path);
54 char32 *const asterisk1 = str32chr (searchDirectory.string, U'*');
55 char32 *const asterisk2 = str32rchr (searchDirectory.string, U'*');
56 if (asterisk1) {
57 /*
58 The path is a wildcarded path.
59 */
60 *asterisk1 = U'\0';
61 *asterisk2 = U'\0';
62 searchDirectory. length = asterisk1 - searchDirectory.string; // probably superfluous, but correct
63 char32 *lastSlash = str32rchr (searchDirectory.string, U'/');
64 if (! lastSlash)
65 lastSlash = str32rchr (searchDirectory.string, U'\\');
66 if (lastSlash) {
67 *lastSlash = U'\0'; // this fixes searchDirectory
68 searchDirectory. length = lastSlash - searchDirectory.string; // probably superfluous, but correct
69 MelderString_copy (& left, lastSlash + 1);
70 } else {
71 MelderString_copy (& left, searchDirectory.string); // quickly save...
72 MelderString_empty (& searchDirectory); // ...before destruction
73 }
74 if (asterisk1 != asterisk2) {
75 MelderString_copy (& middle, asterisk1 + 1);
76 }
77 MelderString_copy (& right, asterisk2 + 1);
78 } else {
79 /*
80 We're finished. No asterisk, hence the path is a directory name.
81 */
82 }
83 WIN32_FIND_DATAW findData;
84 HANDLE searchHandle = FindFirstFileW (Melder_peek32toW_fileSystem (
85 Melder_cat (searchDirectory.string [0] ? searchDirectory.string : U".", U"/*")), & findData);
86 if (searchHandle == INVALID_HANDLE_VALUE)
87 Melder_throw (U"Cannot open folder ", searchDirectory.string, U".");
88 autoSTRVEC strings;
89 do {
90 if ((! wantDirectories && (findData. dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0)
91 || (wantDirectories && (findData. dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0))
92 {
93 if (findData. cFileName [0] == L'.')
94 continue;
95 MelderString_copy (& filePath, searchDirectory.string [0] ? searchDirectory.string : U".");
96 MelderString_appendCharacter (& filePath, Melder_DIRECTORY_SEPARATOR);
97 char32 buffer32 [kMelder_MAXPATH+1];
98 Melder_sprint (buffer32,kMelder_MAXPATH+1, Melder_peekWto32 (findData. cFileName));
99 MelderString_append (& filePath, buffer32);
100 const int64 length = Melder_length (buffer32);
101 integer numberOfMatchedCharacters = 0;
102 bool doesTheLeftMatch = true;
103 if (left. length != 0) {
104 doesTheLeftMatch = str32nequ_optionallyCaseSensitive (buffer32, left.string, left. length, caseSensitive);
105 if (doesTheLeftMatch)
106 numberOfMatchedCharacters = left.length;
107 }
108 bool doesTheMiddleMatch = true;
109 if (middle. length != 0) {
110 const char32 *const position = str32str_optionallyCaseSensitive (buffer32 + numberOfMatchedCharacters, middle.string, caseSensitive);
111 doesTheMiddleMatch = !! position;
112 if (doesTheMiddleMatch)
113 numberOfMatchedCharacters = position - buffer32 + middle.length;
114 }
115 bool doesTheRightMatch = true;
116 if (right. length != 0) {
117 const int64 startOfRight = length - right. length;
118 doesTheRightMatch = startOfRight >= numberOfMatchedCharacters &&
119 str32equ_optionallyCaseSensitive (buffer32 + startOfRight, right.string, caseSensitive);
120 }
121 if (buffer32 [0] != U'.' && doesTheLeftMatch && doesTheMiddleMatch && doesTheRightMatch)
122 strings. append (buffer32);
123 }
124 } while (FindNextFileW (searchHandle, & findData));
125 FindClose (searchHandle);
126 sort_STRVEC_inout (strings.get());
127 return strings;
128 } catch (MelderError) {
129 throw;
130 }
131 #else // use stat
132 DIR *d = nullptr;
133 try {
134 /*
135 Parse the path.
136 This can be either a directory name such as "/Users/paul/sounds"
137 or a wildcarded path such as "/Users/paul/sounds/h*.wav".
138 Example: in "/Users/paul/sounds/h*llo.*av",
139 the search directory is "/Users/paul/sounds",
140 the left environment is "h", the middle environment is "llo.", and the right environment is "av".
141 */
142 autoMelderStringcxx . . $ autoMelderString# searchDirectorycxx . . $ autoMelderString#autoMelderString(49f6e7a06ebc5aa8)., leftcxx . . $ autoMelderString#autoMelderString(49f6e7a06ebc5aa8)., middlecxx . . $ autoMelderString#autoMelderString(49f6e7a06ebc5aa8)., rightcxx . . $ autoMelderString#autoMelderString(49f6e7a06ebc5aa8)., filePathcxx . . $ autoMelderString#autoMelderString(49f6e7a06ebc5aa8).;
143 MelderString_copycxx . . $ MelderString_copy(65be51a793e0ff96). (& searchDirectory, path);
144 char32cxx . . $ char32# *const asterisk1 = str32chrcxx . . $ str32chr(328a9ede0fba853a). (searchDirectory.stringcxx . . $ MelderString#string., U'*');
145 char32cxx . . $ char32# *const asterisk2 = str32rchrcxx . . $ str32rchr(328a9ede0fba853a). (searchDirectory.stringcxx . . $ MelderString#string., U'*');
146 if (asterisk1) {
147 /*
148 The path is a wildcarded path.
149 */
150 *asterisk1 = U'\0';
151 *asterisk2 = U'\0';
152 searchDirectory. lengthcxx . . $ MelderString#length. = asterisk1 - searchDirectory.stringcxx . . $ MelderString#string.; // probably superfluous, but correct
153 char32cxx . . $ char32# *const lastSlash = str32rchrcxx . . $ str32rchr(328a9ede0fba853a). (searchDirectory.stringcxx . . $ MelderString#string., Melder_DIRECTORY_SEPARATORcxx . . $ `melder/melder_files.h:24:10`!);
154 if (lastSlash) {
155 *lastSlash = U'\0'; // this fixes searchDirectory
156 searchDirectory. lengthcxx . . $ MelderString#length. = lastSlash - searchDirectory.stringcxx . . $ MelderString#string.; // probably superfluous, but correct
157 MelderString_copycxx . . $ MelderString_copy(65be51a793e0ff96). (& left, lastSlash + 1);
158 } else {
159 MelderString_copycxx . . $ MelderString_copy(65be51a793e0ff96). (& left, searchDirectory.stringcxx . . $ MelderString#string.); // quickly save...
160 MelderString_emptycxx . . $ MelderString_empty(93db1090f27e5462). (& searchDirectory); // ...before destruction
161 }
162 if (asterisk1 != asterisk2) {
163 MelderString_copycxx . . $ MelderString_copy(65be51a793e0ff96). (& middle, asterisk1 + 1);
164 }
165 MelderString_copycxx . . $ MelderString_copy(65be51a793e0ff96). (& right, asterisk2 + 1);
166 } else {
167 /*
168 We're finished. No asterisk, hence the path is a directory name.
169 */
170 }
171 char buffer8 [kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1];
172 Melder_32to8_fileSystem_inplacecxx . . $ Melder_32to8_fileSystem_inplace(d86258d5722a82b2). (searchDirectory.stringcxx . . $ MelderString#string., buffer8);
173 d = opendir (buffer8 [0] ? buffer8 : ".");
174 if (! d)
175 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Cannot open folder ", searchDirectory.string, U".");
176 //Melder_casual (U"opened");
177 autoSTRVECcxx . . $ autoSTRVEC# stringscxx . . $ _autostringautovector#_autostringautovector(49f6e7a06ebc5aa8).;
178 struct dirent *entry;
179 while (!! (entry = readdir (d))) {
180 MelderString_copycxx . . $ MelderString_copy(65be51a793e0ff96). (& filePath, searchDirectory.stringcxx . . $ MelderString#string. [0] ? searchDirectory.stringcxx . . $ MelderString#string. : U".");
181 MelderString_appendCharactercxx . . $ MelderString_appendCharacter(f4ab64866cffe712). (& filePath, Melder_DIRECTORY_SEPARATORcxx . . $ `melder/melder_files.h:24:10`!);
182 char32cxx . . $ char32# buffer32 [kMelder_MAXPATHcxx . . $ `melder/melder_files.h:30:9`!+1];
183 Melder_8bitFileRepresentationToStr32_inplacecxx . . $ Melder_8bitFileRepresentationToStr32_inplace(9a630526fa4dddfe). (entry -> d_name, buffer32);
184 MelderString_appendcxx . . $ MelderString_append(65be51a793e0ff96). (& filePath, buffer32);
185 //Melder_casual (U"read ", filePath.string);
186 Melder_32to8_fileSystem_inplacecxx . . $ Melder_32to8_fileSystem_inplace(d86258d5722a82b2). (filePath.stringcxx . . $ MelderString#string., buffer8);
187 struct stat stats;
188 if (stat (buffer8, & stats) != 0) {
189 //Melder_throw (U"Cannot look at file ", filePath.string, U".");
190 //stats. st_mode = -1L;
191 }
192 //Melder_casual (U"statted ", filePath.string);
193 //Melder_casual (U"file ", filePath.string, U" mode ", stats. st_mode / 4096);
194 if ((! wantDirectories && S_ISREGcxx . . $ `<external>/531885356569766532/stat.h:128:9`! (stats. st_mode)) || (wantDirectories && S_ISDIRcxx . . $ `<external>/531885356569766532/stat.h:125:9`! (stats. st_mode))) {
195 Melder_8bitFileRepresentationToStr32_inplacecxx . . $ Melder_8bitFileRepresentationToStr32_inplace(9a630526fa4dddfe). (entry -> d_name, buffer32);
196 const int64cxx . . $ int64# length = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (buffer32);
197 integercxx . . $ integer# numberOfMatchedCharacters = 0;
198 bool doesTheLeftMatch = true;
199 if (left. lengthcxx . . $ MelderString#length. != 0) {
200 doesTheLeftMatch = str32nequ_optionallyCaseSensitivecxx . . $ `melder/melder_str32.h:129:9`! (buffer32, left.stringcxx . . $ MelderString#string., left. lengthcxx . . $ MelderString#length., caseSensitive);
201 if (doesTheLeftMatch)
202 numberOfMatchedCharacters = left.lengthcxx . . $ MelderString#length.;
203 }
204 bool doesTheMiddleMatch = true;
205 if (middle. lengthcxx . . $ MelderString#length. != 0) {
206 const char32cxx . . $ char32# *const position = str32str_optionallyCaseSensitivecxx . . $ str32str_optionallyCaseSensitive(7e4d3d07cdd0a044). (buffer32 + numberOfMatchedCharacters, middle.stringcxx . . $ MelderString#string., caseSensitive);
207 doesTheMiddleMatch = !! position;
208 if (doesTheMiddleMatch)
209 numberOfMatchedCharacters = position - buffer32 + middle.lengthcxx . . $ MelderString#length.;
210 }
211 bool doesTheRightMatch = true;
212 if (right. lengthcxx . . $ MelderString#length. != 0) {
213 const int64cxx . . $ int64# startOfRight = length - right. lengthcxx . . $ MelderString#length.;
214 doesTheRightMatch = startOfRight >= numberOfMatchedCharacters &&
215 str32equ_optionallyCaseSensitivecxx . . $ `melder/melder_str32.h:128:9`! (buffer32 + startOfRight, right.stringcxx . . $ MelderString#string., caseSensitive);
216 }
217 if (buffer32 [0] != U'.' && doesTheLeftMatch && doesTheMiddleMatch && doesTheRightMatch)
218 strings. appendcxx . . $ _autostringautovector#append(ff00d524ab0355ec). (buffer32);
219 }
220 }
221 closedir (d);
222 sort_STRVEC_inoutcxx . . $ sort_STRVEC_inout(276ad51d55a4042f). (strings.getcxx . . $ _autostringautovector#get(9d56076a9ef7dd02).());
223 return stringscxx . . $ _autostringautovector#_autostringautovector(f820baba8b4ac957).;
224 } catch (MelderErrorcxx . . $ MelderError#) {
225 if (d)
226 closedir (d); // "finally"
227 throw;
228 }
229 #endif
230 }
231 autoSTRVECcxx . . $ autoSTRVEC# fileNames_STRVECcxx . . $ fileNames_STRVEC(d3c03a92d990d008). (conststring32cxx . . $ conststring32# path /* cattable */) {
232 return fileOrFolderNames_STRVECcxx . . $ fileOrFolderNames_STRVEC(51211524670ca482). (path, false, true);
233 }
234 autoSTRVECcxx . . $ autoSTRVEC# folderNames_STRVECcxx . . $ folderNames_STRVEC(d3c03a92d990d008). (conststring32cxx . . $ conststring32# path /* cattable */) {
235 return fileOrFolderNames_STRVECcxx . . $ fileOrFolderNames_STRVEC(51211524670ca482). (path, true, true);
236 }
237 autoSTRVECcxx . . $ autoSTRVEC# fileNames_caseInsensitive_STRVECcxx . . $ fileNames_caseInsensitive_STRVEC(d3c03a92d990d008). (conststring32cxx . . $ conststring32# path /* cattable */) {
238 return fileOrFolderNames_STRVECcxx . . $ fileOrFolderNames_STRVEC(51211524670ca482). (path, false, false);
239 }
240 autoSTRVECcxx . . $ autoSTRVEC# folderNames_caseInsensitive_STRVECcxx . . $ folderNames_caseInsensitive_STRVEC(d3c03a92d990d008). (conststring32cxx . . $ conststring32# path /* cattable */) {
241 return fileOrFolderNames_STRVECcxx . . $ fileOrFolderNames_STRVEC(51211524670ca482). (path, true, false);
242 }
243
244 autoSTRVECcxx . . $ autoSTRVEC# readLinesFromFile_STRVECcxx . . $ readLinesFromFile_STRVEC(963377c6631bedd0). (MelderFilecxx . . $ MelderFile# file) {
245 autoMelderReadTextcxx . . $ autoMelderReadText# text = MelderReadText_createFromFilecxx . . $ MelderReadText_createFromFile(30337d9e1397edb4). (file);
246 int64cxx . . $ int64# numberOfLines = MelderReadText_getNumberOfLinescxx . . $ MelderReadText_getNumberOfLines(c4cc05fb41712971). (text.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).());
247 if (numberOfLines == 0)
248 return autoSTRVECcxx . . $ _autostringautovector#_autostringautovector(49f6e7a06ebc5aa8). ();
249 autoSTRVECcxx . . $ autoSTRVEC# result = autoSTRVECcxx . . $ autoSTRVEC# (numberOfLines);
250 for (integercxx . . $ integer# iline = 1; iline <= numberOfLines; iline ++) {
251 const mutablestring32cxx . . $ mutablestring32# line = MelderReadText_readLinecxx . . $ MelderReadText_readLine(f4aa1d9b94177768). (text.getcxx . . $ std/unique_ptr#get(ea7391e9eeeaf20e).());
252 result [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).iline] =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (line);
253 }
254 return resultcxx . . $ _autostringautovector#_autostringautovector(f820baba8b4ac957).;
255 }
256
257 autoSTRVECcxx . . $ autoSTRVEC# splitByWhitespace_STRVECcxx . . $ splitByWhitespace_STRVEC(d3c03a92d990d008). (conststring32cxx . . $ conststring32# string) {
258 if (! string)
259 return autoSTRVECcxx . . $ _autostringautovector#_autostringautovector(49f6e7a06ebc5aa8).(); // accept null pointer input
260 integercxx . . $ integer# n = NUMnumberOfTokenscxx . . $ NUMnumberOfTokens(3fae411214b133ad). (string);
261 if (n == 0)
262 return autoSTRVECcxx . . $ _autostringautovector#_autostringautovector(49f6e7a06ebc5aa8).();
263 autoSTRVECcxx . . $ autoSTRVEC# resultcxx . . $ _autostringautovector#_autostringautovector(1624ee8275b10bda). (n);
264
265 integercxx . . $ integer# itoken = 0;
266 const char32cxx . . $ char32# *p = & string [0];
267 for (;;) {
268 Melder_skipHorizontalOrVerticalSpacecxx . . $ Melder_skipHorizontalOrVerticalSpace(1babf4dbe7380927). (& p);
269 if (*p == U'\0')
270 break;
271 const char32cxx . . $ char32# *beginOfInk = p;
272 p ++; // step over first nonspace
273 p = Melder_findEndOfInkcxx . . $ Melder_findEndOfInk(5553299aa8c4fee9). (p);
274 const integercxx . . $ integer# numberOfCharacters = p - beginOfInk;
275 autostring32cxx . . $ autostring32# tokencxx . . $ _autostring#_autostring(f153f132b2bc41c2). (numberOfCharacters);
276 str32ncpycxx . . $ str32ncpy(a0fb29e7f5bbd1dc). (token.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), beginOfInk, numberOfCharacters);
277 result [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).++ itoken] =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). token.movecxx . . $ _autostring#move(bd43c759570f178f).();
278 }
279 return resultcxx . . $ _autostringautovector#_autostringautovector(f820baba8b4ac957).;
280 }
281
282 autoSTRVECcxx . . $ autoSTRVEC# splitBy_STRVECcxx . . $ splitBy_STRVEC(7166cd7864177c8). (conststring32cxx . . $ conststring32# string, conststring32cxx . . $ conststring32# separator) {
283 if (! string)
284 return autoSTRVECcxx . . $ _autostringautovector#_autostringautovector(49f6e7a06ebc5aa8).(); // accept null pointer input
285 const integercxx . . $ integer# separatorLength = Melder_lengthcxx . . $ Melder_length(3fae411214b133ad). (separator);
286 const char32cxx . . $ char32# *p = & string [0];
287 const char32cxx . . $ char32# *locationOfSeparator = str32strcxx . . $ str32str(2de17cd8f7c584a6). (p, separator);
288 if (! locationOfSeparator) {
289 if (string [0] == U'\0') {
290 /*
291 This is ambiguous: either a single empty string, or no strings at all.
292 We decide that, as with space separation, empty strings are somewhat hard to find.
293 So we decide that this is a single empty string.
294 */
295 return autoSTRVECcxx . . $ _autostringautovector#_autostringautovector(49f6e7a06ebc5aa8).();
296 }
297 return autoSTRVECcxx . . $ autoSTRVEC# ({ string });
298 }
299 integercxx . . $ integer# n = 1;
300 do {
301 n += 1;
302 p = locationOfSeparator + separatorLength;
303 locationOfSeparator = str32strcxx . . $ str32str(2de17cd8f7c584a6). (p, separator);
304 } while (locationOfSeparator);
305 autoSTRVECcxx . . $ autoSTRVEC# resultcxx . . $ _autostringautovector#_autostringautovector(1624ee8275b10bda). (n);
306 integercxx . . $ integer# itoken = 0;
307 p = & string [0];
308 do {
309 locationOfSeparator = str32strcxx . . $ str32str(2de17cd8f7c584a6). (p, separator);
310 if (locationOfSeparator) {
311 const integercxx . . $ integer# numberOfCharacters = locationOfSeparator - p;
312 autostring32cxx . . $ autostring32# tokencxx . . $ _autostring#_autostring(f153f132b2bc41c2). (numberOfCharacters);
313 str32ncpycxx . . $ str32ncpy(a0fb29e7f5bbd1dc). (token.getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), p, numberOfCharacters);
314 result [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).++ itoken] =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). token.movecxx . . $ _autostring#move(bd43c759570f178f).();
315 p = locationOfSeparator + separatorLength;
316 } else {
317 result [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).++ itoken] =cxx . . $ _autostring#`operator=`(f562943cbc4bcf37). Melder_dupcxx . . $ Melder_dup(87bf156b1f548402). (p);
318 }
319 } while (locationOfSeparator);
320 return resultcxx . . $ _autostringautovector#_autostringautovector(f820baba8b4ac957).;
321 }
322
323 /* End of file STRVEC.cpp */