1{-# INCLUDE <uuid/uuid.h> #-}
2{-# INCLUDE <string.h> #-}
3{-# LANGUAGE ForeignFunctionInterface #-}
7-- Copyright : (c) 2008 Antoine Latter
11-- Maintainer : aslatter@gmail.com
12-- Stability : experimental
13-- Portability : portable
15-- Haskell bindings to /libuuid/.
16-- The library /libuuid/ is available as a part of e2fsprogs:
17-- <http://e2fsprogs.sourceforge.net/>.
19-- This library is useful for creating, comparing, parsing and
20-- printing Universally Unique Identifiers.
21-- See <http://en.wikipedia.org/wiki/UUID> for the general idea.
35import Foreign.C.String
37import Foreign.ForeignPtr
41import Data.Generics.Basics
43import Prelude hiding (null)
45import Data.UUID.Internal
48 a == b = compare a b == EQ
50instance Ord UUID where
51 compare (U fp1) (U fp2) = unsafePerformIO $
52 withForeignPtr fp1 $ \p1 ->
53 withForeignPtr fp2 $ \p2 ->
54 case c_compare p1 p2 of
59instance Show UUID where
62instance Read UUID where
63 readsPrec _ str = case fromString (take 36 str) of
65 Just u -> [(u,drop 36 str)]
67instance Typeable UUID where
68 typeOf _ = mkTyConApp (mkTyCon "Data.UUID.UUID") []
70instance Storable UUID where
71 sizeOf _ = (16 *) $ alignment (undefined :: CChar)
72 alignment _ = alignment (undefined :: CChar)
75 fp <- mallocForeignPtrArray 16
76 withForeignPtr fp $ \pdest ->
77 memcpy pdest psource $ fromIntegral $ sizeOf (undefined :: UUID)
80 poke pdest (U fp) = withForeignPtr fp $ \psource ->
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.
86instance Data UUID where
87 toConstr uu = mkConstr uuidType (show uu) [] (error "fixity")
88 gunfold _ _ = error "gunfold"
89 dataTypeOf _ = uuidType
91uuidType = mkNorepType "Data.UUID.UUID"
94-- |Creates a new 'UUID'. If \/dev\/urandom is available, it will be used.
95-- Otherwise a UUID will be generated based on the current time and the
96-- hardware MAC address, if available.
99 fp <- mallocForeignPtrArray 16
100 withForeignPtr fp $ \p -> c_generate p
103-- |Create a new 'UUID'. If \/dev\/urandom is available, it will be used.
104-- Otherwise a psuedorandom generator will be used.
105generateRandom :: IO UUID
107 fp <- mallocForeignPtrArray 16
108 withForeignPtr fp $ \p -> c_generate_random p
111-- |Create a new 'UUID'. The UUID will be generated based on the
112-- current time and the hardware MAC address, if available.
113generateTime :: IO UUID
115 fp <- mallocForeignPtrArray 16
116 withForeignPtr fp $ \p -> c_generate_time p
119-- |Returns 'True' if the passed-in 'UUID' is the null UUID.
121null (U fp) = unsafePerformIO $
122 withForeignPtr fp $ \p ->
123 return $ c_null p == 1
125-- |If the passed in 'String' can be parsed as a 'UUID', it will be.
126-- The hyphens may not be omitted.
130-- fromString \"c2cc10e1-57d6-4b6f-9899-38d972112d8c\"
133-- Hex digits may be upper or lower-case.
134fromString :: String -> Maybe UUID
135fromString s = unsafePerformIO $ do
136 fp <- mallocForeignPtrArray 16
137 res <- withCAString s $ \chars ->
138 withForeignPtr fp $ \p ->
139 c_read (castPtr chars) p
141 0 -> return . Just $ U fp
144-- |Returns a 'String' representation of the passed in 'UUID'.
145-- Hex digits occuring in the output will be either upper or
146-- lower-case depending on system defaults and locale.
147toString :: UUID -> String
148toString (U fp) = unsafePerformIO $ do
149 chars <- mallocBytes 37
150 withForeignPtr fp $ \p -> c_show p chars
151 st <- peekCAString chars
155-- |Returns a 'String' representation of the passed in 'UUID'.
156-- Hex digits occuring in the output will be lower-case.
157toStringLower :: UUID -> String
158toStringLower (U fp) = unsafePerformIO $ do
159 chars <- mallocBytes 37
160 withForeignPtr fp $ \p -> c_show_lower p chars
161 st <- peekCAString chars
165-- |Returns a 'String' representation of the passed in 'UUID'.
166-- Hex digits occuring in the output will be upper-case.
167toStringUpper :: UUID -> String
168toStringUpper (U fp) = unsafePerformIO $ do
169 chars <- mallocBytes 37
170 withForeignPtr fp $ \p -> c_show_upper p chars
171 st <- peekCAString chars
176-- FFI calls to do the work
178type C_UUID = Ptr CChar
181foreign import ccall unsafe "uuid_compare"
182 c_compare :: C_UUID -> C_UUID -> CInt
184-- making random UUIDs
185foreign import ccall unsafe "uuid_generate"
186 c_generate :: C_UUID -> IO ()
188foreign import ccall unsafe "uuid_generate_time"
189 c_generate_time :: C_UUID -> IO ()
191foreign import ccall unsafe "uuid_generate_random"
192 c_generate_random :: C_UUID -> IO ()
195foreign import ccall unsafe "uuid_is_null"
196 c_null :: C_UUID -> CInt
199foreign import ccall unsafe "uuid_parse"
200 c_read :: CString -> C_UUID ->IO CInt
204foreign import ccall unsafe "uuid_unparse"
205 c_show :: C_UUID -> CString -> IO ()
207foreign import ccall unsafe "uuid_unparse_lower"
208 c_show_lower :: C_UUID -> CString -> IO ()
210foreign import ccall unsafe "uuid_unparse_upper"
211 c_show_upper :: C_UUID -> CString -> IO ()
216foreign import ccall unsafe "uuid_type"
217 c_type :: C_UUID -> CInt
219foreign import ccall unsafe "uuid_variant"
220 c_variant :: C_UUID -> CInt
224foreign import ccall unsafe "memcpy"
225 memcpy :: Ptr a -> Ptr b -> CSize -> IO ()
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list