refactored some pointers

This commit is contained in:
2025-11-02 19:57:21 +01:00
parent 83e51e87fe
commit 465e34e919
3 changed files with 79 additions and 100 deletions

View File

@@ -3,7 +3,7 @@
#include <algorithm>
DirectoryNode::DirectoryNode(const String &nodeName)
: name(nodeName), currentPlaying(nullptr)
: name(nodeName), currentPlaying("")
{
id = DirectoryNode::idCounter;
DirectoryNode::idCounter++;
@@ -37,19 +37,19 @@ const std::vector<String> &DirectoryNode::getMP3Files() const
return mp3Files;
}
void DirectoryNode::setCurrentPlaying(const String *mp3File)
void DirectoryNode::setCurrentPlaying(const String mp3File)
{
currentPlaying = mp3File;
for (int i = 0; i < mp3Files.size(); i++)
{
if (mp3Files[i] == *mp3File && ids.size() > i)
if (mp3Files[i] == mp3File && ids.size() > i)
{
currentPlayingId = ids[i];
}
}
}
const String *DirectoryNode::getCurrentPlaying() const
const String DirectoryNode::getCurrentPlaying() const
{
return currentPlaying;
}
@@ -219,7 +219,7 @@ void DirectoryNode::advanceToFirstMP3InThisNode()
{
if (mp3Files.size() > 0)
{
setCurrentPlaying(&mp3Files[0]);
setCurrentPlaying(mp3Files[0]);
}
}
@@ -231,7 +231,7 @@ DirectoryNode *DirectoryNode::advanceToMP3(const uint16_t id)
if (id == ids[i])
{
// Found the current MP3 file
currentPlaying = &mp3Files[i];
currentPlaying = mp3Files[i];
currentPlayingId = id;
return this;
}
@@ -249,7 +249,7 @@ DirectoryNode *DirectoryNode::advanceToMP3(const uint16_t id)
// Recursively search in subdirectory
DirectoryNode *result = subdir->advanceToMP3(id);
if (result != nullptr && result->getCurrentPlaying() != nullptr)
if (result != nullptr && !result->getCurrentPlaying().isEmpty())
{
return result;
}
@@ -260,24 +260,24 @@ DirectoryNode *DirectoryNode::advanceToMP3(const uint16_t id)
return nullptr;
}
DirectoryNode *DirectoryNode::advanceToMP3(const String *songName)
DirectoryNode *DirectoryNode::advanceToMP3(const String songName)
{
if (songName == nullptr)
if (songName.isEmpty())
{
Serial.println("advanceToMP3: songName is null");
Serial.println("advanceToMP3: songName is empty");
return nullptr;
}
// Check if the input is an absolute path (starts with '/') or just a filename
bool isAbsolutePath = songName->startsWith("/");
bool isAbsolutePath = songName.startsWith("/");
// Normalize trailing slash for absolute folder path targets
String normalizedPath = *songName;
String normalizedPath = songName;
if (isAbsolutePath && normalizedPath.length() > 1 && normalizedPath.endsWith("/"))
{
normalizedPath.remove(normalizedPath.length() - 1);
}
// Lowercased copies for case-insensitive comparisons (FAT can uppercase names)
String lowTarget = *songName;
String lowTarget = songName;
lowTarget.toLowerCase();
String lowNormPath = normalizedPath;
lowNormPath.toLowerCase();
@@ -287,9 +287,9 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String *songName)
{
if (isAbsolutePath)
{
if (mp3Files[i].equalsIgnoreCase(*songName))
if (mp3Files[i].equalsIgnoreCase(songName))
{
setCurrentPlaying(&mp3Files[i]);
setCurrentPlaying(mp3Files[i]);
return this;
}
}
@@ -299,7 +299,7 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String *songName)
f.toLowerCase();
if (f.endsWith(lowTarget))
{
setCurrentPlaying(&mp3Files[i]);
setCurrentPlaying(mp3Files[i]);
return this;
}
}
@@ -321,7 +321,7 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String *songName)
}
}
if (!isAbsolutePath && subdir->getName().equalsIgnoreCase(*songName))
if (!isAbsolutePath && subdir->getName().equalsIgnoreCase(songName))
{
subdir->advanceToFirstMP3InThisNode();
return subdir;
@@ -333,9 +333,9 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String *songName)
if (isAbsolutePath)
{
if (subdir->mp3Files[i].equalsIgnoreCase(*songName))
if (subdir->mp3Files[i].equalsIgnoreCase(songName))
{
subdir->setCurrentPlaying(&subdir->mp3Files[i]);
subdir->setCurrentPlaying(subdir->mp3Files[i]);
return subdir;
}
}
@@ -345,7 +345,7 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String *songName)
f.toLowerCase();
if (f.endsWith(lowTarget))
{
subdir->setCurrentPlaying(&subdir->mp3Files[i]);
subdir->setCurrentPlaying(subdir->mp3Files[i]);
return subdir;
}
}
@@ -359,7 +359,7 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String *songName)
}
// If we get here, no matching song was found
Serial.println("advanceToMP3: No song found for: " + *songName);
Serial.println("advanceToMP3: No song found for: " + songName);
return nullptr;
}
@@ -374,9 +374,9 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String *songName)
DirectoryNode *DirectoryNode::goToPreviousMP3(uint32_t thresholdSeconds)
{
// Safety check for null pointer
if (currentPlaying == nullptr)
if (currentPlaying.isEmpty())
{
Serial.println("goToPreviousMP3: currentPlaying is null");
Serial.println("goToPreviousMP3: currentPlaying is empty");
return nullptr;
}
@@ -391,7 +391,7 @@ DirectoryNode *DirectoryNode::goToPreviousMP3(uint32_t thresholdSeconds)
int currentIndex = -1;
for (size_t i = 0; i < mp3Files.size(); i++)
{
if (*currentPlaying == mp3Files[i])
if (currentPlaying == mp3Files[i])
{
currentIndex = i;
break;
@@ -403,7 +403,7 @@ DirectoryNode *DirectoryNode::goToPreviousMP3(uint32_t thresholdSeconds)
{
Serial.print("goToPreviousMP3: Moving to previous song in same directory: ");
Serial.println(mp3Files[currentIndex - 1]);
setCurrentPlaying(&mp3Files[currentIndex - 1]);
setCurrentPlaying(mp3Files[currentIndex - 1]);
return this;
}
@@ -413,9 +413,9 @@ DirectoryNode *DirectoryNode::goToPreviousMP3(uint32_t thresholdSeconds)
return nullptr; // Let the caller handle global previous logic
}
DirectoryNode *DirectoryNode::findPreviousMP3Globally(const String *currentGlobal)
DirectoryNode *DirectoryNode::findPreviousMP3Globally(const String currentGlobal)
{
if (currentGlobal == nullptr)
if (currentGlobal.isEmpty())
{
Serial.println("findPreviousMP3Globally: currentGlobal is null");
return nullptr;
@@ -431,7 +431,7 @@ DirectoryNode *DirectoryNode::findPreviousMP3Globally(const String *currentGloba
{
DirectoryNode *node = allMP3s[i].first;
int fileIndex = allMP3s[i].second;
if (node->mp3Files[fileIndex] == *currentGlobal)
if (node->mp3Files[fileIndex] == currentGlobal)
{
currentGlobalIndex = i;
break;
@@ -447,7 +447,7 @@ DirectoryNode *DirectoryNode::findPreviousMP3Globally(const String *currentGloba
Serial.print("findPreviousMP3Globally: Moving to previous song globally: ");
Serial.println(prevNode->mp3Files[prevFileIndex]);
prevNode->setCurrentPlaying(&prevNode->mp3Files[prevFileIndex]);
prevNode->setCurrentPlaying(prevNode->mp3Files[prevFileIndex]);
return prevNode;
}
@@ -475,21 +475,21 @@ const size_t DirectoryNode::getNumOfFiles()
return subdirectories.size();
}
DirectoryNode *DirectoryNode::advanceToNextMP3(const String *currentGlobal)
DirectoryNode *DirectoryNode::advanceToNextMP3(const String currentGlobal)
{
bool useFirst = false;
Serial.println(currentGlobal->c_str());
if (currentGlobal != nullptr)
Serial.println(currentGlobal.c_str());
if (!currentGlobal.isEmpty())
{
for (size_t i = 0; i < mp3Files.size(); i++)
{
if (*currentGlobal == mp3Files[i])
if (currentGlobal == mp3Files[i])
{
// Found the current playing MP3 file
if (i < mp3Files.size() - 1)
{
// Advance to the next MP3 file in the same directory
setCurrentPlaying(&mp3Files[i + 1]);
setCurrentPlaying(mp3Files[i + 1]);
return this;
}
useFirst = true;
@@ -506,20 +506,20 @@ DirectoryNode *DirectoryNode::advanceToNextMP3(const String *currentGlobal)
if (useFirst && subdir->mp3Files.size() > 0)
{
subdir->setCurrentPlaying(&subdir->mp3Files[0]);
subdir->setCurrentPlaying(subdir->mp3Files[0]);
return subdir;
}
// Have each subdirectory advance its song
for (size_t i = 0; i < subdir->mp3Files.size(); i++)
{
if (*currentGlobal == subdir->mp3Files[i])
if (currentGlobal == subdir->mp3Files[i])
{
// Found the current playing MP3 file
if (i < subdir->mp3Files.size() - 1)
{
// Advance to the next MP3 file in the same directory
subdir->setCurrentPlaying(&subdir->mp3Files[i + 1]);
subdir->setCurrentPlaying(subdir->mp3Files[i + 1]);
return subdir;
}
else
@@ -533,7 +533,7 @@ DirectoryNode *DirectoryNode::advanceToNextMP3(const String *currentGlobal)
}
// If we get here, there were no MP3 files or subdirectories left to check
currentPlaying = nullptr;
currentPlaying = "";
Serial.println("no more nodes found");
return this;
}
@@ -583,19 +583,4 @@ void DirectoryNode::streamDirectoryHTML(Print &out) const {
}
void DirectoryNode::appendIndentation(String &html, int level) const
{
for (int i = 0; i < level; i++)
{
html.concat(" ");
}
}
String DirectoryNode::getCurrentPlayingFilePath() const
{
if (currentPlaying != nullptr)
{
return *currentPlaying;
}
return "";
}