第十章 创建Callout Library - 使用 B 链接类型传递短计数字符串
使用 B 链接类型传递短计数字符串
iris-cdzf.h Callout 头文件定义了计数字符串结构 ZARRAY、ZWARRAY 和 ZHARRAY,表示短字符串(InterSystems 旧字符串类型)。这些结构包含一个字符元素数组(分别为 8 位、16 位 Unicode 或 32 位 wchar t)和一个指定数组中元素数量的短整数(最大值 32,768)。例如:
typedef struct zarray {
unsigned short len;
unsigned char data[1]; /* 1 is a dummy value */
} *ZARRAYP;
-
len— 包含数组的长度 -
data— 是包含字符数据的数组。ZARRAY的元素类型为unsignedchar,ZWARRAY的元素类型为unsignedshort,ZHARRAY的元素类型为wchar_t。
B 链接指定指针类型 ZARRAYP、ZWARRAYP 和 ZHARRAYP,对应于三个数组结构。返回的数组的最大大小为 32,767 个字符。
| C Datatype | Input | In/Out | Notes |
|---|---|---|---|
ZARRAYP |
1b or b |
1B or B |
最多包含 32,767 个 8 位字符的国家短字符串。 |
ZWARRAYP |
2b or s |
2B or S |
最多包含 32,767 个 16 位字符的短 Unicode 字符串。 |
ZHARRAYP |
4b |
4B |
最多包含 32,767 个 wchar_t 字符的短 Unicode 字符串。 |
参数的最大总长度取决于每个字符的字节数(请参阅“配置 $ZF 堆”)。
这是一个 Callout 库,它使用所有三种链接类型来返回数字字符串:
使用 B 连接传递计数字符串
以下三个函数均生成一个随机整数,将其转换为最多包含 6 位数字的数字字符串,并使用 B 链接返回字符串 。
#define ZF_DLL // Required when creating a Callout library.
#include <iris-cdzf.h>
#include <stdio.h>
#include <wchar.h> // Required for 16-bit and 16-bit characters
int get_sample_Z(ZARRAYP retval) { // 8-bit, counted
unsigned char numstr[6];
sprintf(numstr,"%d",(rand()%1000000));
retval->len = strlen(numstr);
memcpy(retval->data,numstr,retval->len);
return ZF_SUCCESS;
}
int get_sample_ZW(ZWARRAYP retval) { // 16-bit, counted
unsigned short numstr[6];
swprintf(numstr,6,L"%d",(rand()%1000000));
retval->len = wcslen(numstr);
memcpy(retval->data,numstr,(retval->len*sizeof(unsigned short)));
return ZF_SUCCESS;
}
int get_sample_ZH(ZHARRAYP retval) { // 32-bit, counted
wchar_t numstr[6];
swprintf(numstr,6,L"%d",(rand()%1000000));
retval->len = wcslen(numstr);
memcpy(retval->data,numstr,(retval->len*sizeof(wchar_t)));
return ZF_SUCCESS;
}
ZFBEGIN
ZFENTRY("GetSampleZ","1B",get_sample_Z)
ZFENTRY("GetSampleZW","2B",get_sample_ZW)
ZFENTRY("GetSampleZH","4B",get_sample_ZH)
ZFEND
注意:逗号在包含多个值的输出参数字符串中用作分隔符。由于逗号也可以是计数字符串数组的一部分,因此请在参数列表的末尾声明这些数组,并在每次调用时使用一个数组。