From 2ee168ad4642d23fc2940a4b877acdc52e1428e7 Mon Sep 17 00:00:00 2001 From: jules Date: Tue, 10 Oct 2017 11:33:55 +0100 Subject: [PATCH] Deprecated File::separator and File::separatorString, replacing them by static getter functions, so that File methods can be safely used in static constructors without order-of-initialisation problems --- .../Projucer/Source/Project/jucer_Module.cpp | 8 +-- .../Projucer/Source/Project/jucer_Project.cpp | 8 +-- .../jucer_ProjectExport_Android.h | 4 +- modules/juce_core/files/juce_File.cpp | 53 ++++++++++--------- modules/juce_core/files/juce_File.h | 10 +++- .../juce_core/native/juce_posix_SharedCode.h | 11 ++-- modules/juce_core/native/juce_win32_Files.cpp | 5 +- .../filebrowser/juce_FileBrowserComponent.cpp | 13 ++--- 8 files changed, 64 insertions(+), 48 deletions(-) diff --git a/extras/Projucer/Source/Project/jucer_Module.cpp b/extras/Projucer/Source/Project/jucer_Module.cpp index 04e3780fe4..9bd81eaed8 100644 --- a/extras/Projucer/Source/Project/jucer_Module.cpp +++ b/extras/Projucer/Source/Project/jucer_Module.cpp @@ -565,14 +565,14 @@ void LibraryModule::findAndAddCompiledUnits (ProjectExporter& exporter, static void addFileWithGroups (Project::Item& group, const RelativePath& file, const String& path) { - const int slash = path.indexOfChar (File::separator); + auto slash = path.indexOfChar (File::getSeparatorChar()); if (slash >= 0) { - const String topLevelGroup (path.substring (0, slash)); - const String remainingPath (path.substring (slash + 1)); + auto topLevelGroup = path.substring (0, slash); + auto remainingPath = path.substring (slash + 1); - Project::Item newGroup (group.getOrCreateSubGroup (topLevelGroup)); + auto newGroup = group.getOrCreateSubGroup (topLevelGroup); addFileWithGroups (newGroup, file, remainingPath); } else diff --git a/extras/Projucer/Source/Project/jucer_Project.cpp b/extras/Projucer/Source/Project/jucer_Project.cpp index b72d319b93..4f07718ed0 100644 --- a/extras/Projucer/Source/Project/jucer_Project.cpp +++ b/extras/Projucer/Source/Project/jucer_Project.cpp @@ -502,14 +502,14 @@ String Project::getRelativePathForFile (const File& file) const String p1 (relativePathBase.getFullPathName()); String p2 (file.getFullPathName()); - while (p1.startsWithChar (File::separator)) + while (p1.startsWithChar (File::getSeparatorChar())) p1 = p1.substring (1); - while (p2.startsWithChar (File::separator)) + while (p2.startsWithChar (File::getSeparatorChar())) p2 = p2.substring (1); - if (p1.upToFirstOccurrenceOf (File::separatorString, true, false) - .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::separatorString, true, false))) + if (p1.upToFirstOccurrenceOf (File::getSeparatorString(), true, false) + .equalsIgnoreCase (p2.upToFirstOccurrenceOf (File::getSeparatorString(), true, false))) { filename = FileHelpers::getRelativePathFrom (file, relativePathBase); } diff --git a/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h b/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h index 3c24c10e65..613a647021 100644 --- a/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h +++ b/extras/Projucer/Source/ProjectSaving/jucer_ProjectExport_Android.h @@ -887,11 +887,11 @@ private: auto package = getActivityClassPackage(); auto targetFolder = getTargetFolder(); - auto inAppBillingPath = String ("com.android.vending.billing").replaceCharacter ('.', File::separator); + auto inAppBillingPath = String ("com.android.vending.billing").replaceCharacter ('.', File::getSeparatorChar()); auto javaSourceFolder = coreModule->getFolder().getChildFile ("native").getChildFile ("java"); auto javaInAppBillingTarget = targetFolder.getChildFile ("app/src/main/java").getChildFile (inAppBillingPath); auto javaActivityTarget = targetFolder.getChildFile ("app/src/main/java") - .getChildFile (package.replaceCharacter ('.', File::separator)); + .getChildFile (package.replaceCharacter ('.', File::getSeparatorChar())); copyActivityJavaFiles (javaSourceFolder, javaActivityTarget, package); copyAdditionalJavaFiles (javaSourceFolder, javaInAppBillingTarget); diff --git a/modules/juce_core/files/juce_File.cpp b/modules/juce_core/files/juce_File.cpp index e35a8883b2..cde6e6ba5f 100644 --- a/modules/juce_core/files/juce_File.cpp +++ b/modules/juce_core/files/juce_File.cpp @@ -79,7 +79,7 @@ static String removeEllipsis (const String& path) #endif { StringArray toks; - toks.addTokens (path, File::separatorString, {}); + toks.addTokens (path, File::getSeparatorString(), {}); bool anythingChanged = false; for (int i = 1; i < toks.size(); ++i) @@ -100,7 +100,7 @@ static String removeEllipsis (const String& path) } if (anythingChanged) - return toks.joinIntoString (File::separatorString); + return toks.joinIntoString (File::getSeparatorString()); } return path; @@ -161,7 +161,7 @@ String File::parseAbsolutePath (const String& p) if (path.startsWithChar ('~')) { - if (path[1] == separator || path[1] == 0) + if (path[1] == getSeparatorChar() || path[1] == 0) { // expand a name of the form "~/abc" path = File::getSpecialLocation (File::userHomeDirectory).getFullPathName() @@ -176,7 +176,7 @@ String File::parseAbsolutePath (const String& p) path = addTrailingSeparator (pw->pw_dir) + path.fromFirstOccurrenceOf ("/", false, false); } } - else if (! path.startsWithChar (separator)) + else if (! path.startsWithChar (getSeparatorChar())) { #if JUCE_DEBUG || JUCE_LOG_ASSERTIONS if (! (path.startsWith ("./") || path.startsWith ("../"))) @@ -200,7 +200,7 @@ String File::parseAbsolutePath (const String& p) } #endif - while (path.endsWithChar (separator) && path != separatorString) // careful not to turn a single "/" into an empty string. + while (path.endsWithChar (getSeparatorChar()) && path != getSeparatorString()) // careful not to turn a single "/" into an empty string. path = path.dropLastCharacters (1); return path; @@ -208,8 +208,8 @@ String File::parseAbsolutePath (const String& p) String File::addTrailingSeparator (const String& path) { - return path.endsWithChar (separator) ? path - : path + separator; + return path.endsWithChar (getSeparatorChar()) ? path + : path + getSeparatorChar(); } //============================================================================== @@ -344,13 +344,13 @@ bool File::copyDirectoryTo (const File& newDirectory) const //============================================================================== String File::getPathUpToLastSlash() const { - auto lastSlash = fullPath.lastIndexOfChar (separator); + auto lastSlash = fullPath.lastIndexOfChar (getSeparatorChar()); if (lastSlash > 0) return fullPath.substring (0, lastSlash); if (lastSlash == 0) - return separatorString; + return getSeparatorString(); return fullPath; } @@ -363,12 +363,12 @@ File File::getParentDirectory() const //============================================================================== String File::getFileName() const { - return fullPath.substring (fullPath.lastIndexOfChar (separator) + 1); + return fullPath.substring (fullPath.lastIndexOfChar (getSeparatorChar()) + 1); } String File::getFileNameWithoutExtension() const { - auto lastSlash = fullPath.lastIndexOfChar (separator) + 1; + auto lastSlash = fullPath.lastIndexOfChar (getSeparatorChar()) + 1; auto lastDot = fullPath.lastIndexOfChar ('.'); if (lastDot > lastSlash) @@ -401,7 +401,7 @@ bool File::isAbsolutePath (StringRef path) { auto firstChar = *(path.text); - return firstChar == separator + return firstChar == getSeparatorChar() #if JUCE_WINDOWS || (firstChar != 0 && path.text[1] == ':'); #else @@ -422,6 +422,7 @@ File File::getChildFile (StringRef relativePath) const #endif auto path = fullPath; + auto separatorChar = getSeparatorChar(); while (*r == '.') { @@ -432,14 +433,14 @@ File File::getChildFile (StringRef relativePath) const { auto thirdChar = *++r; - if (thirdChar == separator || thirdChar == 0) + if (thirdChar == separatorChar || thirdChar == 0) { - auto lastSlash = path.lastIndexOfChar (separator); + auto lastSlash = path.lastIndexOfChar (separatorChar); if (lastSlash >= 0) path = path.substring (0, lastSlash); - while (*r == separator) // ignore duplicate slashes + while (*r == separatorChar) // ignore duplicate slashes ++r; } else @@ -448,9 +449,9 @@ File File::getChildFile (StringRef relativePath) const break; } } - else if (secondChar == separator || secondChar == 0) // remove "./" + else if (secondChar == separatorChar || secondChar == 0) // remove "./" { - while (*r == separator) // ignore duplicate slashes + while (*r == separatorChar) // ignore duplicate slashes ++r; } else @@ -520,7 +521,7 @@ Result File::createDirectory() const auto r = parentDir.createDirectory(); if (r.wasOk()) - r = createDirectoryInternal (fullPath.trimCharactersAtEnd (separatorString)); + r = createDirectoryInternal (fullPath.trimCharactersAtEnd (getSeparatorString())); return r; } @@ -663,7 +664,7 @@ String File::getFileExtension() const { auto indexOfDot = fullPath.lastIndexOfChar ('.'); - if (indexOfDot > fullPath.lastIndexOfChar (separator)) + if (indexOfDot > fullPath.lastIndexOfChar (getSeparatorChar())) return fullPath.substring (indexOfDot); return {}; @@ -672,7 +673,7 @@ String File::getFileExtension() const bool File::hasFileExtension (StringRef possibleSuffix) const { if (possibleSuffix.isEmpty()) - return fullPath.lastIndexOfChar ('.') <= fullPath.lastIndexOfChar (separator); + return fullPath.lastIndexOfChar ('.') <= fullPath.lastIndexOfChar (getSeparatorChar()); auto semicolon = possibleSuffix.text.indexOf ((juce_wchar) ';'); @@ -869,7 +870,7 @@ static int countNumberOfSeparators (String::CharPointerType s) if (c == 0) break; - if (c == File::separator) + if (c == File::getSeparatorChar()) ++num; } @@ -883,7 +884,7 @@ String File::getRelativePathFrom (const File& dir) const auto thisPath = fullPath; - while (thisPath.endsWithChar (separator)) + while (thisPath.endsWithChar (getSeparatorChar())) thisPath = thisPath.dropLastCharacters (1); auto dirPath = addTrailingSeparator (dir.existsAsFile() ? dir.getParentDirectory().getFullPathName() @@ -912,7 +913,7 @@ String File::getRelativePathFrom (const File& dir) const ++i; - if (c1 == separator) + if (c1 == getSeparatorChar()) { thisPathAfterCommon = thisPathIter; dirPathAfterCommon = dirPathIter; @@ -922,7 +923,7 @@ String File::getRelativePathFrom (const File& dir) const } // if the only common bit is the root, then just return the full path.. - if (commonBitLength == 0 || (commonBitLength == 1 && thisPath[1] == separator)) + if (commonBitLength == 0 || (commonBitLength == 1 && thisPath[1] == getSeparatorChar())) return fullPath; auto numUpDirectoriesNeeded = countNumberOfSeparators (dirPathAfterCommon); @@ -1110,8 +1111,8 @@ public: expectEquals (tempFile.loadFileAsString(), String ("0123456789")); expect (! demoFolder.containsSubDirectories()); - expectEquals (tempFile.getRelativePathFrom (demoFolder.getParentDirectory()), demoFolder.getFileName() + File::separatorString + tempFile.getFileName()); - expectEquals (demoFolder.getParentDirectory().getRelativePathFrom (tempFile), ".." + File::separatorString + ".." + File::separatorString + demoFolder.getParentDirectory().getFileName()); + expectEquals (tempFile.getRelativePathFrom (demoFolder.getParentDirectory()), demoFolder.getFileName() + File::getSeparatorString() + tempFile.getFileName()); + expectEquals (demoFolder.getParentDirectory().getRelativePathFrom (tempFile), ".." + File::getSeparatorString() + ".." + File::getSeparatorString() + demoFolder.getParentDirectory().getFileName()); expect (demoFolder.getNumberOfChildFiles (File::findFiles) == 1); expect (demoFolder.getNumberOfChildFiles (File::findFilesAndDirectories) == 1); diff --git a/modules/juce_core/files/juce_File.h b/modules/juce_core/files/juce_File.h index 5a194cd96b..4bbe8cab20 100644 --- a/modules/juce_core/files/juce_File.h +++ b/modules/juce_core/files/juce_File.h @@ -927,12 +927,12 @@ public: /** The system-specific file separator character. On Windows, this will be '\', on Mac/Linux, it'll be '/' */ - static const juce_wchar separator; + static juce_wchar getSeparatorChar(); /** The system-specific file separator character, as a string. On Windows, this will be '\', on Mac/Linux, it'll be '/' */ - static const String separatorString; + static StringRef getSeparatorString(); //============================================================================== /** Returns a version of a filename with any illegal characters removed. @@ -1027,6 +1027,12 @@ public: bool foldersFirst; }; + #ifndef DOXYGEN + // Deprecated: use File::getSeparatorChar() and File::getSeparatorString() instead! + JUCE_DEPRECATED (static const juce_wchar separator); + JUCE_DEPRECATED (static const StringRef separatorString); + #endif + private: //============================================================================== String fullPath; diff --git a/modules/juce_core/native/juce_posix_SharedCode.h b/modules/juce_core/native/juce_posix_SharedCode.h index dc81366ad9..b07279e16e 100644 --- a/modules/juce_core/native/juce_posix_SharedCode.h +++ b/modules/juce_core/native/juce_posix_SharedCode.h @@ -188,7 +188,11 @@ static MaxNumFileHandlesInitialiser maxNumFileHandlesInitialiser; //============================================================================== const juce_wchar File::separator = '/'; -const String File::separatorString ("/"); +const StringRef File::separatorString ("/"); + +juce_wchar File::getSeparatorChar() { return '/'; } +StringRef File::getSeparatorString() { return "/"; } + //============================================================================== File File::getCurrentWorkingDirectory() @@ -351,7 +355,7 @@ bool File::hasWriteAccess() const return (hasEffectiveRootFilePermissions() || access (fullPath.toUTF8(), W_OK) == 0); - if ((! isDirectory()) && fullPath.containsChar (separator)) + if ((! isDirectory()) && fullPath.containsChar (getSeparatorChar())) return getParentDirectory().hasWriteAccess(); return false; @@ -360,6 +364,7 @@ bool File::hasWriteAccess() const static bool setFileModeFlags (const String& fullPath, mode_t flags, bool shouldSet) noexcept { juce_statStruct info; + if (! juce_stat (fullPath, info)) return false; @@ -1124,7 +1129,7 @@ public: // Looks like you're trying to launch a non-existent exe or a folder (perhaps on OSX // you're trying to launch the .app folder rather than the actual binary inside it?) jassert (File::getCurrentWorkingDirectory().getChildFile (exe).existsAsFile() - || ! exe.containsChar (File::separator)); + || ! exe.containsChar (File::getSeparatorChar())); int pipeHandles[2] = { 0 }; diff --git a/modules/juce_core/native/juce_win32_Files.cpp b/modules/juce_core/native/juce_win32_Files.cpp index 6534ba6a6a..b93f74402d 100644 --- a/modules/juce_core/native/juce_win32_Files.cpp +++ b/modules/juce_core/native/juce_win32_Files.cpp @@ -129,7 +129,10 @@ namespace WindowsFileHelpers //============================================================================== const juce_wchar File::separator = '\\'; -const String File::separatorString ("\\"); +const StringRef File::separatorString ("\\"); + +juce_wchar File::getSeparatorChar() { return '\\'; } +StringRef File::getSeparatorString() { return "\\"; } void* getUser32Function (const char*); diff --git a/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.cpp b/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.cpp index eb367095ba..441606298c 100644 --- a/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.cpp +++ b/modules/juce_gui_basics/filebrowser/juce_FileBrowserComponent.cpp @@ -222,7 +222,7 @@ void FileBrowserComponent::setRoot (const File& newRootDirectory) String path (newRootDirectory.getFullPathName()); if (path.isEmpty()) - path = File::separatorString; + path = File::getSeparatorString(); StringArray rootNames, rootPaths; getRoots (rootNames, rootPaths); @@ -248,12 +248,13 @@ void FileBrowserComponent::setRoot (const File& newRootDirectory) currentRoot = newRootDirectory; fileList->setDirectory (currentRoot, true, true); - if (FileTreeComponent* tree = dynamic_cast (fileListComponent.get())) + if (auto* tree = dynamic_cast (fileListComponent.get())) tree->refresh(); - String currentRootName (currentRoot.getFullPathName()); + auto currentRootName = currentRoot.getFullPathName(); + if (currentRootName.isEmpty()) - currentRootName = File::separatorString; + currentRootName = File::getSeparatorString(); currentPathBox.setText (currentRootName, dontSendNotification); @@ -431,9 +432,9 @@ void FileBrowserComponent::textEditorTextChanged (TextEditor&) void FileBrowserComponent::textEditorReturnKeyPressed (TextEditor&) { - if (filenameBox.getText().containsChar (File::separator)) + if (filenameBox.getText().containsChar (File::getSeparatorChar())) { - const File f (currentRoot.getChildFile (filenameBox.getText())); + auto f = currentRoot.getChildFile (filenameBox.getText()); if (f.isDirectory()) {