[haiku-development] Re: How to Identify a file?

  • From: Stephan Assmus <superstippi@xxxxxx>
  • To: haiku-development@xxxxxxxxxxxxx
  • Date: Thu, 18 Oct 2007 11:10:38 +0200

Hi Fredrik,

> > The mime type of a file should provide that information.
> thanks..
> this was the way I found it to work.. any suggestions on how to make it 
> smaller/ not that memory intense? I will move the code to it's own bool 
> VerifyFileType(entry_ref *ref, char *type) function. How do I make char 
> *type that can be more than 1 parameter? I want to send both "audio", 
> "video" and perhaps other super types? But this works nicely :)
> 
> refsReceivedMessage are a BMessage and can have one file or allot of 
> files. Are hosted in src/apps/mediaplayer/playlist/Playlist.cpp around 
> line 278
> 
> entry_ref ref;
> BFile *file;
> BNodeInfo *nodeinfo;
> BMimeType* mimetype, temp;
> char str[10];
> for (int i = 0; refsReceivedMessage->FindRef("refs", i, &ref) == B_OK; 
> i++){
>     file = new BFile(&ref, B_READ_ONLY);
>     nodeinfo = new BNodeInfo(file);
>     if(nodeinfo->GetType(str) == B_OK){
>         mimetype = new BMimeType(str);
>         if(mimetype->GetSupertype(&temp) == B_OK){
>             if(temp == "audio" || temp == "video")
>                 AppendToPlaylistRecursive(ref, playlist);
>         }
>     }
> }

I hope you don't mind if I point out some problems in this code. The biggest 
problem is that you are leaking files, node infos and mime types, since you 
create them with "new" but never delete them. Much easier to create them on 
the stack. The string you provide to the GetType() function is too small, 
which will cause memory to be overwritten on the stack. Also, declaring all 
the variables in the beginning of a funcion is C style, but we are writing 
C++ and you should declare the variables where you need them. And it is 
better to chose names that reflect what the variables are for. Here is how I 
would adapt the code:

entry_ref ref;
for (int i = 0; refsReceivedMessage->FindRef("refs", i, &ref) == B_OK; i++) {
        BFile file(&ref, B_READ_ONLY);
        BNodeInfo nodeInfo(&file);
        char mimeString[B_MIME_TYPE_LENGTH];
        if (nodeInfo->GetType(mimeString) == B_OK) {
                BMimeType fileType(mimeString);
                BMimeType superType;
                if (fileType->GetSupertype(&superType) == B_OK) {
                        if (superType == "audio" || superType == "video")
                                AppendToPlaylistRecursive(ref, playlist);
                }
        }
}

I am wondering where exactly you place this code, since you call 
AppendToPlaylistRecursive() in the end. Directly after receiving the refs 
message? It looks to me like recursive adding of folders wouldn't work 
anymore then. It could be better to have some kind of "filter" function, 
which is called directly before appending a file to the playlist. This 
function would just tell the appending function if a file should be appended 
or rejected. But it could work exactly like above, only in different context.

Best regards,
-Stephan



Other related posts: