1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
10 /** \file zypp/HistoryLogData.cc
15 #include "zypp/base/PtrTypes.h"
16 #include "zypp/base/String.h"
17 #include "zypp/base/Logger.h"
18 #include "zypp/parser/ParseException.h"
20 #include "zypp/HistoryLogData.h"
24 ///////////////////////////////////////////////////////////////////
27 using parser::ParseException;
29 ///////////////////////////////////////////////////////////////////
31 // class HistoryActionID
33 ///////////////////////////////////////////////////////////////////
35 const HistoryActionID HistoryActionID::NONE (HistoryActionID::NONE_e);
36 const HistoryActionID HistoryActionID::INSTALL (HistoryActionID::INSTALL_e);
37 const HistoryActionID HistoryActionID::REMOVE (HistoryActionID::REMOVE_e);
38 const HistoryActionID HistoryActionID::REPO_ADD (HistoryActionID::REPO_ADD_e);
39 const HistoryActionID HistoryActionID::REPO_REMOVE (HistoryActionID::REPO_REMOVE_e);
40 const HistoryActionID HistoryActionID::REPO_CHANGE_ALIAS (HistoryActionID::REPO_CHANGE_ALIAS_e);
41 const HistoryActionID HistoryActionID::REPO_CHANGE_URL (HistoryActionID::REPO_CHANGE_URL_e);
43 HistoryActionID::HistoryActionID(const std::string & strval_r)
44 : _id(parse(strval_r))
47 HistoryActionID::ID HistoryActionID::parse( const std::string & strval_r )
49 typedef std::map<std::string,ID> MapType;
50 static MapType _table;
54 _table["install"] = INSTALL_e;
55 _table["remove"] = REMOVE_e;
56 _table["radd"] = REPO_ADD_e;
57 _table["rremove"] = REPO_REMOVE_e;
58 _table["ralias"] = REPO_CHANGE_ALIAS_e;
59 _table["rurl"] = REPO_CHANGE_URL_e;
60 _table["NONE"] = _table["none"] = NONE_e;
63 MapType::const_iterator it = _table.find( strval_r );
64 if ( it != _table.end() )
67 WAR << "Unknown history action ID '" + strval_r + "'" << endl;
72 const std::string & HistoryActionID::asString( bool pad ) const
74 typedef std::pair<std::string,std::string> PairType;
75 typedef std::map<ID, PairType> MapType;
76 static MapType _table;
79 // initialize it pad(7) (for now)
80 _table[INSTALL_e] = PairType( "install" , "install" );
81 _table[REMOVE_e] = PairType( "remove" , "remove " );
82 _table[REPO_ADD_e] = PairType( "radd" , "radd " );
83 _table[REPO_REMOVE_e] = PairType( "rremove" , "rremove" );
84 _table[REPO_CHANGE_ALIAS_e] = PairType( "ralias" , "ralias " );
85 _table[REPO_CHANGE_URL_e] = PairType( "rurl" , "rurl " );
86 _table[NONE_e] = PairType( "NONE" , "NONE " );
89 return( pad ? _table[_id].second : _table[_id].first );
92 std::ostream & operator << (std::ostream & str, const HistoryActionID & id)
93 { return str << id.asString(); }
95 ///////////////////////////////////////////////////////////////////
97 ///////////////////////////////////////////////////////////////////
99 // class HistoryLogData::Impl
101 ///////////////////////////////////////////////////////////////////
102 class HistoryLogData::Impl
105 Impl( FieldVector & fields_r, size_type expect_r )
107 _checkFields( fields_r, expect_r );
108 _field.swap( fields_r );
109 // For whatever reason writer is ' '-padding the action field
110 // but we don't want to modify the vector before we moved it.
111 _field[ACTION_INDEX] = str::trim( _field[ACTION_INDEX] );
112 _action = HistoryActionID( _field[ACTION_INDEX] );
115 Impl( FieldVector & fields_r, HistoryActionID action_r, size_type expect_r )
117 _checkFields( fields_r, expect_r );
118 // For whatever reason writer is ' '-padding the action field
119 // but we don't want to modify the vector before we moved it.
120 std::string trimmed( str::trim( fields_r[ACTION_INDEX] ) );
121 _action = HistoryActionID( trimmed );
122 if ( _action != action_r )
124 ZYPP_THROW( ParseException( str::form( "Bad action id. Got %s, expected %s.",
125 _action.asString().c_str(),
126 action_r.asString().c_str() ) ) );
128 _field.swap( fields_r );
129 // now adjust action field:
130 _field[ACTION_INDEX] = trimmed;
133 void _checkFields( const FieldVector & fields_r, size_type expect_r )
135 if ( expect_r < 2 ) // at least 2 fields (date and action) are required
137 if ( fields_r.size() < expect_r )
139 ZYPP_THROW( ParseException( str::form( "Bad number of fields. Got %zd, expected at least %zd.",
145 _date = Date( fields_r[DATE_INDEX], HISTORY_LOG_DATE_FORMAT );
147 catch ( const std::exception & excpt )
149 ZYPP_THROW( ParseException( excpt.what() ) ); // invalid date format
151 // _action handled later
157 HistoryActionID _action;
160 ///////////////////////////////////////////////////////////////////
162 // class HistoryLogData
164 ///////////////////////////////////////////////////////////////////
166 HistoryLogData::HistoryLogData( FieldVector & fields_r, size_type expect_r )
167 : _pimpl( new Impl( fields_r, expect_r ) )
170 HistoryLogData::HistoryLogData( FieldVector & fields_r, HistoryActionID expectedId_r, size_type expect_r )
171 : _pimpl( new Impl( fields_r, expectedId_r, expect_r ) )
174 HistoryLogData::~HistoryLogData()
177 HistoryLogData::Ptr HistoryLogData::create( FieldVector & fields_r )
179 if ( fields_r.size() >= 2 )
181 // str::trim( _field[ACTION_INDEX] );
182 switch ( HistoryActionID( str::trim( fields_r[ACTION_INDEX] ) ).toEnum() )
184 #define OUTS(E,T) case HistoryActionID::E: return Ptr( new T( fields_r ) ); break;
185 OUTS( INSTALL_e, HistoryLogDataInstall );
186 OUTS( REMOVE_e, HistoryLogDataRemove );
187 OUTS( REPO_ADD_e, HistoryLogDataRepoAdd );
188 OUTS( REPO_REMOVE_e, HistoryLogDataRepoRemove );
189 OUTS( REPO_CHANGE_ALIAS_e, HistoryLogDataRepoAliasChange );
190 OUTS( REPO_CHANGE_URL_e, HistoryLogDataRepoUrlChange );
192 // intentionally no default:
193 case HistoryActionID::NONE_e:
197 // unknown action or invalid fields? Ctor will accept or throw.
198 return Ptr( new HistoryLogData( fields_r ) );
201 bool HistoryLogData::empty() const
202 { return _pimpl->_field.empty(); }
204 HistoryLogData::size_type HistoryLogData::size() const
205 { return _pimpl->_field.size(); }
207 HistoryLogData::const_iterator HistoryLogData::begin() const
208 { return _pimpl->_field.begin(); }
210 HistoryLogData::const_iterator HistoryLogData::end() const
211 { return _pimpl->_field.end(); }
213 const std::string & HistoryLogData::optionalAt( size_type idx_r ) const
215 static const std::string _empty;
216 return( idx_r < size() ? _pimpl->_field[idx_r] : _empty );
219 const std::string & HistoryLogData::at( size_type idx_r ) const
220 { return _pimpl->_field.at( idx_r ); }
223 Date HistoryLogData::date() const
224 { return _pimpl->_date; }
226 HistoryActionID HistoryLogData::action() const
227 { return _pimpl->_action; }
230 std::ostream & operator<<( std::ostream & str, const HistoryLogData & obj )
231 { return str << str::joinEscaped( obj.begin(), obj.end(), '|' ); }
233 ///////////////////////////////////////////////////////////////////
234 // class HistoryLogDataInstall
235 ///////////////////////////////////////////////////////////////////
236 HistoryLogDataInstall::HistoryLogDataInstall( FieldVector & fields_r )
237 : HistoryLogData( fields_r )
239 std::string HistoryLogDataInstall::name() const { return optionalAt( NAME_INDEX ); }
240 Edition HistoryLogDataInstall::edition() const { return Edition( optionalAt( EDITION_INDEX ) ); }
241 Arch HistoryLogDataInstall::arch() const { return Arch( optionalAt( ARCH_INDEX ) ); }
242 std::string HistoryLogDataInstall::reqby() const { return optionalAt( REQBY_INDEX ); }
243 std::string HistoryLogDataInstall::repoAlias() const { return optionalAt( REPOALIAS_INDEX ); }
244 CheckSum HistoryLogDataInstall::checksum() const { return optionalAt( CHEKSUM_INDEX ); }
245 std::string HistoryLogDataInstall::userdata() const { return optionalAt( USERDATA_INDEX ); }
247 ///////////////////////////////////////////////////////////////////
248 // class HistoryLogDataRemove
249 ///////////////////////////////////////////////////////////////////
250 HistoryLogDataRemove::HistoryLogDataRemove( FieldVector & fields_r )
251 : HistoryLogData( fields_r )
253 std::string HistoryLogDataRemove::name() const { return optionalAt( NAME_INDEX ); }
254 Edition HistoryLogDataRemove::edition() const { return Edition( optionalAt( EDITION_INDEX ) ); }
255 Arch HistoryLogDataRemove::arch() const { return Arch( optionalAt( ARCH_INDEX ) ); }
256 std::string HistoryLogDataRemove::reqby() const { return optionalAt( REQBY_INDEX ); }
257 std::string HistoryLogDataRemove::userdata() const { return optionalAt( USERDATA_INDEX ); }
259 ///////////////////////////////////////////////////////////////////
260 // class HistoryLogDataRepoAdd
261 ///////////////////////////////////////////////////////////////////
262 HistoryLogDataRepoAdd::HistoryLogDataRepoAdd( FieldVector & fields_r )
263 : HistoryLogData( fields_r )
265 std::string HistoryLogDataRepoAdd::alias() const { return optionalAt( ALIAS_INDEX ); }
266 Url HistoryLogDataRepoAdd::url() const { return optionalAt( URL_INDEX ); }
267 std::string HistoryLogDataRepoAdd::userdata() const { return optionalAt( USERDATA_INDEX ); }
269 ///////////////////////////////////////////////////////////////////
270 // class HistoryLogDataRepoRemove
271 ///////////////////////////////////////////////////////////////////
272 HistoryLogDataRepoRemove::HistoryLogDataRepoRemove( FieldVector & fields_r )
273 : HistoryLogData( fields_r )
275 std::string HistoryLogDataRepoRemove::alias() const { return optionalAt( ALIAS_INDEX ); }
276 std::string HistoryLogDataRepoRemove::userdata() const { return optionalAt( USERDATA_INDEX ); }
278 ///////////////////////////////////////////////////////////////////
279 // class HistoryLogDataRepoAliasChange
280 ///////////////////////////////////////////////////////////////////
281 HistoryLogDataRepoAliasChange::HistoryLogDataRepoAliasChange( FieldVector & fields_r )
282 : HistoryLogData( fields_r )
284 std::string HistoryLogDataRepoAliasChange::oldAlias() const { return optionalAt( OLDALIAS_INDEX ); }
285 std::string HistoryLogDataRepoAliasChange::newAlias() const { return optionalAt( NEWALIAS_INDEX ); }
286 std::string HistoryLogDataRepoAliasChange::userdata() const { return optionalAt( USERDATA_INDEX ); }
288 ///////////////////////////////////////////////////////////////////
289 // class HistoryLogDataRepoUrlChange
290 ///////////////////////////////////////////////////////////////////
291 HistoryLogDataRepoUrlChange::HistoryLogDataRepoUrlChange( FieldVector & fields_r )
292 : HistoryLogData( fields_r )
294 std::string HistoryLogDataRepoUrlChange::alias() const { return optionalAt( ALIAS_INDEX ); }
295 Url HistoryLogDataRepoUrlChange::newUrl() const { return optionalAt( NEWURL_INDEX ); }
296 std::string HistoryLogDataRepoUrlChange::userdata() const { return optionalAt( USERDATA_INDEX ); }
299 ///////////////////////////////////////////////////////////////////