diff options
author | Thilo Schulz <arny@ats.s.bawue.de> | 2011-05-15 14:11:26 +0000 |
---|---|---|
committer | Tim Angus <tim@ngus.net> | 2013-01-09 22:27:37 +0000 |
commit | b135216385b8821dee57d20cca4464332b00ecc0 (patch) | |
tree | e51c57aa5d80c97923e4cc9bd9db6ddd28bc7694 /src | |
parent | c253917e539fa6a6196cdea8f1733b72feb30107 (diff) |
Refactor and bugfix COM_*Extension functions, patch by DevHC
Diffstat (limited to 'src')
-rw-r--r-- | src/qcommon/q_shared.c | 71 |
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); } /* |