Rockbox Technical Forums

Rockbox Development => Starting Development and Compiling => Topic started by: Job Van Dam on January 16, 2008, 02:14:53 AM

Title: Need to take off file extension from filename and display filename
Post by: Job Van Dam on January 16, 2008, 02:14:53 AM
I've been hacking away on this small, tiny cosmetic change for way too long now I gotta know, how the heck do you strip the last 4 characters from a filename and actually have the filename left over?
 
I'm not a C guy. This pointer, smointer stuff I sorta get but not enough. Any help at all would be greatly appreciated, thank you.
Title: Re: Need to take off file extension from filename and display filename
Post by: GodEater on January 16, 2008, 04:23:26 AM
Here's one quick (and very dirty) way to do it ;)

Code: [Select]
char oldfilename[13] = "filename.bmp";
char newfilename[13];
char *pointer;

pointer = strrchr(oldfilename,'.');
*pointer = '\0';

strcpy(newfilename,oldfilename);
Title: Re: Need to take off file extension from filename and display filename
Post by: linuxstb on January 16, 2008, 04:45:18 AM
Out of curiousity, what are you trying to achieve?  Rockbox already has a setting to strip file extensions when displaying filenames, is that not working somewhere?
Title: Re: Need to take off file extension from filename and display filename
Post by: bascule on January 16, 2008, 04:52:54 AM
... or alternatively find the Rockbox code in SVN that performs that built-in function and copy it...
Title: Re: Need to take off file extension from filename and display filename
Post by: Job Van Dam on January 16, 2008, 11:24:51 PM
Thanks tenfold GodEater. Tweaked it a bit and it works like a charm. I owe you buddy.
After I wrote that first comment I had the idea because strings in C are char arrays I could get rid of the last 4 elements in the char array and accomplish the same thing but it didn't work for some reason.  I used strlen as the index but it wanted an int as the value to put in.

linuxstb, I'm just writing a small plugin for myself that randomly selects a theme. The part where I was stumped is I wanted the name of the theme, minus the extension, to splash onscreen. It had nothing to do with functionality but it was something I wanted.
The strip_extension function you're talking about is in albumart.c . To use it I needed to add it to the plugin API but why bother when GodEater's code worked just the same and for me it was easier to undertstand.

Thanks to everyone, especially GodEater, for the patience and help. At the next meeting I'm gonna tell Canada you guys are super great. ;D
Title: Re: Need to take off file extension from filename and display filename
Post by: Job Van Dam on January 17, 2008, 01:44:31 AM
Code: [Select]
#include "plugin.h"

PLUGIN_HEADER

static struct plugin_api* rb;



enum plugin_status plugin_start(struct plugin_api* api, void* parameter)
{
    (void)parameter;

    DIR* dir;
    struct dirent *entry;

    char files[60][60], folders[6][36]; //first is number of elements, second is length
    char display_path[MAX_PATH];
    char *pointer;
    int fileCount, folderCount, randNum;


    rb = api;
    fileCount = 0;
    folderCount = 0;
    randNum = 0;
    rb->strcpy(folders[0],THEME_DIR);

  do
  {

     if(randNum > 0){
           rb->strcat(folders[0],folders[randNum]);
          }

   dir = rb->opendir(folders[0]);

          rb->strcpy(folders[0],THEME_DIR);


       entry = rb->readdir(dir);
       entry = rb->readdir(dir);

       while(0 != (entry = rb->readdir(dir)))
       {

         if(entry->attribute & ATTR_DIRECTORY)
          {
      folderCount++;

      rb->strcpy(folders[folderCount],"/");
      rb->strcat(folders[folderCount],entry->d_name);
          }

           else if( !rb->strcasecmp(rb->strrchr(entry->d_name,'.'),".cfg") &&
                    rb->strcmp(entry->d_name,"rockbox_default.cfg") )
              {
                    if(randNum > 0){
                       rb->strcpy(files[fileCount],folders[randNum]);
                       rb->strcat(files[fileCount],"/");
                       }
                        else{
   rb->strcpy(files[fileCount],"/");
                        }
                         rb->strcat(files[fileCount],entry->d_name);

                    fileCount++;
                }
       }

    rb->closedir(dir);

      randNum++;
   }while(randNum <= folderCount);


/******************************************************************/
/* Gets random number and reduces it to within range of fileCount */

