-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f3b623e
commit 4f2fa91
Showing
3 changed files
with
32 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,35 @@ | ||
module FnMatch (fnmatch) where | ||
module FnMatch (fnmatch, FnMatchFlags(..)) where | ||
|
||
import Foreign.C | ||
import qualified Data.ByteString as BS | ||
import qualified Data.ByteString.Unsafe as BU | ||
import GHC.IO (unsafePerformIO) | ||
import Data.Bits ((.|.)) | ||
|
||
|
||
foreign import ccall "fnmatch" c_fnmatch | ||
:: CString -> CString -> CInt -> IO CInt | ||
|
||
fnmatch :: BS.ByteString -> BS.ByteString -> Int -> Bool | ||
data FnMatchFlags | ||
= FlagNoEscape | ||
| FlagPathName | ||
| FlagPeriod | ||
| FlagLeadingDir | ||
| FlagCaseFold | ||
| FlagExtMatch | ||
deriving (Eq, Show) | ||
|
||
fnmatch :: BS.ByteString -> BS.ByteString -> [FnMatchFlags] -> Bool | ||
fnmatch pattern str flags = unsafePerformIO $ | ||
BU.unsafeUseAsCString pattern $ \c_pattern -> | ||
BU.unsafeUseAsCString str $ \c_str -> do | ||
result <- c_fnmatch c_pattern c_str (fromIntegral flags) | ||
return (result == 0) | ||
result <- c_fnmatch c_pattern c_str flags' | ||
return (result == 0) | ||
where | ||
flags' = foldr (\flag acc -> acc .|. flag) 0 (map flagToCInt flags) | ||
flagToCInt FlagNoEscape = 1 | ||
flagToCInt FlagPathName = 2 | ||
flagToCInt FlagPeriod = 4 | ||
flagToCInt FlagLeadingDir = 8 | ||
flagToCInt FlagCaseFold = 16 | ||
flagToCInt FlagExtMatch = 32 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters