Skip to content

Commit d098fa8

Browse files
Fix a bug when files doubled when browsing a folder
1 parent efd55a1 commit d098fa8

File tree

3 files changed

+87
-75
lines changed

3 files changed

+87
-75
lines changed

src/JPEGView/FileList.cpp

Lines changed: 84 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Helpers.h"
55
#include "DirectoryWatcher.h"
66
#include "Shlwapi.h"
7+
#include <sstream>
78

89
///////////////////////////////////////////////////////////////////////////////////
910
// Helpers
@@ -60,7 +61,7 @@ static bool IsNoLogicalStrCmpSetInRegistryHive(HKEY hKeyRoot) {
6061
static bool UseLogicalStringCompare() {
6162
if (!s_bUseLogicalStringCompareValid) {
6263
s_bUseLogicalStringCompareValid = true;
63-
s_bUseLogicalStringCompare = !(IsNoLogicalStrCmpSetInRegistryHive(HKEY_LOCAL_MACHINE) ||
64+
s_bUseLogicalStringCompare = !(IsNoLogicalStrCmpSetInRegistryHive(HKEY_LOCAL_MACHINE) ||
6465
IsNoLogicalStrCmpSetInRegistryHive(HKEY_CURRENT_USER));
6566
}
6667
return s_bUseLogicalStringCompare;
@@ -123,19 +124,12 @@ void CFileDesc::SetModificationDate(const FILETIME& lastModDate) {
123124
// Public interface
124125
///////////////////////////////////////////////////////////////////////////////////
125126

126-
// image file types supported internally (there are additional endings for RAW and WIC - these come from INI file)
127-
// NOTE: when adding more supported filetypes, update installer to add another extension for "SupportedTypes"
128-
static const int cnNumEndingsInternal = 17;
129-
static const TCHAR* csFileEndingsInternal[cnNumEndingsInternal] = {_T("jpg"), _T("jpeg"), _T("jfif"), _T("bmp"), _T("png"),
130-
_T("tif"), _T("tiff"), _T("gif"), _T("webp"), _T("jxl"), _T("avif"), _T("heif"), _T("heic"), _T("tga"), _T("qoi"), _T("psd"), _T("psb") };
127+
// Image file types supported internally and come from INI file.
128+
typedef std::set<std::wstring> file_endings_type;
129+
131130
// supported camera RAW formats
132131
static const TCHAR* csFileEndingsRAW = _T("*.pef;*.dng;*.crw;*.nef;*.cr2;*.mrw;*.rw2;*.orf;*.x3f;*.arw;*.kdc;*.nrw;*.dcr;*.sr2;*.raf");
133132

134-
135-
static const int MAX_ENDINGS = 48;
136-
static int nNumEndings;
137-
static LPCTSTR* sFileEndings;
138-
139133
__declspec(dllimport) bool __stdcall WICPresent(void);
140134

141135
// Check if Windows Image Codecs library is present
@@ -148,46 +142,58 @@ static bool WICPresentGuarded(void) {
148142
}
149143

150144
// Parses a semicolon separated list of file endings of the form "*.nef;*.cr2;*.dng"
151-
static void ParseAndAddFileEndings(LPCTSTR sEndings) {
152-
if (_tcslen(sEndings) > 2) {
153-
LPTSTR buffer = new TCHAR[_tcslen(sEndings) + 1]; // this buffer will not be freed deliberately!
154-
_tcscpy(buffer, sEndings);
155-
LPTSTR sStart = buffer, sCurrent = buffer;
156-
while (*sCurrent != 0 && nNumEndings < MAX_ENDINGS) {
157-
while (*sCurrent != 0 && *sCurrent != _T(';')) {
158-
sCurrent++;
159-
}
160-
if (*sCurrent == _T(';')) {
161-
*sCurrent = 0;
162-
sCurrent++;
163-
}
164-
if (_tcslen(sStart) > 2) {
165-
sFileEndings[nNumEndings++] = sStart + 2;
166-
}
167-
sStart = sCurrent;
168-
}
145+
template<typename OUT_ITER>
146+
static void ParseAndAddFileEndings(const std::wstring& input, OUT_ITER output) {
147+
if (input.size() > 2)
148+
{
149+
std::wstringstream ss(input);
150+
std::wstring item;
151+
while (std::getline(ss, item, L';'))
152+
output = item.substr(2);
169153
}
170154
}
171155

172156
// Gets all supported file endings, including the ones from WIC.
173157
// The length of the returned list is nNumEndings
174-
static LPCTSTR* GetSupportedFileEndingList() {
175-
if (sFileEndings == NULL) {
176-
sFileEndings = new LPCTSTR[MAX_ENDINGS];
177-
for (nNumEndings = 0; nNumEndings < cnNumEndingsInternal; nNumEndings++) {
178-
sFileEndings[nNumEndings] = csFileEndingsInternal[nNumEndings];
179-
}
180-
181-
LPCTSTR sFileEndingsWIC = CSettingsProvider::This().FilesProcessedByWIC();
182-
if (_tcslen(sFileEndingsWIC) > 2 && WICPresentGuarded()) {
183-
ParseAndAddFileEndings(sFileEndingsWIC);
184-
}
185-
ParseAndAddFileEndings(CSettingsProvider::This().FileEndingsRAW());
186-
}
187-
return sFileEndings;
188-
}
189-
190-
CFileList::CFileList(const CString & sInitialFile, CDirectoryWatcher & directoryWatcher,
158+
static file_endings_type& GetSupportedFileEndingList()
159+
{
160+
static file_endings_type file_endings;
161+
if (!file_endings.empty())
162+
return file_endings;
163+
164+
// Gets all supported file endings, including the ones from WIC.
165+
// Please, keep it ordered when adding a new extension.
166+
file_endings =
167+
{
168+
_T("avif"),
169+
_T("bmp"),
170+
_T("gif"),
171+
_T("heic"),
172+
_T("heif"),
173+
_T("jfif"),
174+
_T("jpeg"),
175+
_T("jpg"),
176+
_T("jxl"),
177+
_T("png"),
178+
_T("psb"),
179+
_T("psd"),
180+
_T("qoi"),
181+
_T("tga"),
182+
_T("tif"),
183+
_T("tiff"),
184+
_T("webp")
185+
};
186+
187+
LPCTSTR sFileEndingsWIC = CSettingsProvider::This().FilesProcessedByWIC();
188+
if (_tcslen(sFileEndingsWIC) > 2 && WICPresentGuarded()) {
189+
ParseAndAddFileEndings(sFileEndingsWIC, std::inserter(file_endings, file_endings.end()));
190+
}
191+
ParseAndAddFileEndings(CSettingsProvider::This().FileEndingsRAW(), std::inserter(file_endings, file_endings.end()));
192+
193+
return file_endings;
194+
}
195+
196+
CFileList::CFileList(const CString & sInitialFile, CDirectoryWatcher & directoryWatcher,
191197
Helpers::ESorting eInitialSorting, bool isSortedAscending, bool bWrapAroundFolder, int nLevel, bool forceSorting)
192198
: m_directoryWatcher(directoryWatcher) {
193199

@@ -206,7 +212,7 @@ CFileList::CFileList(const CString & sInitialFile, CDirectoryWatcher & directory
206212
bool bImageFile = !bIsDirectory && IsImageFile(sExtensionInitialFile);
207213
if (!bIsDirectory && !bImageFile && !sExtensionInitialFile.IsEmpty() && _tcsstr(csFileEndingsRAW, _T("*.") + sExtensionInitialFile) != NULL) {
208214
// initial file is a supported camera raw file but was excluded in INI file - add temporarily to file ending list
209-
ParseAndAddFileEndings(_T("*.") + sExtensionInitialFile);
215+
ParseAndAddFileEndings((LPCWSTR)(_T("*.") + sExtensionInitialFile), std::inserter(GetSupportedFileEndingList(), GetSupportedFileEndingList().end()));
210216
CSettingsProvider::This().AddTemporaryRAWFileEnding(sExtensionInitialFile);
211217
bImageFile = true;
212218
}
@@ -242,15 +248,22 @@ CFileList::~CFileList() {
242248
m_fileList.clear();
243249
}
244250

245-
CString CFileList::GetSupportedFileEndings() {
246-
LPCTSTR* allFileEndings = GetSupportedFileEndingList();
247-
CString sList;
248-
for (int i = 0; i < nNumEndings; i++) {
249-
sList += _T("*.");
250-
sList += allFileEndings[i];
251-
if (i+1 < nNumEndings) sList += _T(";");
251+
CString CFileList::GetSupportedFileEndings()
252+
{
253+
const auto& extensions = GetSupportedFileEndingList();
254+
std::wostringstream str;
255+
bool first = true;
256+
for (const auto& extension : extensions)
257+
{
258+
if (!first)
259+
str << L";";
260+
else
261+
first = false;
262+
263+
str << L"*." << extension;
252264
}
253-
return sList;
265+
266+
return CString(str.str().c_str());
254267
}
255268

256269
void CFileList::Reload(LPCTSTR sFileName, bool clearForwardHistory) {
@@ -614,7 +627,7 @@ void CFileList::DeleteHistory(bool onlyForward) {
614627
m_prev = NULL;
615628
}
616629
m_next = NULL;
617-
630+
618631
}
619632

620633
///////////////////////////////////////////////////////////////////////////////////
@@ -815,12 +828,14 @@ void CFileList::FindFiles() {
815828
m_fileList.clear();
816829
if (!m_sDirectory.IsEmpty()) {
817830
CFindFile fileFind;
818-
LPCTSTR* allFileEndings = GetSupportedFileEndingList();
819-
for (int i = 0; i < nNumEndings; i++) {
820-
if (fileFind.FindFile(m_sDirectory + _T("\\*.") + allFileEndings[i])) {
821-
AddToFileList(m_fileList, fileFind, allFileEndings[i]);
831+
const auto& extensions = GetSupportedFileEndingList();
832+
for (const auto& extension : extensions)
833+
{
834+
const auto c_extension = extension.c_str();
835+
if (fileFind.FindFile(m_sDirectory + _T("\\*.") + c_extension)) {
836+
AddToFileList(m_fileList, fileFind, c_extension);
822837
while (fileFind.FindNextFile()) {
823-
AddToFileList(m_fileList, fileFind, allFileEndings[i]);
838+
AddToFileList(m_fileList, fileFind, c_extension);
824839
}
825840
}
826841
}
@@ -841,16 +856,11 @@ void CFileList::VerifyFiles() {
841856
}
842857
}
843858

844-
bool CFileList::IsImageFile(const CString & sEnding) {
859+
bool CFileList::IsImageFile(const CString & sEnding) const {
845860
CString sEndingLC = sEnding;
846861
sEndingLC.MakeLower();
847-
LPCTSTR* allFileEndings = GetSupportedFileEndingList();
848-
for (int i = 0; i < nNumEndings; i++) {
849-
if (allFileEndings[i] == sEndingLC) {
850-
return true;
851-
}
852-
}
853-
return false;
862+
const auto& allFileEndings = GetSupportedFileEndingList();
863+
return allFileEndings.find((LPCWSTR)sEndingLC) != allFileEndings.end();
854864
}
855865

856866
bool CFileList::TryReadingSlideShowList(const CString & sSlideShowFile) {
@@ -965,9 +975,10 @@ bool CFileList::TryReadingSlideShowList(const CString & sSlideShowFile) {
965975
pStart--;
966976
} else {
967977
// try to find file name with relative path
968-
LPCTSTR* allFileEndings = GetSupportedFileEndingList();
969-
for (int i = 0; i < nNumEndings; i++) {
970-
pStart = Helpers::stristr(lineBuff, (LPCTSTR)(CString(_T(".")) + allFileEndings[i]));
978+
const auto& extensions = GetSupportedFileEndingList();
979+
for (const auto& extension : extensions)
980+
{
981+
pStart = Helpers::stristr(lineBuff, (LPCTSTR)(CString(_T(".")) + extension.c_str()));
971982
if (pStart != NULL) {
972983
while (pStart >= lineBuff && *pStart != _T('"') && *pStart != _T('>')) pStart--;
973984
pStart++;
@@ -976,7 +987,7 @@ bool CFileList::TryReadingSlideShowList(const CString & sSlideShowFile) {
976987
}
977988
}
978989
}
979-
990+
980991
}
981992
if (pStart != NULL) {
982993
// extract file name and add to list of files to show

src/JPEGView/FileList.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,6 @@ class CFileList
179179
CFileList* WrapToPrevImage();
180180
void FindFiles();
181181
void VerifyFiles();
182-
bool IsImageFile(const CString & sEnding);
182+
bool IsImageFile(const CString & sEnding) const;
183183
bool TryReadingSlideShowList(const CString & sSlideShowFile);
184184
};

src/JPEGView/stdafx.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ extern CAppModule _Module;
3838
#pragma warning(pop)
3939

4040
// STL stuff
41-
#include <list>
41+
#include <list>
42+
#include <set>
4243

4344
#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS
4445

0 commit comments

Comments
 (0)