libzypp  10.5.0
MediaProducts.h
Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #ifndef ZYPP_MEDIAPRODUCTS_H_
00013 #define ZYPP_MEDIAPRODUCTS_H_
00014 
00015 #include <iterator>
00016 #include <iostream>
00017 #include <fstream>
00018 #include "zypp/ZConfig.h"
00019 #include "zypp/base/Logger.h"
00020 #include "zypp/media/MediaManager.h"
00021 #include "zypp/base/UserRequestException.h"
00022 
00023 #include "zypp/ProgressData.h"
00024 
00025 namespace zypp
00026 {
00030   struct MediaProductEntry
00031   {
00032     Pathname    _dir;
00033     std::string _name;
00034 
00038     MediaProductEntry( const Pathname & dir_r = "/", const std::string & name_r = std::string() )
00039       : _dir(dir_r), _name(name_r)
00040     {
00041     }
00042 
00043     bool operator<( const MediaProductEntry &rhs ) const
00044     {
00045       return ( _name < rhs._name );
00046     }
00047   };
00048 
00052   typedef std::set<MediaProductEntry> MediaProductSet;
00053 
00057   template <class _OutputIterator>
00058   static void scanProductsFile( const Pathname & file_r, _OutputIterator result )
00059   {
00060     std::ifstream pfile( file_r.asString().c_str() );
00061     while ( pfile.good() ) {
00062 
00063       std::string value = str::getline( pfile, str::TRIM );
00064       if ( pfile.bad() ) {
00065         ERR << "Error parsing " << file_r << std::endl;
00066         ZYPP_THROW(Exception("Error parsing " + file_r.asString()));
00067       }
00068       if ( pfile.fail() ) {
00069         break; // no data on last line
00070       }
00071       std::string tag = str::stripFirstWord( value, true );
00072 
00073       if ( tag.size() ) {
00074         *result = MediaProductEntry( tag, value );
00075       }
00076     }
00077   }
00078 
00087   template <class _OutputIterator>
00088   void productsInMedia( const Url & url_r, _OutputIterator result )
00089   {
00090     media::MediaManager media_mgr;
00091     // open the media
00092     media::MediaId id = media_mgr.open(url_r);
00093     media_mgr.attach(id);
00094     Pathname products_file = Pathname("media.1/products");
00095 
00096     try  {
00097       media_mgr.provideFile (id, products_file);
00098       products_file = media_mgr.localPath (id, products_file);
00099       scanProductsFile (products_file, result);
00100     }
00101     catch ( const Exception & excpt ) {
00102       ZYPP_CAUGHT(excpt);
00103       MIL << "No products description found on the Url" << std::endl;
00104     }
00105     media_mgr.release(id, "");
00106  }
00107 
00116   void productsInMedia( const Url & url_r, MediaProductSet &set )
00117   {
00118     productsInMedia(url_r, std::inserter(set, set.end()));
00119   }
00120 
00121 } // ns zypp
00122 
00123 #endif
00124 
00125 // vim: set ts=2 sts=2 sw=2 et ai: