简体   繁体   中英

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.

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...

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.

Note: You should probably need std::vector instead, read their documentation to decide.

This is a pure cen-xfs thing so having just C++ knowledge is not enough. All lpszExtra fields of XFS structures are special formatted C strings.

So the correct way to fill lpszExtra field of a xfs capabilities struct is to use double NULL terminated and NULL separated string. 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.

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.

That way you can use normal C string methods for manipulation in your own code.

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.

Note this is aplicable only for those lpszExtra fields in XFS structs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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