1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
10 /** \file HistoryLogReader.cc
15 #include "zypp/base/InputStream.h"
16 #include "zypp/base/IOStream.h"
17 #include "zypp/base/Logger.h"
18 #include "zypp/parser/ParseException.h"
20 #include "zypp/parser/HistoryLogReader.h"
24 ///////////////////////////////////////////////////////////////////
26 { /////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////
29 { /////////////////////////////////////////////////////////////////
32 /////////////////////////////////////////////////////////////////////
34 // CLASS NAME: HistoryLogReader::Impl
36 /////////////////////////////////////////////////////////////////////
38 struct HistoryLogReader::Impl
40 Impl( const Pathname & historyFile, const ProcessItem & callback );
44 HistoryItem::Ptr createHistoryItem(HistoryItem::FieldVector & fields);
46 void readAll(const ProgressData::ReceiverFnc & progress);
49 ProcessItem _callback;
53 HistoryLogReader::Impl::Impl( const Pathname & historyFile,
54 const ProcessItem & callback )
55 : _filename(historyFile), _callback(callback), _ignoreInvalid(false)
59 HistoryLogReader::Impl::createHistoryItem(HistoryItem::FieldVector & fields)
61 HistoryActionID aid(str::trim(fields[1]));
64 case HistoryActionID::INSTALL_e:
65 return HistoryItemInstall::Ptr(new HistoryItemInstall(fields));
68 case HistoryActionID::REMOVE_e:
69 return HistoryItemRemove::Ptr(new HistoryItemRemove(fields));
72 case HistoryActionID::REPO_ADD_e:
73 return HistoryItemRepoAdd::Ptr(new HistoryItemRepoAdd(fields));
76 case HistoryActionID::REPO_REMOVE_e:
77 return HistoryItemRepoRemove::Ptr(new HistoryItemRepoRemove(fields));
80 case HistoryActionID::REPO_CHANGE_ALIAS_e:
81 return HistoryItemRepoAliasChange::Ptr(new HistoryItemRepoAliasChange(fields));
84 case HistoryActionID::REPO_CHANGE_URL_e:
85 return HistoryItemRepoUrlChange::Ptr(new HistoryItemRepoUrlChange(fields));
89 WAR << "Unknown history log action type: " << fields[1] << endl;
92 return HistoryItem::Ptr();
95 void HistoryLogReader::Impl::readAll(const ProgressData::ReceiverFnc & progress)
97 InputStream is(_filename);
98 iostr::EachLine line(is);
101 pd.sendTo( progress );
104 HistoryItem::FieldVector fields;
105 HistoryItem::Ptr item_ptr;
106 for (; line; line.next(), pd.tick(), fields.clear(), item_ptr.reset())
108 const string & s = *line;
109 if (s[0] == '#') // ignore comments
113 str::splitEscaped(s, back_inserter(fields), "|", true);
115 if (fields.size() <= 2)
116 ZYPP_THROW(ParseException(
117 str::form("Bad number of fields. Got %ld, expected more than %d.",
122 item_ptr = createHistoryItem(fields);
124 catch (const Exception & e)
127 ERR << "Invalid history log entry on line #" << line.lineNo() << ":" << endl
133 str::form("Error in history log on line #%u.", line.lineNo() ) );
141 else if (!_ignoreInvalid)
144 e(str::form("Error in history log on line #%u.", line.lineNo()));
145 e.addHistory("Unknown entry type.");
154 /////////////////////////////////////////////////////////////////////
156 // CLASS NAME: HistoryLogReader
158 /////////////////////////////////////////////////////////////////////
160 HistoryLogReader::HistoryLogReader( const Pathname & historyFile,
161 const ProcessItem & callback )
162 : _pimpl(new HistoryLogReader::Impl(historyFile, callback))
165 HistoryLogReader::~HistoryLogReader()
168 void HistoryLogReader::setIgnoreInvalidItems(bool ignoreInvalid)
169 { _pimpl->_ignoreInvalid = ignoreInvalid; }
171 bool HistoryLogReader::ignoreInvalidItems() const
172 { return _pimpl->_ignoreInvalid; }
174 void HistoryLogReader::readAll(const ProgressData::ReceiverFnc & progress)
175 { _pimpl->readAll(progress); }
178 /////////////////////////////////////////////////////////////////
179 } // namespace parser
180 ///////////////////////////////////////////////////////////////////
181 /////////////////////////////////////////////////////////////////
183 ///////////////////////////////////////////////////////////////////