@@ -2,7 +2,17 @@
2{-# INCLUDE <string.h> #-}
2{-# INCLUDE <string.h> #-}
3{-# LANGUAGE ForeignFunctionInterface #-}
3{-# LANGUAGE ForeignFunctionInterface #-}
7+-- Copyright : (c) 2008 Antoine Latter
11+-- Maintainer : aslatter@gmail.com
12+-- Stability : experimental
13+-- Portability : portable
5−-- |Haskell bindings to /libuuid/.
15+-- Haskell bindings to /libuuid/.
6-- The library /libuuid/ is available as a part of e2fsprogs:
16-- The library /libuuid/ is available as a part of e2fsprogs:
7-- <http://e2fsprogs.sourceforge.net/>.
17-- <http://e2fsprogs.sourceforge.net/>.
@@ -28,6 +38,7 @@ import Foreign.ForeignPtr
28import Foreign
38import Foreign
30import Data.Typeable
40import Data.Typeable
41+import Data.Generics.Basics
32import Prelude hiding (null)
43import Prelude hiding (null)
@@ -41,7 +52,7 @@ instance Ord UUID where
41 withForeignPtr fp1 $ \p1 ->
52 withForeignPtr fp1 $ \p1 ->
42 withForeignPtr fp2 $ \p2 ->
53 withForeignPtr fp2 $ \p2 ->
43 case c_compare p1 p2 of
54 case c_compare p1 p2 of
44− 0 -> return EQ
55+ 0 -> return EQ
45 n|n<0 -> return LT
56 n|n<0 -> return LT
46 |n>0 -> return GT
57 |n>0 -> return GT
@@ -70,6 +81,16 @@ instance Storable UUID where
70 memcpy pdest psource $ fromIntegral $ sizeOf (undefined :: UUID)
81 memcpy pdest psource $ fromIntegral $ sizeOf (undefined :: UUID)
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
91+uuidType = mkNorepType "Data.UUID.UUID"
73-- |Creates a new 'UUID'. If \/dev\/urandom is available, it will be used.
94-- |Creates a new 'UUID'. If \/dev\/urandom is available, it will be used.
74-- Otherwise a UUID will be generated based on the current time and the
95-- Otherwise a UUID will be generated based on the current time and the
75-- hardware MAC address, if available.
96-- hardware MAC address, if available.
@@ -113,7 +134,7 @@ null (U fp) = unsafePerformIO $
113fromString :: String -> Maybe UUID
134fromString :: String -> Maybe UUID
114fromString s = unsafePerformIO $ do
135fromString s = unsafePerformIO $ do
115 fp <- mallocForeignPtrArray 16
136 fp <- mallocForeignPtrArray 16
116− res <- withCString s $ \chars ->
137+ res <- withCAString s $ \chars ->
117 withForeignPtr fp $ \p ->
138 withForeignPtr fp $ \p ->
118 c_read (castPtr chars) p
139 c_read (castPtr chars) p
119 case res of
140 case res of
@@ -127,7 +148,7 @@ toString :: UUID -> String
127toString (U fp) = unsafePerformIO $ do
148toString (U fp) = unsafePerformIO $ do
128 chars <- mallocBytes 37
149 chars <- mallocBytes 37
129 withForeignPtr fp $ \p -> c_show p chars
150 withForeignPtr fp $ \p -> c_show p chars
130− st <- peekCString chars
151+ st <- peekCAString chars
131 free chars
152 free chars
132 return st
153 return st
@@ -137,7 +158,7 @@ toStringLower :: UUID -> String
137toStringLower (U fp) = unsafePerformIO $ do
158toStringLower (U fp) = unsafePerformIO $ do
138 chars <- mallocBytes 37
159 chars <- mallocBytes 37
139 withForeignPtr fp $ \p -> c_show_lower p chars
160 withForeignPtr fp $ \p -> c_show_lower p chars
140− st <- peekCString chars
161+ st <- peekCAString chars
141 free chars
162 free chars
142 return st
163 return st
@@ -147,7 +168,7 @@ toStringUpper :: UUID -> String
147toStringUpper (U fp) = unsafePerformIO $ do
168toStringUpper (U fp) = unsafePerformIO $ do
148 chars <- mallocBytes 37
169 chars <- mallocBytes 37
149 withForeignPtr fp $ \p -> c_show_upper p chars
170 withForeignPtr fp $ \p -> c_show_upper p chars
150− st <- peekCString chars
171+ st <- peekCAString chars
151 free chars
172 free chars
152 return st
173 return st