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

Last change on this file since 150 was 150, checked in by navarro, 18 years ago

english and nicer header (3a)

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