base-4.7.0.0: Basic libraries

Copyright(c) The FFI task force 2001
LicenseBSD-style (see the file libraries/base/LICENSE)
Maintainerffi@haskell.org
Stabilityprovisional
Portabilityportable
Safe HaskellTrustworthy
LanguageHaskell2010

Foreign.C.String

Contents

Description

Utilities for primitive marshalling of C strings.

The marshalling converts each Haskell character, representing a Unicode code point, to one or more bytes in a manner that, by default, is determined by the current locale. As a consequence, no guarantees can be made about the relative length of a Haskell string and its corresponding C string, and therefore all the marshalling routines include memory allocation. The translation between Unicode and the encoding of the current locale may be lossy.

Synopsis

C strings

type CString = Ptr CChar Source

A C string is a reference to an array of C characters terminated by NUL.

type CStringLen = (Ptr CChar, Int) Source

A string with explicit length information in bytes instead of a terminating NUL (allowing NUL characters in the middle of the string).

Using a locale-dependent encoding

These functions are different from their CAString counterparts in that they will use an encoding determined by the current locale, rather than always assuming ASCII.

peekCStringCStringIO String Source

Marshal a NUL terminated C string into a Haskell string.

peekCStringLenCStringLenIO String Source

Marshal a C string with explicit length into a Haskell string.

newCStringStringIO CString Source

Marshal a Haskell string into a NUL terminated C string.

  • the Haskell string may not contain any NUL characters
  • new storage is allocated for the C string and must be explicitly freed using free or finalizerFree.

newCStringLenStringIO CStringLen Source

Marshal a Haskell string into a C string (ie, character array) with explicit length information.

  • new storage is allocated for the C string and must be explicitly freed using free or finalizerFree.

withCStringString → (CStringIO a) → IO a Source

Marshal a Haskell string into a NUL terminated C string using temporary storage.

  • the Haskell string may not contain any NUL characters
  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.

withCStringLenString → (CStringLenIO a) → IO a Source

Marshal a Haskell string into a C string (ie, character array) in temporary storage, with explicit length information.

  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.

Using 8-bit characters

These variants of the above functions are for use with C libraries that are ignorant of Unicode. These functions should be used with care, as a loss of information can occur.

castCharToCCharCharCChar Source

Convert a Haskell character to a C character. This function is only safe on the first 256 characters.

castCCharToCharCCharChar Source

Convert a C byte, representing a Latin-1 character, to the corresponding Haskell character.

castCharToCUCharCharCUChar Source

Convert a Haskell character to a C unsigned char. This function is only safe on the first 256 characters.

castCUCharToCharCUCharChar Source

Convert a C unsigned char, representing a Latin-1 character, to the corresponding Haskell character.

castCharToCSCharCharCSChar Source

Convert a Haskell character to a C signed char. This function is only safe on the first 256 characters.

castCSCharToCharCSCharChar Source

Convert a C signed char, representing a Latin-1 character, to the corresponding Haskell character.

peekCAStringCStringIO String Source

Marshal a NUL terminated C string into a Haskell string.

peekCAStringLenCStringLenIO String Source

Marshal a C string with explicit length into a Haskell string.

newCAStringStringIO CString Source

Marshal a Haskell string into a NUL terminated C string.

  • the Haskell string may not contain any NUL characters
  • new storage is allocated for the C string and must be explicitly freed using free or finalizerFree.

newCAStringLenStringIO CStringLen Source

Marshal a Haskell string into a C string (ie, character array) with explicit length information.

  • new storage is allocated for the C string and must be explicitly freed using free or finalizerFree.

withCAStringString → (CStringIO a) → IO a Source

Marshal a Haskell string into a NUL terminated C string using temporary storage.

  • the Haskell string may not contain any NUL characters
  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.

withCAStringLenString → (CStringLenIO a) → IO a Source

Marshal a Haskell string into a C string (ie, character array) in temporary storage, with explicit length information.

  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.

C wide strings

These variants of the above functions are for use with C libraries that encode Unicode using the C wchar_t type in a system-dependent way. The only encodings supported are

  • UTF-32 (the C compiler defines 201103L), or
  • UTF-16 (as used on Windows systems).

type CWString = Ptr CWchar Source

A C wide string is a reference to an array of C wide characters terminated by NUL.

type CWStringLen = (Ptr CWchar, Int) Source

A wide character string with explicit length information in CWchars instead of a terminating NUL (allowing NUL characters in the middle of the string).

peekCWStringCWStringIO String Source

Marshal a NUL terminated C wide string into a Haskell string.

peekCWStringLenCWStringLenIO String Source

Marshal a C wide string with explicit length into a Haskell string.

newCWStringStringIO CWString Source

Marshal a Haskell string into a NUL terminated C wide string.

  • the Haskell string may not contain any NUL characters
  • new storage is allocated for the C wide string and must be explicitly freed using free or finalizerFree.

newCWStringLenStringIO CWStringLen Source

Marshal a Haskell string into a C wide string (ie, wide character array) with explicit length information.

  • new storage is allocated for the C wide string and must be explicitly freed using free or finalizerFree.

withCWStringString → (CWStringIO a) → IO a Source

Marshal a Haskell string into a NUL terminated C wide string using temporary storage.

  • the Haskell string may not contain any NUL characters
  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.

withCWStringLenString → (CWStringLenIO a) → IO a Source

Marshal a Haskell string into a C wide string (i.e. wide character array) in temporary storage, with explicit length information.

  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.