[2] | 1 | ;+ |
---|
[226] | 2 | ; |
---|
[150] | 3 | ; @file_comments |
---|
| 4 | ; Convert a structure to an "executable string" |
---|
[2] | 5 | ; |
---|
[150] | 6 | ; @categories |
---|
[157] | 7 | ; Utilities |
---|
[226] | 8 | ; |
---|
[150] | 9 | ; @param STRUCT {in}{required} |
---|
| 10 | ; A structure |
---|
[2] | 11 | ; |
---|
[163] | 12 | ; @keyword MAX_STRUCT_LENGTH {default=10000l} |
---|
[150] | 13 | ; The maximum length of the structure |
---|
[226] | 14 | ; permitted to convert the structure to string. |
---|
[2] | 15 | ; |
---|
[150] | 16 | ; @keyword DIRECT2STRING |
---|
| 17 | ; To get a string instead an "executable string" |
---|
[2] | 18 | ; |
---|
[150] | 19 | ; @keyword CUT_IN_STRING |
---|
| 20 | ; Try it |
---|
[2] | 21 | ; |
---|
[150] | 22 | ; @restrictions |
---|
[231] | 23 | ; Use <pro>tostr</pro>, cf this function header! |
---|
[2] | 24 | ; |
---|
[150] | 25 | ; @examples |
---|
[2] | 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' |
---|
[226] | 30 | ; ,40.0000,'Y_PX_CM',40.0000,'N_COLORS',16777216,'TABLE_SIZE' |
---|
[2] | 31 | ; ,256,'FILL_DIST',1,'WINDOW',32,'UNIT',0,'FLAGS',328124,'ORIGIN' |
---|
| 32 | ; ,[0,0],'ZOOM',[1,1]) |
---|
| 33 | ; |
---|
[150] | 34 | ; @history |
---|
[157] | 35 | ; Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
[2] | 36 | ; 2000 07 03 |
---|
[150] | 37 | ; |
---|
| 38 | ; @version |
---|
| 39 | ; $Id$ |
---|
| 40 | ; |
---|
[2] | 41 | ;- |
---|
[327] | 42 | FUNCTION struct2string, struct $ |
---|
| 43 | , CUT_IN_STRING=cut_in_string $ |
---|
| 44 | , MAX_STRUCT_LENGTH=max_struct_length $ |
---|
| 45 | , DIRECT2STRING=direct2string |
---|
[114] | 46 | ; |
---|
| 47 | compile_opt idl2, strictarrsubs |
---|
| 48 | ; |
---|
[2] | 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 |
---|
[226] | 64 | |
---|
[2] | 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 |
---|