sourcemm/util.cpp
author David Anderson <dvander@alliedmods.net>
Sun Aug 07 03:26:58 2005 +0000 (2005-08-07)
changeset 95 beffdec7d04c
parent 84 3f77bc9e64d8
child 122 7c36121e2dfe
permissions -rw-r--r--
Bumped version number
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2005 Metamod:Source Development Team
     3 * No warranties of any kind
     4 *
     5 * License: zlib/libpng
     6 *
     7 * Author(s): David "BAILOPAN" Anderson
     8 * ============================
     9 */
    10 
    11 #include <ctype.h>
    12 #include <string.h>
    13 #include "util.h"
    14 
    15 /**
    16  * @brief Utility functons
    17  * @file util.cpp
    18  */
    19 
    20 /* UTIL_GetExtension
    21  * Returns a pointer to the extension in a file name
    22  */
    23 const char *UTIL_GetExtension(const char *file)
    24 {
    25 	int len = strlen(file);
    26 	int i = 0;
    27 
    28 	for (i=len-1; i>=0; i--)
    29 	{
    30 		if (file[i] == '/' || file[i] == '\\')
    31 			return NULL;
    32 
    33 		if ((file[i] == '.') && (i != len-1))
    34 		{
    35 			return (const char *)&(file[i+1]);
    36 		}
    37 	}
    38 
    39 	return NULL;
    40 }
    41 
    42 /* UTIL_TrimLeft
    43  * Removes whitespace characters from left side of string
    44  */
    45 void UTIL_TrimLeft(char *buffer)
    46 {
    47 	// Let's think of this as our iterator
    48 	char *i = buffer;
    49 
    50 	// Make sure the buffer isn't null
    51 	if (i && *i)
    52 	{
    53 		// Add up number of whitespace characters
    54 		while(isspace(*i))
    55 			i++;
    56 
    57 		// If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer
    58 		if (i != buffer)
    59 			memcpy(buffer, i, (strlen(i) + 1) * sizeof(char));
    60 	}
    61 }
    62 
    63 /* UTIL_TrimLeft
    64  * Removes whitespace characters from right side of string
    65  */
    66 void UTIL_TrimRight(char *buffer)
    67 {
    68 	// Make sure buffer isn't null
    69 	if (buffer)
    70 	{
    71 		// Loop through buffer backwards while replacing whitespace chars with null chars
    72 		for (unsigned int i = strlen(buffer) - 1; i >= 0; i--)
    73 		{
    74 			if (isspace(buffer[i]))
    75 				buffer[i] = '\0';
    76 			else
    77 				break;
    78 		}
    79 	}
    80 }
    81