     rb->srand(*rb->current_tick);
     randNum = rb->rand();

  while(randNum > 10000){ randNum = randNum / fileCount; }
  while(randNum >= fileCount){ randNum = randNum - fileCount; }


/************************************************/
/* strips ".cfg" from end and "/" from begining */
/* filename to display                          */

rb->strcpy(display_path,files[randNum]);

pointer = rb->strrchr(display_path,'.');
*pointer = '\0';

pointer = rb->strrchr(display_path,'/')+1;

    rb->splash(50,pointer);
    rb->lcd_update();


/******************************************/
/* Compiles entire path of selected theme */

    rb->strcpy(display_path,folders[0]);
    rb->strcat(display_path,files[randNum]);

    rb->settings_load_config(display_path,true);


//rb->button_get(true);

    return PLUGIN_OK;
}
Sorry guys but I need some more help. For some reason the above code doesn't work at all on my actual Gigabeat F40, it just completely stalls. On the Simulator it works perfectly fine though.

Both compile fine.
settings_load_config I added to the plugin API so it's not that.
I'm thinking maybe it's the fact I'm storing every filename and if it's in a folder I store that as well. But I wrote, sorta, this same plugin before and it worked fine.

To those who abhor ugly, unefficient code I apologize.
Any kind of help you can offer me would be greatly appreciated, thank you.
Title: Re: Need to take off file extension from filename and display filename
Post by: GodEater on January 17, 2008, 02:42:10 AM
Firstly - I'd put this on flyspray somewhere, that's where code like this should go really - not the forums.

Secondly, I'd definitely not use that code that I gave you - it *was* just an example, and it *is* dirty. It doesn't, for example, check the return value of the strrchr() call - which *might* return NULL. Following NULL pointers in code is the way to madness.

I'd definitely use the strip_extension() function - it's not too tough to add this to the plugin API really.

BTW - there appear to be two implementations of this function, one (as you say) in apps/recorder/albumart.c, and one in apps/tree.c - I'm not sure why this is so - about to investigate ;)

--EDIT--

There is now only one implementation of strip_extension() in rockbox, and that's now in misc.c
Title: Re: Need to take off file extension from filename and display filename
Post by: NicolasP on January 18, 2008, 02:02:03 PM
I think your plugin might be crashing on target because you allocate too much data on the stack. I don't know the exact size of it, but I can see you are putting a lot of data on it. These arrays (at least "files" and "folders", maybe "display_path" too) should really be declared static, either inside the function or outside.
I have found this to be a frequent cause of behaviour difference between the sim and the actual target.
Title: Re: Need to take off file extension from filename and display filename
Post by: Job Van Dam on January 19, 2008, 03:00:07 PM
You're very right Nic. My old code was unacceptable.

I altered it so now it just searches the theme directory twice, no more storing the names of files and folders. I searches the first time to count the number of files and a second time to send me the file equal to the random number.
I'm even going further because in in the plugin stats.c I see that the coder actually called the same function he currently was in to check out folders he found along the way (so cool!), it just seems like a more effcient way as opposed to the way I search folders now.

Thanks to GodEater, Nic and everyone else for all the help.
Title: Re: Need to take off file extension from filename and display filename
Post by: Job Van Dam on January 19, 2008, 10:04:15 PM
Alright something is wrong. I rewrote my plugin so it no longer stores the filenames and foldernames. In fact it's very similar to the stats.c plugin.

Again on the Sim it works perfectly, very fast. Used it many times over, no errors, no abends, nothing.
I then compile for the actual target and it just freezes everytime.
GodEater when you have time could you please give me a link to the task of this problem on Flyspray.
Title: Re: Need to take off file extension from filename and display filename
Post by: Llorean on January 20, 2008, 12:47:33 AM
Please use the "Modify" button, as per the forum guidelines.