source: trunk/SRC/ToBeReviewed/STRING/strtok.pro @ 163

Last change on this file since 163 was 163, checked in by navarro, 18 years ago

header improvements : type of parameters and keywords, default values, spell checking + idldoc assistant (IDL online_help)

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