ocarina/libsaria/path/dir.cpp
Bryan Schumaker a508b7ff72 libsaria: Rip out old library
The code was a bit messy and didn't make use of namespaces very well.
By converting to a set of functions (instead of a class) I can use
each function as a function pointer if I want to.  I am also able to
remove libsaria/library.cpp since this was just a set of wrapper
functions to the old static class functions.
2011-10-18 10:02:55 -04:00

76 lines
1.3 KiB
C++

#include <dirent.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <libsaria/path.h>
#include <iostream>
using namespace std;
/* I want to use the d_type field in a dirent */
#ifndef __USE_BSD
#define __USE_BSD
#endif
static void sort_entry(struct dirent *dirp, list<file> &files, list<file> &dirs)
{
struct file entry;
if (dirp == NULL)
return;
entry.name = dirp->d_name;
entry.d_ino = dirp->d_ino;
if (entry.name == "." || entry.name == "..")
return;
switch(dirp->d_type) {
case DT_DIR:
dirs.push_back(entry);
break;
case DT_REG:
files.push_back(entry);
default:
break;
}
}
void readdir(string dir, list<file> &files, list<file> &dirs)
{
DIR *dp;
struct dirent *dirp;
dp = opendir(dir.c_str());
if (dp == NULL)
return;
do {
dirp = readdir(dp);
sort_entry(dirp, files, dirs);
} while (dirp != NULL);
closedir(dp);
}
#ifdef DEBUG
static string SARIA_DIR = "/saria-debug";
#else /* DEBUG */
static string SARIA_DIR = "/saria";
#endif /* DEBUG */
string get_saria_dir()
{
string saria = ".config";
char *xdg = getenv("XDG_CONFIG_HOME");
if (xdg != NULL)
saria = xdg;
return saria += SARIA_DIR;
}
void make_saria_dir()
{
string saria = get_saria_dir();
mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
mkdir(saria.c_str(), mode);
}