MediaMetadataParser.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 #include <fstream>
00014 #include <sstream>
00015 
00016 #include <boost/tokenizer.hpp>
00017 #include <boost/algorithm/string.hpp>
00018 
00019 #include "zypp/base/Logger.h"
00020 #include "zypp/base/Exception.h"
00021 #include "zypp/base/PtrTypes.h"
00022 #include "zypp/base/String.h"
00023 
00024 #include "zypp/parser/tagfile/TagFileParser.h"
00025 #include "zypp/source/susetags/MediaMetadataParser.h"
00026 #include <boost/regex.hpp>
00027 
00028 using namespace std;
00029 using namespace boost;
00030 
00032 namespace zypp
00033 { 
00034 
00035   namespace source
00036   { 
00037 
00038     namespace susetags
00039     { 
00040 
00041       static void dumpRegexpResults( const boost::smatch &what )
00042       {
00043         for ( unsigned int k=0; k < what.size(); k++)
00044         {
00045           DBG << "[match "<< k << "] [" << what[k] << "]" << std::endl; 
00046         }
00047       }
00048       
00049       /*
00050         File:  media
00051         Location  /media.N/ directory on media
00052         Content  two or more lines of ASCII as follows
00053         <vendor>
00054         <YYYYMMDDHHMMSS>
00055         [<media count>]
00056         [<media flags>]
00057         [<media names>]
00058 
00059         Currently defined flags:
00060          
00061         doublesided 
00062         media is double sided, YaST will ask for 'front side' for odd-numbered media and 'back side' for even-numbered media.
00063         The default is single-sided media.
00064 
00065         <media names> may define alternate strings to use when asking to insert a certain media.
00066          They are defined as <key><whitespace><value> pairs, separated by \n.
00067          
00068       */
00069       
00071       //
00072       //        METHOD NAME : Parser::parse
00073       //        METHOD TYPE : void
00074       //
00075       void MediaMetadataParser::parse( const Pathname & file_r, MediaEntry &entry_r )
00076       {
00077         std::ifstream file(file_r.asString().c_str());
00078         if (!file) {
00079             ZYPP_THROW(Exception("Can't read media file "+file_r.asString()));
00080         }
00081 
00082         std::string buffer;
00083         // read vendor
00084         getline(file, entry_r.vendor);
00085         // read timestamp
00086         getline(file, entry_r.timestamp);
00087         // skip flags for now
00088         std::string media_count_str;
00089         
00090         getline(file, buffer);
00091         regex is_digit_rx("^[\\d]+$");
00092         boost::smatch what_digit;
00093         
00094         // for the first line here we dont have to consume one if
00095         // there was no media
00096         bool consume = false;  
00097 
00098         if(boost::regex_match(buffer, what_digit, is_digit_rx))
00099         {
00100           // it was the media count
00101           str::strtonum(buffer, entry_r.count);
00102           // consume another line
00103           consume = true;
00104         }
00105         else
00106         {
00107           // media count defaults to 1
00108           entry_r.count = 1;
00109         }
00110         
00111         while(file && !file.eof())
00112         {
00113           // probably is the first line after we dont find the media number
00114           if (consume)
00115             getline(file, buffer);
00116           
00117           // only skip once
00118           consume = true;
00119           boost::regex e("^MEDIA([\\d]+)(\\.([_A-Za-z]+)){0,1} (.+)$");
00120           boost::smatch what;
00121           if(boost::regex_match(buffer, what, e, boost::match_extra))
00122           {
00123             if ( what.size() < 5 ) {
00124               ZYPP_THROW (Exception("Can't match MEDIA in '" + buffer + "'"));
00125             }
00126            
00127             dumpRegexpResults(what);
00128             
00129             unsigned int number = 1;
00130             str::strtonum( what[1], number);
00131             std::string lang = what[3];
00132             std::string desc = what[4];
00133             entry_r.alternate_names[number][lang] = desc;
00134           }
00135           else
00136           {
00137               ZYPP_THROW (Exception("Can't find MEDIA in '" + buffer + "'"));
00138           }
00139           
00140         }
00141       }
00142 
00144     } // namespace tagfile
00147   } // namespace parser
00150 } // namespace zypp

Generated on Thu Jul 6 00:07:24 2006 for zypp by  doxygen 1.4.6