Added support for glob
thanks to tsoding/glob.h
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "func.h"
|
||||
#include "glob.h"
|
||||
|
||||
Pass* find_password(Buffer* buff, char* label)
|
||||
{
|
||||
@@ -6,7 +7,7 @@ Pass* find_password(Buffer* buff, char* label)
|
||||
|
||||
for (int i = 0; i < buff->taken / sizeof(Pass); i++)
|
||||
{
|
||||
if (!strcmp(passwords[i].label, label))
|
||||
if (glob(label, passwords[i].label) == Glob_Result::MATCHED)
|
||||
{
|
||||
return &passwords[i];
|
||||
}
|
||||
@@ -66,7 +67,7 @@ void print_usage()
|
||||
printf_s(" Note: <label> max len %d char\n\n", MAX_STRING_SIZE);
|
||||
printf_s(" Flags:\n\n");
|
||||
printf_s(" -h \t print this message\n");
|
||||
printf_s(" <label> \t get password of this label\n");
|
||||
printf_s(" <label> \t get password of this label can use GLOB\n");
|
||||
printf_s(" -g <label> \t generate or update password of this label\n");
|
||||
printf_s(" -d <label> \t delete password of this label\n");
|
||||
printf_s(" -i <label> \t input password for this label max %d char\n", MAX_STRING_SIZE);
|
||||
|
94
Password_manager/source/glob.cpp
Normal file
94
Password_manager/source/glob.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
#include "glob.h"
|
||||
|
||||
Glob_Result glob(const char* pattern, const char* text)
|
||||
{
|
||||
while (*pattern != '\0' && *text != '\0') {
|
||||
switch (*pattern) {
|
||||
case '?': {
|
||||
pattern += 1;
|
||||
text += 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case '*': {
|
||||
Glob_Result result = glob(pattern + 1, text);
|
||||
if (result != Glob_Result::UNMATCHED) return result;
|
||||
text += 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case '[': {
|
||||
bool matched = false;
|
||||
bool negate = false;
|
||||
|
||||
pattern += 1; // skipping [
|
||||
if (*pattern == '\0') return Glob_Result::SYNTAX_ERROR; // unclosed [
|
||||
|
||||
if (*pattern == '!') {
|
||||
negate = true;
|
||||
pattern += 1;
|
||||
if (*pattern == '\0') return Glob_Result::SYNTAX_ERROR; // unclosed [
|
||||
}
|
||||
|
||||
char prev = *pattern;
|
||||
matched |= prev == *text;
|
||||
pattern += 1;
|
||||
|
||||
while (*pattern != ']' && *pattern != '\0') {
|
||||
switch (*pattern) {
|
||||
case '-': {
|
||||
pattern += 1;
|
||||
switch (*pattern) {
|
||||
case ']':
|
||||
matched |= '-' == *text;
|
||||
break;
|
||||
case '\0':
|
||||
return Glob_Result::SYNTAX_ERROR; // unclosed [
|
||||
default: {
|
||||
matched |= prev <= *text && *text <= *pattern;
|
||||
prev = *pattern;
|
||||
pattern += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default: {
|
||||
prev = *pattern;
|
||||
matched |= prev == *text;
|
||||
pattern += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (*pattern != ']') return Glob_Result::SYNTAX_ERROR; // unclosed [
|
||||
if (negate) matched = !matched;
|
||||
if (!matched) return Glob_Result::UNMATCHED;
|
||||
|
||||
pattern += 1;
|
||||
text += 1;
|
||||
}
|
||||
break;
|
||||
|
||||
case '\\':
|
||||
pattern += 1;
|
||||
if (*pattern == '\0') return Glob_Result::SYNTAX_ERROR; // unfinished escape
|
||||
// fallthrough
|
||||
default: {
|
||||
if (*pattern == *text) {
|
||||
pattern += 1;
|
||||
text += 1;
|
||||
}
|
||||
else {
|
||||
return Glob_Result::UNMATCHED;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (*text == '\0') {
|
||||
while (*pattern == '*') pattern += 1;
|
||||
if (*pattern == '\0') return Glob_Result::MATCHED;
|
||||
}
|
||||
|
||||
return Glob_Result::UNMATCHED;
|
||||
}
|
Reference in New Issue
Block a user