source: trunk/SRC/ToBeReviewed/STRUCTURE/struct2string.pro @ 114

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.8 KB
Line 
1;------------------------------------------------------------
2;------------------------------------------------------------
3;------------------------------------------------------------
4;+
5; NAME:struct2string
6;
7; PURPOSE:convert a structure to an "executable string"
8;
9; CATEGORY:bidouille
10;
11; CALLING SEQUENCE:sting=struct2string(struct)
12;
13; INPUTS:struct: a structure
14;
15; KEYWORD PARAMETERS:
16;
17;      MAX_STRUCT_LENGTH : the maximum length of the structure
18;      permetted to convert the structure to string. Default is
19;      10000l.
20;
21;      /DIRECT2STRING: to get a string instead an "executable string"
22;
23;      /CUT_IN_STRING: try it
24;
25; OUTPUTS:
26;
27; SIDE EFFECTS:use tostr.pro, cf this function header!
28;
29; RESTRICTIONS:use tostr.pro, cf this function header!
30;
31; EXAMPLE:
32;
33;      IDL> print, struct2string(!d)
34;      create_struct('NAME','X','X_SIZE',891,'Y_SIZE',630,'X_VSIZE'
35;      ,891,'Y_VSIZE',630,'X_CH_SIZE',6,'Y_CH_SIZE',10,'X_PX_CM'
36;      ,40.0000,'Y_PX_CM',40.0000,'N_COLORS',16777216,'TABLE_SIZE'
37;      ,256,'FILL_DIST',1,'WINDOW',32,'UNIT',0,'FLAGS',328124,'ORIGIN'
38;      ,[0,0],'ZOOM',[1,1])
39;
40; MODIFICATION HISTORY:Sebastien Masson (smasson@lodyc.jussieu.fr)
41;                      2000 07 03
42;-
43;------------------------------------------------------------
44;------------------------------------------------------------
45;------------------------------------------------------------
46FUNCTION struct2string, struct, CUT_IN_STRING = cut_in_string, MAX_STRUCT_LENGTH = max_struct_length, DIRECT2STRING = direct2string
47;
48  compile_opt idl2, strictarrsubs
49;
50   if size(struct, /type) NE 8 then return,  ''
51   if NOT keyword_set(max_struct_length) then max_struct_length = 10000l
52   if n_tags(struct, /length) GT max_struct_length then begin
53      rien = report('The structure is too big to be converted to string! !C See the MAX_STRUCT_LENGTH keyword')
54      return, ''
55   endif
56   names = tag_names(struct)
57   case 1 of
58      keyword_set(direct2string):BEGIN
59         res = names[0]+'='+tostr(struct.(0))
60         if n_tags(struct) GT 1 then begin
61            FOR i = 1, n_tags(struct)-1 do begin
62               res = res+', '+names[i]+'='+tostr(struct.(i))
63            endfor
64         endif
65         
66      END
67      keyword_set(CUT_IN_STRING):BEGIN
68         res = 'create_struct('''+names[0]+''','+tostr(struct.(0))+')'
69         if n_tags(struct) GT 1 then begin
70            FOR i = 1, n_tags(struct)-1 do begin
71               res = [res, 'create_struct(res,'''+names[i]+''','+tostr(struct.(i))+')']
72            endfor
73         endif
74      END
75      ELSE:BEGIN
76         res = 'create_struct('''+names[0]+''','+tostr(struct.(0))
77         if n_tags(struct) GT 1 then begin
78            FOR i = 1, n_tags(struct)-1 do begin
79               res = res+','''+names[i]+''','+tostr(struct.(i))
80            endfor
81         endif
82         res = res+')'
83      END
84   endcase
85   return, res
86end
Note: See TracBrowser for help on using the repository browser.