diff -r e90762b422d1 -r ba7337defd34 sourcemm/util.cpp --- a/sourcemm/util.cpp Sat Mar 03 06:57:30 2007 +0000 +++ b/sourcemm/util.cpp Wed Mar 21 17:45:28 2007 +0000 @@ -10,67 +10,86 @@ #include #include -#include #include #include "util.h" #include "oslink.h" /** - * @brief Utility functons + * @brief Utility functions * @file util.cpp */ -/* UTIL_GetExtension - * Returns a pointer to the extension in a file name - */ const char *UTIL_GetExtension(const char *file) { int len = strlen(file); int i = 0; - for (i=len-1; i>=0; i--) + for (i = len - 1; i >= 0; i--) { if (file[i] == '/' || file[i] == '\\') + { return NULL; + } - if ((file[i] == '.') && (i != len-1)) + if ((file[i] == '.') && (i != len - 1)) { - return (const char *)&(file[i+1]); + return (const char *)&(file[i + 1]); } } return NULL; } -/* UTIL_TrimLeft - * Removes whitespace characters from left side of string - */ void UTIL_TrimLeft(char *buffer) { - // Let's think of this as our iterator + /* Let's think of this as our iterator */ char *i = buffer; - // Make sure the buffer isn't null + /* Make sure the buffer isn't null */ if (i && *i) { - // Add up number of whitespace characters - while(isspace(*i)) + /* Add up number of whitespace characters */ + while(isspace((unsigned char) *i)) + { i++; + } - // If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer + /* If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer */ if (i != buffer) + { memmove(buffer, i, (strlen(i) + 1) * sizeof(char)); + } } } -//:TODO: this should skip string literals +void UTIL_TrimRight(char *buffer) +{ + /* Make sure buffer isn't null */ + if (buffer) + { + size_t len = strlen(buffer); + + /* Loop through buffer backwards while replacing whitespace chars with null chars */ + for (size_t i = len - 1; i >= 0; i--) + { + if (isspace((unsigned char) buffer[i])) + { + buffer[i] = '\0'; + } else { + break; + } + } + } +} + +/* :TODO: this should skip string literals */ void UTIL_TrimComments(char *buffer) { int num_sc = 0; size_t len = strlen(buffer); if (buffer) { - for (int i=(int)len-1; i>=0; i--) + for (int i = len - 1; i >= 0; i--) { if (buffer[i] == '/') { @@ -87,72 +106,62 @@ } num_sc = 0; } - //size_t won't go below 0, manually break out + /* size_t won't go below 0, manually break out */ if (i == 0) + { break; + } } } } -/* UTIL_TrimRight - * Removes whitespace characters from right side of string - */ -void UTIL_TrimRight(char *buffer) -{ - // Make sure buffer isn't null - if (buffer) - { - // Loop through buffer backwards while replacing whitespace chars with null chars - for (int i = (int)strlen(buffer) - 1; i >= 0; i--) - { - if (isspace(buffer[i])) - buffer[i] = '\0'; - else - break; - } - } -} - -/* UTIL_KeySplit - * Breaks a string at the first space until it reaches a nonspace - */ void UTIL_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2) { size_t start; size_t len = strlen(str); - for (start=0; start= len1) + { break; + } buf1[c] = str[i]; } buf1[c] = '\0'; - for (start=end; start= len2) + { break; + } buf2[c] = str[start]; } buf2[c] = '\0'; @@ -172,32 +181,61 @@ if (path1[pos1] == PATH_SEP_CHAR) { if (path2[pos2] != PATH_SEP_CHAR) + { return false; - //look for extra path chars + } + + /* Look for extra path chars */ while (path1[++pos1]) { if (path1[pos1] != PATH_SEP_CHAR) + { break; + } } while (path2[++pos2]) { if (path2[pos2] != PATH_SEP_CHAR) + { break; + } } continue; } - //if we're at a different non-alphanumeric, the next character MUST Match + /* If we're at a different non-alphanumeric, the next character MUST match */ if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2])) + { return false; + } -#ifdef WIN32 + #ifdef WIN32 if (toupper(path1[pos1]) != toupper(path2[pos2])) -#else + #else if (path1[pos1] != path2[pos2]) -#endif + #endif + { return false; + } + pos1++; pos2++; } } + +size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + size_t len = vsnprintf(buffer, maxlength, fmt, ap); + va_end(ap); + + if (len >= maxlength) + { + len = maxlength - 1; + buffer[len] = '\0'; + } + + return len; +}