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

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

introducing hyperlinks in idldoc outputs (1/2)

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