1.1 --- a/sourcemm/util.cpp Thu Apr 26 18:14:21 2007 +0000
1.2 +++ b/sourcemm/util.cpp Wed May 02 14:08:57 2007 +0000
1.3 @@ -239,3 +239,171 @@
1.4
1.5 return len;
1.6 }
1.7 +
1.8 +inline bool pathchar_isalpha(char a)
1.9 +{
1.10 + return (((a & 1<<7) == 0) && isalpha(a));
1.11 +}
1.12 +
1.13 +inline bool pathchar_sep(char a)
1.14 +{
1.15 +#if defined WIN32
1.16 + return (a == '/' || a == '\\');
1.17 +#elif defined __linux__
1.18 + return (a == '/');
1.19 +#endif
1.20 +}
1.21 +
1.22 +inline bool pathstr_isabsolute(const char *str)
1.23 +{
1.24 +#if defined WIN32
1.25 + return (pathchar_isalpha(str[0])
1.26 + && str[1] == ':'
1.27 + && pathchar_sep(str[2]));
1.28 +#elif defined __linux__
1.29 + return (str[0] == '/');
1.30 +#endif
1.31 +}
1.32 +
1.33 +inline bool pathchar_cmp(char a, char b)
1.34 +{
1.35 +#if defined WIN32
1.36 + if (pathchar_isalpha(a) && pathchar_isalpha(b))
1.37 + {
1.38 + return (tolower(a) == tolower(b));
1.39 + }
1.40 + /* Either path separator is acceptable */
1.41 + if (pathchar_sep(a))
1.42 + {
1.43 + return pathchar_sep(b);
1.44 + }
1.45 +#endif
1.46 + return (a == b);
1.47 +}
1.48 +
1.49 +/**
1.50 + * @brief Forms a relative path given two absolute paths.
1.51 + *
1.52 + * @param buffer Buffer to store relative path in.
1.53 + * @param maxlength Maximum length of the output buffer.
1.54 + * @param relTo Destination folder to use as a working directory.
1.55 + * Final folder name should not be pathchar-terminated.
1.56 + * @param relFrom Source file or folder to use as a target.
1.57 + * @return True on success, false on failure.
1.58 + */
1.59 +bool UTIL_Relatize(char buffer[],
1.60 + size_t maxlength,
1.61 + const char *relTo,
1.62 + const char *relFrom)
1.63 +{
1.64 + /* We don't allow relative paths in here, force
1.65 + * the user to resolve these himself!
1.66 + */
1.67 + if (!pathstr_isabsolute(relTo)
1.68 + || !pathstr_isabsolute(relFrom))
1.69 + {
1.70 + return false;
1.71 + }
1.72 +
1.73 +#if defined WIN32
1.74 + /* Relative paths across drives are not possible */
1.75 + if (!pathchar_cmp(relTo[0], relFrom[0]))
1.76 + {
1.77 + return false;
1.78 + }
1.79 + /* Get rid of the drive and semicolon part */
1.80 + relTo = &relTo[2];
1.81 + relFrom = &relFrom[2];
1.82 +#endif
1.83 +
1.84 + /* Eliminate the common root between the paths */
1.85 + const char *rootTo = NULL;
1.86 + const char *rootFrom = NULL;
1.87 + while (*relTo != '\0' && *relFrom != '\0')
1.88 + {
1.89 + /* If we get to a new path sequence, start over */
1.90 + if (pathchar_sep(*relTo)
1.91 + && pathchar_sep(*relFrom))
1.92 + {
1.93 + rootTo = relTo;
1.94 + rootFrom = relFrom;
1.95 + /* If the paths don't compare, stop looking for a common root */
1.96 + } else if (!pathchar_cmp(*relTo, *relFrom)) {
1.97 + break;
1.98 + }
1.99 + relTo++;
1.100 + relFrom++;
1.101 + }
1.102 +
1.103 + /* NULLs shouldn't happen! */
1.104 + if (rootTo == NULL
1.105 + || rootFrom == NULL)
1.106 + {
1.107 + return false;
1.108 + }
1.109 +
1.110 + size_t numLevels = 0;
1.111 +
1.112 + /* The root case is special!
1.113 + * Don't count anything from it.
1.114 + */
1.115 + if (*(rootTo + 1) != '\0')
1.116 + {
1.117 + /* Search for how many levels we need to go up.
1.118 + * Since the root pointer points to a '/', we increment
1.119 + * the initial pointer by one.
1.120 + */
1.121 + while (*rootTo != '\0')
1.122 + {
1.123 + if (pathchar_sep(*rootTo))
1.124 + {
1.125 + /* Check for an improper trailing slash,
1.126 + * just to be nice even though the user
1.127 + * should NOT have done this!
1.128 + */
1.129 + if (*(rootTo + 1) == '\0')
1.130 + {
1.131 + break;
1.132 + }
1.133 + numLevels++;
1.134 + }
1.135 + rootTo++;
1.136 + }
1.137 + }
1.138 +
1.139 + /* Now build the new relative path. */
1.140 + char *ptr = buffer;
1.141 + size_t len, total = 0;
1.142 + while (numLevels--)
1.143 + {
1.144 + len = _snprintf(&buffer[total], maxlength - total, "..\\");
1.145 + if (len >= maxlength - total)
1.146 + {
1.147 + /* Not enough space in the buffer */
1.148 + return false;
1.149 + }
1.150 + total += len;
1.151 + }
1.152 +
1.153 + /* Add the absolute path. */
1.154 + len = _snprintf(&buffer[total], maxlength - total, "%s", &rootFrom[1]);
1.155 + if (len >= maxlength - total)
1.156 + {
1.157 + return false;
1.158 + }
1.159 +
1.160 + return true;
1.161 +}
1.162 +
1.163 +size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params)
1.164 +{
1.165 + size_t len = vsnprintf(buffer, maxlength, fmt, params);
1.166 +
1.167 + if (len >= maxlength)
1.168 + {
1.169 + len = maxlength - 1;
1.170 + buffer[len] = '\0';
1.171 + }
1.172 +
1.173 + return len;
1.174 +}