1 | ;+ |
---|
2 | ; |
---|
3 | ; @file_comments |
---|
4 | ; Convert a structure to an "executable string" |
---|
5 | ; |
---|
6 | ; @categories |
---|
7 | ; Utilities |
---|
8 | ; |
---|
9 | ; @param STRUCT {in}{required} |
---|
10 | ; A structure |
---|
11 | ; |
---|
12 | ; @keyword MAX_STRUCT_LENGTH {default=10000l} |
---|
13 | ; The maximum length of the structure |
---|
14 | ; permitted to convert the structure to string. |
---|
15 | ; |
---|
16 | ; @keyword DIRECT2STRING |
---|
17 | ; To get a string instead an "executable string" |
---|
18 | ; |
---|
19 | ; @keyword CUT_IN_STRING |
---|
20 | ; Try it |
---|
21 | ; |
---|
22 | ; @restrictions |
---|
23 | ; Use <pro>tostr</pro>, cf this function header! |
---|
24 | ; |
---|
25 | ; @examples |
---|
26 | ; |
---|
27 | ; IDL> print, struct2string(!d) |
---|
28 | ; create_struct('NAME','X','X_SIZE',891,'Y_SIZE',630,'X_VSIZE' |
---|
29 | ; ,891,'Y_VSIZE',630,'X_CH_SIZE',6,'Y_CH_SIZE',10,'X_PX_CM' |
---|
30 | ; ,40.0000,'Y_PX_CM',40.0000,'N_COLORS',16777216,'TABLE_SIZE' |
---|
31 | ; ,256,'FILL_DIST',1,'WINDOW',32,'UNIT',0,'FLAGS',328124,'ORIGIN' |
---|
32 | ; ,[0,0],'ZOOM',[1,1]) |
---|
33 | ; |
---|
34 | ; @history |
---|
35 | ; Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
36 | ; 2000 07 03 |
---|
37 | ; |
---|
38 | ; @version |
---|
39 | ; $Id$ |
---|
40 | ; |
---|
41 | ;- |
---|
42 | FUNCTION struct2string, struct $ |
---|
43 | , CUT_IN_STRING=cut_in_string $ |
---|
44 | , MAX_STRUCT_LENGTH=max_struct_length $ |
---|
45 | , DIRECT2STRING=direct2string |
---|
46 | ; |
---|
47 | compile_opt idl2, strictarrsubs |
---|
48 | ; |
---|
49 | if size(struct, /type) NE 8 then return, '' |
---|
50 | if NOT keyword_set(max_struct_length) then max_struct_length = 10000l |
---|
51 | if n_tags(struct, /length) GT max_struct_length then begin |
---|
52 | rien = report('The structure is too big to be converted to string! !C See the MAX_STRUCT_LENGTH keyword') |
---|
53 | return, '' |
---|
54 | endif |
---|
55 | names = tag_names(struct) |
---|
56 | case 1 of |
---|
57 | keyword_set(direct2string):BEGIN |
---|
58 | res = names[0]+'='+tostr(struct.(0)) |
---|
59 | if n_tags(struct) GT 1 then begin |
---|
60 | FOR i = 1, n_tags(struct)-1 do begin |
---|
61 | res = res+', '+names[i]+'='+tostr(struct.(i)) |
---|
62 | endfor |
---|
63 | endif |
---|
64 | |
---|
65 | END |
---|
66 | keyword_set(CUT_IN_STRING):BEGIN |
---|
67 | res = 'create_struct('''+names[0]+''','+tostr(struct.(0))+')' |
---|
68 | if n_tags(struct) GT 1 then begin |
---|
69 | FOR i = 1, n_tags(struct)-1 do begin |
---|
70 | res = [res, 'create_struct(res,'''+names[i]+''','+tostr(struct.(i))+')'] |
---|
71 | endfor |
---|
72 | endif |
---|
73 | END |
---|
74 | ELSE:BEGIN |
---|
75 | res = 'create_struct('''+names[0]+''','+tostr(struct.(0)) |
---|
76 | if n_tags(struct) GT 1 then begin |
---|
77 | FOR i = 1, n_tags(struct)-1 do begin |
---|
78 | res = res+','''+names[i]+''','+tostr(struct.(i)) |
---|
79 | endfor |
---|
80 | endif |
---|
81 | res = res+')' |
---|
82 | END |
---|
83 | endcase |
---|
84 | return, res |
---|
85 | end |
---|