Convert variable of a user-defined type to/from string

Syntax

s.getstruct(var [flags])
s.setstruct(var [flags2])

 

Parameters

s - str variable.

var - variable of a user-defined type.

flags:

1

Add member names. If this flag not used, the string will contain only values.

 

flags2:

1

Use this flag if s includes member names. When using this flag, the order of members in s is not important. Some members in s may be missing. Some lines in s may be empty or contain any data. This is useful when the string may be edited by the user. If this flag not used, the string must contain only values, exactly as generated by getstruct without flag 1.

2 Member names in the string don't have to match case.
4 Don't clear var members that are missing in s.
8 Error if some members are missing in s.

 

Remarks

getstruct converts and stores values of all members of a variable of a user-defined type (var) into a single string (s).

setstruct extracts values from s (which is previously generated by getstruct or other code) and populates var.

 

For example, if var is of a type that has two int members x and y, an example of s may be:

 

10
20

 

If using flag 1, it will be:

 

x 10
y 20

 

Member name and value are separated by one or more spaces.

 

The variable also can contain strings. For example, if var is of a type that has four str members s1, s2, s3 and s4, an example of s may be:

 

s1 string1
s2 "string that contains spaces[]or ''escape sequences''"
s3
s4 ""

 

If a string contains spaces or characters that must be escaped, in s it is or must be escaped and enclosed in double quotes, like s2 in the example. Otherwise enclosing is not necessary. setstruct does not unescape strings that are not enclosed. Empty strings are like s3 in the example. Zero length strings are like s4 in the example.

 

Embedded arrays look like:

 

array_of_int_word_byte_long_double 2 -5 184 0
array_of_byte_also_can_be "string"
array_of_str[0] string1
array_of_str[1] string2
array_of_POINT[0].x 10
array_of_POINT[0].y 20
array_of_POINT[1].x 30
array_of_POINT[1].y 40

 

If an int/word/byte member name contains "flags", getstruct formats its value in hexadecimal format.

 

The functions are slightly slower when using flag 1.

 

The string generated by getstruct with flag 1 can be passed to AddList function of IStrinMap. Don't forget to remove quotes and unescape enclosed values.

 

Not all types are supported. Restrictions:

  1. var cannot be of an intrinsic type (int, byte, word, str, lpstr, long, double), pointer, interface pointer
  2. var cannot contain lpstr. Cannot be or contain ARRAY, BSTR, VARIANT, pointer, interface pointer.

QM 2.3.2. The restriction 2 is not applied to getstruct. It allows you to convert these types for debugging purposes.

 

Added in QM 2.2.1.

 

Example

out
type T1 byte'x word'y str's
type T2 int'i str's T1't double'd long'k byte'b[2]

T2 t tt
t.i=-5
t.s="''string''[]line2"
t.t.x=200
t.t.y=50000
t.t.s="string2"
t.d=1.55
t.k=0x100000000
t.b[0]=3
t.b[1]=4

str s
s.getstruct(t 1)
out s
out "---"
s.setstruct(tt 1)

out tt.i
out tt.s
out tt.t.x
out tt.t.y
out tt.t.s
out tt.d
out tt.k
out tt.b[0]
out tt.b[1]

 Output:
 i -5
 s "''string''[]line2"
 t.x 200
 t.y 50000
 t.s string2
 d 1.55
 k 4294967296
 b[0] 3
 b[1] 4
 
 ---
 -5
 "string"
 line2
 200
 50000
 string2
 1.55
 4294967296
 3
 4