HistoryLogReader added
[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   /////////////////////////////////////////////////////////////////////
117   //
118   // CLASS NAME: HistoryItemInstall
119   //
120   /////////////////////////////////////////////////////////////////////
121
122   HistoryItemInstall::HistoryItemInstall(FieldVector & fields)
123     : HistoryItem(fields)
124   {
125     if (fields.size() != 8)
126       ZYPP_THROW(ParseException(
127         str::form("Bad number of fields. Got %lu, expected %u.",
128           fields.size(), 8)));
129
130     name      = fields[2];
131     edition   = Edition(fields[3]);
132     arch      = Arch(fields[4]);
133     reqby     = fields[5];
134     repoalias = fields[6];
135     checksum  = CheckSum::sha(fields[7]);
136   }
137
138   const std::string HistoryItemInstall::asString() const
139   {
140     ostringstream str;
141     str
142       << date.form(HISTORY_LOG_DATE_FORMAT) << "|"
143       << action.asString() << "|"
144       << name << "|"
145       << edition << "|"
146       << arch << "|"
147       << reqby << "|"
148       << repoalias << "|"
149       << checksum;
150     return str.str();
151   }
152
153   std::ostream & operator<<(std::ostream & str, const HistoryItemInstall & obj)
154   {
155     return str << obj.asString();
156   }
157
158
159   /////////////////////////////////////////////////////////////////////
160   //
161   // CLASS NAME: HistoryItemRemove
162   //
163   /////////////////////////////////////////////////////////////////////
164
165   HistoryItemRemove::HistoryItemRemove(FieldVector & fields)
166     : HistoryItem(fields)
167   {
168     if (fields.size() != 6)
169       ZYPP_THROW(ParseException(
170         str::form("Bad number of fields. Got %lu, expected %u.",
171           fields.size(), 6)));
172
173     name      = fields[2];
174     edition   = Edition(fields[3]);
175     arch      = Arch(fields[4]);
176     reqby     = fields[5];
177   }
178
179
180   /////////////////////////////////////////////////////////////////////
181   //
182   // CLASS NAME: HistoryItemRepoAdd
183   //
184   /////////////////////////////////////////////////////////////////////
185
186   HistoryItemRepoAdd::HistoryItemRepoAdd(FieldVector & fields)
187     : HistoryItem(fields)
188   {
189     if (fields.size() != 4)
190       ZYPP_THROW(ParseException(
191         str::form("Bad number of fields. Got %lu, expected %u.",
192           fields.size(), 4)));
193
194     alias = fields[2];
195     url = Url(fields[3]);
196   }
197
198
199   /////////////////////////////////////////////////////////////////////
200   //
201   // CLASS NAME: HistoryItemRepoRemove
202   //
203   /////////////////////////////////////////////////////////////////////
204
205   HistoryItemRepoRemove::HistoryItemRepoRemove(FieldVector & fields)
206     : HistoryItem(fields)
207   {
208     if (fields.size() != 3)
209       ZYPP_THROW(ParseException(
210         str::form("Bad number of fields. Got %lu, expected %u.",
211           fields.size(), 3)));
212
213     alias = fields[2];
214   }
215
216
217   /////////////////////////////////////////////////////////////////////
218   //
219   // CLASS NAME: HistoryItemRepoAliasChange
220   //
221   /////////////////////////////////////////////////////////////////////
222
223   HistoryItemRepoAliasChange::HistoryItemRepoAliasChange(FieldVector & fields)
224     : HistoryItem(fields)
225   {
226     if (fields.size() != 4)
227       ZYPP_THROW(ParseException(
228         str::form("Bad number of fields. Got %lu, expected %u.",
229           fields.size(), 4)));
230
231     oldalias = fields[2];
232     newalias = fields[3];
233   }
234
235
236   /////////////////////////////////////////////////////////////////////
237   //
238   // CLASS NAME: HistoryItemRepoUrlChange
239   //
240   /////////////////////////////////////////////////////////////////////
241
242   HistoryItemRepoUrlChange::HistoryItemRepoUrlChange(FieldVector & fields)
243     : HistoryItem(fields)
244   {
245     if (fields.size() != 4)
246       ZYPP_THROW(ParseException(
247         str::form("Bad number of fields. Got %lu, expected %u.",
248           fields.size(), 4)));
249
250     alias = fields[2];
251     newurl = Url(fields[3]);
252   }
253
254
255 }