public bool MovePrev(AdvanceFlags flags) { // If before beginning or after end if (fCurrentDirectoryList == null) { if (fRootDirectories == null || fRootDirectories.Length == 0) { return(false); } // if before beginning, load up the first lists if (fCurrentFileIndex < 0) { return(false); } // Else, after end else { // Load up the stack fDirectoryStack.Clear(); fCurrentDirectoryList = new DirectoryList(fRootDirectories, fIncludeExtensions.Keys); for (;;) { DirectoryList directoryList = fCurrentDirectoryList.GetSubDirectoryList(); if (directoryList.Directories.Length == 0) { break; } fDirectoryStack.Push(fCurrentDirectoryList); fCurrentDirectoryList = directoryList; fCurrentDirectoryList.CurrentIndex = fCurrentDirectoryList.Directories.Length - 1; } fCurrentFiles = fCurrentDirectoryList.GetCurrentFiles(); fCurrentFileIndex = fCurrentFiles.Length - 1; } } // Else, if advance to previous folder else if ((flags & AdvanceFlags.NextFolder) != 0) { fCurrentFileIndex = -1; } // Else, advance to previous image else { --fCurrentFileIndex; } // Keep trying to load up an image until success or beginning is reached for (; ;) { // Get current file if available if (fCurrentFiles != null && fCurrentFiles.Length > 0 && fCurrentFileIndex >= 0) { fCurrent = fCurrentFiles[fCurrentFileIndex]; return(true); } // Move to previous folder else { fCurrentFiles = null; fCurrentFileIndex = 0; // Pop the stack if necessary if (fCurrentDirectoryList.CurrentIndex == 0) { // We're at the very beginning. Cannot go further. if (fDirectoryStack.Count <= 0) { fCurrentFileIndex = -1; fCurrentFiles = null; fCurrentDirectoryList = null; fCurrent = null; return(false); } // Pop one directory fCurrentDirectoryList = fDirectoryStack.Pop(); } else { // Move to the previous peer folder --fCurrentDirectoryList.CurrentIndex; // Load up all subfolders for (; ;) { DirectoryList directoryList = fCurrentDirectoryList.GetSubDirectoryList(); if (directoryList.Directories.Length == 0) { break; } fDirectoryStack.Push(fCurrentDirectoryList); fCurrentDirectoryList = directoryList; fCurrentDirectoryList.CurrentIndex = fCurrentDirectoryList.Directories.Length - 1; } } // Load up all files fCurrentFiles = fCurrentDirectoryList.GetCurrentFiles(); fCurrentFileIndex = fCurrentFiles.Length - 1; } } // End of retry loop }
public bool SetCurrent(string path) { // Local versions of tracking variables DirectoryList directoryList = new DirectoryList(fRootDirectories, fIncludeExtensions.Keys); Stack <DirectoryList> directoryStack = new Stack <DirectoryList>(); string[] currentFiles = null; int currentFileIndex = 0; if (path == null || path.Length == 0) { return(false); } int lastSlash = path.LastIndexOf('\\'); int matchLength = -1; for (; ;) { if (directoryList.Directories.Length == 0) { return(false); } while (directoryList.CurrentIndex < directoryList.Directories.Length) { string str = directoryList.Directories[directoryList.CurrentIndex]; if (path.Length > str.Length && path.StartsWith(str, StringComparison.OrdinalIgnoreCase) && path[str.Length] == '\\') { matchLength = str.Length; break; } ++directoryList.CurrentIndex; } if (directoryList.CurrentIndex >= directoryList.Directories.Length) { return(false); } if (matchLength >= lastSlash) { break; } directoryStack.Push(directoryList); directoryList = directoryList.GetSubDirectoryList(); } currentFiles = directoryList.GetCurrentFiles(); currentFileIndex = Array.BinarySearch(currentFiles, path, FileNameComparer.Value); if (currentFileIndex < 0) { return(false); } // Found, set the local variables fDirectoryStack = directoryStack; fCurrentDirectoryList = directoryList; fCurrentFiles = currentFiles; fCurrentFileIndex = currentFileIndex; fCurrent = fCurrentFiles[fCurrentFileIndex]; return(true); }
public bool MoveNext(AdvanceFlags flags) { // If before beginning or after end if (fCurrentDirectoryList == null) { if (fRootDirectories == null || fRootDirectories.Length == 0) { return(false); } if (fCurrentFileIndex >= 0 && 0 != (flags & AdvanceFlags.Wrap)) { Reset(); } // if before beginning, load up the first lists if (fCurrentFileIndex < 0) { fDirectoryStack.Clear(); fCurrentDirectoryList = new DirectoryList(fRootDirectories, fIncludeExtensions.Keys); fCurrentFiles = fCurrentDirectoryList.GetCurrentFiles(); fCurrentFileIndex = 0; } // Else, after end else { return(false); } } // Else, if Advance to next folder else if ((flags & AdvanceFlags.NextFolder) != 0) { fCurrentFileIndex = int.MaxValue; } // Else, advance image else { ++fCurrentFileIndex; } // Keep trying to load up an image until success or end is reached for (; ;) { // Get next file if available if (fCurrentFiles != null && fCurrentFileIndex < fCurrentFiles.Length) { fCurrent = fCurrentFiles[fCurrentFileIndex]; return(true); } // Advance folder else { fCurrentFiles = null; fCurrentFileIndex = 0; // Load up the subFolders DirectoryList directoryList = fCurrentDirectoryList.GetSubDirectoryList(); if (directoryList.Directories.Length != 0) { fDirectoryStack.Push(fCurrentDirectoryList); fCurrentDirectoryList = directoryList; } // Else, move to the next peer folder else { ++fCurrentDirectoryList.CurrentIndex; // If all folders are used up, pop levels off the stack if (fCurrentDirectoryList.CurrentIndex >= fCurrentDirectoryList.Directories.Length) { fCurrentDirectoryList = null; while (fDirectoryStack.Count > 0) { fCurrentDirectoryList = fDirectoryStack.Pop(); ++fCurrentDirectoryList.CurrentIndex; if (fCurrentDirectoryList.CurrentIndex < fCurrentDirectoryList.Directories.Length) { break; } fCurrentDirectoryList = null; } } } // We've popped the entire stack. At the end. if (fCurrentDirectoryList == null) { if (0 == (flags & AdvanceFlags.Wrap)) { fCurrentFileIndex = 0; fCurrentFiles = null; fCurrentDirectoryList = null; fDirectoryStack.Clear(); fCurrent = null; return(false); } else { // Wrap back to the beginning fCurrentDirectoryList = new DirectoryList(fRootDirectories, fIncludeExtensions.Keys); fCurrentFileIndex = 0; } } // Get the files from the current folder fCurrentFiles = fCurrentDirectoryList.GetCurrentFiles(); } } // End of retry loop }