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