fad3019d83
And add tracks to the library playlist. Signed-off-by: Bryan Schumaker <bjschuma@gmail.com>
116 lines
2.2 KiB
C++
116 lines
2.2 KiB
C++
// Copyright (c) 2012 Bryan Schumaker.
|
|
#include <libsaria/fs.h>
|
|
#include <libsaria/task.h>
|
|
#include <libsaria/print.h>
|
|
|
|
#include <dirent.h>
|
|
#include <sys/stat.h>
|
|
#include <stdlib.h>
|
|
#include <fstream>
|
|
using namespace std;
|
|
|
|
/* I want to use the d_type field in a dirent */
|
|
#ifndef __USE_BSD
|
|
#define __USE_BSD
|
|
#endif
|
|
|
|
static string appdir;
|
|
static mode_t dirmode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
|
|
|
|
static void make_dir(string path)
|
|
{
|
|
println("Making directory: " + path);
|
|
mkdir(path.c_str(), dirmode);
|
|
}
|
|
|
|
static void handle_entry(string &dir, list<string> &file_list, struct dirent *dirp)
|
|
{
|
|
string name = dirp->d_name;
|
|
if (name == "." || name == "..")
|
|
return;
|
|
name = dir + "/" + name;
|
|
|
|
switch(dirp->d_type) {
|
|
case DT_DIR:
|
|
libsaria::list_dir(name, file_list);
|
|
break;
|
|
default:
|
|
file_list.push_back(name);
|
|
};
|
|
}
|
|
|
|
namespace libsaria
|
|
{
|
|
|
|
void list_dir(string &dir, list<string> &file_list)
|
|
{
|
|
DIR *dp;
|
|
struct dirent *dirp;
|
|
|
|
dp = opendir(dir.c_str());
|
|
if (dp == NULL)
|
|
return;
|
|
|
|
do {
|
|
dirp = readdir(dp);
|
|
if (dirp)
|
|
handle_entry(dir, file_list, dirp);
|
|
} while (dirp != NULL);
|
|
closedir(dp);
|
|
}
|
|
|
|
void app::list_dir(string &dir, list<string> &file_list)
|
|
{
|
|
list<string> files;
|
|
list<string>::iterator it;
|
|
|
|
string d = appdir + "/" + dir;
|
|
libsaria::list_dir(d, files);
|
|
|
|
/* Strip out appdir information */
|
|
for (it = files.begin(); it != files.end(); it++)
|
|
file_list.push_back( (*it).substr(d.size() + 1) );
|
|
}
|
|
|
|
void app::mkdir()
|
|
{
|
|
make_dir(appdir.c_str());
|
|
}
|
|
|
|
void app::mkdir(string dir)
|
|
{
|
|
string d = appdir + "/" + dir;
|
|
make_dir(d.c_str());
|
|
}
|
|
|
|
void app::init(string name)
|
|
{
|
|
string home = getenv("HOME");
|
|
appdir = home + "/." + name;
|
|
#ifdef DEBUG
|
|
appdir += "-debug";
|
|
#endif
|
|
mkdir();
|
|
}
|
|
|
|
void app::save(string file, void (*func)(ofstream &, void *),
|
|
DataState *state, void *data)
|
|
{
|
|
new WriteTask(appdir + "/" + file, func, state, data);
|
|
}
|
|
|
|
void app::read(string file, void (*func)(ifstream &))
|
|
{
|
|
new ReadTask(appdir + "/" + file, func);
|
|
}
|
|
|
|
void app::read_now(string file, void (*func)(ifstream &))
|
|
{
|
|
ifstream stream;
|
|
stream.open((appdir + "/" + file).c_str());
|
|
func(stream);
|
|
stream.close();
|
|
}
|
|
|
|
} /* Namespace: libsaria */
|