core/logic/stringutil.cpp
changeset 3929 0bc0ffdb2eba
parent 3010 8ea042c00c80
child 3930 bf78b97106e5
     1.1 --- a/core/logic/stringutil.cpp	Sun Jun 30 22:04:04 2013 -0400
     1.2 +++ b/core/logic/stringutil.cpp	Wed Jul 03 23:14:55 2013 -0400
     1.3 @@ -303,3 +303,35 @@
     1.4  	return written;
     1.5  }
     1.6  
     1.7 +#define PATHSEPARATOR(c) ((c) == '\\' || (c) == '/')
     1.8 +
     1.9 +void UTIL_StripExtension(const char *in, char *out, int outSize)
    1.10 +{
    1.11 +	// Find the last dot. If it's followed by a dot or a slash, then it's part of a 
    1.12 +	// directory specifier like ../../somedir/./blah.
    1.13 +
    1.14 +	// scan backward for '.'
    1.15 +	int end = strlen(in) - 1;
    1.16 +	while (end > 0 && in[end] != '.' && !PATHSEPARATOR(in[end]))
    1.17 +	{
    1.18 +		--end;
    1.19 +	}
    1.20 +
    1.21 +	if (end > 0 && !PATHSEPARATOR(in[end]) && end < outSize)
    1.22 +	{
    1.23 +		int nChars = min(end, outSize-1);
    1.24 +		if (out != in)
    1.25 +		{
    1.26 +			memcpy(out, in, nChars);
    1.27 +		}
    1.28 +		out[nChars] = 0;
    1.29 +	}
    1.30 +	else
    1.31 +	{
    1.32 +		// nothing found
    1.33 +		if (out != in)
    1.34 +		{
    1.35 +			strncopy(out, in, outSize);
    1.36 +		}
    1.37 +	}
    1.38 +}