问题
I'm trying to implement glob(3), or glob
-alike function in C++.
I already have a function that reads directory contents into an std::vector<std::string>
container (let's call this function ListDirectory()
), so I'd obviously only need the stringmatching part - My questions:
- What kind of concept should one follow when implementing it?
- Are there common gotchas one should keep in mind?
- Is it wise to use a fullblown regexp library (like PCRE), or rather simple pattern matching a la Lua?
- If using simple pattern matching is better, are there already working functions/libraries/classes available (what about
scanf
and friends)?
回答1:
If you are searching platform independent wildcard library, for example, there is shwild library.
If you are examining pattern matching for self-educational purpose, as for basic regular expression by backtracking, I think chapter one of Beautiful Code illustrates well.
When once you are at home in regular expression, probably converting wildcard to regular expression, or converting regular expression code to wildcard matcher, won't be a hard job.
With regard to realistic regular expression by NFA, detailed explanations will be found in Russ Cox's web site.
Hope this helps
回答2:
I use this one: wildcmp, in a mildly adapted form to reject directory seperators /
in a *
. If you want the slightly adapted code (I also converted the pointers to strings/iterators, for the fun of it :)
). It's clean and simple, no need for anything more fancy.
来源:https://stackoverflow.com/questions/5211752/implementing-a-platform-independent-glob