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

Last change on this file since 327 was 325, checked in by pinsard, 17 years ago

modification of some headers (+some corrections) to prepare usage of the new idldoc

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