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

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

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

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