[2] | 1 | ; |
---|
| 2 | ; This is a really, really cool way to turn keywords into |
---|
| 3 | ; a structure. |
---|
| 4 | ; |
---|
[97] | 5 | function too_cool,_extra=extra |
---|
[114] | 6 | ; |
---|
| 7 | compile_opt idl2, strictarrsubs |
---|
| 8 | ; |
---|
[2] | 9 | return,extra |
---|
| 10 | end |
---|
| 11 | |
---|
| 12 | ;+ |
---|
| 13 | ; NAME: |
---|
| 14 | ; stringToStructure |
---|
| 15 | ; |
---|
| 16 | ; PURPOSE: |
---|
| 17 | ; Takes an input string set up as keywords and returns an anonymous structure. |
---|
| 18 | ; This is particularly useful for taking keywords entered by a user in a text |
---|
| 19 | ; field and passing then to other routines. |
---|
| 20 | ; |
---|
| 21 | ; CATEGORY: |
---|
| 22 | ; Utility |
---|
| 23 | ; |
---|
| 24 | ; CALLING SEQUENCE: |
---|
| 25 | ; extra=stringToStructure('xrange=[0,10],linestyle=2') |
---|
| 26 | ; plot,findgen(100),_extra=extra |
---|
| 27 | ; |
---|
| 28 | ; INPUTS: |
---|
| 29 | ; String set up as keywords. Keywords require a little special treatment. Such as |
---|
| 30 | ; plot,findgen(100),_extra=stringToStructure('title="testing"') |
---|
| 31 | ; |
---|
| 32 | ; KEYWORD PARAMETERS: |
---|
| 33 | ; |
---|
| 34 | ; None |
---|
| 35 | ; |
---|
| 36 | ; OUTPUTS: |
---|
| 37 | ; This function returns the string as an anonymous structure. If an |
---|
| 38 | ; error was found then this function returns a structure with a null field. |
---|
| 39 | ; |
---|
| 40 | ; COMMON BLOCKS: |
---|
| 41 | ; None. |
---|
| 42 | ; |
---|
| 43 | ; EXAMPLE: |
---|
| 44 | ; The code below creates a widget that uses this routine. |
---|
| 45 | ; pro tPlot,event |
---|
| 46 | ; widget_control,event.top,get_uvalue=field |
---|
| 47 | ; widget_control,field,get_value=strVal |
---|
| 48 | ; extra = stringToStructure(strVal) |
---|
| 49 | ; plot,findgen(100),_extra=extra |
---|
| 50 | ; wshow |
---|
| 51 | ; return |
---|
| 52 | ; end |
---|
| 53 | ; |
---|
| 54 | ; pro testWid |
---|
| 55 | ; ;enter any keyword to plot and see how it works |
---|
| 56 | ; base = widget_base(/col) |
---|
| 57 | ; field = cw_field(base,title='test',value='ax=0',/string) |
---|
| 58 | ; void = widget_button(base,value='plot',event_pro='tPlot') |
---|
| 59 | ; widget_control,base,/realize,set_uvalue=field |
---|
| 60 | ; xmanager,'testWid',base,/no_block |
---|
| 61 | ; return |
---|
| 62 | ; end |
---|
| 63 | ; |
---|
| 64 | ; MODIFICATION HISTORY: |
---|
| 65 | ; Written by: |
---|
| 66 | ; RLK, Ronn Kling Consulting. |
---|
| 67 | ; ronn@rlkling.com |
---|
| 68 | ; www.rlkling.com |
---|
| 69 | ; May, 1999 |
---|
| 70 | ;- |
---|
| 71 | |
---|
[97] | 72 | function string2struct,strVal |
---|
[114] | 73 | ; |
---|
| 74 | compile_opt idl2, strictarrsubs |
---|
| 75 | ; |
---|
[2] | 76 | r = execute('extra = too_cool(' + strVal[0] +')') |
---|
| 77 | ;if r = 0 then user did not enter keywords correctly so |
---|
| 78 | ;return a structure with a null field. |
---|
| 79 | if r eq 0 then begin |
---|
| 80 | print,'Error in input string' |
---|
| 81 | return,{null:0} |
---|
| 82 | endif |
---|
| 83 | return,extra |
---|
| 84 | end |
---|
| 85 | |
---|