summaryrefslogtreecommitdiff
path: root/music.cpp
blob: bd50719b50f65ad356b0fead7f00a070950b80e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "music.h"

#include <cstdio>
#include <stdexcept>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>

namespace music {

MusicDirectory *root_directory = NULL;

void init(const std::string root) {
	root_directory = new MusicDirectory(root);
}

};

MusicTrack::MusicTrack(const std::string filename) {
	printf("%s\n", filename.c_str());
	this->filename = filename;
}

MusicDirectory::MusicDirectory(const std::string root) {
	printf("%s\n", root.c_str());

	DIR *d = opendir(root.c_str());
	if(!d) {
		throw std::runtime_error(strerror(errno));
	}

	struct dirent *dir;
	while((dir = readdir(d)) != NULL) {
		if(strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0) continue;
		if(dir->d_type == DT_DIR) {
			directories.push_back(MusicDirectory(root + "/" + dir->d_name));
		} else if(dir->d_type == DT_REG) {
			tracks.push_back(MusicTrack(root + "/" + dir->d_name));
		}
	}

	closedir(d);
}