mirror of
https://github.com/juce-framework/JUCE.git
synced 2026-01-14 00:14:18 +00:00
Refactored some directory iteration code and other minor clean-ups.
This commit is contained in:
parent
5bbe222266
commit
a7e824032f
37 changed files with 1692 additions and 2016 deletions
|
|
@ -64,24 +64,6 @@ bool juce_setFileTimes (const String& fileName,
|
|||
return utime (fileName.toUTF8(), ×) == 0;
|
||||
}
|
||||
|
||||
bool juce_setFileReadOnly (const String& fileName, bool isReadOnly)
|
||||
{
|
||||
struct stat info;
|
||||
const int res = stat (fileName.toUTF8(), &info);
|
||||
if (res != 0)
|
||||
return false;
|
||||
|
||||
info.st_mode &= 0777; // Just permissions
|
||||
|
||||
if( isReadOnly )
|
||||
info.st_mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
|
||||
else
|
||||
// Give everybody write permission?
|
||||
info.st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
|
||||
|
||||
return chmod (fileName.toUTF8(), info.st_mode) == 0;
|
||||
}
|
||||
|
||||
bool juce_copyFile (const String& s, const String& d)
|
||||
{
|
||||
const File source (s), dest (d);
|
||||
|
|
@ -113,11 +95,9 @@ bool juce_copyFile (const String& s, const String& d)
|
|||
return ok;
|
||||
}
|
||||
|
||||
const StringArray juce_getFileSystemRoots()
|
||||
void File::findFileSystemRoots (Array<File>& destArray)
|
||||
{
|
||||
StringArray s;
|
||||
s.add ("/");
|
||||
return s;
|
||||
destArray.add (File ("/"));
|
||||
}
|
||||
|
||||
//==============================================================================
|
||||
|
|
@ -275,42 +255,54 @@ bool File::moveToTrash() const
|
|||
}
|
||||
|
||||
//==============================================================================
|
||||
struct FindFileStruct
|
||||
class DirectoryIterator::NativeIterator::Pimpl
|
||||
{
|
||||
String parentDir, wildCard;
|
||||
DIR* dir;
|
||||
|
||||
bool getNextMatch (String& result, bool* const isDir, bool* const isHidden, int64* const fileSize,
|
||||
Time* const modTime, Time* const creationTime, bool* const isReadOnly)
|
||||
public:
|
||||
Pimpl (const File& directory, const String& wildCard_)
|
||||
: parentDir (File::addTrailingSeparator (directory.getFullPathName())),
|
||||
wildCard (wildCard_),
|
||||
dir (opendir (directory.getFullPathName().toUTF8()))
|
||||
{
|
||||
const char* const wildcardUTF8 = wildCard.toUTF8();
|
||||
if (wildCard == "*.*")
|
||||
wildCard = "*";
|
||||
|
||||
wildcardUTF8 = wildCard.toUTF8();
|
||||
}
|
||||
|
||||
~Pimpl()
|
||||
{
|
||||
if (dir != 0)
|
||||
closedir (dir);
|
||||
}
|
||||
|
||||
bool next (String& filenameFound,
|
||||
bool* const isDir, bool* const isHidden, int64* const fileSize,
|
||||
Time* const modTime, Time* const creationTime, bool* const isReadOnly)
|
||||
{
|
||||
if (dir == 0)
|
||||
return false;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
struct dirent* const de = readdir (dir);
|
||||
|
||||
if (de == 0)
|
||||
break;
|
||||
return false;
|
||||
|
||||
if (fnmatch (wildcardUTF8, de->d_name, FNM_CASEFOLD) == 0)
|
||||
{
|
||||
result = String::fromUTF8 (de->d_name);
|
||||
filenameFound = String::fromUTF8 (de->d_name);
|
||||
|
||||
const String path (parentDir + result);
|
||||
const String path (parentDir + filenameFound);
|
||||
|
||||
if (isDir != 0 || fileSize != 0)
|
||||
{
|
||||
struct stat info;
|
||||
const bool statOk = (stat (path.toUTF8(), &info) == 0);
|
||||
|
||||
if (isDir != 0)
|
||||
*isDir = path.isEmpty() || (statOk && ((info.st_mode & S_IFDIR) != 0));
|
||||
|
||||
if (isHidden != 0)
|
||||
*isHidden = (de->d_name[0] == '.');
|
||||
|
||||
if (fileSize != 0)
|
||||
*fileSize = statOk ? info.st_size : 0;
|
||||
if (isDir != 0) *isDir = path.isEmpty() || (statOk && ((info.st_mode & S_IFDIR) != 0));
|
||||
if (isHidden != 0) *isHidden = (de->d_name[0] == '.');
|
||||
if (fileSize != 0) *fileSize = statOk ? info.st_size : 0;
|
||||
}
|
||||
|
||||
if (modTime != 0 || creationTime != 0)
|
||||
|
|
@ -318,11 +310,8 @@ struct FindFileStruct
|
|||
int64 m, a, c;
|
||||
juce_getFileTimes (path, m, a, c);
|
||||
|
||||
if (modTime != 0)
|
||||
*modTime = m;
|
||||
|
||||
if (creationTime != 0)
|
||||
*creationTime = c;
|
||||
if (modTime != 0) *modTime = m;
|
||||
if (creationTime != 0) *creationTime = c;
|
||||
}
|
||||
|
||||
if (isReadOnly != 0)
|
||||
|
|
@ -331,71 +320,35 @@ struct FindFileStruct
|
|||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
String parentDir, wildCard;
|
||||
const char* wildcardUTF8;
|
||||
DIR* dir;
|
||||
|
||||
Pimpl (const Pimpl&);
|
||||
Pimpl& operator= (const Pimpl&);
|
||||
};
|
||||
|
||||
// returns 0 on failure
|
||||
void* juce_findFileStart (const String& directory, const String& wildCard, String& firstResultFile,
|
||||
bool* isDir, bool* isHidden, int64* fileSize, Time* modTime,
|
||||
Time* creationTime, bool* isReadOnly)
|
||||
DirectoryIterator::NativeIterator::NativeIterator (const File& directory, const String& wildCard)
|
||||
: pimpl (new DirectoryIterator::NativeIterator::Pimpl (directory, wildCard))
|
||||
{
|
||||
DIR* d = opendir (directory.toUTF8());
|
||||
|
||||
if (d != 0)
|
||||
{
|
||||
FindFileStruct* ff = new FindFileStruct();
|
||||
ff->parentDir = directory;
|
||||
|
||||
if (!ff->parentDir.endsWithChar (File::separator))
|
||||
ff->parentDir += File::separator;
|
||||
|
||||
ff->wildCard = wildCard;
|
||||
if (wildCard == "*.*")
|
||||
ff->wildCard = "*";
|
||||
|
||||
ff->dir = d;
|
||||
|
||||
if (ff->getNextMatch (firstResultFile, isDir, isHidden, fileSize, modTime, creationTime, isReadOnly))
|
||||
{
|
||||
return ff;
|
||||
}
|
||||
else
|
||||
{
|
||||
firstResultFile = String::empty;
|
||||
isDir = false;
|
||||
isHidden = false;
|
||||
closedir (d);
|
||||
delete ff;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool juce_findFileNext (void* handle, String& resultFile,
|
||||
bool* isDir, bool* isHidden, int64* fileSize, Time* modTime, Time* creationTime, bool* isReadOnly)
|
||||
DirectoryIterator::NativeIterator::~NativeIterator()
|
||||
{
|
||||
FindFileStruct* const ff = (FindFileStruct*) handle;
|
||||
|
||||
if (ff != 0)
|
||||
return ff->getNextMatch (resultFile, isDir, isHidden, fileSize, modTime, creationTime, isReadOnly);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void juce_findFileClose (void* handle)
|
||||
bool DirectoryIterator::NativeIterator::next (String& filenameFound,
|
||||
bool* const isDir, bool* const isHidden, int64* const fileSize,
|
||||
Time* const modTime, Time* const creationTime, bool* const isReadOnly)
|
||||
{
|
||||
FindFileStruct* const ff = (FindFileStruct*) handle;
|
||||
|
||||
if (ff != 0)
|
||||
{
|
||||
closedir (ff->dir);
|
||||
delete ff;
|
||||
}
|
||||
return pimpl->next (filenameFound, isDir, isHidden, fileSize, modTime, creationTime, isReadOnly);
|
||||
}
|
||||
|
||||
|
||||
//==============================================================================
|
||||
bool juce_launchFile (const String& fileName,
|
||||
const String& parameters)
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue