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   {
00080     ZYPP_THROW(Exception("Can't read media file "+file_r.asString()));
00081   }
00082 
00083   std::string buffer;
00084   // read vendor
00085   getline(file, entry_r.vendor);
00086   // read timestamp
00087   getline(file, entry_r.timestamp);
00088   // skip flags for now
00089   std::string media_count_str;
00090 
00091   getline(file, buffer);
00092   regex is_digit_rx("^[\\d]+$");
00093   boost::smatch what_digit;
00094 
00095   // for the first line here we dont have to consume one if
00096   // there was no media
00097   bool consume = false;
00098 
00099   if (boost::regex_match(buffer, what_digit, is_digit_rx))
00100   {
00101     // it was the media count
00102     str::strtonum(buffer, entry_r.count);
00103     // consume another line
00104     consume = true;
00105   }
00106   else
00107   {
00108     // media count defaults to 1
00109     entry_r.count = 1;
00110   }
00111 
00112   while (file && !file.eof())
00113   {
00114     // probably is the first line after we dont find the media number
00115     if (consume)
00116       getline(file, buffer);
00117 
00118     // only skip once
00119     consume = true;
00120     boost::regex e("^MEDIA([\\d]+)(\\.([_A-Za-z]+)){0,1} (.+)$");
00121     boost::smatch what;
00122     if (boost::regex_match(buffer, what, e, boost::match_extra))
00123     {
00124       if ( what.size() < 5 )
00125       {
00126         ZYPP_THROW (Exception("Can't match MEDIA in '" + buffer + "'"));
00127       }
00128 
00129       dumpRegexpResults(what);
00130 
00131       unsigned int number = 1;
00132       str::strtonum( what[1], number);
00133       std::string lang = what[3];
00134       std::string desc = what[4];
00135       entry_r.alternate_names[number][lang] = desc;
00136     }
00137     else
00138     {
00139       ZYPP_THROW (Exception("Can't find MEDIA in '" + buffer + "'"));
00140     }
00141 
00142   }
00143 }
00144 
00146 } // namespace tagfile
00149 } // namespace parser
00152 } // namespace zypp

Generated on Tue Nov 28 16:49:32 2006 for zypp by  doxygen 1.5.0