<commit_before>{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE BangPatterns #-}
module GDAL.Internal.CPLString (
    OptionList
  , withOptionList
  , toOptionListPtr
  , fromOptionListPtr
  , peekCPLString
) where

import Control.Exception (bracket, bracketOnError)
import Control.Monad (forM, foldM, liftM, when, void)

import Data.ByteString.Internal (ByteString(..))
import Data.Text (Text)
import qualified Data.Text as T
import Data.Word (Word8)

import Foreign.C.String (CString)
import Foreign.C.Types (CInt(..), CChar(..))
import Foreign.Ptr (Ptr, castPtr, nullPtr)
import Foreign.Storable (peek, peekElemOff)
import Foreign.ForeignPtr (newForeignPtr)
import Foreign.Marshal.Utils (with)

import GDAL.Internal.Util (useAsEncodedCString, peekEncodedCString)
import GDAL.Internal.CPLConv (c_cplFree, cplFree)

#include "cpl_string.h"

type OptionList = [(Text,Text)]

withOptionList :: OptionList -> (Ptr CString -> IO c) -> IO c
withOptionList opts = bracket (toOptionListPtr opts) freeOptionList
  where freeOptionList = {#call unsafe CSLDestroy as ^#} . castPtr

toOptionListPtr :: OptionList -> IO (Ptr CString)
toOptionListPtr = foldM folder nullPtr
  where
    folder acc (k,v) =
      useAsEncodedCString k $ \k' ->
      useAsEncodedCString v $ \v' ->
        {#call unsafe CSLSetNameValue as ^#} acc k' v'<commit_msg>fix for 7.8.4<commit_after>{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE BangPatterns #-}
module GDAL.Internal.CPLString (
    OptionList
  , withOptionList
  , toOptionListPtr
  , fromOptionListPtr
  , peekCPLString
) where

import Control.Exception (bracket, bracketOnError)
import Control.Monad (forM, foldM, liftM, when, void)

import Data.ByteString.Internal (ByteString(..))
import Data.Monoid (mempty)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Word (Word8)

import Foreign.C.String (CString)
import Foreign.C.Types (CInt(..), CChar(..))
import Foreign.Ptr (Ptr, castPtr, nullPtr)
import Foreign.Storable (peek, peekElemOff)
import Foreign.ForeignPtr (newForeignPtr)
import Foreign.Marshal.Utils (with)

import GDAL.Internal.Util (useAsEncodedCString, peekEncodedCString)
import GDAL.Internal.CPLConv (c_cplFree, cplFree)

#include "cpl_string.h"

type OptionList = [(Text,Text)]

withOptionList :: OptionList -> (Ptr CString -> IO c) -> IO c
withOptionList opts = bracket (toOptionListPtr opts) freeOptionList
  where freeOptionList = {#call unsafe CSLDestroy as ^#} . castPtr

toOptionListPtr :: OptionList -> IO (Ptr CString)
toOptionListPtr = foldM folder nullPtr
  where
    folder acc (k,v) =
      useAsEncodedCString k $ \k' ->
      useAsEncodedCString v $ \v' ->
        {#call unsafe CSLSetNameValue as ^#} acc k' v'
{'fraction_non_alphanumeric': 0.11262924667651403, 'fraction_numerical': 0.001846381093057607, 'mean_word_length': 4.632016632016632, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2863450', 'n_tokens_mistral': 918, 'n_tokens_neox': 900, 'n_words': 244}