Requirements:
The Access Authorization configuration file is sometimes configured in several dimensions, for example, in the format of "company | product | sys:
1. configuring "Sina | Weibo | pusher" indicates that the pusher System of Weibo products of Sina can be accessed, while "Sina | Weibo | sign" does not allow access.
2. configuring "Sina | * | pusher" indicates that the pusher system of all Sina products can be accessed.
3. configuring "* | pusher" indicates that pusher systems of all products of all companies can be accessed.
...
There are many similar scenarios. Well, simple things won't be broken.
Implementation:
In the face of this demand, I first thought about how to design the mode string and how to quickly implement the function, because I am writing a C service, so what I first came to my mind was a bunch of things, such as strchr (XXX, '*'), strchr (XXX, '|') and so on. Later I found that this things didn't have to be made on their own, there are ready-made functions that can be used, that is, fnmatch.
Google once found that there are not many fnmatch materials, most of them are about PhP functions, so there is no way, you can only write your own test.
# Include <iostream> # include <fnmatch. h ># include <vector> using namespace STD; int main () {const char * orgin_str = "Sina | Weibo | pusher "; char pattern_arr [] [20] = {"Sina | * | pusher"}, {"Sina | *"}, {"* | Weibo | *"}, // {"Sina | PIC | *"}, {"* | sign"}, {"* | Weibo | sign"} that cannot be matched "}, {"* | PIC | sign" },{ "Sina | PIC | sign" },{ "* | *" }}; static int pattern_arr_size = sizeof (pattern_arr) /sizeof (pattern_arr [0]); vector <char *> vec_str; For (INT I = 0; I <pattern_arr_size; I ++) {vec_str.push_back (pattern_arr [I]);} int ret; int z = 0; while (z <1) {for (INT I = 0; I <vec_str.size (); I ++) {ret = fnmatch (vec_str.at (I), orgin_str, fnm_pathname); If (fnm_nomatch = RET) {cout <"sorry I'm failed [" <vec_str.at (I) <"]" <Endl ;}++ Z ;}}
Result:
Experiment, the results are not good, fully meet the needs:
When the requirements are met, I am worried about another problem, that is, performance. comment out the cout output, adjust the while Z statement to 1,000,000, and re-compile and run it:
Time./fnmatch
It seems that the efficiency is not bad. 2.1 matching times were performed in S, with an average of 2 us, and the performance requirements were also met...
References:
Fnmatch source code implementation: http://www.opensource.apple.com/source/sudo/sudo-16/sudo/fnmatch.c
MAN: http://linux.die.net/man/3/fnmatch