/* -------------------------------- ** FUNCTION: ** fnGetFileNameExtensionFromString ** DESCRIPTION: ** This function extracts the file name extension ** for a file name or a file path (with file name). ** The extension is considered to be everything up ** to the first 'dot' considering the file name ** in reverse. A string containing the extension is ** returned (or the "" empty string if there is no ** file name extension). ** DEPENDENCIES: ** fnReverseString ** fnStripLeadingSpaceFromString ** SEE ALSO: ** DATE: ** -------------------------------- */ function fnGetFileNameExtensionFromString(sString) { //-- Verify parameters if (sString == null) { //-->-->-->-->-->-->-->-->-->-->-->--> //-- write out some nasty error message //-- somewhere, mortally insulting the programmer //-- for such a ridiculous error return -1; } var sReturn = sString; var iIndexOfSlash = -1; var iIndexOfDot = -1; sReturn = fnReverseString(sReturn); sReturn = fnStripLeadingSpaceFromString(sReturn); iIndexOfSlash = sReturn.indexOf("/"); iIndexOfDot = sReturn.indexOf("."); //-->-->-->-->-->-->-->-->-->-->-->--> //-- Cases: //-- 1. There is no dot //-- 2. the dot is further //-- away from us than the slash (remember that //-- we are looking at the file or url in reverse //-- at the moment) if ((iIndexOfDot > iIndexOfSlash) || (iIndexOfDot == -1)) { sReturn = ""; return sReturn; } //-->-->-->-->-->-->-->-->-->-->-->--> //-- Cases: //-- 1. There are no slash's //-- a) There is at least one dot //-- b) There is no dot //-- 2. There is at least one slash //-- a) The dot is further away than the slash //-- (from a reversed point of view) //-- b) The dot is closer than the slash //-- (from a reversed point of view) if (iIndexOfSlash == -1) { if (iIndexOfDot != -1) { sReturn = sReturn.substring(0, iIndexOfDot); sReturn = fnReverseString(sReturn); } else { sReturn = ""; return sReturn; } } else { if (iIndexOfDot > iIndexOfSlash) { sReturn = ""; return sReturn; } else { sReturn = sReturn.substring(0, iIndexOfDot); sReturn = fnReverseString(sReturn); } } //-- if, else (any slashes) return sReturn; } //-- fnGetFileNameExtensionFromString()