;------------------------------------------------------------ ;------------------------------------------------------------ ;------------------------------------------------------------ ;+ ; NAME:struct2string ; ; PURPOSE:convert a structure to an "executable string" ; ; CATEGORY:bidouille ; ; CALLING SEQUENCE:sting=struct2string(struct) ; ; INPUTS:struct: a structure ; ; KEYWORD PARAMETERS: ; ; MAX_STRUCT_LENGTH : the maximum length of the structure ; permetted to convert the structure to string. Default is ; 10000l. ; ; /DIRECT2STRING: to get a string instead an "executable string" ; ; /CUT_IN_STRING: try it ; ; OUTPUTS: ; ; SIDE EFFECTS:use tostr.pro, cf this function header! ; ; RESTRICTIONS:use tostr.pro, cf this function header! ; ; EXAMPLE: ; ; IDL> print, struct2string(!d) ; create_struct('NAME','X','X_SIZE',891,'Y_SIZE',630,'X_VSIZE' ; ,891,'Y_VSIZE',630,'X_CH_SIZE',6,'Y_CH_SIZE',10,'X_PX_CM' ; ,40.0000,'Y_PX_CM',40.0000,'N_COLORS',16777216,'TABLE_SIZE' ; ,256,'FILL_DIST',1,'WINDOW',32,'UNIT',0,'FLAGS',328124,'ORIGIN' ; ,[0,0],'ZOOM',[1,1]) ; ; MODIFICATION HISTORY:Sebastien Masson (smasson@lodyc.jussieu.fr) ; 2000 07 03 ;- ;------------------------------------------------------------ ;------------------------------------------------------------ ;------------------------------------------------------------ FUNCTION struct2string, struct, CUT_IN_STRING = cut_in_string, MAX_STRUCT_LENGTH = max_struct_length, DIRECT2STRING = direct2string if size(struct, /type) NE 8 then return, '' if NOT keyword_set(max_struct_length) then max_struct_length = 10000l if n_tags(struct, /length) GT max_struct_length then begin rien = report('The structure is too big to be converted to string! !C See the MAX_STRUCT_LENGTH keyword') return, '' endif names = tag_names(struct) case 1 of keyword_set(direct2string):BEGIN res = names[0]+'='+tostr(struct.(0)) if n_tags(struct) GT 1 then begin FOR i = 1, n_tags(struct)-1 do begin res = res+', '+names[i]+'='+tostr(struct.(i)) endfor endif END keyword_set(CUT_IN_STRING):BEGIN res = 'create_struct('''+names[0]+''','+tostr(struct.(0))+')' if n_tags(struct) GT 1 then begin FOR i = 1, n_tags(struct)-1 do begin res = [res, 'create_struct(res,'''+names[i]+''','+tostr(struct.(i))+')'] endfor endif END ELSE:BEGIN res = 'create_struct('''+names[0]+''','+tostr(struct.(0)) if n_tags(struct) GT 1 then begin FOR i = 1, n_tags(struct)-1 do begin res = res+','''+names[i]+''','+tostr(struct.(i)) endfor endif res = res+')' END endcase return, res end