Wildcard multiple Extensions

    This site uses cookies. By continuing to browse this site, you are agreeing to our Cookie Policy.

    • Wildcard multiple Extensions

      Hey Guys,

      I am having trouble using the WildCardMatch function specified in the source code, It works just fine for one extension, but not for more then one. I have tried doing it like
      *.jpg*.png
      and also the way the Win32 LoadFileDialog does it
      *.jpg\0*.png\0\0
      and some others

      What is the proper syntax with this function, the function seems like its meant to do a second check when it encounters another '*' , any help would be appreciated
      PC - Custom Built
      CPU: 3rd Gen. Intel i7 3770 3.4Ghz
      GPU: ATI Radeon HD 7959 3GB
      RAM: 16GB

      Laptop - Alienware M17x
      CPU: 3rd Gen. Intel i7 - Ivy Bridge
      GPU: NVIDIA GeForce GTX 660M - 2GB GDDR5
      RAM: 8GB Dual Channel DDR3 @ 1600mhz
    • Hi,

      you're right, it can handle multiple wildcards, but not the way you're expecting it to. For example you can look for all files starting with test and ending in fast no matter the extension with test-*-fast.*, but you can't match for alternatives. The WildCardMatch function can match only patterns with literal characters and the wildcard character * in it. If you want to check for multiple extensions you could use a regular expression library. For example boost::regex or the new regex standard library.

      Oh and of course the easiest way is to apply multiple WildCardMatch calls to each pattern, once for each extension.

      HTH

      Turing.

      P.S. Using WildCardMatch to check solely for file extensions is a bit overkill. It suffices to check if the last N characters are equal to .EXTENSION

      The post was edited 1 time, last by Turing ().

    • I ended up writing my own function for multiple extensions, I couldn't find any good articles on the regex class. I'll post mine if anyone want's to use it, it is likely relatively slow and could use some improvement maybe to make sure it isn't stuck in an infinite loop.

      Source Code

      1. bool ExtensionPatternMatch(string pattern, string str)
      2. {
      3. //Wildcard
      4. if(pattern == "*")
      5. return true;
      6. //Extract the Extension of the str String
      7. string extStr = str.substr(str.find_last_of('.'), str.back());
      8. //Extract the Pattern between '*'s
      9. while(1)
      10. {
      11. size_t extPos = pattern.find_first_of('*');
      12. size_t extPos2 = pattern.find_first_of('*', extPos+1);
      13. string patExt = pattern.substr(extPos+1, extPos2-1);
      14. if(patExt == extStr)
      15. return true;
      16. pattern = pattern.substr(extPos2, pattern.back());
      17. if(pattern.empty() || pattern == "**")
      18. break;
      19. }
      20. return false;
      21. }
      Display All
      PC - Custom Built
      CPU: 3rd Gen. Intel i7 3770 3.4Ghz
      GPU: ATI Radeon HD 7959 3GB
      RAM: 16GB

      Laptop - Alienware M17x
      CPU: 3rd Gen. Intel i7 - Ivy Bridge
      GPU: NVIDIA GeForce GTX 660M - 2GB GDDR5
      RAM: 8GB Dual Channel DDR3 @ 1600mhz
    • This seems like overkill to me. Regular expressions are notoriously slow and, as Turing said, you could probably implement it a lot more cleanly and much faster by just specializing the code a bit.

      If you're just looking for files on your hard drive, Windows has an API specifically for this. Look up FindFirstFile() and you should find all the various methods for it. It takes in search expression which is the same syntax as Windows-style wildcards. I imagine it's a lot faster, though I haven't benchmarked it.

      -Rez
    • I am only using it in the Resource Loader Class, so it will only be used on an initial file load or a cache miss. I wanted something more cross platform so the one for the Win32 API isn't really for me.

      The reason I need multiple extension checking is because of some of the library's I am using. ASSIMP can load in a ton of model format's and convert them all into one uniform class, so my MeshResourceLoader actually loads in .dae, .3ds, or whatever all from the same loader.

      Also the Image library I use for loading textures, SDL_Image is similar in that it can handle jpg, png, gif, bmp, etc.

      If I was to use something really performance intensive I may find something more efficient, for now though as it is only used when loading a file, I should be ok.
      PC - Custom Built
      CPU: 3rd Gen. Intel i7 3770 3.4Ghz
      GPU: ATI Radeon HD 7959 3GB
      RAM: 16GB

      Laptop - Alienware M17x
      CPU: 3rd Gen. Intel i7 - Ivy Bridge
      GPU: NVIDIA GeForce GTX 660M - 2GB GDDR5
      RAM: 8GB Dual Channel DDR3 @ 1600mhz
    • Originally posted by mholley519
      I am only using it in the Resource Loader Class, so it will only be used on an initial file load or a cache miss. I wanted something more cross platform so the one for the Win32 API isn't really for me.

      That's why I have it abstracted away in a function. The Win32 implementation uses the Win32 stuff, while other implementations can use whatever I want.


      The reason I need multiple extension checking is because of some of the library's I am using. ASSIMP can load in a ton of model format's and convert them all into one uniform class, so my MeshResourceLoader actually loads in .dae, .3ds, or whatever all from the same loader.

      Also the Image library I use for loading textures, SDL_Image is similar in that it can handle jpg, png, gif, bmp, etc.

      Okay, that makes sense. You aren't looking for a file, you have a file and are checking to see what type it is. Honestly, I'd be interested to see what the performance is between using your reg ex vs just use an if statement with strcmp(). The overhead of your reg ex is probably higher than a simple strcmp() (or std::string's '==' operator). I'm guessing the reg ex is more expensive.

      -Rez