Main Page   Namespace List   Alphabetical List   Compound List   File List   Compound Members   File Members  

EmpParser.cpp

Go to the documentation of this file.
00001 
00024 #include "EmpParser.h"
00025 #include "xmlutils.h"
00026 #include "StringUtils.h"
00027 #include <algorithm>
00028 
00029 using namespace std;
00030 using namespace xmlpp;
00031 
00033 struct ArtistNotEqualTo : public unary_function<RmpTrack&, bool> {
00034   ArtistNotEqualTo(const string& artist) : m_artist(artist){}
00035   bool operator()(RmpTrack& trk) { return trk.artist != m_artist;}
00036   const string& m_artist;
00037 };
00038 
00047 bool EmpParser::parse_file(const char* emp_file, RmpSession& sess, TrackList& tracks)
00048 {
00049   bool success(false);
00050 
00051   DomParser emptree(emp_file);
00052   if (emptree && 
00053         get_session_info(emptree.get_root_node(), sess)) {
00054      
00055      success = get_track_info(emptree.get_root_node(), sess, tracks);
00056      
00057      if (! tracks.empty()) {        
00058         TrackList::iterator iter = tracks.begin();
00059         string artist = iter->artist;       
00060         
00061         iter = find_if(++iter, tracks.end(), ArtistNotEqualTo(artist));
00062         sess.package_artist = (iter == tracks.end()) ? artist : "Various Artists";
00063      }   
00064   }
00065 
00066   return success;
00067 }
00068 
00070 
00076 bool EmpParser::get_session_info(Node* root, RmpSession& sess)
00077 {
00078   Node* node;
00079 
00080   if ((node = getFirstChild(root, "PROVIDER")) != NULL) {
00081      getChildContent(node, "NAME", sess.provider);
00082   }
00083   
00084   getChildContent(root, "TITLE", sess.package_title);
00085         
00086   sess.base_url = "http://";
00087   if ((node = getFirstChild(root, "SERVER")) != NULL) {
00088      Node* entry;
00089 
00090      if ((entry = getFirstChild(node, "NETNAME")) != NULL) {
00091         sess.base_url += getChildContent(entry);
00092      }
00093 
00094      getChildContent(node, "LOCATION", sess.location_template);
00095      
00096   }
00097 
00098   return ! (sess.package_title.empty() || sess.base_url.empty());
00099 }
00100 
00101 
00102 
00104 
00112 bool EmpParser::get_track_info(Node* root, const RmpSession& sess, TrackList& tracks)
00113 {
00114   tracks.clear();
00115   Node* trackListNode;
00116 
00117   if ((trackListNode = getFirstChild(root, "TRACKLIST")) != NULL) {
00118      Node::NodeList track_nodes = trackListNode->get_children("TRACK");
00119      RmpTrack info;
00120 
00121      Node::NodeList::iterator curr_track = track_nodes.begin();
00122      for(; curr_track != track_nodes.end(); ++curr_track) {
00123         
00124         getChildContent(*curr_track, "TITLE", info.title);     
00125         getChildContent(*curr_track, "ARTIST", info.artist);     
00126         getChildContent(*curr_track, "TRACKNUM", info.track_num);  
00127         getChildContent(*curr_track, "FILENAME", info.file_name);  
00128         getChildContent(*curr_track, "TRACKID", info.track_id);   
00129         getChildContent(*curr_track, "GENRE", info.genre);      
00130         getChildContent(*curr_track, "FORMAT", info.format);     
00131         getChildContent(*curr_track, "QUALITY", info.quality);    
00132         getChildContent(*curr_track, "ALBUMART", info.cover);      
00133         getChildContent(*curr_track, "DURATION", info.duration);      
00134         getChildContent(*curr_track, "CHANNELS", info.channels);      
00135 
00136         info.url = build_track_url(sess, info);
00137         tracks.push_back(info);
00138      }
00139      
00140   }
00141   return (tracks.size() > 0);
00142 }
00143 
00144 
00146 
00152 string EmpParser::build_track_url(const RmpSession& sess, const RmpTrack& track)
00153 {
00154   // Make the following substitutions
00155   // %f   -> track filename
00156   // %fid -> track id  
00157 
00158   string url(sess.location_template);
00159 
00160   replace_str(url, "%fid", track.track_id);
00161   replace_str(url, "%f", track.file_name);
00162 
00163   url = sess.base_url + url;
00164 
00165   return url;
00166 }

Generated on Sat May 3 09:16:13 2003 for empxform by doxygen1.3-rc3