uint32 numExtensions, // IN:
...) // IN:
{
- char *path;
- char *base;
+ char *p;
+ char *place;
char *result;
- va_list arguments;
- UnicodeIndex index;
+ size_t newExtLen;
+ size_t resultLen;
+ size_t pathNameLen;
ASSERT(pathName);
ASSERT(newExtension);
ASSERT(*newExtension == '.');
- File_GetPathName(pathName, &path, &base);
+ pathNameLen = strlen(pathName);
+ newExtLen = strlen(newExtension);
+ resultLen = pathNameLen + newExtLen + 1;
+ result = Util_SafeMalloc(resultLen);
- index = Unicode_FindLast(base, ".");
+ memcpy(result, pathName, pathNameLen + 1);
- if (index != UNICODE_INDEX_NOT_FOUND) {
- char *oldBase = base;
-
- if (numExtensions) {
- uint32 i;
+ p = strrchr(result, DIRSEPC);
+ if (p == NULL) {
+ p = strrchr(result, '.');
+ } else {
+ p = strrchr(p, '.');
+ }
- /*
- * Only truncate the old extension from the base if it exists in
- * in the valid extensions list.
- */
+ if (p == NULL) {
+ /* No extension... just append */
+ place = &result[pathNameLen]; // The NUL
+ } else if (numExtensions == 0) {
+ /* Always truncate the old extension if extension list is empty. */
+ place = p; // The '.'
+ } else {
+ uint32 i;
+ va_list arguments;
- va_start(arguments, numExtensions);
+ /*
+ * Only truncate the old extension if it exists in the valid
+ * extensions list.
+ */
- for (i = 0; i < numExtensions ; i++) {
- char *oldExtension = va_arg(arguments, char *);
+ place = &result[pathNameLen]; // The NUL
- ASSERT(*oldExtension == '.');
+ va_start(arguments, numExtensions);
- if (Unicode_CompareRange(base, index, -1,
- oldExtension, 0, -1, FALSE) == 0) {
- base = Unicode_Truncate(oldBase, index); // remove '.'
- break;
- }
- }
+ for (i = 0; i < numExtensions ; i++) {
+ char *oldExtension = va_arg(arguments, const char *);
- va_end(arguments);
- } else {
- /* Always truncate the old extension if extension list is empty . */
- base = Unicode_Truncate(oldBase, index); // remove '.'
- }
+ ASSERT(*oldExtension == '.');
- if (oldBase != base) {
- Posix_Free(oldBase);
+ if (strcmp(p, oldExtension) == 0) {
+ place = p; // The '.'
+ break;
+ }
}
- }
- if (Unicode_IsEmpty(path)) {
- result = Unicode_Append(base, newExtension);
- } else {
- result = Unicode_Join(path, DIRSEPS, base, newExtension, NULL);
+ va_end(arguments);
}
- Posix_Free(path);
- Posix_Free(base);
+ /* Add the new extension - in the appropriate place - to pathName */
+ memcpy(place, newExtension, newExtLen + 1);
return result;
}
char *
File_RemoveExtension(const char *pathName) // IN:
{
- UnicodeIndex index;
+ char *p;
+ char *result;
- ASSERT(pathName);
+ ASSERT(pathName != NULL);
- index = Unicode_FindLast(pathName, ".");
- ASSERT(index != UNICODE_INDEX_NOT_FOUND);
+ result = Util_SafeStrdup(pathName);
- return Unicode_Truncate(pathName, index);
+ p = strrchr(result, DIRSEPC);
+ if (p == NULL) {
+ p = strrchr(result, '.');
+ } else {
+ p = strrchr(p, '.');
+ }
+
+ ASSERT(p != NULL);
+
+ if (p != NULL) {
+ *p = '\0';
+ }
+
+ return result;
}