Skip to content

Commit cd13cdf

Browse files
committedNov 28, 2024·
[实现 GlobMatch 功能]: 主要实现了 GlobMatch 功能,并对项目文件进行了相应的更新。
- 在项目中添加了 Dependabot 配置文件 `.github/dependabot.yml`,以便自动维护 GitHub Actions 的依赖。 - 更新了 `.github/workflows/build.yml` 文件,移除了不再需要的 `save-always` 配置。 - 在 `CMakeLists.txt` 中添加了 `GlobMatch` 子目录,以支持新功能的构建。 - 添加了 `GlobMatch` 目录和相关文件,包括 `CMakeLists.txt`、`globmatcher.cc`、`globmatcher.hpp` 和 `main.cc`,实现了基于 glob 模式的匹配功能。 - 在 `README.md` 中记录了 `GlobMatch` 功能的添加,并对列表进行了相应的更新。 - 更新了 `vcpkg.json` 文件,以反映项目依赖的变化。
1 parent 9869a6c commit cd13cdf

File tree

9 files changed

+131
-14
lines changed

9 files changed

+131
-14
lines changed
 

‎.github/dependabot.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# To get started with Dependabot version updates, you'll need to specify which
2+
# package ecosystems to update and where the package manifests are located.
3+
# Please see the documentation for all configuration options:
4+
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
5+
6+
version: 2
7+
updates:
8+
- package-ecosystem: "github-actions" # See documentation for possible values
9+
directory: "/" # Location of package manifests
10+
schedule:
11+
interval: "weekly"
12+

‎.github/workflows/build.yml

-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ jobs:
7878
${{ matrix.os }}-vcpkg-installed-${{ runner.os }}-
7979
${{ matrix.os }}-vcpkg-installed-
8080
${{ matrix.os }}-
81-
save-always: true
8281
8382
- name: Configure and build windows
8483
if: startsWith(matrix.os, 'windows')

‎CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ add_subdirectory(ByteOrder)
5555
add_subdirectory(CountDownLatch)
5656
add_subdirectory(Crashpad)
5757
add_subdirectory(Curl)
58+
add_subdirectory(GlobMatch)
5859
add_subdirectory(DesignPattern)
5960
add_subdirectory(Glog)
6061
add_subdirectory(LinkedList)

‎GlobMatch/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_executable(globmatcher_test globmatcher.cc globmatcher.hpp main.cc)

‎GlobMatch/globmatcher.cc

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include "globmatcher.hpp"
2+
3+
#include <fstream>
4+
#include <iostream>
5+
6+
std::vector<std::string> readGlobPatternsFromFile(const std::string &filename)
7+
{
8+
std::vector<std::string> patterns;
9+
std::ifstream file(filename);
10+
std::string pattern;
11+
if (file.is_open()) {
12+
while (std::getline(file, pattern)) {
13+
patterns.push_back(pattern);
14+
}
15+
file.close();
16+
} else {
17+
std::cerr << "Unable to open file: " << filename << std::endl;
18+
}
19+
return patterns;
20+
}
21+
22+
bool globMatch(const std::string &text, const std::string &pattern)
23+
{
24+
size_t n = text.size(), m = pattern.size();
25+
size_t i = 0, j = 0;
26+
while (i < n && j < m) {
27+
if (pattern[j] == '*') {
28+
while (j < m && pattern[j] == '*')
29+
++j;
30+
if (j == m)
31+
return true;
32+
while (i < n && !globMatch(text.substr(i), pattern.substr(j))) {
33+
++i;
34+
}
35+
if (i > 0)
36+
--i;
37+
} else if (pattern[j] == '?' || pattern[j] == text[i]) {
38+
++i;
39+
++j;
40+
} else {
41+
return false;
42+
}
43+
}
44+
while (j < m && pattern[j] == '*') {
45+
++j;
46+
}
47+
return j == m && i == n;
48+
}
49+
50+
GlobMatcher::GlobMatcher(const std::string &patternFile)
51+
{
52+
m_patterns = readGlobPatternsFromFile(patternFile);
53+
}
54+
55+
bool GlobMatcher::match(const std::string &text)
56+
{
57+
for (const auto &pattern : m_patterns) {
58+
if (globMatch(text, pattern)) {
59+
return true;
60+
}
61+
}
62+
return false;
63+
}

‎GlobMatch/globmatcher.hpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <utils/object.hpp>
4+
5+
#include <string>
6+
#include <vector>
7+
8+
class GlobMatcher : noncopyable
9+
{
10+
public:
11+
explicit GlobMatcher(const std::string &patternFile);
12+
13+
bool match(const std::string &text);
14+
15+
private:
16+
std::vector<std::string> m_patterns;
17+
};

