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

Last change on this file since 325 was 325, checked in by pinsard, 17 years ago

modification of some headers (+some corrections) to prepare usage of the new idldoc

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