[85] | 1 | ;+ |
---|
| 2 | ; NAME: |
---|
| 3 | ; FSC_BASE_FILENAME |
---|
| 4 | ; |
---|
| 5 | ; PURPOSE: |
---|
| 6 | ; |
---|
| 7 | ; The purpose of this is to extract from a long file path, the |
---|
| 8 | ; base file name. That is, the name of the actual file without |
---|
| 9 | ; the preceeding directory information or the final file extension. |
---|
| 10 | ; The directory information and file extension can be obtained via |
---|
| 11 | ; keywords. The file is named so as not to interfere with FILE_BASENAME, |
---|
| 12 | ; which was introduced in IDL 6.0 and performs a similar function. |
---|
| 13 | ; |
---|
| 14 | ; AUTHOR: |
---|
| 15 | ; |
---|
| 16 | ; FANNING SOFTWARE CONSULTING |
---|
| 17 | ; David Fanning, Ph.D. |
---|
| 18 | ; 1645 Sheely Drive |
---|
| 19 | ; Fort Collins, CO 80526 USA |
---|
| 20 | ; Phone: 970-221-0438 |
---|
| 21 | ; E-mail: davidf@dfanning.com |
---|
| 22 | ; Coyote's Guide to IDL Programming: http://www.dfanning.com/ |
---|
| 23 | ; |
---|
| 24 | ; CATEGORY: |
---|
| 25 | ; |
---|
| 26 | ; Utility. |
---|
| 27 | ; |
---|
| 28 | ; CALLING SEQUENCE: |
---|
| 29 | ; |
---|
| 30 | ; baseFilename = FSC_Base_Filename(thePath) |
---|
| 31 | ; |
---|
| 32 | ; INPUTS: |
---|
| 33 | ; |
---|
| 34 | ; thePath: This is the file path you wish to extract a base file name from. |
---|
| 35 | ; It is a string variable of the sort returned from Dialog_Pickfile. |
---|
| 36 | ; |
---|
| 37 | ; KEYWORDS: |
---|
| 38 | ; |
---|
| 39 | ; DIRECTORY: The directory information obtained from the input file path. |
---|
| 40 | ; The directory always ends in a directory separator character. |
---|
| 41 | ; |
---|
| 42 | ; EXTENSION: The file extension associated with the input file path. |
---|
| 43 | ; |
---|
| 44 | ; PATH_SEPARATOR: The string to use as a path separator. If undefined, the output |
---|
| 45 | ; of PATH_SEP() will be used. |
---|
| 46 | ; |
---|
| 47 | ; RETURN_VALUE: |
---|
| 48 | ; |
---|
| 49 | ; baseFilename: The base filename, stripped of directory and file extension information. |
---|
| 50 | ; |
---|
| 51 | ; RESTRICTIONS: |
---|
| 52 | ; |
---|
| 53 | ; This is a quick and dirty program. It has been tested on Windows machines and *lightly* |
---|
| 54 | ; tested on UNIX machines. Please contact me at the e-mail address above if you discover |
---|
| 55 | ; problems. |
---|
| 56 | ; |
---|
| 57 | ; EXAMPLE: |
---|
| 58 | ; |
---|
| 59 | ; IDL> thePath = "C:\rsi\idl7.8\lib\jester.pro" |
---|
| 60 | ; IDL> Print, FSC_Base_Filename(thePath, Directory=theDirectory, Extension=theExtension) |
---|
| 61 | ; jester |
---|
| 62 | ; IDL> Print, theDirectory |
---|
| 63 | ; C:\rsi\idl7.8\lib\ |
---|
| 64 | ; IDL> Print, theExtension |
---|
| 65 | ; pro |
---|
| 66 | ; |
---|
| 67 | ; |
---|
| 68 | ; MODIFICATION HISTORY: |
---|
| 69 | ; |
---|
| 70 | ; Written by: David W. Fanning, 31 July 2003. |
---|
| 71 | ; Modified by KaRo, 13 Feb. 2005 to allow dots in the filename. |
---|
| 72 | ; Added PATH_SEPARATOR keyword. 25 July 2005. DWF. |
---|
| 73 | ; Added ability to recongnize directory by path separator in last character. 19 Sept 2005. DWF. |
---|
| 74 | ;- |
---|
| 75 | ;########################################################################### |
---|
| 76 | ; |
---|
| 77 | ; LICENSE |
---|
| 78 | ; |
---|
| 79 | ; This software is OSI Certified Open Source Software. |
---|
| 80 | ; OSI Certified is a certification mark of the Open Source Initiative. |
---|
| 81 | ; |
---|
| 82 | ; Copyright © 2003-2005 Fanning Software Consulting |
---|
| 83 | ; |
---|
| 84 | ; This software is provided "as-is", without any express or |
---|
| 85 | ; implied warranty. In no event will the authors be held liable |
---|
| 86 | ; for any damages arising from the use of this software. |
---|
| 87 | ; |
---|
| 88 | ; Permission is granted to anyone to use this software for any |
---|
| 89 | ; purpose, including commercial applications, and to alter it and |
---|
| 90 | ; redistribute it freely, subject to the following restrictions: |
---|
| 91 | ; |
---|
| 92 | ; 1. The origin of this software must not be misrepresented; you must |
---|
| 93 | ; not claim you wrote the original software. If you use this software |
---|
| 94 | ; in a product, an acknowledgment in the product documentation |
---|
| 95 | ; would be appreciated, but is not required. |
---|
| 96 | ; |
---|
| 97 | ; 2. Altered source versions must be plainly marked as such, and must |
---|
| 98 | ; not be misrepresented as being the original software. |
---|
| 99 | ; |
---|
| 100 | ; 3. This notice may not be removed or altered from any source distribution. |
---|
| 101 | ; |
---|
| 102 | ; For more information on Open Source Software, visit the Open Source |
---|
| 103 | ; web site: http://www.opensource.org. |
---|
| 104 | ; |
---|
| 105 | ;########################################################################### |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | FUNCTION FSC_Base_Filename, filename, $ |
---|
| 109 | Directory=directory, $ |
---|
| 110 | Extension=extension, $ |
---|
| 111 | Path_Separator=pathsep |
---|
| 112 | |
---|
| 113 | On_Error, 2 |
---|
| 114 | |
---|
| 115 | ; Default values. |
---|
| 116 | directory = "" |
---|
| 117 | extension = "" |
---|
| 118 | file = "" |
---|
| 119 | |
---|
| 120 | ; If there is no filename, return NULL. |
---|
| 121 | IF (N_Elements(filename) EQ 0) OR (filename EQ "") THEN RETURN, file |
---|
| 122 | |
---|
| 123 | ; Is a path separator specified? |
---|
| 124 | IF N_Elements(pathsep) EQ 0 THEN pathsep = Path_Sep() |
---|
| 125 | |
---|
| 126 | ; If the last element of filename is a path separator, then separation is easy. |
---|
| 127 | IF StrMid(filename, StrLen(filename)-1, 1) EQ pathsep THEN BEGIN |
---|
| 128 | directory = filename |
---|
| 129 | RETURN, file |
---|
| 130 | ENDIF |
---|
| 131 | |
---|
| 132 | ; Split the file by the path separator and extract into parts. |
---|
| 133 | parts = StrSplit(filename, pathsep, /Extract) |
---|
| 134 | IF StrMid(filename, 0, 1) EQ pathsep AND N_Elements(parts) GT 1 THEN parts[0] = pathsep + parts[0] |
---|
| 135 | numParts = N_Elements(parts) |
---|
| 136 | |
---|
| 137 | ; Put the parts back together after identifying them. |
---|
| 138 | CASE numParts OF |
---|
| 139 | 1: BEGIN |
---|
| 140 | subparts = StrSplit(filename, ".", /Extract) |
---|
| 141 | numsubParts = N_Elements(subparts) |
---|
| 142 | CASE numsubParts OF |
---|
| 143 | 1: file = subparts[0] |
---|
| 144 | 2: BEGIN |
---|
| 145 | file = subparts[0] |
---|
| 146 | extension = subparts[1] |
---|
| 147 | END |
---|
| 148 | ELSE: BEGIN |
---|
| 149 | file = StrJoin(subparts[0:numsubParts-2],'.') |
---|
| 150 | extension = subparts[numsubParts-1] |
---|
| 151 | END |
---|
| 152 | ENDCASE |
---|
| 153 | END |
---|
| 154 | |
---|
| 155 | 2: BEGIN |
---|
| 156 | file = parts[1] |
---|
| 157 | directory = parts[0] + pathsep |
---|
| 158 | subparts = StrSplit(file, ".", /Extract) |
---|
| 159 | numsubParts = N_Elements(subparts) |
---|
| 160 | CASE numsubParts OF |
---|
| 161 | 1: file = subparts[0] |
---|
| 162 | 2: BEGIN |
---|
| 163 | file = subparts[0] |
---|
| 164 | extension = subparts[1] |
---|
| 165 | END |
---|
| 166 | ELSE: BEGIN |
---|
| 167 | file = StrJoin(subparts[0:numsubParts-2],'.') |
---|
| 168 | extension = subparts[numsubParts-1] |
---|
| 169 | END |
---|
| 170 | ENDCASE |
---|
| 171 | END |
---|
| 172 | |
---|
| 173 | ELSE: BEGIN |
---|
| 174 | |
---|
| 175 | file = parts[numParts-1] |
---|
| 176 | subparts = StrSplit(file, ".", /Extract) |
---|
| 177 | numsubParts = N_Elements(subparts) |
---|
| 178 | CASE numsubParts OF |
---|
| 179 | 1: file = subparts[0] |
---|
| 180 | 2: BEGIN |
---|
| 181 | file = subparts[0] |
---|
| 182 | extension = subparts[1] |
---|
| 183 | END |
---|
| 184 | ELSE: BEGIN |
---|
| 185 | file = StrJoin(subparts[0:numsubParts-2],'.') |
---|
| 186 | extension = subparts[numsubParts-1] |
---|
| 187 | END |
---|
| 188 | ENDCASE |
---|
| 189 | directory = parts[0] |
---|
| 190 | FOR j=1,numParts-2 DO BEGIN |
---|
| 191 | directory = directory + pathsep + parts[j] |
---|
| 192 | ENDFOR |
---|
| 193 | directory = directory + pathsep |
---|
| 194 | END |
---|
| 195 | |
---|
| 196 | ENDCASE |
---|
| 197 | |
---|
| 198 | RETURN, file |
---|
| 199 | |
---|
| 200 | END |
---|
| 201 | |
---|