简体   繁体   English

在C ++中将列表作为属性传递

[英]Passing a list as an attribute in C ++

I need to create a list of LPSTR and put this in a LPSTR attribute of a struct. 我需要创建一个LPSTR列表并将其放在struct的LPSTR属性中。

typedef struct _wfs_pin_caps
{
...
LPSTR               lpszExtra; //This attribute should receive
} WFSPINCAPS, * LPWFSPINCAPS;

I need something of this kind. 我需要这种东西。

WFSPINCAPS PinCapabilities;

list<LPSTR> Keys;
Keys[0] = (LPSTR) "value=key";
Keys[1] = (LPSTR) "value1=key1";
Keys[2] = (LPSTR) "value2=key2";

PinCapabilities.lpszExtra = Keys;

I need to pass lists with VARIOUS values... 我需要传递具有VARIOUS值的列表......

It's simple, just do this 这很简单,就这样做

struct _wfs_pin_caps {
    // ... other fields ...
    std::list<const LPSTR> lpszExtra;
};
list<const LPSTR> &extra(PinCapabilities.lpszExtra);
extra.push_back(TEXT("value1=key1"));
extra.push_back(TEXT("value2=key2"));
// ... more items ...
extra.push_back(TEXT("valueN=keyN"));

Read about The TEXT macro so you don't do that awkward cast at all, which is wrong BTW. 阅读有关TEXT宏的内容,这样你根本不会做那个笨拙的演员,这是错误的BTW。

Note: You should probably need std::vector instead, read their documentation to decide. 注意:你可能应该需要std::vector ,阅读他们的文档来决定。

This is a pure cen-xfs thing so having just C++ knowledge is not enough. 这是一个纯粹的cen-xfs,所以只有C ++知识是不够的。 All lpszExtra fields of XFS structures are special formatted C strings. XFS结构的所有lpszExtra字段都是特殊格式的C字符串。

So the correct way to fill lpszExtra field of a xfs capabilities struct is to use double NULL terminated and NULL separated string. 因此,填充xfs功能结构的lpszExtra字段的正确方法是使用双NULL终止和NULL分隔的字符串。 And as all of these fields are key value pairs so the format is: "key1=value1\\0key2=value2\\0...\\0keyN=valueN\\0\\0" Note here "keyX" does NOT mean PINPAD key definition, but the way all XFS lpszExtra field data is formatted so first key_name =-sign key_value. 因为所有这些字段都是键值对,所以格式为:“key1 = value1 \\ 0key2 = value2 \\ 0 ... \\ 0keyN = valueN \\ 0 \\ 0”注意这里“keyX”并不意味着PINPAD键定义,但所有XFS lpszExtra字段数据的格式化方式首先是key_name = -sign key_value。

How you handle those strings are up to you, but I like to use normal newlines instead of '\\0' as key-value pair separators and then just convert to/from that to the XFS specific NULL char separated and double NULL terminated format. 你如何处理这些字符串取决于你,但我喜欢使用普通的换行符而不是'\\ 0'作为键值对分隔符然后只是转换为/从它转换为XFS特定的NULL char分隔和双NULL终止格式。

That way you can use normal C string methods for manipulation in your own code. 这样,您可以使用普通的C字符串方法在您自己的代码中进行操作。

Easy conversion is allocate memory, make copy and swap '\\n' to '\\0' and '\\0' to "\\0\\0" while copying from C string to XFS and reverse when converting from XFS to C string. 轻松转换是分配内存,复制和交换'\\ n'到'\\ 0'和'\\ 0'到“\\ 0 \\ 0”,同时从C字符串复制到XFS,当从XFS转换为C字符串时反向。

Note this is aplicable only for those lpszExtra fields in XFS structs. 请注意,这仅适用于XFS结构中的lpszExtra字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM