1.1 --- a/sourcemm/util.cpp Wed Oct 05 21:51:02 2005 +0000
1.2 +++ b/sourcemm/util.cpp Thu Oct 06 00:59:01 2005 +0000
1.3 @@ -10,7 +10,10 @@
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 @@ -55,11 +58,43 @@
1.15 i++;
1.16
1.17 // If whitespace chars in buffer then adjust string so first non-whitespace char is at start of buffer
1.18 + // :TODO: change this to not use memcpy()!
1.19 if (i != buffer)
1.20 memcpy(buffer, i, (strlen(i) + 1) * sizeof(char));
1.21 }
1.22 }
1.23
1.24 +//:TODO: this should skip string literals
1.25 +void UTIL_TrimComments(char *buffer)
1.26 +{
1.27 + int num_sc = 0;
1.28 + size_t len = strlen(buffer);
1.29 + if (buffer)
1.30 + {
1.31 + for (int i=(int)len-1; i>=0; i--)
1.32 + {
1.33 + if (buffer[i] == '/')
1.34 + {
1.35 + if (++num_sc >= 2 && i==0)
1.36 + {
1.37 + buffer[i] = '\0';
1.38 + return;
1.39 + }
1.40 + } else {
1.41 + if (num_sc >= 2)
1.42 + {
1.43 + buffer[i] = '\0';
1.44 + return;
1.45 + }
1.46 + num_sc = 0;
1.47 + }
1.48 + //size_t won't go below 0, manually break out
1.49 + if (i == 0)
1.50 + break;
1.51 + }
1.52 + }
1.53 +}
1.54 +
1.55 /* UTIL_TrimLeft
1.56 * Removes whitespace characters from right side of string
1.57 */
1.58 @@ -69,7 +104,7 @@
1.59 if (buffer)
1.60 {
1.61 // Loop through buffer backwards while replacing whitespace chars with null chars
1.62 - for (unsigned int i = strlen(buffer) - 1; i >= 0; i--)
1.63 + for (int i = (int)strlen(buffer) - 1; i >= 0; i--)
1.64 {
1.65 if (isspace(buffer[i]))
1.66 buffer[i] = '\0';
1.67 @@ -79,3 +114,110 @@
1.68 }
1.69 }
1.70
1.71 +/* UTIL_KeySplit
1.72 + * Breaks a string at the first space until it reaches a nonspace
1.73 + */
1.74 +void UTIL_KeySplit(const char *str, char *buf1, size_t len1, char *buf2, size_t len2)
1.75 +{
1.76 + size_t start;
1.77 + size_t len = strlen(str);
1.78 +
1.79 + for (start=0; start<len; start++)
1.80 + {
1.81 + if (!isspace(str[start]))
1.82 + break;
1.83 + }
1.84 +
1.85 + size_t end;
1.86 + for (end=start; end<len; end++)
1.87 + {
1.88 + if (isspace(str[end]))
1.89 + break;
1.90 + }
1.91 +
1.92 + size_t i, c=0;
1.93 + for (i=start; i<end; i++,c++)
1.94 + {
1.95 + if (c >= len1)
1.96 + break;
1.97 + buf1[c] = str[i];
1.98 + }
1.99 + buf1[c] = '\0';
1.100 +
1.101 + for (start=end; start<len; start++)
1.102 + {
1.103 + if (!isspace(str[start]))
1.104 + break;
1.105 + }
1.106 +
1.107 + for (c=0; start<len; start++,c++)
1.108 + {
1.109 + if (c >= len2)
1.110 + break;
1.111 + buf2[c] = str[start];
1.112 + }
1.113 + buf2[c] = '\0';
1.114 +}
1.115 +
1.116 +/**
1.117 + * Formats a path name for an OS
1.118 + */
1.119 +void UTIL_PathFmt(char *buffer, size_t len, const char *fmt, ...)
1.120 +{
1.121 + va_list ap;
1.122 + va_start(ap,fmt);
1.123 + size_t mylen = vsnprintf(buffer, len, fmt, ap);
1.124 + va_end(ap);
1.125 +
1.126 + for (size_t i=0; i<mylen; i++)
1.127 + {
1.128 + if (buffer[i] == ALT_SEP_CHAR)
1.129 + buffer[i] = PATH_SEP_CHAR;
1.130 + }
1.131 +}
1.132 +
1.133 +bool UTIL_PathCmp(const char *path1, const char *path2)
1.134 +{
1.135 + size_t len1=strlen(path1);
1.136 + size_t len2=strlen(path2);
1.137 + size_t pos1=0,pos2=0;
1.138 +
1.139 + while (true)
1.140 + {
1.141 + if (path1[pos1] == '\0' || path2[pos2] == '\0')
1.142 + {
1.143 + return (path1[pos1] == path2[pos2]);
1.144 + }
1.145 +
1.146 + if (path1[pos1] == PATH_SEP_CHAR)
1.147 + {
1.148 + if (path2[pos2] != PATH_SEP_CHAR)
1.149 + return false;
1.150 + //look for extra path chars
1.151 + while (path1[++pos1])
1.152 + {
1.153 + if (path1[pos1] != PATH_SEP_CHAR)
1.154 + break;
1.155 + }
1.156 + while (path2[++pos2])
1.157 + {
1.158 + if (path2[pos2] != PATH_SEP_CHAR)
1.159 + break;
1.160 + }
1.161 + continue;
1.162 + }
1.163 +
1.164 + //if we're at a different non-alphanumeric, the next character MUST Match
1.165 + if (!isalpha(path1[pos1]) && (path1[pos1] != path2[pos2]))
1.166 + return false;
1.167 +
1.168 +#ifdef WIN32
1.169 + if (toupper(path1[pos1]) != toupper(path2[pos2]))
1.170 +#else
1.171 + if (path1[pos1] != path2[pos2])
1.172 +#endif
1.173 + return false;
1.174 + pos1++;
1.175 + pos2++;
1.176 + }
1.177 +}