sourcemm/util.cpp
changeset 84 3f77bc9e64d8
parent 33 3c7faa7abf09
child 122 7c36121e2dfe
     1.1 --- a/sourcemm/util.cpp	Sat Jul 02 10:09:40 2005 +0000
     1.2 +++ b/sourcemm/util.cpp	Sun Jul 03 16:11:28 2005 +0000
     1.3 @@ -8,6 +8,7 @@
     1.4  * ============================
     1.5  */
     1.6  
     1.7 +#include <ctype.h>
     1.8  #include <string.h>
     1.9  #include "util.h"
    1.10  
    1.11 @@ -38,3 +39,43 @@
    1.12  	return NULL;
    1.13  }
    1.14  
    1.15 +/* UTIL_TrimLeft
    1.16 + * Removes whitespace characters from left side of string
    1.17 + */
    1.18 +void UTIL_TrimLeft(char *buffer)
    1.19 +{
    1.20 +	// Let's think of this as our iterator
    1.21 +	char *i = buffer;
    1.22 +
    1.23 +	// Make sure the buffer isn't null
    1.24 +	if (i && *i)
    1.25 +	{
    1.26 +		// Add up number of whitespace characters
    1.27 +		while(isspace(*i))
    1.28 +			i++;
    1.29 +
    1.30 +		// If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer
    1.31 +		if (i != buffer)
    1.32 +			memcpy(buffer, i, (strlen(i) + 1) * sizeof(char));
    1.33 +	}
    1.34 +}
    1.35 +
    1.36 +/* UTIL_TrimLeft
    1.37 + * Removes whitespace characters from right side of string
    1.38 + */
    1.39 +void UTIL_TrimRight(char *buffer)
    1.40 +{
    1.41 +	// Make sure buffer isn't null
    1.42 +	if (buffer)
    1.43 +	{
    1.44 +		// Loop through buffer backwards while replacing whitespace chars with null chars
    1.45 +		for (unsigned int i = strlen(buffer) - 1; i >= 0; i--)
    1.46 +		{
    1.47 +			if (isspace(buffer[i]))
    1.48 +				buffer[i] = '\0';
    1.49 +			else
    1.50 +				break;
    1.51 +		}
    1.52 +	}
    1.53 +}
    1.54 +