aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/common/file_search.cpp
blob: bfb54ce72e40e72ae9d17290b43130e1a5780e4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright 2013 Dolphin Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.


#include "common/common.h"

#ifndef _WIN32
#include <dirent.h>
#else
#include <windows.h>
#endif

#include <algorithm>

#include "common/file_search.h"
#include "common/string_util.h"


CFileSearch::CFileSearch(const CFileSearch::XStringVector& _rSearchStrings, const CFileSearch::XStringVector& _rDirectories)
{
    // Reverse the loop order for speed?
    for (size_t j = 0; j < _rSearchStrings.size(); j++)
    {
        for (size_t i = 0; i < _rDirectories.size(); i++)
        {
            FindFiles(_rSearchStrings[j], _rDirectories[i]);
        }
    }
}


void CFileSearch::FindFiles(const std::string& _searchString, const std::string& _strPath)
{
    std::string GCMSearchPath;
    Common::BuildCompleteFilename(GCMSearchPath, _strPath, _searchString);
#ifdef _WIN32
    WIN32_FIND_DATA findData;
    HANDLE FindFirst = FindFirstFile(Common::UTF8ToTStr(GCMSearchPath).c_str(), &findData);

    if (FindFirst != INVALID_HANDLE_VALUE)
    {
        bool bkeepLooping = true;

        while (bkeepLooping)
        {
            if (findData.cFileName[0] != '.')
            {
                std::string strFilename;
                Common::BuildCompleteFilename(strFilename, _strPath, Common::TStrToUTF8(findData.cFileName));
                m_FileNames.push_back(strFilename);
            }

            bkeepLooping = FindNextFile(FindFirst, &findData) ? true : false;
        }
    }
    FindClose(FindFirst);


#else
    // TODO: super lame/broken

    auto end_match(_searchString);

    // assuming we have a "*.blah"-like pattern
    if (!end_match.empty() && end_match[0] == '*')
        end_match.erase(0, 1);

    // ugly
    if (end_match == ".*")
        end_match.clear();

    DIR* dir = opendir(_strPath.c_str());

    if (!dir)
        return;

    while (auto const dp = readdir(dir))
    {
        std::string found(dp->d_name);

        if ((found != ".") && (found != "..")
            && (found.size() >= end_match.size())
            && std::equal(end_match.rbegin(), end_match.rend(), found.rbegin()))
        {
            std::string full_name;
            if (_strPath.c_str()[_strPath.size()-1] == DIR_SEP_CHR)
                full_name = _strPath + found;
            else
                full_name = _strPath + DIR_SEP + found;

            m_FileNames.push_back(full_name);
        }
    }

    closedir(dir);
#endif
}

const CFileSearch::XStringVector& CFileSearch::GetFileNames() const
{
    return m_FileNames;
}