/*-------------------------------------------------------------------------------
  extractfilename.ado: extract file- and directory name from full file path

    Copyright (C) 2011-2015  Daniel Bela (daniel.bela@lifbi.de)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

-------------------------------------------------------------------------------*/
*! extractfilename.ado: extract file- and directory name from full file path
*! Daniel Bela (daniel.bela@lifbi.de), Leibniz Institute for Educational Trajectories (LIfBi), Germany
*! version 1.3 27 February 2015 - re-worked string parsing; fixed detection of file paths containing "." [bug report K. Wenzig, DIW/GSOEP (thanks!)]
*! version 1.2 14 September 2012 - fixed returned file path to report working directory when querying relative file names
*! version 1.1 25 February 2012 - extract file extension as well, and make everything quicker
*! version 1.0 21 September 2011 - initial release
program define extractfilename, rclass
	syntax [anything(everything equalok)]
	local extdelim .
	local dirdelim `c(dirsep)'
	if (`"`anything'"'=="") {
		capture: assert `"`c(filename)'"'!=""
		if (_rc==0) local anything `c(filename)'
		else {
			noisily: display as error in smcl `"Error: you did not specify a string to parse, nor could Stata find an opened dataset file. Please read the {it:{stata help extractfilename:help file}}."'
			exit 198
		}
	}
	// replace any backward slashes, double quotes and double quotes in string
	local fullname: subinstr local anything "\" "`c(dirsep)'", all
	if (regexm(`"`fullname'"',`"^[\`]?["](.*)["][']?$"')) local fullname=regexs(1)
	// determine position of delimiters
	local fulllength=length(`"`fullname'"')
	foreach delim in ext dir {
		local `delim'found 0
		local `delim'revpos=strpos(reverse(`"`fullname'"'),"``delim'delim'")
		if (``delim'revpos'!=0) local `delim'found 1
		local `delim'pos=`fulllength'-``delim'revpos'+``delim'found'
		if (``delim'pos'==`fulllength') local `delim'pos=0
	}
	// extension delimiter has to be after directory delimter, otherwise it is no extension delimiter
	if (`extfound'==1 & `extpos'<`dirpos') {
		local extfound 0
		local extpos 0
	}
	// split string
	local path=substr(`"`fullname'"',`dirfound',`=`dirpos'-`dirfound'')
	if (missing(`"`path'"') & `"`anything'"'==`"`c(filename)'"') local path `c(pwd)'
	local ext=substr(`"`fullname'"',`=`extpos'+`extfound'',.)
	local file=substr(`"`fullname'"',`=`dirpos'+1',`=`fulllength'-`dirpos'-length(`"`ext'"')'-`extfound')
	// return results
	noisily : display as result in smcl "{p2colset 3 15 28 10}"
	if (!missing(`"`path'"')) noisily : display as result in smcl `"{p2col:directory:}{text}`path'{p_end}"'
	if (!missing(`"`file'"')) noisily : display as result in smcl `"{p2col:filename:}{text}`file'{p_end}"'
	if (!missing(`"`ext'"')) noisily : display as result in smcl `"{p2col:extension:}{text}`ext'{p_end}"'
	return local path `"`path'"'
	return local extension `"`ext'"'
	return local filename `"`file'"'
end
// EOF