static Strings Strings_createAsFileOrDirectoryList (const wchar_t *path, int type) { #if USE_STAT /* * Initialize. */ DIR *d = NULL; try { autoMelderString filePath, searchDirectory, left, right; /* * Parse the path. * Example: in /Users/paul/sounds/h*.wav", * the search directory is "/Users/paul/sounds", * the left environment is "h", and the right environment is ".wav". */ MelderString_copy (& searchDirectory, path); wchar_t *asterisk = wcsrchr (searchDirectory. string, '*'); if (asterisk != NULL) { *asterisk = '\0'; searchDirectory. length = asterisk - searchDirectory. string; // probably superfluous, but correct wchar_t *lastSlash = wcsrchr (searchDirectory. string, Melder_DIRECTORY_SEPARATOR); if (lastSlash != NULL) { *lastSlash = '\0'; // This fixes searchDirectory. searchDirectory. length = lastSlash - searchDirectory. string; // probably superfluous, but correct MelderString_copy (& left, lastSlash + 1); } else { MelderString_copy (& left, searchDirectory. string); // quickly save... MelderString_empty (& searchDirectory); // ...before destruction } MelderString_copy (& right, asterisk + 1); } char buffer8 [1+kMelder_MAXPATH]; Melder_wcsTo8bitFileRepresentation_inline (searchDirectory. string, buffer8); d = opendir (buffer8 [0] ? buffer8 : "."); if (d == NULL) Melder_throw ("Cannot open directory ", searchDirectory. string, "."); //Melder_casual ("opened"); autoStrings me = Thing_new (Strings); my strings = NUMvector <wchar_t *> (1, 1000000); struct dirent *entry; while ((entry = readdir (d)) != NULL) { MelderString_copy (& filePath, searchDirectory. string [0] ? searchDirectory. string : L"."); MelderString_appendCharacter (& filePath, Melder_DIRECTORY_SEPARATOR); wchar_t bufferW [1+kMelder_MAXPATH]; Melder_8bitFileRepresentationToWcs_inline (entry -> d_name, bufferW); MelderString_append (& filePath, bufferW); //Melder_casual ("read %s", filePath. string); Melder_wcsTo8bitFileRepresentation_inline (filePath. string, buffer8); struct stat stats; if (stat (buffer8, & stats) != 0) { Melder_throw ("Cannot look at file ", filePath. string, "."); //stats. st_mode = -1L; } //Melder_casual ("statted %s", filePath. string); //Melder_casual ("file %s mode %s", filePath. string, Melder_integer (stats. st_mode / 4096)); if ((type == Strings_createAsFileOrDirectoryList_TYPE_FILE && S_ISREG (stats. st_mode)) || (type == Strings_createAsFileOrDirectoryList_TYPE_DIRECTORY && S_ISDIR (stats. st_mode))) { Melder_8bitFileRepresentationToWcs_inline (entry -> d_name, bufferW); unsigned long length = wcslen (bufferW); if (bufferW [0] != '.' && (left. length == 0 || wcsnequ (bufferW, left. string, left. length)) && (right. length == 0 || (length >= right. length && wcsequ (bufferW + (length - right. length), right. string)))) { my strings [++ my numberOfStrings] = Melder_wcsdup (bufferW); } } } closedir (d); Strings_sort (me.peek()); return me.transfer(); } catch (MelderError) { if (d) closedir (d); // "finally" throw; } #elif defined (_WIN32) try { wchar_t searchPath [1+kMelder_MAXPATH]; int len = wcslen (path), hasAsterisk = wcschr (path, '*') != NULL, endsInSeparator = len != 0 && path [len - 1] == '\\'; autoStrings me = Thing_new (Strings); my strings = NUMvector <wchar_t *> (1, 1000000); swprintf (searchPath, 1+kMelder_MAXPATH, L"%ls%ls%ls", path, hasAsterisk || endsInSeparator ? L"" : L"\\", hasAsterisk ? L"" : L"*"); WIN32_FIND_DATAW findData; HANDLE searchHandle = FindFirstFileW (searchPath, & findData); if (searchHandle != INVALID_HANDLE_VALUE) { do { if ((type == Strings_createAsFileOrDirectoryList_TYPE_FILE && (findData. dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) || (type == Strings_createAsFileOrDirectoryList_TYPE_DIRECTORY && (findData. dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)) { if (findData. cFileName [0] != '.') { my strings [++ my numberOfStrings] = Melder_wcsdup (findData. cFileName); } } } while (FindNextFileW (searchHandle, & findData)); FindClose (searchHandle); } Strings_sort (me.peek()); return me.transfer(); } catch (MelderError) { throw; } #endif }
static autoStrings Strings_createAsFileOrDirectoryList (const char32 *path /* cattable */, int type) { #if USE_STAT /* * Initialize. */ DIR *d = nullptr; try { autoMelderString filePath, searchDirectory, left, right; /* * Parse the path. * Example: in /Users/paul/sounds/h*.wav", * the search directory is "/Users/paul/sounds", * the left environment is "h", and the right environment is ".wav". */ MelderString_copy (& searchDirectory, path); char32 *asterisk = str32rchr (searchDirectory. string, '*'); if (asterisk) { *asterisk = '\0'; searchDirectory. length = asterisk - searchDirectory. string; // probably superfluous, but correct char32 *lastSlash = str32rchr (searchDirectory. string, Melder_DIRECTORY_SEPARATOR); if (lastSlash) { *lastSlash = '\0'; // This fixes searchDirectory. searchDirectory. length = lastSlash - searchDirectory. string; // probably superfluous, but correct MelderString_copy (& left, lastSlash + 1); } else { MelderString_copy (& left, searchDirectory. string); // quickly save... MelderString_empty (& searchDirectory); // ...before destruction } MelderString_copy (& right, asterisk + 1); } char buffer8 [kMelder_MAXPATH+1]; Melder_str32To8bitFileRepresentation_inline (searchDirectory. string, buffer8); d = opendir (buffer8 [0] ? buffer8 : "."); if (! d) Melder_throw (U"Cannot open directory ", searchDirectory. string, U"."); //Melder_casual (U"opened"); autoStrings me = Thing_new (Strings); my strings = NUMvector <char32 *> (1, 1000000); struct dirent *entry; while (!! (entry = readdir (d))) { MelderString_copy (& filePath, searchDirectory. string [0] ? searchDirectory. string : U"."); MelderString_appendCharacter (& filePath, Melder_DIRECTORY_SEPARATOR); char32 buffer32 [kMelder_MAXPATH+1]; Melder_8bitFileRepresentationToStr32_inline (entry -> d_name, buffer32); MelderString_append (& filePath, buffer32); //Melder_casual (U"read ", filePath. string); Melder_str32To8bitFileRepresentation_inline (filePath. string, buffer8); struct stat stats; if (stat (buffer8, & stats) != 0) { //Melder_throw (U"Cannot look at file ", filePath. string, U"."); //stats. st_mode = -1L; } //Melder_casual (U"statted ", filePath. string); //Melder_casual (U"file ", filePath. string, U" mode ", stats. st_mode / 4096); if ((type == Strings_createAsFileOrDirectoryList_TYPE_FILE && S_ISREG (stats. st_mode)) || (type == Strings_createAsFileOrDirectoryList_TYPE_DIRECTORY && S_ISDIR (stats. st_mode))) { Melder_8bitFileRepresentationToStr32_inline (entry -> d_name, buffer32); int64 length = str32len (buffer32); if (buffer32 [0] != U'.' && (left. length == 0 || str32nequ (buffer32, left. string, left. length)) && (right. length == 0 || (length >= right. length && str32equ (buffer32 + (length - right. length), right. string)))) { my strings [++ my numberOfStrings] = Melder_dup (buffer32); } } } closedir (d); Strings_sort (me.get()); return me; } catch (MelderError) { if (d) closedir (d); // "finally" throw; } #elif defined (_WIN32) try { char32 searchPath [kMelder_MAXPATH+1]; int len = str32len (path); bool hasAsterisk = !! str32chr (path, U'*'); bool endsInSeparator = ( len != 0 && path [len - 1] == U'\\' ); autoStrings me = Thing_new (Strings); my strings = NUMvector <char32 *> (1, 1000000); Melder_sprint (searchPath, kMelder_MAXPATH+1, path, hasAsterisk || endsInSeparator ? U"" : U"\\", hasAsterisk ? U"" : U"*"); WIN32_FIND_DATAW findData; HANDLE searchHandle = FindFirstFileW (Melder_peek32toW (searchPath), & findData); if (searchHandle != INVALID_HANDLE_VALUE) { do { if ((type == Strings_createAsFileOrDirectoryList_TYPE_FILE && (findData. dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) || (type == Strings_createAsFileOrDirectoryList_TYPE_DIRECTORY && (findData. dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)) { if (findData. cFileName [0] != L'.') { my strings [++ my numberOfStrings] = Melder_dup (Melder_peekWto32 (findData. cFileName)); } } } while (FindNextFileW (searchHandle, & findData)); FindClose (searchHandle); } Strings_sort (me.get()); return me; } catch (MelderError) { throw; } #endif }