summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--httpd.cpp2
-rw-r--r--music.cpp54
-rw-r--r--music.h14
3 files changed, 26 insertions, 44 deletions
diff --git a/httpd.cpp b/httpd.cpp
index 0c1015d..9e6d132 100644
--- a/httpd.cpp
+++ b/httpd.cpp
@@ -22,7 +22,7 @@ void HTTPConnection::handle_read(const boost::system::error_code& error, size_t
HTTPResponse res(socket);
- MusicListing *ml = music::find(req.path);
+ MusicListing::p ml = music::get(req.path);
if(ml) {
res.code = 200;
res.status = "OK";
diff --git a/music.cpp b/music.cpp
index c1ce8f4..2c3b788 100644
--- a/music.cpp
+++ b/music.cpp
@@ -9,40 +9,28 @@
namespace music {
-MusicDirectory *root_directory = NULL;
+fs::path root_directory;
void init(std::string root) {
// remove trailing slash
if(boost::algorithm::ends_with(root, "/"))
root = root.substr(0, root.size()-1);
- root_directory = new MusicDirectory(root);
+ root_directory = root;
}
-MusicListing *find(std::string path, MusicDirectory& root) {
- // remove trailing slash
- if(boost::algorithm::ends_with(path, "/"))
- path = path.substr(0, path.size()-1);
-
- fs::path full_path = root_directory->path / path;
- if(root.path == full_path)
- return &root;
+MusicListing::p get(const std::string path) {
+ // prefix path with our root_directory
+ fs::path p = root_directory / path;
- for(MusicDirectories::iterator it = root.directories.begin(); it != root.directories.end(); it++) {
- MusicListing *ml = find(path, *it);
- if(ml) return ml;
- }
- for(MusicTracks::iterator it = root.tracks.begin(); it != root.tracks.end(); it++) {
- std::string rel_path = it->path.string().substr(root_directory->path.string().size());
- std::cout << rel_path << std::endl;
- if(rel_path == path)
- return &(*it);
+ if(fs::is_directory(p)) {
+ boost::shared_ptr<MusicListing> ml(new MusicDirectory(p));
+ return ml;
+ } else if(fs::is_regular_file(p)) {
+ boost::shared_ptr<MusicListing> ml(new MusicTrack(p));
+ return ml;
}
- return NULL;
-}
-
-MusicListing *find(std::string path) {
- return find(path, *root_directory);
+ return MusicListing::p();
}
}; // namespace music
@@ -50,12 +38,12 @@ MusicListing *find(std::string path) {
void MusicDirectory::render(HTTPRequest& req, HTTPResponse& res) {
res.add_header("content-type", "text/html");
- for(MusicDirectories::iterator it = directories.begin(); it != directories.end(); it++) {
- std::string rel_path = it->path.string().substr(music::root_directory->path.string().size());
+ for(PathListings::iterator it = directories.begin(); it != directories.end(); it++) {
+ std::string rel_path = it->string().substr(music::root_directory.string().size());
res.write(boost::str(boost::format("<a href=\"%s\">%s</a><br />") % rel_path % rel_path));
}
- for(MusicTracks::iterator it = tracks.begin(); it != tracks.end(); it++) {
- std::string rel_path = it->path.string().substr(music::root_directory->path.string().size());
+ for(PathListings::iterator it = tracks.begin(); it != tracks.end(); it++) {
+ std::string rel_path = it->string().substr(music::root_directory.string().size());
res.write(boost::str(boost::format("<a href=\"%s\">%s</a><br />") % rel_path % rel_path));
}
}
@@ -67,16 +55,14 @@ MusicTrack::MusicTrack(const fs::path path) {
MusicDirectory::MusicDirectory(const fs::path root) {
this->path = root;
- std::cout << root << std::endl;
-
- fs::path p(root);
+ std::cout << this->path << std::endl;
fs::directory_iterator end_itr;
- for(fs::directory_iterator it(p); it != end_itr; it++) {
+ for(fs::directory_iterator it(this->path); it != end_itr; it++) {
if(fs::is_directory(it->status())) {
- directories.push_back(MusicDirectory(it->path().string()));
+ directories.push_back(it->path());
} else if(fs::is_regular_file(it->status())) {
- tracks.push_back(MusicTrack(it->path().string()));
+ tracks.push_back(it->path());
}
}
}
diff --git a/music.h b/music.h
index a86c6ac..7f1c813 100644
--- a/music.h
+++ b/music.h
@@ -11,6 +11,7 @@ namespace fs = boost::filesystem;
class MusicListing {
public:
+ typedef boost::shared_ptr<MusicListing> p;
fs::path path;
virtual void render(HTTPRequest& req, HTTPResponse& res) = 0;
};
@@ -21,24 +22,19 @@ class MusicTrack : public MusicListing {
virtual void render(HTTPRequest& req, HTTPResponse& res);
};
-class MusicDirectory;
-typedef std::vector<MusicDirectory> MusicDirectories;
-typedef std::vector<MusicTrack> MusicTracks;
-
class MusicDirectory : public MusicListing {
public:
- MusicDirectories directories;
- MusicTracks tracks;
+ typedef std::vector<fs::path> PathListings;
+ PathListings directories;
+ PathListings tracks;
MusicDirectory(const fs::path root);
virtual void render(HTTPRequest& req, HTTPResponse& res);
};
namespace music {
- extern MusicDirectory *root_directory;
void init(std::string root);
- MusicListing *find(std::string path, MusicDirectory& root);
- MusicListing *find(std::string path);
+ MusicListing::p get(const std::string path);
};
#endif