‎GlobMatch/main.cc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include "globmatcher.hpp"
2+
3+
#include <filesystem>
4+
#include <iostream>
5+
6+
auto main() -> int
7+
{
8+
auto path = std::filesystem::current_path().parent_path().parent_path();
9+
std::cout << path << std::endl;
10+
auto filePath = path / ".gitignore";
11+
12+
GlobMatcher globMatcher(filePath.string());
13+
for (const auto &entry : std::filesystem::directory_iterator(path)) {
14+
auto currentPath = entry.path();
15+
if (globMatcher.match(currentPath.filename().string())) {
16+
std::cout << currentPath << " is ignored by .gitignore" << std::endl;
17+
} else {
18+
std::cout << currentPath << " is not ignored by .gitignore" << std::endl;
19+
}
20+
}
21+
22+
return 0;
23+
}

‎README.md

+10-9
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
14. [MVC](/DesignPattern/MVC/model.hpp)——mvc模式;
2626
15. [Observer](/DesignPattern/Observer/observer.hpp)——观察者模式;
2727
16. [Singleton](/DesignPattern/Singleton/singleton.hpp)——单例模式;
28-
17. [Glog](/Glog/main.cc)——google glog的例子;
29-
18. [Icmp](/Icmp/icmp.hpp)——linux icmp协议的简单封装;
30-
19. [LinkedList](/LinkedList/linkedlist.hpp)——链表的相关操作,插入、移除、反转、打印;
31-
20. [Memcpy](/Memcpy/memcpy.hpp)——`memcpy`函数实现;
32-
21. [MonitorDir](/MonitorDir/monitordir.hpp)——windows(`ReadDirectoryChangesW`),macos(`FSEvents`)和linux(`inotify`)目录监控的简单例子;
33-
22. [Mutex](/Mutex/mutex.hpp)——使用std::atomic_flag实现的简单互斥锁和自旋锁;
34-
23. [OpenSSL](/OpenSSL)——openssl的一些例子;
28+
17. [GlobMatch](/GlobMatch/globmatcher.hpp)——glob模式匹配的简单实现;
29+
18. [Glog](/Glog/main.cc)——google glog的例子;
30+
19. [Icmp](/Icmp/icmp.hpp)——linux icmp协议的简单封装;
31+
20. [LinkedList](/LinkedList/linkedlist.hpp)——链表的相关操作,插入、移除、反转、打印;
32+
21. [Memcpy](/Memcpy/memcpy.hpp)——`memcpy`函数实现;
33+
22. [MonitorDir](/MonitorDir/monitordir.hpp)——windows(`ReadDirectoryChangesW`),macos(`FSEvents`)和linux(`inotify`)目录监控的简单例子;
34+
23. [Mutex](/Mutex/mutex.hpp)——使用std::atomic_flag实现的简单互斥锁和自旋锁;
35+
24. [OpenSSL](/OpenSSL)——openssl的一些例子;
3536
1. [aes](/OpenSSL/openssl_aes.cc)——aes加解密的例子;
3637
2. [base64](/OpenSSL/openssl_base64.cc)——base64编解码的例子;
3738
3. [hash](/OpenSSL/openssl_hash.cc)——sha256的例子;
@@ -41,10 +42,10 @@
4142
7. [sm4](/OpenSSL/openssl_sm4.cc)——sm4加解密的例子;
4243
8. [x509](/OpenSSL/openssl_x509.cc)——x509证书的例子;
4344
9. [bash](/OpenSSL/openssl_bash.sh)——openssl命令行的例子;
44-
24. [Server](/Server)——linux server的一些例子;
45+
25. [Server](/Server)——linux server的一些例子;
4546
1. [server_epoll](/Server/server_epoll.cc)——epoll的例子;
4647
2. [server_poll](/Server/server_poll.cc)——poll的例子;
4748
3. [server_select](/Server/server_select.cc)——select的例子;
48-
25. [Thread](/Thread/)——基于std::thread实现的线程类,包括线程池;
49+
26. [Thread](/Thread/)——基于std::thread实现的线程类,包括线程池;
4950
1. [Thread](/Thread/thread.hpp)——线程类;
5051
2. [ThreadPool](/Thread/threadpool.hpp)——线程池;

‎vcpkg.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"breakpad",
99
"benchmark",
1010
"gtest",
11+
"crashpad",
1112
{
1213
"name": "openssl",
1314
"features": [
@@ -23,8 +24,7 @@
2324
"http2",
2425
"tool"
2526
]
26-
},
27-
"crashpad"
27+
}
2828
],
29-
"builtin-baseline": "8150939b69720adc475461978e07c2d2bf5fb76e"
30-
}
29+
"builtin-baseline": "5f4628b89f3f98cd9a0b43c27ded2aa53da1f790"
30+
}

0 commit comments

Comments
 (0)
Please sign in to comment.