axeld+stippi, inspired on a patch by Maxime Simon:

* Renamed add_extensions() to merge_extensions(), since that is what it does.
* Improved some other variable names in that function for clarity.
* Fixed the duplicate extension detection code, also extended it to the list
  of extensions which are supposed to be added, just in case it already
  contains duplicates.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@30040 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2009-04-08 18:23:16 +00:00
parent e7f2fa91bc
commit 54dceb1316
3 changed files with 27 additions and 18 deletions
+25 -16
View File
@@ -38,8 +38,10 @@ compare_extensions(const void* _a, const void* _b)
} }
//! newExtensionsList contains all the entries (char*) which are to be added.
status_t status_t
add_extensions(BMimeType& type, BList& list, const char* removeExtension) merge_extensions(BMimeType& type, const BList& newExtensionsList,
const char* removeExtension)
{ {
BMessage extensions; BMessage extensions;
status_t status = type.GetFileExtensions(&extensions); status_t status = type.GetFileExtensions(&extensions);
@@ -47,32 +49,38 @@ add_extensions(BMimeType& type, BList& list, const char* removeExtension)
return status; return status;
// replace the entry, and remove any equivalent entries // replace the entry, and remove any equivalent entries
BList newList; BList mergedList;
newList.AddList(&list); mergedList.AddList(&newExtensionsList);
int32 originalCount = mergedList.CountItems();
const char* extension; const char* extension;
for (int32 i = 0; extensions.FindString("extensions", i, for (int32 i = 0; extensions.FindString("extensions", i,
&extension) == B_OK; i++) { &extension) == B_OK; i++) {
bool add = true;
for (int32 j = list.CountItems(); j-- > 0;) { for (int32 j = originalCount; j-- > 0;) {
if ((removeExtension && !strcmp(removeExtension, extension)) if (!strcmp((const char*)mergedList.ItemAt(j), extension)) {
|| !strcmp((const char*)list.ItemAt(j), extension)) { // Do not add this old item again, since it's already
// remove this item // there.
continue; mergedList.RemoveItem(j);
originalCount--;
} }
} }
if (add) // The item will be added behind "originalCount", so we cannot
newList.AddItem((void *)extension); // remove it accidentally in the next iterations, it's is added
// for good.
if (removeExtension == NULL || strcmp(removeExtension, extension))
mergedList.AddItem((void *)extension);
} }
newList.SortItems(compare_extensions); mergedList.SortItems(compare_extensions);
// Copy them to a new message (their memory is still part of the // Copy them to a new message (their memory is still part of the
// original BMessage) // original BMessage)
BMessage newExtensions; BMessage newExtensions;
for (int32 i = 0; i < newList.CountItems(); i++) { for (int32 i = 0; i < mergedList.CountItems(); i++) {
newExtensions.AddString("extensions", (const char*)newList.ItemAt(i)); newExtensions.AddString("extensions",
(const char*)mergedList.ItemAt(i));
} }
return type.SetFileExtensions(&newExtensions); return type.SetFileExtensions(&newExtensions);
@@ -80,12 +88,13 @@ add_extensions(BMimeType& type, BList& list, const char* removeExtension)
status_t status_t
replace_extension(BMimeType& type, const char* newExtension, const char* oldExtension) replace_extension(BMimeType& type, const char* newExtension,
const char* oldExtension)
{ {
BList list; BList list;
list.AddItem((void *)newExtension); list.AddItem((void *)newExtension);
return add_extensions(type, list, oldExtension); return merge_extensions(type, list, oldExtension);
} }
+1 -1
View File
@@ -33,7 +33,7 @@ class ExtensionWindow : public BWindow {
BButton* fAcceptButton; BButton* fAcceptButton;
}; };
extern status_t add_extensions(BMimeType& type, BList& list, extern status_t merge_extensions(BMimeType& type, const BList& newExtensions,
const char* removeExtension = NULL); const char* removeExtension = NULL);
extern status_t replace_extension(BMimeType& type, const char* newExtension, extern status_t replace_extension(BMimeType& type, const char* newExtension,
const char* oldExtension); const char* oldExtension);
@@ -227,7 +227,7 @@ ExtensionListView::MessageReceived(BMessage* message)
list.AddItem(strdup(++point)); list.AddItem(strdup(++point));
} }
add_extensions(fType, list); merge_extensions(fType, list);
// delete extension list // delete extension list
for (int32 index = list.CountItems(); index-- > 0;) { for (int32 index = list.CountItems(); index-- > 0;) {