[project @ 2003-12-15 17:59:05 by panne]
[ghc-base.git] / Text / Regex / Posix.hsc
index 9cc06f7..56544e1 100644 (file)
 -----------------------------------------------------------------------------
 
 -- ToDo: should have an interface using PackedStrings.
+#include "config.h"
 
 module Text.Regex.Posix (
        -- * The @Regex@ type
        Regex,          -- abstract
 
+#if !defined(__HUGS__) || defined(HAVE_REGEX_H)
        -- * Compiling a regular expression
        regcomp,        -- :: String -> Int -> IO Regex
 
@@ -34,19 +36,26 @@ module Text.Regex.Posix (
                        --               String,     -- everything after match
                        --               [String]))  -- subexpression matches
 
+#endif
   ) where
 
+#if !defined(__HUGS__) || defined(HAVE_REGEX_H)
 #include <sys/types.h>
 #include "regex.h"
+#endif
 
 import Prelude
 
 import Foreign
 import Foreign.C
 
+type CRegex    = ()
+
 -- | A compiled regular expression
 newtype Regex = Regex (ForeignPtr CRegex)
 
+#if !defined(__HUGS__) || defined(HAVE_REGEX_H)
+-- to the end
 -- -----------------------------------------------------------------------------
 -- regcomp
 
@@ -60,9 +69,9 @@ regcomp pattern flags = do
   r <- withCString pattern $ \cstr ->
         withForeignPtr regex_fptr $ \p ->
            c_regcomp p cstr (fromIntegral flags)
-  addForeignPtrFinalizer regex_fptr ptr_regfree
   if (r == 0)
-     then return (Regex regex_fptr)
+     then do addForeignPtrFinalizer ptr_regfree regex_fptr
+            return (Regex regex_fptr)
      else error "Text.Regex.Posix.regcomp: error in pattern" -- ToDo
 
 -- -----------------------------------------------------------------------------
@@ -154,7 +163,6 @@ unpack string p_match = do
        REG_ERANGE, \
        REG_ESPACE
 
-type CRegex    = ()
 type CRegMatch = ()
 
 foreign import ccall unsafe "regcomp"
@@ -166,3 +174,5 @@ foreign import ccall  unsafe "&regfree"
 foreign import ccall unsafe "regexec"
   c_regexec :: Ptr CRegex -> CString -> CSize
            -> Ptr CRegMatch -> CInt -> IO CInt
+
+#endif /* HAVE_REGEX_H */