source: trunk/STRUCTURE/struct2string.pro @ 2

Last change on this file since 2 was 2, checked in by opalod, 22 years ago

Initial revision

  • 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   if size(struct, /type) NE 8 then return,  ''
48   if NOT keyword_set(max_struct_length) then max_struct_length = 10000l
49   if n_tags(struct, /length) GT max_struct_length then begin
50      rien = report('The structure is too big to be converted to string! !C See the MAX_STRUCT_LENGTH keyword')
51      return, ''
52   endif
53   names = tag_names(struct)
54   case 1 of
55      keyword_set(direct2string):BEGIN
56         res = names[0]+'='+tostr(struct.(0))
57         if n_tags(struct) GT 1 then begin
58            FOR i = 1, n_tags(struct)-1 do begin
59               res = res+', '+names[i]+'='+tostr(struct.(i))
60            endfor
61         endif
62         
63      END
64      keyword_set(CUT_IN_STRING):BEGIN
65         res = 'create_struct('''+names[0]+''','+tostr(struct.(0))+')'
66         if n_tags(struct) GT 1 then begin
67            FOR i = 1, n_tags(struct)-1 do begin
68               res = [res, 'create_struct(res,'''+names[i]+''','+tostr(struct.(i))+')']
69            endfor
70         endif
71      END
72      ELSE:BEGIN
73         res = 'create_struct('''+names[0]+''','+tostr(struct.(0))
74         if n_tags(struct) GT 1 then begin
75            FOR i = 1, n_tags(struct)-1 do begin
76               res = res+','''+names[i]+''','+tostr(struct.(i))
77            endfor
78         endif
79         res = res+')'
80      END
81   endcase
82   return, res
83end
Note: See TracBrowser for help on using the repository browser.