WSNewUnicodeContainer (C 函数)
WSUnicodeContainer * WSNewUnicodeContainer(void *s, int l, enum WSUnicodeContainerType t)
分配并返回一个新的 Unicode 集合,该集合包括长度为 l、字符串类型为 t 的 Unicode 字符串 s 的内容副本.
更多信息
- WSNewUnicodeContainer() 为储存必须释放的 Unicode 字符串 s 而分配内存. 为了释放这些内存,可用 WSNewUnicodeContainer() 返回的 WSUnicodeContainer * 对象来调用 WSReleaseUnicodeContainer().
- 若发生错误,则 WSNewUnicodeContainer() 会返回 NULL.
- 可使用 WSNewUnicodeContainer() 来分配 WSUnicodeContainer 对象,以便在 WSTP 模板文件里使用.
- WSNewUnicodeContainer() 可与 WSReleaseUnicodeContainer() 一起使用,以在 WSTP 模板文件中创建和破坏 WSUnicodeContainer 对象. WSUnicodeContainer 对于在模板文件的函数间传递 Unicode 字符串和其长度而言是一个合适的对象.
- WSTP 的标头文件 wstp.h 已对 WSNewUnicodeContainer() 作出声明.
范例
基本范例 (1)
#include "wstp.h"
/* A WSTP template program for converting a string to a symbol */
:Begin:
:Function: convertStringToSymbol
:Pattern: ConvertStringToSymbol[string_String]
:Arguments: {string}
:ArgumentTypes: {String}
:ReturnType: UTF8Symbol
:End:
WSUnicodeContainer * convertStringToSymbol(const char *s)
{
WSUnicodeContainer *newContainer;
WSLINK link;
int error;
unsigned char *utf8String;
int utf8Length, utf8Chars;
/* Use a loopback link to convert the string from Mathematica
form to a UTF-8 encoded version */
link = WSLoopbackOpen(stdenv, &error);
if(link == NULL || error != WSEOK)
{ /* Unable to create loopback link */ }
if(! WSPutString(link, s))
{ /* Unable to send the string to the loopback link */ }
if(! WSFlush(link))
[ /* Unable to flush the link buffers */ }
if(! WSGetUTF8String(link, &utf8String, &utf8Length, &utf8Chars))
{ /* Unable to read the UTF-8 encoded string */ }
/* The WSUnicodeContainer object is just for passing around
Unicode strings and their lengths in an easy manner. The
WSTP template code will call WSReleaseUnicodeContainer()
for the memory allocated by WSNewUnicodeContainer() */
/* UTF8ContainerType is the enum value that indicates the
WSUnicodeContainer object contains an UTF-8 encoded string */
newContainer = WSNewUnicodeContainer(utf8String, utf8Length,
UTF8ContainerType);
WSReleaseUTF8String(link, utf8String, utf8Length);
return newContainer;
}
int main(int argc, char **argv)
{
return WSMain(argc, argv);
}