| 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" |
| 30 |
|
| 31 |
static autoSTRVEC fileOrFolderNames_STRVEC (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_SEPARATOR || path [1] == U'\0') ) |
| 37 |
) { |
| 38 |
char32 absolutePath [kMelder_MAXPATH+1]; |
| 39 |
Melder_sprint (absolutePath,kMelder_MAXPATH+1, Melder_peek8to32_u (getenv ("HOME")), & path [1]); |
| 40 |
return fileOrFolderNames_STRVEC (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 |
autoMelderString searchDirectory, left, middle, right, filePath; |
| 143 |
MelderString_copy (& searchDirectory, path); |
| 144 |
char32 *const asterisk1 = str32chr (searchDirectory.string, U'*'); |
| 145 |
char32 *const asterisk2 = str32rchr (searchDirectory.string, U'*'); |
| 146 |
if (asterisk1) { |
| 147 |
/* |
| 148 |
The path is a wildcarded path. |
| 149 |
*/ |
| 150 |
*asterisk1 = U'\0'; |
| 151 |
*asterisk2 = U'\0'; |
| 152 |
searchDirectory. length = asterisk1 - searchDirectory.string; // probably superfluous, but correct |
| 153 |
char32 *const lastSlash = str32rchr (searchDirectory.string, Melder_DIRECTORY_SEPARATOR); |
| 154 |
if (lastSlash) { |
| 155 |
*lastSlash = U'\0'; // this fixes searchDirectory |
| 156 |
searchDirectory. length = lastSlash - searchDirectory.string; // probably superfluous, but correct |
| 157 |
MelderString_copy (& left, lastSlash + 1); |
| 158 |
} else { |
| 159 |
MelderString_copy (& left, searchDirectory.string); // quickly save... |
| 160 |
MelderString_empty (& searchDirectory); // ...before destruction |
| 161 |
} |
| 162 |
if (asterisk1 != asterisk2) { |
| 163 |
MelderString_copy (& middle, asterisk1 + 1); |
| 164 |
} |
| 165 |
MelderString_copy (& 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_MAXPATH+1]; |
| 172 |
Melder_32to8_fileSystem_inplace (searchDirectory.string, buffer8); |
| 173 |
d = opendir (buffer8 [0] ? buffer8 : "."); |
| 174 |
if (! d) |
| 175 |
Melder_throw (U"Cannot open folder ", searchDirectory.string, U"."); |
| 176 |
//Melder_casual (U"opened"); |
| 177 |
autoSTRVEC strings; |
| 178 |
struct dirent *entry; |
| 179 |
while (!! (entry = readdir (d))) { |
| 180 |
MelderString_copy (& filePath, searchDirectory.string [0] ? searchDirectory.string : U"."); |
| 181 |
MelderString_appendCharacter (& filePath, Melder_DIRECTORY_SEPARATOR); |
| 182 |
char32 buffer32 [kMelder_MAXPATH+1]; |
| 183 |
Melder_8bitFileRepresentationToStr32_inplace (entry -> d_name, buffer32); |
| 184 |
MelderString_append (& filePath, buffer32); |
| 185 |
//Melder_casual (U"read ", filePath.string); |
| 186 |
Melder_32to8_fileSystem_inplace (filePath.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_ISREG (stats. st_mode)) || (wantDirectories && S_ISDIR (stats. st_mode))) { |
| 195 |
Melder_8bitFileRepresentationToStr32_inplace (entry -> d_name, buffer32); |
| 196 |
const int64 length = Melder_length (buffer32); |
| 197 |
integer numberOfMatchedCharacters = 0; |
| 198 |
bool doesTheLeftMatch = true; |
| 199 |
if (left. length != 0) { |
| 200 |
doesTheLeftMatch = str32nequ_optionallyCaseSensitive (buffer32, left.string, left. length, caseSensitive); |
| 201 |
if (doesTheLeftMatch) |
| 202 |
numberOfMatchedCharacters = left.length; |
| 203 |
} |
| 204 |
bool doesTheMiddleMatch = true; |
| 205 |
if (middle. length != 0) { |
| 206 |
const char32 *const position = str32str_optionallyCaseSensitive (buffer32 + numberOfMatchedCharacters, middle.string, caseSensitive); |
| 207 |
doesTheMiddleMatch = !! position; |
| 208 |
if (doesTheMiddleMatch) |
| 209 |
numberOfMatchedCharacters = position - buffer32 + middle.length; |
| 210 |
} |
| 211 |
bool doesTheRightMatch = true; |
| 212 |
if (right. length != 0) { |
| 213 |
const int64 startOfRight = length - right. length; |
| 214 |
doesTheRightMatch = startOfRight >= numberOfMatchedCharacters && |
| 215 |
str32equ_optionallyCaseSensitive (buffer32 + startOfRight, right.string, caseSensitive); |
| 216 |
} |
| 217 |
if (buffer32 [0] != U'.' && doesTheLeftMatch && doesTheMiddleMatch && doesTheRightMatch) |
| 218 |
strings. append (buffer32); |
| 219 |
} |
| 220 |
} |
| 221 |
closedir (d); |
| 222 |
sort_STRVEC_inout (strings.get()); |
| 223 |
return strings; |
| 224 |
} catch (MelderError) { |
| 225 |
if (d) |
| 226 |
closedir (d); // "finally" |
| 227 |
throw; |
| 228 |
} |
| 229 |
#endif |
| 230 |
} |
| 231 |
autoSTRVEC fileNames_STRVEC (conststring32 path /* cattable */) { |
| 232 |
return fileOrFolderNames_STRVEC (path, false, true); |
| 233 |
} |
| 234 |
autoSTRVEC folderNames_STRVEC (conststring32 path /* cattable */) { |
| 235 |
return fileOrFolderNames_STRVEC (path, true, true); |
| 236 |
} |
| 237 |
autoSTRVEC fileNames_caseInsensitive_STRVEC (conststring32 path /* cattable */) { |
| 238 |
return fileOrFolderNames_STRVEC (path, false, false); |
| 239 |
} |
| 240 |
autoSTRVEC folderNames_caseInsensitive_STRVEC (conststring32 path /* cattable */) { |
| 241 |
return fileOrFolderNames_STRVEC (path, true, false); |
| 242 |
} |
| 243 |
|
| 244 |
autoSTRVEC readLinesFromFile_STRVEC (MelderFile file) { |
| 245 |
autoMelderReadText text = MelderReadText_createFromFile (file); |
| 246 |
int64 numberOfLines = MelderReadText_getNumberOfLines (text.get()); |
| 247 |
if (numberOfLines == 0) |
| 248 |
return autoSTRVEC (); |
| 249 |
autoSTRVEC result = autoSTRVEC (numberOfLines); |
| 250 |
for (integer iline = 1; iline <= numberOfLines; iline ++) { |
| 251 |
const mutablestring32 line = MelderReadText_readLine (text.get()); |
| 252 |
result [iline] = Melder_dup (line); |
| 253 |
} |
| 254 |
return result; |
| 255 |
} |
| 256 |
|
| 257 |
autoSTRVEC splitByWhitespace_STRVEC (conststring32 string) { |
| 258 |
if (! string) |
| 259 |
return autoSTRVEC(); // accept null pointer input |
| 260 |
integer n = NUMnumberOfTokens (string); |
| 261 |
if (n == 0) |
| 262 |
return autoSTRVEC(); |
| 263 |
autoSTRVEC result (n); |
| 264 |
|
| 265 |
integer itoken = 0; |
| 266 |
const char32 *p = & string [0]; |
| 267 |
for (;;) { |
| 268 |
Melder_skipHorizontalOrVerticalSpace (& p); |
| 269 |
if (*p == U'\0') |
| 270 |
break; |
| 271 |
const char32 *beginOfInk = p; |
| 272 |
p ++; // step over first nonspace |
| 273 |
p = Melder_findEndOfInk (p); |
| 274 |
const integer numberOfCharacters = p - beginOfInk; |
| 275 |
autostring32 token (numberOfCharacters); |
| 276 |
str32ncpy (token.get(), beginOfInk, numberOfCharacters); |
| 277 |
result [++ itoken] = token.move(); |
| 278 |
} |
| 279 |
return result; |
| 280 |
} |
| 281 |
|
| 282 |
autoSTRVEC splitBy_STRVEC (conststring32 string, conststring32 separator) { |
| 283 |
if (! string) |
| 284 |
return autoSTRVEC(); // accept null pointer input |
| 285 |
const integer separatorLength = Melder_length (separator); |
| 286 |
const char32 *p = & string [0]; |
| 287 |
const char32 *locationOfSeparator = str32str (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 autoSTRVEC(); |
| 296 |
} |
| 297 |
return autoSTRVEC ({ string }); |
| 298 |
} |
| 299 |
integer n = 1; |
| 300 |
do { |
| 301 |
n += 1; |
| 302 |
p = locationOfSeparator + separatorLength; |
| 303 |
locationOfSeparator = str32str (p, separator); |
| 304 |
} while (locationOfSeparator); |
| 305 |
autoSTRVEC result (n); |
| 306 |
integer itoken = 0; |
| 307 |
p = & string [0]; |
| 308 |
do { |
| 309 |
locationOfSeparator = str32str (p, separator); |
| 310 |
if (locationOfSeparator) { |
| 311 |
const integer numberOfCharacters = locationOfSeparator - p; |
| 312 |
autostring32 token (numberOfCharacters); |
| 313 |
str32ncpy (token.get(), p, numberOfCharacters); |
| 314 |
result [++ itoken] = token.move(); |
| 315 |
p = locationOfSeparator + separatorLength; |
| 316 |
} else { |
| 317 |
result [++ itoken] = Melder_dup (p); |
| 318 |
} |
| 319 |
} while (locationOfSeparator); |
| 320 |
return result; |
| 321 |
} |
| 322 |
|
| 323 |
/* End of file STRVEC.cpp */ |