00001
00002
00003
00004
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
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00071
00072
00073
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
00085 getline(file, entry_r.vendor);
00086
00087 getline(file, entry_r.timestamp);
00088
00089 std::string media_count_str;
00090
00091 getline(file, buffer);
00092 regex is_digit_rx("^[\\d]+$");
00093 boost::smatch what_digit;
00094
00095
00096
00097 bool consume = false;
00098
00099 if (boost::regex_match(buffer, what_digit, is_digit_rx))
00100 {
00101
00102 str::strtonum(buffer, entry_r.count);
00103
00104 consume = true;
00105 }
00106 else
00107 {
00108
00109 entry_r.count = 1;
00110 }
00111
00112 while (file && !file.eof())
00113 {
00114
00115 if (consume)
00116 getline(file, buffer);
00117
00118
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 }
00149 }
00152 }