Fix data corruption, undefined return value, unneeded static var
[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   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);
42
43   HistoryActionID::HistoryActionID(const std::string & strval_r)
44     : _id(parse(strval_r))
45   {}
46
47   HistoryActionID::ID HistoryActionID::parse( const std::string & strval_r )
48   {
49     typedef std::map<std::string,ID> MapType;
50     static MapType _table;
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"] = NONE_e;
61     }
62
63     MapType::const_iterator it = _table.find( strval_r );
64     if ( it == _table.end() )
65     {
66       WAR << "Unknown history action ID '" + strval_r + "'" << endl;
67       return NONE_e;
68     }
69     return it->second;
70   }
71
72
73   const std::string & HistoryActionID::asString( bool pad ) const
74   {
75     typedef std::pair<std::string,std::string> PairType;
76     typedef std::map<ID, PairType> MapType;
77     static MapType _table;
78     if ( _table.empty() )
79     {
80       // initialize it                                     pad(7) (for now)
81       _table[INSTALL_e]           = PairType( "install" , "install" );
82       _table[REMOVE_e]            = PairType( "remove"  , "remove " );
83       _table[REPO_ADD_e]          = PairType( "radd"    , "radd   " );
84       _table[REPO_REMOVE_e]       = PairType( "rremove" , "rremove" );
85       _table[REPO_CHANGE_ALIAS_e] = PairType( "ralias"  , "ralias " );
86       _table[REPO_CHANGE_URL_e]   = PairType( "rurl"    , "rurl   " );
87       _table[NONE_e]              = PairType( "NONE"    , "NONE   " );
88     }
89
90     return( pad ? _table[_id].second : _table[_id].first );
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 %zd, 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 %zu, 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 %zu, 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 %zu, 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 %zu, 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 %zu, 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 %zu, 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 }