; Copyright (c) 1998-2008 A.P. Hitchcock  All rights reserved
;+
;NAME:
;	AX_NAME
;
;LAST CHANGED: ----------------------------------- 20-May-13 (bw)
;
;PURPOSE:
;	This function parses an arbitrary text string as
; a filename and returns independently the path, main_name, & extension
; The path ends with the separator appropriate to the current operating system.
; The separator of the main_name & extension is not returned.
;
;CATEGORY:
;	STAND ALONE: utility
;
;CALLING SEQUENCE:
;	Result = ax_name(FILENAME)
;
;CALLED FROM AXIS:
;	(MANY PLACES)
;
;KEYWORDS: none
;
;INPUT: string with filename
;
;OUTPUTS:
;	RESULT - 3-element array:
; result(0) = path    result(1) = main_name    result(2) = extension
;
;COMMON BLOCKS: none
;
;EXAMPLE:
; Create a name and parse it
;	name = 'c:\axis\axis_dev\test-code\circle.axb'
;	t = ax_name(name)
;	print, t(0), '   ', t(1), '   ', t(2)
;   	c:\axis\axis_dev\test-code\   circle   axb
;
; MODIFICATION HISTORY:
; (25-jun-99 aph) first developed
; (13-aug-06 jso) ensure compatible with 532 - string aray element 0 specified
; (11-Oct-08 aph) adapt to work with main names containing '.'s - assume extension
;                 is all parts of string from back to first '.' (NB may not work with IDL 5.1)
; (30-Dec-08 aph) modified to work with IDL 5.2
; (20-May-13 bw)  removed forcing lower case in file paths and names for platform compatability
;-

Function ax_name, in_filename

on_error,2

file = in_filename(0)	;Made explicit the dimensioning for compatibility with 5.2 (jso)
fileshort = file
; ------------ extract the path
path=''
upos = rstrpos(file,'/')       ; check for either type of subdirectory separators
wpos = rstrpos(file,'\')
spos = max([upos,wpos])
if (spos ne -1) then begin
	fileshort=strmid(file,spos+1,strlen(file)) 	; strip leader if it exists
	path = strmid(file,0,spos+1)
endif
; --------- extract the extension
ext = ''
;if fix(srtmid(!version.release,0,1)) LT 6 then begin
;	spos=strpos(fileshort,'.')			; /reverse_search only introduced in IDL 6
;endif else begin
;	spos=strpos(fileshort,'.', /reverse_search, /reverse_offset)
;endelse
spos = rstrpos(fileshort,'.')
if (spos ne -1) then begin
	ext = strmid(fileshort, spos+1,strlen(fileshort))
	fileshort=strmid(fileshort,0,spos)		; strip extension if it exists
endif

t = strarr(3)
t(0) = path
t(1) = fileshort
t(2) = ext

; print, 'path: |',t(0),'|   name:|',t(1),'|  ext:|',t(2),'|'

return, t
end

