sourcemm/util.cpp
changeset 296 ba7337defd34
parent 282 7a237e9ea5a1
child 320 c955cb69efe3
     1.1 --- a/sourcemm/util.cpp	Sat Mar 03 06:57:30 2007 +0000
     1.2 +++ b/sourcemm/util.cpp	Wed Mar 21 17:45:28 2007 +0000
     1.3 @@ -10,67 +10,86 @@
     1.4  
     1.5  #include <ctype.h>
     1.6  #include <string.h>
     1.7 -#include <stdarg.h>
     1.8  #include <stdio.h>
     1.9  #include "util.h"
    1.10  #include "oslink.h"
    1.11  
    1.12  /**
    1.13 - * @brief Utility functons
    1.14 + * @brief Utility functions
    1.15   * @file util.cpp
    1.16   */
    1.17  
    1.18 -/* UTIL_GetExtension
    1.19 - * Returns a pointer to the extension in a file name
    1.20 - */
    1.21  const char *UTIL_GetExtension(const char *file)
    1.22  {
    1.23  	int len = strlen(file);
    1.24  	int i = 0;
    1.25  
    1.26 -	for (i=len-1; i>=0; i--)
    1.27 +	for (i = len - 1; i >= 0; i--)
    1.28  	{
    1.29  		if (file[i] == '/' || file[i] == '\\')
    1.30 +		{
    1.31  			return NULL;
    1.32 +		}
    1.33  
    1.34 -		if ((file[i] == '.') && (i != len-1))
    1.35 +		if ((file[i] == '.') && (i != len - 1))
    1.36  		{
    1.37 -			return (const char *)&(file[i+1]);
    1.38 +			return (const char *)&(file[i + 1]);
    1.39  		}
    1.40  	}
    1.41  
    1.42  	return NULL;
    1.43  }
    1.44  
    1.45 -/* UTIL_TrimLeft
    1.46 - * Removes whitespace characters from left side of string
    1.47 - */
    1.48  void UTIL_TrimLeft(char *buffer)
    1.49  {
    1.50 -	// Let's think of this as our iterator
    1.51 +	/* Let's think of this as our iterator */
    1.52  	char *i = buffer;
    1.53  
    1.54 -	// Make sure the buffer isn't null
    1.55 +	/* Make sure the buffer isn't null */
    1.56  	if (i && *i)
    1.57  	{
    1.58 -		// Add up number of whitespace characters
    1.59 -		while(isspace(*i))
    1.60 +		/* Add up number of whitespace characters */
    1.61 +		while(isspace((unsigned char) *i))
    1.62 +		{
    1.63  			i++;
    1.64 +		}
    1.65  
    1.66 -		// If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer
    1.67 +		/* If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer */
    1.68  		if (i != buffer)
    1.69 +		{
    1.70  			memmove(buffer, i, (strlen(i) + 1) * sizeof(char));
    1.71 +		}
    1.72  	}
    1.73  }
    1.74  
    1.75 -//:TODO: this should skip string literals
    1.76 +void UTIL_TrimRight(char *buffer)
    1.77 +{
    1.78 +	/* Make sure buffer isn't null */
    1.79 +	if (buffer)
    1.80 +	{
    1.81 +		size_t len = strlen(buffer);
    1.82 +
    1.83 +		/* Loop through buffer backwards while replacing whitespace chars with null chars */
    1.84 +		for (size_t i = len - 1; i >= 0; i--)
    1.85 +		{
    1.86 +			if (isspace((unsigned char) buffer[i]))
    1.87 +			{
    1.88 +				buffer[i] = '\0';
    1.89 +			} else {
    1.90 +				break;
    1.91 +			}
    1.92 +		}
    1.93 +	}
    1.94 +}
    1.95 +
    1.96 +/* :TODO: this should skip string literals */
    1.97  void UTIL_TrimComments(char *buffer)
    1.98  {
    1.99  	int num_sc = 0;
   1.100  	size_t len = strlen(buffer);
   1.101  	if (buffer)
   1.102  	{
   1.103 -		for (int i=(int)len-1; i>=0; i--)
   1.104 +		for (int i = len - 1; i >= 0; i--)
   1.105  		{
   1.106  			if (buffer[i] == '/')
   1.107  			{
   1.108 @@ -87,72 +106,62 @@
   1.109  				}
   1.110  				num_sc = 0;
   1.111  			}
   1.112 -			//size_t won't go below 0, manually break out
   1.113 +			/* size_t won't go below 0, manually break out */
   1.114  			if (i == 0)
   1.115 +			{
   1.116  				break;
   1.117 +			}
   1.118  		}
   1.119  	}
   1.120  }
   1.121  
   1.122 -/* UTIL_TrimRight
   1.123 - * Removes whitespace characters from right side of string
   1.124 - */
   1.125 -void UTIL_TrimRight(char *buffer)
   1.126 -{
   1.127 -	// Make sure buffer isn't null
   1.128 -	if (buffer)
   1.129 -	{
   1.130 -		// Loop through buffer backwards while replacing whitespace chars with null chars
   1.131 -		for (int i = (int)strlen(buffer) - 1; i >= 0; i--)
   1.132 -		{
   1.133 -			if (isspace(buffer[i]))
   1.134 -				buffer[i] = '\0';
   1.135 -			else
   1.136 -				break;
   1.137 -		}
   1.138 -	}
   1.139 -}
   1.140 -
   1.141 -/* UTIL_KeySplit
   1.142 - * Breaks a string at the first space until it reaches a nonspace
   1.143 - */
   1.144  void UTIL_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2)
   1.145  {
   1.146  	size_t start;
   1.147  	size_t len = strlen(str);
   1.148  
   1.149 -	for (start=0; start<len; start++)
   1.150 +	for (start = 0; start < len; start++)
   1.151  	{
   1.152  		if (!isspace(str[start]))
   1.153 +		{
   1.154  			break;
   1.155 +		}
   1.156  	}
   1.157  
   1.158  	size_t end;
   1.159 -	for (end=start; end<len; end++)
   1.160 +	for (end = start; end < len; end++)
   1.161  	{
   1.162  		if (isspace(str[end]))
   1.163 +		{
   1.164  			break;
   1.165 +		}
   1.166  	}
   1.167  	
   1.168 -	size_t i, c=0;
   1.169 -	for (i=start; i<end; i++,c++)
   1.170 +	size_t i, c = 0;
   1.171 +	for (i = start; i < end; i++, c++)
   1.172  	{
   1.173  		if (c >= len1)
   1.174 +		{
   1.175  			break;
   1.176 +		}
   1.177  		buf1[c] = str[i];
   1.178  	}
   1.179  	buf1[c] = '\0';
   1.180  
   1.181 -	for (start=end; start<len; start++)
   1.182 +	for (start = end; start < len; start++)
   1.183  	{
   1.184  		if (!isspace(str[start]))
   1.185 +		{
   1.186  			break;
   1.187 +		}
   1.188  	}
   1.189  
   1.190 -	for (c=0; start<len; start++,c++)
   1.191 +	for (c = 0; start < len; start++, c++)
   1.192  	{
   1.193  		if (c >= len2)
   1.194 +		{
   1.195  			break;
   1.196 +		}
   1.197  		buf2[c] = str[start];
   1.198  	}
   1.199  	buf2[c] = '\0';
   1.200 @@ -172,32 +181,61 @@
   1.201  		if (path1[pos1] == PATH_SEP_CHAR)
   1.202  		{
   1.203  			if (path2[pos2] != PATH_SEP_CHAR)
   1.204 +			{
   1.205  				return false;
   1.206 -			//look for extra path chars
   1.207 +			}
   1.208 +
   1.209 +			/* Look for extra path chars */
   1.210  			while (path1[++pos1])
   1.211  			{
   1.212  				if (path1[pos1] != PATH_SEP_CHAR)
   1.213 +				{
   1.214  					break;
   1.215 +				}
   1.216  			}
   1.217  			while (path2[++pos2])
   1.218  			{
   1.219  				if (path2[pos2] != PATH_SEP_CHAR)
   1.220 +				{
   1.221  					break;
   1.222 +				}
   1.223  			}
   1.224  			continue;
   1.225  		}
   1.226  
   1.227 -		//if we're at a different non-alphanumeric, the next character MUST Match
   1.228 +		/* If we're at a different non-alphanumeric, the next character MUST match */
   1.229  		if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
   1.230 +		{
   1.231  			return false;
   1.232 +		}
   1.233  
   1.234 -#ifdef WIN32
   1.235 +	#ifdef WIN32
   1.236  		if (toupper(path1[pos1]) != toupper(path2[pos2]))
   1.237 -#else
   1.238 +	#else
   1.239  		if (path1[pos1] != path2[pos2])
   1.240 -#endif
   1.241 +	#endif
   1.242 +		{
   1.243  			return false;
   1.244 +		}
   1.245 +
   1.246  		pos1++;
   1.247  		pos2++;
   1.248  	}
   1.249  }
   1.250 +
   1.251 +size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
   1.252 +{
   1.253 +	va_list ap;
   1.254 +
   1.255 +	va_start(ap, fmt);
   1.256 +	size_t len = vsnprintf(buffer, maxlength, fmt, ap);
   1.257 +	va_end(ap);
   1.258 +
   1.259 +	if (len >= maxlength)
   1.260 +	{
   1.261 +		len = maxlength - 1;
   1.262 +		buffer[len] = '\0';
   1.263 +	}
   1.264 +
   1.265 +	return len;
   1.266 +}