source: trunk/STRING/strtok.pro @ 7

Last change on this file since 7 was 7, checked in by pinsard, 18 years ago

1st commit according to cerbere.lodyc.jussieu.fr:/usr/home/smasson/IDL_RD/

  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1;
2;+
3; NAME:
4;       STRTOK
5; PURPOSE:
6;       Retrieve portion of string up to token.
7; CATEGORY:
8;       text/strings
9; CALLING SEQUENCE:
10;       new = strtok( old, token )
11; INPUTS:
12;       old   -- String to be split.  Contains text after    in, out
13;                token on output.
14;       token -- Token to use in splitting old.              in
15; KEYWORD PARAMETERS:
16;       /TRIM -- set to remove leading blanks from old
17;                before returning.
18;       /HELP -- print useful message and exit.
19; OUTPUTS:
20;       new   -- portion of string up to token.              out
21;       old   -- portion of old after token.                 out, in
22; COMMON BLOCKS:
23; SIDE EFFECTS:
24;       Input parameter old is modified.
25; NOTES:
26;       Token may be one or more characters.
27;       If token is not found, returns old and sets old to ''.
28; EXAMPLE:
29;       If old is 'foo44 bar', then strtok( old, '44' ) would return
30;       'foo', and upon return, old will be left with ' bar'.  If /TRIM
31;       were set, old would be 'bar' on return.
32;
33;       If old='xyz', then new=strtok(old,'a') would return with
34;       new='xyz' and old=''.
35; THANKS:
36;       To D. Linder who wrote GETTOK, part of the goddard library,
37;       upon which this is based.
38; MODIFICATION HISTORY:
39;       $Id$
40;       $Log: strtok.pro,v $
41;       Revision 1.3  1996/06/14 20:00:27  mcraig
42;       Updated Copyright info.
43;
44;       Revision 1.2  1996/05/09 00:22:17  mcraig
45;       Added built in help.
46;
47;       Revision 1.1  1996/01/31 18:47:37  mcraig
48;       Initial revision
49;
50; RELEASE:
51;       $Name: Rel_1_2 $
52;
53; COPYRIGHT:
54;  Copyright (C) 1996 The Regents of the University of California, All
55;  Rights Reserved.  Written by Matthew W. Craig.
56;  See the file COPYRIGHT for restrictions on distrubting this code.
57;  This code comes with absolutely NO warranty; see DISCLAIMER for details.
58;-
59FUNCTION Strtok, string, token, $
60                 TRIM=trim, HELP=Help
61
62; Back to the caller if error occurs.
63    On_error, 2
64
65    IF (n_params() NE 2) OR keyword_set(Help) THEN BEGIN
66        offset = '   '
67        print, offset+'Retrieve portion of string up to token.'
68        print, offset+'new = strtok( old, token )'
69        print, offset+'Inputs:'
70        print, offset+offset+'old   -- String to be split.  Contains text after    in, out'
71        print, offset+offset+'         token on output.'
72        print, offset+offset+'token -- Token to use in splitting old.              in'
73        print, offset+'Keywords:'
74        print, offset+offset+'/TRIM -- set to remove leading blanks from old '
75        print, offset+offset+'         before returning.'
76        print, offset+offset+'/HELP -- print useful message and exit.'
77        print, offset+'Outputs:'
78        print, offset+offset+'new   -- portion of string up to token.              out'
79        print, offset+offset+'old   -- portion of old after token.                 out, in'
80        print, offset+'Side effects:'
81        print, offset+offset+'Input parameter old is modified.'
82        print, offset+'Notes:'
83        print, offset+offset+'Token may be one or more characters.'
84        print, offset+offset+"If token is not found, returns old and sets old to ''."
85        print, offset+'Examples:'
86        print, offset+offset+"If old is 'foo44 bar', then strtok( old, '44' ) would return'"
87        print, offset+offset+"  'foo', and upon return, old will be left with ' bar'.  If /TRIM"
88        print, offset+offset+"  were set, old would be 'bar' on return."
89;'
90        print, offset+offset+"If old='xyz', then new=strtok(old,'a') would return with"
91        print, offset+offset+"  new='xyz' and old=''."
92        return, -1
93    ENDIF
94
95    pos = strpos(string, token)
96
97    IF (pos GE 0) THEN BEGIN
98        front = strmid(string, 0, pos)
99        string = strmid(string, pos + strlen(token), strlen(string))
100        IF keyword_set(trim) THEN string = strtrim(string, 1)
101        return, front
102    ENDIF
103   
104    front = string
105    string = ''
106    return, front
107   
108END
109
Note: See TracBrowser for help on using the repository browser.