    -- GENERATED by C->Haskell Compiler, version 0.13.13 (gtk2hs branch) "Bin IO", 27 May 2012 (Haskell)
    -- Edit the ORIGINAL .chs file instead!

    {-# LINE 1 "./System/Glib/GString.chs" #-}
    -- -*-haskell-*-
    --  GIMP Toolkit (GTK)
    --
    --  Author : Andreas Baldeau
    --
    --  Created: 14 November 2010
    --
    --  Copyright (C) 2010 Andreas Baldeau
    --
    --  This library is free software; you can redistribute it and/or
    --  modify it under the terms of the GNU Lesser General Public
    --  License as published by the Free Software Foundation; either
    --  version 2.1 of the License, or (at your option) any later version.
    --
    --  This library is distributed in the hope that it will be useful,
    --  but WITHOUT ANY WARRANTY; without even the implied warranty of
    --  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    --  Lesser General Public License for more details.
    --
    -- |
    -- Maintainer  : gtk2hs-users@lists.sourceforge.net
    -- Stability   : provisional
    -- Portability : portable (depends on GHC)
    --
    -- Defines functions to extract data from a GString.
    --
    module System.Glib.GString (
      GString,
      readGString,
      readGStringByteString,
      fromGString,
      ) where

    import Foreign
    import Control.Exception        (bracket)
    import Control.Monad            (foldM)
    import Data.ByteString          (ByteString, packCStringLen)

    import System.Glib.FFI

    {-# LINE 41 "./System/Glib/GString.chs" #-}

    type GString = Ptr (())
    {-# LINE 43 "./System/Glib/GString.chs" #-}

    -- methods

    -- Turn a GString into a String but don't destroy it.
    --
    readGString :: GString -> IO (Maybe String)
    readGString :: GString -> IO (Maybe String)
    readGString GString
    gstring
      | GString
    gstring GString -> GString -> Bool
    forall a. Eq a => a -> a -> Bool
    == GString
    forall a. Ptr a
    nullPtr = Maybe String -> IO (Maybe String)
    forall a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Maybe String
    forall a. Maybe a
    Nothing
      | Bool
    otherwise          = do
        Ptr CChar
    gstr <- (\GString
    ptr -> do {GString -> Int -> IO (Ptr CChar)
    forall b. Ptr b -> Int -> IO (Ptr CChar)
    forall a b. Storable a => Ptr b -> Int -> IO a
    peekByteOff GString
    ptr Int
    0 ::IO (Ptr CChar)}) GString
    gstring
        CULong
    len <- (\GString
    ptr -> do {GString -> Int -> IO CULong
    forall b. Ptr b -> Int -> IO CULong
    forall a b. Storable a => Ptr b -> Int -> IO a
    peekByteOff GString
    ptr Int
    8 ::IO CULong}) GString
    gstring
        (String -> Maybe String) -> IO String -> IO (Maybe String)
    forall a b. (a -> b) -> IO a -> IO b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap String -> Maybe String
    forall a. a -> Maybe a
    Just (IO String -> IO (Maybe String)) -> IO String -> IO (Maybe String)
    forall a b. (a -> b) -> a -> b
    $ CStringLen -> IO String
    peekCStringLen (Ptr CChar
    gstr, CULong -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral CULong
    len)

    -- Turn a GString into a ByteString but don't destroy it.
    --
    readGStringByteString :: GString -> IO (Maybe ByteString)
    readGStringByteString :: GString -> IO (Maybe ByteString)
    readGStringByteString GString
    gstring
      | GString
    gstring GString -> GString -> Bool
    forall a. Eq a => a -> a -> Bool
    == GString
    forall a. Ptr a
    nullPtr = Maybe ByteString -> IO (Maybe ByteString)
    forall a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Maybe ByteString
    forall a. Maybe a
    Nothing
      | Bool
    otherwise          = do
        Ptr CChar
    gstr <- (\GString
    ptr -> do {GString -> Int -> IO (Ptr CChar)
    forall b. Ptr b -> Int -> IO (Ptr CChar)
    forall a b. Storable a => Ptr b -> Int -> IO a
    peekByteOff GString
    ptr Int
    0 ::IO (Ptr CChar)}) GString
    gstring
        CULong
    len <- (\GString
    ptr -> do {GString -> Int -> IO CULong
    forall b. Ptr b -> Int -> IO CULong
    forall a b. Storable a => Ptr b -> Int -> IO a
    peekByteOff GString
    ptr Int
    8 ::IO CULong}) GString
    gstring
        (ByteString -> Maybe ByteString)
    -> IO ByteString -> IO (Maybe ByteString)
    forall a b. (a -> b) -> IO a -> IO b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap ByteString -> Maybe ByteString
    forall a. a -> Maybe a
    Just (IO ByteString -> IO (Maybe ByteString))
    -> IO ByteString -> IO (Maybe ByteString)
    forall a b. (a -> b) -> a -> b
    $ CStringLen -> IO ByteString
    packCStringLen (Ptr CChar
    gstr, CULong -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral CULong
    len)

    -- Turn a GList into a list of pointers (freeing the GList in the process).
    --
    fromGString :: GString -> IO (Maybe String)
    fromGString :: GString -> IO (Maybe String)
    fromGString GString
    gstring
      | GString
    gstring GString -> GString -> Bool
    forall a. Eq a => a -> a -> Bool
    == GString
    forall a. Ptr a
    nullPtr = Maybe String -> IO (Maybe String)
    forall a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Maybe String
    forall a. Maybe a
    Nothing
      | Bool
    otherwise          = do
        Ptr CChar
    gstr <- (\GString
    ptr -> do {GString -> Int -> IO (Ptr CChar)
    forall b. Ptr b -> Int -> IO (Ptr CChar)
    forall a b. Storable a => Ptr b -> Int -> IO a
    peekByteOff GString
    ptr Int
    0 ::IO (Ptr CChar)}) GString
    gstring
        CULong
    len <- (\GString
    ptr -> do {GString -> Int -> IO CULong
    forall b. Ptr b -> Int -> IO CULong
    forall a b. Storable a => Ptr b -> Int -> IO a
    peekByteOff GString
    ptr Int
    8 ::IO CULong}) GString
    gstring
        Maybe String
    str <- (String -> Maybe String) -> IO String -> IO (Maybe String)
    forall a b. (a -> b) -> IO a -> IO b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap String -> Maybe String
    forall a. a -> Maybe a
    Just (IO String -> IO (Maybe String)) -> IO String -> IO (Maybe String)
    forall a b. (a -> b) -> a -> b
    $ CStringLen -> IO String
    peekCStringLen (Ptr CChar
    gstr, CULong -> Int
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral CULong
    len)
        Ptr CChar
    _ <- GString -> CInt -> IO (Ptr CChar)
    g_string_free GString
    gstring (CInt -> IO (Ptr CChar)) -> CInt -> IO (Ptr CChar)
    forall a b. (a -> b) -> a -> b
    $ Bool -> CInt
    forall a. Num a => Bool -> a
    fromBool Bool
    True
        Maybe String -> IO (Maybe String)
    forall a. a -> IO a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Maybe String
    str

    foreign import ccall unsafe "g_string_free"
      g_string_free :: ((Ptr ()) -> (CInt -> (IO (Ptr CChar))))