.entry-views-count{ display: none !important; }

Efficient coding with strings…

I’m currently creating a modified podzilla 2 build with a few custom functions. The first is loading modules from different locations specified by the passed argments (ie “Podzilla2-Mod /usr/lib/Media /usr/lib/Misc” loads modules from both the “/usr/lib/Media” and “/usr/lib/Misc” folders, and any other ones I may add on). I’ve already got that implemented and fully working. The second one is menu path sorting based on the module’s current folder location. While I have things working nicely at the moment, due to my lack of string manipulation knowledge in C, I’m pretty sure my code at the moment is quite ineffective (memory-wise and probably in other aspects too).

My added code: (note that “MODULEDIR” is defined elsewhere as “/usr/lib”
// KERIPO MOD
// Module sub-menu based on data path
static char menupath_mod[128]; // Should be suffice space
static char buffer[128];
static char menupath_new[128];

void clear_menupath_mod() {
// Clear everything
strncpy(menupath_mod, "/", strlen(menupath_mod));
strncpy(buffer, "/", strlen(buffer));
}

void set_menupath_mod(PzModule *mod) {
clear_menupath_mod(); // Safeguard
if (strstr(pz_module_get_datapath(mod, ""), MODULEDIR) != NULL) {
// Start from third '/' to second last char, excluding beginning "/usr/lib" part and ending '/'
strncpy(
buffer,
strchr(strchr(strchr(pz_module_get_datapath(mod, ""), '/')+1,'/')+1,'/'),
strlen(strchr(strchr(strchr(pz_module_get_datapath(mod, ""), '/')+1,'/')+1,'/'))-1
);
pz_error("buffer: %s", buffer); // Testing
// Third '/' to second last '/', excluding beginning "/usr/lib" part and ending module name
strncpy(
menupath_mod,
buffer,
strlen(buffer) - strlen(strrchr(buffer, '/'))+1
);
pz_error("menupath_mod: %s", menupath_mod); // Testing
} // Note: if MODULEDIR isn't in the datapath, menupath_mod will still be "/"
}

const char *newpath (const char *menupath) {
// Because sticking everything under "/Extras/" is borrrrring ; )
if (strstr(menupath, "/Extras/") != NULL) {
// Clear things up
strncpy(menupath_new, "/", strlen(menupath_new));
// Add the module's converted menu path
strcpy(menupath_new, menupath_mod);
if (
// Submenus of SVN modules
strstr(menupath, "/Extras/Demos/") != NULL
|| strstr(menupath, "/Extras/Games/") != NULL
|| strstr(menupath, "/Extras/Utilities/") != NULL
) {
// After the "/Extras/Blah/"
strcat(menupath_new, strchr(strchr(strchr(menupath, '/')+1, '/')+1, '/')+1);
} else {
// After the "/Extras/"
strcat(menupath_new, strchr(strchr(menupath, '/')+1, '/')+1);
}
pz_error("menupath_new: %s", menupath_new); // Testing
return menupath_new;
} else {
// Not "/Extras/"
return menupath;
}
}

In the module.c file, I’ve replaced “do_init (c->mod);” with:
set_menupath_mod(c->mod); // So cheap ; P
do_init (c->mod);
clear_menupath_mod();

Basically before I load the module, I set the “menupath_mod” variable based on the module’s current folder location (found with pz_module_get_datapath). When “resolve_menupath” is called by any pz_add_* functions, the menupath is modified so that “/Extras/” is replaced by the module’s folder path.

For example, when I run the binary on my iPod, loading the mandelpod module gives the pz_errors:
buffer: /Media/mandelpod
menupath_mod: /Media/
menupath_new: /Media/MandelPod
menupath_new: /Media/JuliaPod

while loading the clocks module gives the pz_errors:
buffer: /Misc/clocks
menupath_mod: /Misc/
menupath_new: /Misc/Clock/Local Clock
menupath_new: /Misc/Clock/World Clock
menupath_new: /Misc/Clock/World TZ
menupath_new: /Misc/Clock/World DST

and, as intended, there are the working menu items “/Media/MandelPod”, “/Misc/Clock/Local Clock”, etc.

While everything works nicely as I intended, I’m sure my coding can be vastly cleaned up. The numerous strchr’s I used just to get to the third ‘/’ looks really inefficient and I dislike the idea of creating those 128 static chars (though making them non-static just kills memory and freeses PZ2, so don’t have much choice here). I’m not a regular C programmer, so I’m not too familiar with all the functions and how to best use them.

Any suggestions on code improvement?

~Keripo

http://ipl.derpapst.org/oldforums/viewtopic.php?t=28592

Check Also

Beats2 Update

Post Views: 313 Beats2 Test Demo – Multiplatform Demo It’s been a while since the …