[2] | 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$ |
---|
| 41 | ; Revision 1.1 2002/09/11 10:17:16 opalod |
---|
| 42 | ; Initial revision |
---|
| 43 | ; |
---|
| 44 | ; Revision 1.3 1996/06/14 20:00:27 mcraig |
---|
| 45 | ; Updated Copyright info. |
---|
| 46 | ; |
---|
| 47 | ; Revision 1.2 1996/05/09 00:22:17 mcraig |
---|
| 48 | ; Added built in help. |
---|
| 49 | ; |
---|
| 50 | ; Revision 1.1 1996/01/31 18:47:37 mcraig |
---|
| 51 | ; Initial revision |
---|
| 52 | ; |
---|
| 53 | ; RELEASE: |
---|
| 54 | ; $Name$ |
---|
| 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 distrubting this code. |
---|
| 60 | ; This code comes with absolutely NO warranty; see DISCLAIMER for details. |
---|
| 61 | ;- |
---|
| 62 | FUNCTION Strtok, string, token, $ |
---|
| 63 | TRIM=trim, HELP=Help |
---|
| 64 | |
---|
| 65 | ; Back to the caller if error occurs. |
---|
| 66 | On_error, 2 |
---|
| 67 | |
---|
| 68 | IF (n_params() NE 2) OR keyword_set(Help) THEN BEGIN |
---|
| 69 | offset = ' ' |
---|
| 70 | print, offset+'Retrieve portion of string up to token.' |
---|
| 71 | print, offset+'new = strtok( old, token )' |
---|
| 72 | print, offset+'Inputs:' |
---|
| 73 | print, offset+offset+'old -- String to be split. Contains text after in, out' |
---|
| 74 | print, offset+offset+' token on output.' |
---|
| 75 | print, offset+offset+'token -- Token to use in splitting old. in' |
---|
| 76 | print, offset+'Keywords:' |
---|
| 77 | print, offset+offset+'/TRIM -- set to remove leading blanks from old ' |
---|
| 78 | print, offset+offset+' before returning.' |
---|
| 79 | print, offset+offset+'/HELP -- print useful message and exit.' |
---|
| 80 | print, offset+'Outputs:' |
---|
| 81 | print, offset+offset+'new -- portion of string up to token. out' |
---|
| 82 | print, offset+offset+'old -- portion of old after token. out, in' |
---|
| 83 | print, offset+'Side effects:' |
---|
| 84 | print, offset+offset+'Input parameter old is modified.' |
---|
| 85 | print, offset+'Notes:' |
---|
| 86 | print, offset+offset+'Token may be one or more characters.' |
---|
| 87 | print, offset+offset+"If token is not found, returns old and sets old to ''." |
---|
| 88 | print, offset+'Examples:' |
---|
| 89 | print, offset+offset+"If old is 'foo44 bar', then strtok( old, '44' ) would return'" |
---|
| 90 | print, offset+offset+" 'foo', and upon return, old will be left with ' bar'. If /TRIM" |
---|
| 91 | print, offset+offset+" were set, old would be 'bar' on return." |
---|
| 92 | ;' |
---|
| 93 | print, offset+offset+"If old='xyz', then new=strtok(old,'a') would return with" |
---|
| 94 | print, offset+offset+" new='xyz' and old=''." |
---|
| 95 | return, -1 |
---|
| 96 | ENDIF |
---|
| 97 | |
---|
| 98 | pos = strpos(string, token) |
---|
| 99 | |
---|
| 100 | IF (pos GE 0) THEN BEGIN |
---|
| 101 | front = strmid(string, 0, pos) |
---|
| 102 | string = strmid(string, pos + strlen(token), strlen(string)) |
---|
| 103 | IF keyword_set(trim) THEN string = strtrim(string, 1) |
---|
| 104 | return, front |
---|
| 105 | ENDIF |
---|
| 106 | |
---|
| 107 | front = string |
---|
| 108 | string = '' |
---|
| 109 | return, front |
---|
| 110 | |
---|
| 111 | END |
---|
| 112 | |
---|