@@ -2,7 +2,17 @@
22{-# INCLUDE <string.h> #-}
33{-# LANGUAGE ForeignFunctionInterface #-}
44
5−-- |Haskell bindings to /libuuid/.
5+-- |
6+-- Module : Data.UUID
7+-- Copyright : (c) 2008 Antoine Latter
8+--
9+-- License : BSD-style
10+--
11+-- Maintainer : aslatter@gmail.com
12+-- Stability : experimental
13+-- Portability : portable
14+--
15+-- Haskell bindings to /libuuid/.
616-- The library /libuuid/ is available as a part of e2fsprogs:
717-- <http://e2fsprogs.sourceforge.net/>.
818--
@@ -28,6 +38,7 @@ import Foreign.ForeignPtr
2838import Foreign
2939
3040import Data.Typeable
41+import Data.Generics.Basics
3142
3243import Prelude hiding (null)
3344
@@ -41,7 +52,7 @@ instance Ord UUID where
4152 withForeignPtr fp1 $ \p1 ->
4253 withForeignPtr fp2 $ \p2 ->
4354 case c_compare p1 p2 of
44− 0 -> return EQ
55+ 0 -> return EQ
4556 n|n<0 -> return LT
4657 |n>0 -> return GT
4758
@@ -70,6 +81,16 @@ instance Storable UUID where
7081 memcpy pdest psource $ fromIntegral $ sizeOf (undefined :: UUID)
7182
7283
84+-- My goal with this instance was to make it work just enough to do what
85+-- I want when used with the HStringTemplate library.
86+instance Data UUID where
87+ toConstr uu = mkConstr uuidType (show uu) [] (error "fixity")
88+ gunfold _ _ = error "gunfold"
89+ dataTypeOf _ = uuidType
90+
91+uuidType = mkNorepType "Data.UUID.UUID"
92+
93+
7394-- |Creates a new 'UUID'. If \/dev\/urandom is available, it will be used.
7495-- Otherwise a UUID will be generated based on the current time and the
7596-- hardware MAC address, if available.
@@ -113,7 +134,7 @@ null (U fp) = unsafePerformIO $
113134fromString :: String -> Maybe UUID
114135fromString s = unsafePerformIO $ do
115136 fp <- mallocForeignPtrArray 16
116− res <- withCString s $ \chars ->
137+ res <- withCAString s $ \chars ->
117138 withForeignPtr fp $ \p ->
118139 c_read (castPtr chars) p
119140 case res of
@@ -127,7 +148,7 @@ toString :: UUID -> String
127148toString (U fp) = unsafePerformIO $ do
128149 chars <- mallocBytes 37
129150 withForeignPtr fp $ \p -> c_show p chars
130− st <- peekCString chars
151+ st <- peekCAString chars
131152 free chars
132153 return st
133154
@@ -137,7 +158,7 @@ toStringLower :: UUID -> String
137158toStringLower (U fp) = unsafePerformIO $ do
138159 chars <- mallocBytes 37
139160 withForeignPtr fp $ \p -> c_show_lower p chars
140− st <- peekCString chars
161+ st <- peekCAString chars
141162 free chars
142163 return st
143164
@@ -147,7 +168,7 @@ toStringUpper :: UUID -> String
147168toStringUpper (U fp) = unsafePerformIO $ do
148169 chars <- mallocBytes 37
149170 withForeignPtr fp $ \p -> c_show_upper p chars
150− st <- peekCString chars
171+ st <- peekCAString chars
151172 free chars
152173 return st
153174