source: trunk/SRC/ToBeReviewed/STRING/chkeywd.pro

Last change on this file was 495, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo; dupe empty lines; trailing blanks

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1;+
2;
3; @file_comments
4; In a string containing an order to execute with EXECUTE by example.
5; We change the value of one of keywords.
6;
7; More generally, in a string, we look for the character
8; chain: ', keywdname= ..., and we change the value of...
9;
10; @categories
11; String, keywords
12;
13; @param STRINGIN {in}{required}{type=string}
14; it is a string
15;
16; @param KEYWDNAME {in}{required}{type=string}
17; it is a string designating the name of keyword to look for.
18;
19; @param KEYWDVALUE {in}{required}
20; The new value of the keyword to considerate in STRINGIN
21;
22; @keyword SEPARATOR
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
27; look for the comma which delimit the keyword in the string.
28; (see examples)
29;
30; @keyword AFTER
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
35; look for the comma which delimit the keyword in the string.
36; (see examples)
37;
38; @returns
39; stringout=stringin modified if keywdname has been found in stringin
40;
41; @uses
42; <pro>common</pro>
43;
44; @restrictions
45; If keywdvalue is an array, it will be convert in a vector.
46;
47; @restrictions
48; Beware, this function has loops, ifs and cases everywhere. So it can
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,
52; Unsigned Longword Integer, 64-bit Integer or Unsigned 64-bit Integer.
53;
54; @examples
55;
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;
69; @history
70; Sebastien Masson (smasson\@lodyc.jussieu.fr)
71;  - 18/10/1999
72;  - 24/11/1999: adaptation for keywords starting by /
73;
74; @version
75; $Id$
76;
77;-
78FUNCTION chkeywd, stringin, keywdname, keywdvalue $
79                , SEPARATOR=separator, AFTER=after
80;
81  compile_opt idl2, strictarrsubs
82;
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
88; change a keyword starting by /toto
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)
94      ENDIF ELSE BEGIN
95; change a keyword sarting by toto=
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)
108      ENDELSE
109   endwhile
110
111   return,  stringout
112end
Note: See TracBrowser for help on using the repository browser.