HistoryLog reader:
[platform/upstream/libzypp.git] / zypp / HistoryLogData.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9
10 /** \file zypp/HistoryLogData.cc
11  *
12  */
13 #include <sstream>
14
15 #include "zypp/base/PtrTypes.h"
16 #include "zypp/base/String.h"
17 #include "zypp/base/Logger.h"
18 #include "zypp/parser/ParseException.h"
19
20 #include "zypp/HistoryLogData.h"
21
22 using namespace std;
23
24 namespace zypp
25 {
26   using parser::ParseException;
27
28
29   ///////////////////////////////////////////////////////////////////
30   //
31   //    CLASS NAME : HistoryActionID
32   //
33   ///////////////////////////////////////////////////////////////////
34
35   static std::map<std::string,HistoryActionID::ID> _table;
36
37   const HistoryActionID HistoryActionID::NONE(HistoryActionID::NONE_e);
38   const HistoryActionID HistoryActionID::INSTALL(HistoryActionID::INSTALL_e);
39   const HistoryActionID HistoryActionID::REMOVE(HistoryActionID::REMOVE_e);
40   const HistoryActionID HistoryActionID::REPO_ADD(HistoryActionID::REPO_ADD_e);
41   const HistoryActionID HistoryActionID::REPO_REMOVE(HistoryActionID::REPO_REMOVE_e);
42   const HistoryActionID HistoryActionID::REPO_CHANGE_ALIAS(HistoryActionID::REPO_CHANGE_ALIAS_e);
43   const HistoryActionID HistoryActionID::REPO_CHANGE_URL(HistoryActionID::REPO_CHANGE_URL_e);
44
45   HistoryActionID::HistoryActionID(const std::string & strval_r)
46     : _id(parse(strval_r))
47   {}
48
49   HistoryActionID::ID HistoryActionID::parse(const std::string & strval_r)
50   {
51     if (_table.empty())
52     {
53       // initialize it
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"] = HistoryActionID::NONE_e;
61     }
62
63     std::map<std::string,HistoryActionID::ID>::const_iterator it =
64       _table.find(strval_r);
65
66     if (it == _table.end())
67       WAR << "Unknown history action ID '" + strval_r + "'";
68
69     return it->second;
70   }
71
72
73   const std::string & HistoryActionID::asString(bool pad) const
74   {
75     static std::map<ID, std::string> _table;
76     if ( _table.empty() )
77     {
78       // initialize it
79       _table[INSTALL_e]           = "install";
80       _table[REMOVE_e]            = "remove";
81       _table[REPO_ADD_e]          = "radd";
82       _table[REPO_REMOVE_e]       = "rremove";
83       _table[REPO_CHANGE_ALIAS_e] = "ralias";
84       _table[REPO_CHANGE_URL_e]   = "rurl";
85       _table[NONE_e]              = "NONE";
86     }
87     // add spaces so that the size of the returned string is always 7 (for now)
88     if (pad)
89       return _table[_id].append(7 - _table[_id].size(), ' ');
90     return _table[_id];
91   }
92
93   std::ostream & operator << (std::ostream & str, const HistoryActionID & id)
94   { return str << id.asString(); }
95
96   ///////////////////////////////////////////////////////////////////
97
98
99   /////////////////////////////////////////////////////////////////////
100   //
101   // CLASS NAME: HistoryItem
102   //
103   /////////////////////////////////////////////////////////////////////
104
105   HistoryItem::HistoryItem(FieldVector & fields)
106   {
107     if (fields.size() <= 2)
108       ZYPP_THROW(ParseException(
109         str::form("Bad number of fields. Got %ld, expected more than %d.",
110           fields.size(), 2)));
111
112     date = Date(fields[0], HISTORY_LOG_DATE_FORMAT);
113     action = HistoryActionID(str::trim(fields[1]));
114   }
115
116   void HistoryItem::dumpTo(ostream & str) const
117   {
118     str << date.form(HISTORY_LOG_DATE_FORMAT) << "|" << action.asString();
119   }
120
121   ostream & operator<<(ostream & str, const HistoryItem & obj)
122   {
123     obj.dumpTo(str);
124     return str;
125   }
126
127
128   /////////////////////////////////////////////////////////////////////
129   //
130   // CLASS NAME: HistoryItemInstall
131   //
132   /////////////////////////////////////////////////////////////////////
133
134   HistoryItemInstall::HistoryItemInstall(FieldVector & fields)
135     : HistoryItem(fields)
136   {
137     if (fields.size() != 8)
138       ZYPP_THROW(ParseException(
139         str::form("Bad number of fields. Got %lu, expected %u.",
140           fields.size(), 8)));
141
142     name      = fields[2];
143     edition   = Edition(fields[3]);
144     arch      = Arch(fields[4]);
145     reqby     = fields[5];
146     repoalias = fields[6];
147     checksum  = CheckSum::sha(fields[7]);
148   }
149
150   void HistoryItemInstall::dumpTo(ostream & str) const
151   {
152     HistoryItem::dumpTo(str);
153     str << "|"
154       << name << "|"
155       << edition << "|"
156       << arch << "|"
157       << reqby << "|"
158       << repoalias << "|"
159       << checksum;
160   }
161
162   ostream & operator<<(ostream & str, const HistoryItemInstall & obj)
163   {
164     obj.dumpTo(str);
165     return str;
166   }
167
168
169   /////////////////////////////////////////////////////////////////////
170   //
171   // CLASS NAME: HistoryItemRemove
172   //
173   /////////////////////////////////////////////////////////////////////
174
175   HistoryItemRemove::HistoryItemRemove(FieldVector & fields)
176     : HistoryItem(fields)
177   {
178     if (fields.size() != 6)
179       ZYPP_THROW(ParseException(
180         str::form("Bad number of fields. Got %lu, expected %u.",
181           fields.size(), 6)));
182
183     name      = fields[2];
184     edition   = Edition(fields[3]);
185     arch      = Arch(fields[4]);
186     reqby     = fields[5];
187   }
188
189   void HistoryItemRemove::dumpTo(ostream & str) const
190   {
191     HistoryItem::dumpTo(str);
192     str << "|"
193       << name << "|"
194       << edition << "|"
195       << arch << "|"
196       << reqby;
197   }
198
199   ostream & operator<<(ostream & str, const HistoryItemRemove & obj)
200   {
201     obj.dumpTo(str);
202     return str;
203   }
204
205
206   /////////////////////////////////////////////////////////////////////
207   //
208   // CLASS NAME: HistoryItemRepoAdd
209   //
210   /////////////////////////////////////////////////////////////////////
211
212   HistoryItemRepoAdd::HistoryItemRepoAdd(FieldVector & fields)
213     : HistoryItem(fields)
214   {
215     if (fields.size() != 4)
216       ZYPP_THROW(ParseException(
217         str::form("Bad number of fields. Got %lu, expected %u.",
218           fields.size(), 4)));
219
220     alias = fields[2];
221     url = Url(fields[3]);
222   }
223
224   void HistoryItemRepoAdd::dumpTo(ostream & str) const
225   {
226     HistoryItem::dumpTo(str);
227     str << "|"
228       << alias << "|"
229       << url;
230   }
231
232   ostream & operator<<(ostream & str, const HistoryItemRepoAdd & obj)
233   {
234     obj.dumpTo(str);
235     return str;
236   }
237
238
239   /////////////////////////////////////////////////////////////////////
240   //
241   // CLASS NAME: HistoryItemRepoRemove
242   //
243   /////////////////////////////////////////////////////////////////////
244
245   HistoryItemRepoRemove::HistoryItemRepoRemove(FieldVector & fields)
246     : HistoryItem(fields)
247   {
248     if (fields.size() != 3)
249       ZYPP_THROW(ParseException(
250         str::form("Bad number of fields. Got %lu, expected %u.",
251           fields.size(), 3)));
252
253     alias = fields[2];
254   }
255
256   void HistoryItemRepoRemove::dumpTo(ostream & str) const
257   {
258     HistoryItem::dumpTo(str);
259     str << "|" << alias;
260   }
261
262   ostream & operator<<(ostream & str, const HistoryItemRepoRemove & obj)
263   {
264     obj.dumpTo(str);
265     return str;
266   }
267
268
269   /////////////////////////////////////////////////////////////////////
270   //
271   // CLASS NAME: HistoryItemRepoAliasChange
272   //
273   /////////////////////////////////////////////////////////////////////
274
275   HistoryItemRepoAliasChange::HistoryItemRepoAliasChange(FieldVector & fields)
276     : HistoryItem(fields)
277   {
278     if (fields.size() != 4)
279       ZYPP_THROW(ParseException(
280         str::form("Bad number of fields. Got %lu, expected %u.",
281           fields.size(), 4)));
282
283     oldalias = fields[2];
284     newalias = fields[3];
285   }
286
287   void HistoryItemRepoAliasChange::dumpTo(ostream & str) const
288   {
289     HistoryItem::dumpTo(str);
290     str << "|" << oldalias << "|" << newalias;
291   }
292
293   ostream & operator<<(ostream & str, const HistoryItemRepoAliasChange & obj)
294   {
295     obj.dumpTo(str);
296     return str;
297   }
298
299
300   /////////////////////////////////////////////////////////////////////
301   //
302   // CLASS NAME: HistoryItemRepoUrlChange
303   //
304   /////////////////////////////////////////////////////////////////////
305
306   HistoryItemRepoUrlChange::HistoryItemRepoUrlChange(FieldVector & fields)
307     : HistoryItem(fields)
308   {
309     if (fields.size() != 4)
310       ZYPP_THROW(ParseException(
311         str::form("Bad number of fields. Got %lu, expected %u.",
312           fields.size(), 4)));
313
314     alias = fields[2];
315     newurl = Url(fields[3]);
316   }
317
318   void HistoryItemRepoUrlChange::dumpTo(ostream & str) const
319   {
320     HistoryItem::dumpTo(str);
321     str << "|" << alias << "|" << newurl;
322   }
323
324   ostream & operator<<(ostream & str, const HistoryItemRepoUrlChange & obj)
325   {
326     obj.dumpTo(str);
327     return str;
328   }
329
330
331 }