Expand paths with ~ with HOME environment variable.

Also: add a bit of doc about default_drumkit_locations and RATE_CONV_QUALITY
This commit is contained in:
Nick Lanham 2012-02-14 12:16:35 +01:00
parent 48bf1f33b9
commit f109726bd7

View File

@ -29,6 +29,16 @@
#include "drmr_hydrogen.h" #include "drmr_hydrogen.h"
#include "expat.h" #include "expat.h"
/* Below is a list of the locations that DrMr will
* search for drumkit files. It will scan each sub-directory
* in these locations (non-recursive) for a drumkit.xml
* file, and if found, parse it and add it to the list
* of available kits.
*
* Strings that start with a ~ will be expanded to the HOME
* environment variable. NB: only a ~ at the start of a string
* will be expanded, ones in the middle will be left in place.
*/
static char* default_drumkit_locations[] = { static char* default_drumkit_locations[] = {
"/usr/share/hydrogen/data/drumkits/", "/usr/share/hydrogen/data/drumkits/",
"/usr/local/share/hydrogen/data/drumkits/", "/usr/local/share/hydrogen/data/drumkits/",
@ -38,6 +48,9 @@ static char* default_drumkit_locations[] = {
NULL NULL
}; };
// Quality of conversion for libsamplerate.
// See http://www.mega-nerd.com/SRC/api_misc.html#Converters
// for info about availble qualities
#define RATE_CONV_QUALITY SRC_SINC_MEDIUM_QUALITY #define RATE_CONV_QUALITY SRC_SINC_MEDIUM_QUALITY
#define MAX_CHAR_DATA 512 #define MAX_CHAR_DATA 512
@ -211,6 +224,25 @@ struct kit_list {
struct kit_list* next; struct kit_list* next;
}; };
// see note above at default_drumkit_locations
// for how this function works
static char* expand_path(char* path, char* buf) {
char *home_dir;
int n;
if (*path != '~') return path;
home_dir = getenv("HOME");
if (!home_dir) {
fprintf(stderr,"Home dir not set, can't expand ~ paths\n");
return 0;
}
n = snprintf(buf,BUFSIZ,"%s%s",home_dir,path+1);
if (n >= BUFSIZ) {
fprintf(stderr,"Path too long for buffer, can't expand: %s\n",path);
return 0;
}
return buf;
}
kits* scan_kits() { kits* scan_kits() {
DIR* dp; DIR* dp;
FILE* file; FILE* file;
@ -228,6 +260,11 @@ kits* scan_kits() {
ret->num_kits = 0; ret->num_kits = 0;
while (cur_path) { while (cur_path) {
cur_path = expand_path(cur_path,buf);
if (!cur_path) {
cur_path = default_drumkit_locations[cp++];
continue;
}
dp = opendir (cur_path); dp = opendir (cur_path);
if (dp != NULL) { if (dp != NULL) {
while (ep = readdir (dp)) { while (ep = readdir (dp)) {