Doxygen
Loading...
Searching...
No Matches
fileinfo.h
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Copyright (C) 1997-2021 by Dimitri van Heesch.
4 *
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
10 *
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
13 *
14 */
15
16#ifndef FILEINFO_H
17#define FILEINFO_H
18
19#include <string>
20
21#include "regex.h"
22#include "utf8.h"
23
24/** @brief Minimal replacement for QFileInfo. */
26{
27 public:
28 explicit FileInfo(const std::string &name) : m_name(name) {}
29 bool exists() const;
30 size_t size() const;
31 bool isWritable() const;
32 bool isReadable() const;
33 bool isExecutable() const;
34 bool isRelative() const;
35 bool isFile() const;
36 bool isDir() const;
37 bool isSymLink() const;
38 std::string readLink() const;
39 std::string filePath() const;
40 std::string absFilePath() const;
41 std::string fileName() const;
42 std::string baseName() const;
43 std::string extension(bool complete) const;
44 std::string dirPath(bool absPath = true) const;
45
46 /*!
47 * @brief Match the file name against a list of patterns.
48 *
49 * The pattern list can be a list of strings or a list of objects that can be converted to strings.
50 * The pattern is matched against the file name, the file path and the absolute file path.
51 * If a match is found, the matching pattern is returned in elem (if not nullptr).
52 *
53 * @param patList List of patterns to match against.
54 * @param caseSenseNames Whether to match case-sensitively or not.
55 * @param elem Pointer to store the matching pattern (optional).
56 * @param getter Function to convert pattern elements to strings (optional).
57 * @return True if a match is found, false otherwise.
58 */
59 template<class PatternList, class PatternElem = std::string, typename PatternGet = std::string(*)(const PatternElem &)>
60 bool match(const PatternList &patList,
61 bool caseSenseNames,
62 PatternElem *elem = nullptr,
63 PatternGet getter = [](const std::string &s){ return s; }
64 ) const
65 {
66 bool found = false;
67
68 if (!patList.empty())
69 {
70 std::string fn = fileName();
71 std::string fp = filePath();
72 std::string afp= absFilePath();
73
74 for (const auto &li : patList)
75 {
76 std::string pattern = getter(li);
77 if (!pattern.empty())
78 {
79 size_t i=pattern.find('=');
80 if (i!=std::string::npos) pattern=pattern.substr(0,i); // strip of the extension specific filter name
81
82 if (!caseSenseNames)
83 {
84 pattern = convertUTF8ToLower(pattern);
85 fn = convertUTF8ToLower(fn);
86 fp = convertUTF8ToLower(fp);
87 afp = convertUTF8ToLower(afp);
88 }
89 reg::Ex re(pattern,reg::Ex::Mode::Wildcard);
90 found = re.isValid() && (reg::match(fn,re) ||
91 (fn!=fp && reg::match(fp,re)) ||
92 (fn!=afp && fp!=afp && reg::match(afp,re)));
93 if (found)
94 {
95 if (elem) *elem = li;
96 break;
97 }
98 //printf("Matching '%s' against pattern '%s' found=%d\n",
99 // qPrint(fi->fileName()),qPrint(pattern),found);
100 }
101 }
102 }
103 return found;
104 }
105
106 private:
107 static void correctPath(std::string &s);
108 std::string m_name;
109};
110
111#endif
std::string m_name
Definition fileinfo.h:108
std::string readLink() const
Definition fileinfo.cpp:88
bool isRelative() const
Definition fileinfo.cpp:62
bool isSymLink() const
Definition fileinfo.cpp:81
bool isExecutable() const
Definition fileinfo.cpp:55
FileInfo(const std::string &name)
Definition fileinfo.h:28
bool exists() const
Definition fileinfo.cpp:34
bool isWritable() const
Definition fileinfo.cpp:41
size_t size() const
Definition fileinfo.cpp:27
std::string extension(bool complete) const
Definition fileinfo.cpp:134
bool match(const PatternList &patList, bool caseSenseNames, PatternElem *elem=nullptr, PatternGet getter=[](const std::string &s){ return s;}) const
Match the file name against a list of patterns.
Definition fileinfo.h:60
std::string fileName() const
Definition fileinfo.cpp:122
bool isReadable() const
Definition fileinfo.cpp:48
bool isDir() const
Definition fileinfo.cpp:74
static void correctPath(std::string &s)
Definition fileinfo.cpp:100
bool isFile() const
Definition fileinfo.cpp:67
std::string dirPath(bool absPath=true) const
Definition fileinfo.cpp:141
std::string filePath() const
Definition fileinfo.cpp:95
std::string baseName() const
Definition fileinfo.cpp:127
std::string absFilePath() const
Definition fileinfo.cpp:105
@ Wildcard
simple globbing pattern.
Definition regex.h:45
bool match(std::string_view str, Match &match, const Ex &re)
Matches a given string str for a match against regular expression re.
Definition regex.cpp:861
std::string convertUTF8ToLower(const std::string &input)
Converts the input string into a lower case version, also taking into account non-ASCII characters th...
Definition utf8.cpp:191
Various UTF8 related helper functions.