Rockbox Development > Starting Development and Compiling

Need to take off file extension from filename and display filename

<< < (2/3) > >>

Job Van Dam:

--- Code: ---#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 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;
}
--- End code ---
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.

GodEater:
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

NicolasP:
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.

Job Van Dam:
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.

Job Van Dam:
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.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version