sourcemm/util.cpp
author Scott Ehlert <ds@alliedmods.net>
Wed Aug 23 16:41:49 2006 +0000 (2006-08-23)
changeset 255 dede26d5f38e
parent 238 c27105949293
child 282 7a237e9ea5a1
permissions -rw-r--r--
Hi there. Not much to see here.
     1 /* ======== SourceMM ========
     2 * Copyright (C) 2004-2006 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 <stdarg.h>
    14 #include <stdio.h>
    15 #include "util.h"
    16 #include "oslink.h"
    17 
    18 /**
    19  * @brief Utility functons
    20  * @file util.cpp
    21  */
    22 
    23 /* UTIL_GetExtension
    24  * Returns a pointer to the extension in a file name
    25  */
    26 const char *UTIL_GetExtension(const char *file)
    27 {
    28 	int len = strlen(file);
    29 	int i = 0;
    30 
    31 	for (i=len-1; i>=0; i--)
    32 	{
    33 		if (file[i] == '/' || file[i] == '\\')
    34 			return NULL;
    35 
    36 		if ((file[i] == '.') && (i != len-1))
    37 		{
    38 			return (const char *)&(file[i+1]);
    39 		}
    40 	}
    41 
    42 	return NULL;
    43 }
    44 
    45 /* UTIL_TrimLeft
    46  * Removes whitespace characters from left side of string
    47  */
    48 void UTIL_TrimLeft(char *buffer)
    49 {
    50 	// Let's think of this as our iterator
    51 	char *i = buffer;
    52 
    53 	// Make sure the buffer isn't null
    54 	if (i && *i)
    55 	{
    56 		// Add up number of whitespace characters
    57 		while(isspace(*i))
    58 			i++;
    59 
    60 		// If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer
    61 		if (i != buffer)
    62 			memmove(buffer, i, (strlen(i) + 1) * sizeof(char));
    63 	}
    64 }
    65 
    66 //:TODO: this should skip string literals
    67 void UTIL_TrimComments(char *buffer)
    68 {
    69 	int num_sc = 0;
    70 	size_t len = strlen(buffer);
    71 	if (buffer)
    72 	{
    73 		for (int i=(int)len-1; i>=0; i--)
    74 		{
    75 			if (buffer[i] == '/')
    76 			{
    77 				if (++num_sc >= 2 && i==0)
    78 				{
    79 					buffer[i] = '\0';
    80 					return;
    81 				}
    82 			} else {
    83 				if (num_sc >= 2)
    84 				{
    85 					buffer[i] = '\0';
    86 					return;
    87 				}
    88 				num_sc = 0;
    89 			}
    90 			//size_t won't go below 0, manually break out
    91 			if (i == 0)
    92 				break;
    93 		}
    94 	}
    95 }
    96 
    97 /* UTIL_TrimRight
    98  * Removes whitespace characters from right side of string
    99  */
   100 void UTIL_TrimRight(char *buffer)
   101 {
   102 	// Make sure buffer isn't null
   103 	if (buffer)
   104 	{
   105 		// Loop through buffer backwards while replacing whitespace chars with null chars
   106 		for (int i = (int)strlen(buffer) - 1; i >= 0; i--)
   107 		{
   108 			if (isspace(buffer[i]))
   109 				buffer[i] = '\0';
   110 			else
   111 				break;
   112 		}
   113 	}
   114 }
   115 
   116 /* UTIL_KeySplit
   117  * Breaks a string at the first space until it reaches a nonspace
   118  */
   119 void UTIL_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2)
   120 {
   121 	size_t start;
   122 	size_t len = strlen(str);
   123 
   124 	for (start=0; start<len; start++)
   125 	{
   126 		if (!isspace(str[start]))
   127 			break;
   128 	}
   129 
   130 	size_t end;
   131 	for (end=start; end<len; end++)
   132 	{
   133 		if (isspace(str[end]))
   134 			break;
   135 	}
   136 	
   137 	size_t i, c=0;
   138 	for (i=start; i<end; i++,c++)
   139 	{
   140 		if (c >= len1)
   141 			break;
   142 		buf1[c] = str[i];
   143 	}
   144 	buf1[c] = '\0';
   145 
   146 	for (start=end; start<len; start++)
   147 	{
   148 		if (!isspace(str[start]))
   149 			break;
   150 	}
   151 
   152 	for (c=0; start<len; start++,c++)
   153 	{
   154 		if (c >= len2)
   155 			break;
   156 		buf2[c] = str[start];
   157 	}
   158 	buf2[c] = '\0';
   159 }
   160 
   161 bool UTIL_PathCmp(const char *path1, const char *path2)
   162 {
   163 	size_t pos1 = 0, pos2 = 0;
   164 
   165 	while (true)
   166 	{
   167 		if (path1[pos1] == '\0' || path2[pos2] == '\0')
   168 		{
   169 			return (path1[pos1] == path2[pos2]);
   170 		}
   171 
   172 		if (path1[pos1] == PATH_SEP_CHAR)
   173 		{
   174 			if (path2[pos2] != PATH_SEP_CHAR)
   175 				return false;
   176 			//look for extra path chars
   177 			while (path1[++pos1])
   178 			{
   179 				if (path1[pos1] != PATH_SEP_CHAR)
   180 					break;
   181 			}
   182 			while (path2[++pos2])
   183 			{
   184 				if (path2[pos2] != PATH_SEP_CHAR)
   185 					break;
   186 			}
   187 			continue;
   188 		}
   189 
   190 		//if we're at a different non-alphanumeric, the next character MUST Match
   191 		if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
   192 			return false;
   193 
   194 #ifdef WIN32
   195 		if (toupper(path1[pos1]) != toupper(path2[pos2]))
   196 #else
   197 		if (path1[pos1] != path2[pos2])
   198 #endif
   199 			return false;
   200 		pos1++;
   201 		pos2++;
   202 	}
   203 }