summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/qcommon/q_shared.c71
1 files changed, 24 insertions, 47 deletions
diff --git a/src/qcommon/q_shared.c b/src/qcommon/q_shared.c
index ba0c5ffe..3e95a398 100644
--- a/src/qcommon/q_shared.c
+++ b/src/qcommon/q_shared.c
@@ -59,20 +59,13 @@ char *COM_SkipPath (char *pathname)
COM_GetExtension
============
*/
-const char *COM_GetExtension( const char *name ) {
- int length, i;
-
- length = strlen(name)-1;
- i = length;
-
- while (name[i] != '.')
- {
- i--;
- if (name[i] == '/' || i == 0)
- return ""; // no extension
- }
-
- return &name[i+1];
+const char *COM_GetExtension( const char *name )
+{
+ const char *dot = strrchr(name, '.'), *slash;
+ if (dot && (!(slash = strrchr(name, '/')) || slash < dot))
+ return dot + 1;
+ else
+ return "";
}
@@ -81,47 +74,31 @@ const char *COM_GetExtension( const char *name ) {
COM_StripExtension
============
*/
-void COM_StripExtension( const char *in, char *out, int destsize ) {
- int length;
-
- Q_strncpyz(out, in, destsize);
-
- length = strlen(out)-1;
- while (length > 0 && out[length] != '.')
- {
- length--;
- if (out[length] == '/')
- return; // no extension
- }
- if (length)
- out[length] = 0;
+void COM_StripExtension( const char *in, char *out, int destsize )
+{
+ const char *dot = strrchr(in, '.'), *slash;
+ if (dot && (!(slash = strrchr(in, '/')) || slash < dot))
+ Q_strncpyz(out, in, (destsize < dot-in+1 ? destsize : dot-in+1));
+ else
+ Q_strncpyz(out, in, destsize);
}
/*
==================
COM_DefaultExtension
+
+if path doesn't have an extension, then append
+ the specified one (which should include the .)
==================
*/
-void COM_DefaultExtension (char *path, int maxSize, const char *extension ) {
- char oldPath[MAX_QPATH];
- char *src;
-
-//
-// if path doesn't have a .EXT, append extension
-// (extension should include the .)
-//
- src = path + strlen(path) - 1;
-
- while (*src != '/' && src != path) {
- if ( *src == '.' ) {
- return; // it has an extension
- }
- src--;
- }
-
- Q_strncpyz( oldPath, path, sizeof( oldPath ) );
- Com_sprintf( path, maxSize, "%s%s", oldPath, extension );
+void COM_DefaultExtension( char *path, int maxSize, const char *extension )
+{
+ const char *dot = strrchr(path, '.'), *slash;
+ if (dot && (!(slash = strrchr(path, '/')) || slash < dot))
+ return;
+ else
+ Q_strcat(path, maxSize, extension);
}
/*