[2] | 1 | ;+ |
---|
| 2 | ; |
---|
[142] | 3 | ; @file_comments |
---|
[238] | 4 | ; In a string containing an order to execute with EXECUTE by example. |
---|
[142] | 5 | ; We change the value of one of keywords. |
---|
[2] | 6 | ; |
---|
[495] | 7 | ; More generally, in a string, we look for the character |
---|
[372] | 8 | ; chain: ', keywdname= ..., and we change the value of... |
---|
| 9 | ; |
---|
[142] | 10 | ; @categories |
---|
[157] | 11 | ; String, keywords |
---|
[2] | 12 | ; |
---|
[163] | 13 | ; @param STRINGIN {in}{required}{type=string} |
---|
[142] | 14 | ; it is a string |
---|
[2] | 15 | ; |
---|
[163] | 16 | ; @param KEYWDNAME {in}{required}{type=string} |
---|
[142] | 17 | ; it is a string designating the name of keyword to look for. |
---|
[2] | 18 | ; |
---|
[238] | 19 | ; @param KEYWDVALUE {in}{required} |
---|
[142] | 20 | ; The new value of the keyword to considerate in STRINGIN |
---|
[2] | 21 | ; |
---|
[142] | 22 | ; @keyword SEPARATOR |
---|
[238] | 23 | ; To look for the keyword, we look for the first sign = which follow |
---|
| 24 | ; the position of keywdname. By default, we substitute the string |
---|
| 25 | ; before the comma. With the keyword SEPARATOR,we can modify the cut |
---|
| 26 | ; of the string. SEPARATOR give a Character before the one we have to |
---|
[142] | 27 | ; look for the comma which delimit the keyword in the string. |
---|
| 28 | ; (see examples) |
---|
[2] | 29 | ; |
---|
[142] | 30 | ; @keyword AFTER |
---|
[238] | 31 | ; To look for the keyword, we look for the first sign = which follow |
---|
| 32 | ; the position of keywdname. By default, we substitute the string |
---|
| 33 | ; before the comma. With the keyword AFTER,we can modify the cut |
---|
| 34 | ; of the string. AFTER give a Character after the one we have to |
---|
[142] | 35 | ; look for the comma which delimit the keyword in the string. |
---|
| 36 | ; (see examples) |
---|
[2] | 37 | ; |
---|
[238] | 38 | ; @returns |
---|
[142] | 39 | ; stringout=stringin modified if keywdname has been found in stringin |
---|
[2] | 40 | ; |
---|
[238] | 41 | ; @uses |
---|
[370] | 42 | ; <pro>common</pro> |
---|
[2] | 43 | ; |
---|
[142] | 44 | ; @restrictions |
---|
| 45 | ; If keywdvalue is an array, it will be convert in a vector. |
---|
[2] | 46 | ; |
---|
[142] | 47 | ; @restrictions |
---|
[372] | 48 | ; Beware, this function has loops, ifs and cases everywhere. So it can |
---|
[238] | 49 | ; not be used by big keywords (with a lot of elements which are big |
---|
| 50 | ; arrays). The input keyword must not contain Complex floatings, structure, |
---|
| 51 | ; Double-precision complex, Pointer, Object reference, Unsigned Integer, |
---|
[142] | 52 | ; Unsigned Longword Integer, 64-bit Integer or Unsigned 64-bit Integer. |
---|
| 53 | ; |
---|
| 54 | ; @examples |
---|
| 55 | ; |
---|
[2] | 56 | ; IDL> b='ok=111, year=[1997,1998,1999], age_capitaine=35' |
---|
| 57 | ; IDL> print, b |
---|
| 58 | ; ok=111, year=[1997,1998,1999], age_capitaine=35 |
---|
| 59 | ; IDL> print, chkeywd(b,'ok','c''est bon') |
---|
| 60 | ; ok='c''est bon', year=[1997,1998,1999], age_capitaine=35 |
---|
| 61 | ; IDL> print, chkeywd(b,'YEAR',indgen(5),sep='=') |
---|
| 62 | ; ok=111, year=[0,1,2,3,4], age_capitaine=35 |
---|
| 63 | ; IDL> print, chkeywd(b,'YEAR',indgen(5),sep=']',/after) |
---|
| 64 | ; ok=111, year=[0,1,2,3,4], age_capitaine=35 |
---|
| 65 | ; IDL> b='ok=111, /year, /age_capitaine' |
---|
| 66 | ; IDL> print, chkeywd(b,'year','c''est bon') |
---|
| 67 | ; ok=111, year='c''est bon', /age_capitaine |
---|
| 68 | ; |
---|
[142] | 69 | ; @history |
---|
[157] | 70 | ; Sebastien Masson (smasson\@lodyc.jussieu.fr) |
---|
[372] | 71 | ; - 18/10/1999 |
---|
| 72 | ; - 24/11/1999: adaptation for keywords starting by / |
---|
[238] | 73 | ; |
---|
[142] | 74 | ; @version |
---|
| 75 | ; $Id$ |
---|
| 76 | ; |
---|
[2] | 77 | ;- |
---|
[327] | 78 | FUNCTION chkeywd, stringin, keywdname, keywdvalue $ |
---|
| 79 | , SEPARATOR=separator, AFTER=after |
---|
[114] | 80 | ; |
---|
| 81 | compile_opt idl2, strictarrsubs |
---|
| 82 | ; |
---|
[2] | 83 | |
---|
| 84 | stringout = stringin |
---|
| 85 | poskeywd = strpos(strlowcase(stringout), strlowcase(keywdname)) |
---|
| 86 | if poskeywd EQ -1 then return, stringout |
---|
| 87 | while poskeywd NE -1 do BEGIN |
---|
[142] | 88 | ; change a keyword starting by /toto |
---|
[2] | 89 | if strmid(stringout,poskeywd-1,1) EQ '/' then BEGIN |
---|
| 90 | ajoute = keywdname+'='+tostr(keywdvalue) |
---|
| 91 | stringout = strmid(stringout, 0, poskeywd-1)+ajoute+strmid(stringout,poskeywd+strlen(keywdname) ) |
---|
| 92 | poskeywd = poskeywd+strlen(ajoute) |
---|
| 93 | poskeywd = strpos(stringout, keywdname, poskeywd) |
---|
[238] | 94 | ENDIF ELSE BEGIN |
---|
[142] | 95 | ; change a keyword sarting by toto= |
---|
[2] | 96 | posegal = strpos(stringout, '=', poskeywd) |
---|
| 97 | if posegal EQ -1 then return, stringout |
---|
| 98 | |
---|
| 99 | if NOT keyword_set(separator) then separator = ',' |
---|
| 100 | posvirgule = strpos(stringout, separator, posegal+1) |
---|
| 101 | if keyword_set(after) then posvirgule = strpos(stringout, ',', posvirgule-1) $ |
---|
| 102 | ELSE posvirgule = rstrpos(stringout, ',', posvirgule+1) |
---|
| 103 | if posvirgule EQ -1 then posvirgule = strlen(stringout) |
---|
| 104 | ; |
---|
| 105 | stringout = strmid(stringout, 0, posegal+1)+tostr(keywdvalue)+strmid(stringout, posvirgule) |
---|
| 106 | ; |
---|
| 107 | poskeywd = strpos(stringout, keywdname, posvirgule+1) |
---|
[238] | 108 | ENDELSE |
---|
[2] | 109 | endwhile |
---|
| 110 | |
---|
| 111 | return, stringout |
---|
| 112 | end |
---|