Imported Upstream version 17.14.0
[platform/upstream/libzypp.git] / zypp / parser / HistoryLogReader.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9
10 /** \file zypp/parser/HistoryLogReader.h
11  *
12  */
13 #ifndef ZYPP_PARSER_HISTORYLOGREADER_H_
14 #define ZYPP_PARSER_HISTORYLOGREADER_H_
15
16 #include "zypp/base/PtrTypes.h"
17 #include "zypp/base/Flags.h"
18 #include "zypp/ProgressData.h"
19 #include "zypp/Pathname.h"
20
21 #include "zypp/HistoryLogData.h"
22
23 ///////////////////////////////////////////////////////////////////
24 namespace zypp
25 {
26
27   class Date;
28
29   ///////////////////////////////////////////////////////////////////
30   namespace parser
31   {
32
33   ///////////////////////////////////////////////////////////////////
34   /// \class HistoryLogReader
35   /// \brief Zypp history file parser
36   /// \ingroup g_ZyppHistory
37   /// \ingroup g_ZyppParser
38   ///
39   /// Reads a zypp history log file and calls the \ref ProcessData callback
40   /// passed in the constructor for each valid history line read. The callbacks
41   /// return value indicates whether to continue parsing.
42   ///
43   /// \code
44   ///   std::vector<HistoryLogData::Ptr> history;
45   ///   parser::HistoryLogReader parser( ZConfig::instance().historyLogFile(),
46   ///                                    HistoryLogReader::Options(),
47   ///     [&history]( HistoryLogData::Ptr ptr )->bool {
48   ///       history.push_back( ptr );
49   ///       return true;
50   ///     } );
51   ///   parser.readAll();
52   ///   ...
53   ///   if ( history[0]->action() == HistoryActionID::INSTALL )
54   ///   {
55   ///     // generic access to data fields plain string values:
56   ///     MIL << (*p)[HistoryLogDataInstall::USERDATA_INDEX] << endl;
57   ///
58   ///     // The same maybe more convenient though derived classes:
59   ///     HistoryLogDataInstall::Ptr ip( dynamic_pointer_cast<HistoryLogDataInstall>( p ) );
60   ///     MIL << ip->userdata() << endl;
61   ///   }
62   /// \endcode
63   /// \see \ref HistoryLogData for how to access the individual data fields.
64   ///
65   ///////////////////////////////////////////////////////////////////
66   class HistoryLogReader
67   {
68   public:
69
70     enum OptionBits     ///< Parser option flags
71     {
72       IGNORE_INVALID_ITEMS      = (1 << 0)      ///< ignore invalid items and continue parsing
73     };
74     ZYPP_DECLARE_FLAGS( Options, OptionBits );
75
76   public:
77     /** Callback type to consume a single history line split into fields.
78      * The return value indicates whether to continue parsing.
79      */
80     typedef function< bool( const HistoryLogData::Ptr & )> ProcessData;
81
82     /** Ctor taking file to parse and data consumer callback.
83      * As \a options_r argument pass \c HistoryLogReader::Options() to
84      * use the default stettings, or an OR'ed combination of \ref OptionBits.
85      */
86     HistoryLogReader( const Pathname & historyFile_r, const Options & options_r, const ProcessData & callback_r );
87
88     ~HistoryLogReader();
89
90     /**
91      * Read the whole log file.
92      *
93      * \param progress An optional progress data receiver function.
94      */
95     void readAll( const ProgressData::ReceiverFnc & progress = ProgressData::ReceiverFnc() );
96
97     /**
98      * Read log from specified \a date.
99      *
100      * \param date     Date from which to read.
101      * \param progress An optional progress data receiver function.
102      *
103      * \see readFromTo()
104      */
105     void readFrom( const Date & date, const ProgressData::ReceiverFnc & progress = ProgressData::ReceiverFnc() );
106
107     /**
108      * Read log between \a fromDate and \a toDate.
109      *
110      * The date comparison's precision goes to seconds. Omitted time parts
111      * get replaced by zeroes, so if e.g. the time is not specified at all, the
112      * date means midnight of the specified date. So
113      *
114      * <code>
115      * fromDate = Date("2009-01-01", "%Y-%m-%d");
116      * toDate   = Date("2009-01-02", "%Y-%m-%d");
117      * </code>
118      *
119      * will yield log entries from midnight of January, 1st untill
120      * one second before midnight of January, 2nd.
121      *
122      * \param fromDate Date from which to read.
123      * \param toDate   Date on which to stop reading.
124      * \param progress An optional progress data receiver function.
125      */
126     void readFromTo( const Date & fromDate, const Date & toDate, const ProgressData::ReceiverFnc & progress = ProgressData::ReceiverFnc() );
127
128     /**
129      * Set the reader to ignore invalid log entries and continue with the rest.
130      *
131      * \param ignoreInvalid <tt>true</tt> will cause the reader to ignore invalid entries
132      */
133     void setIgnoreInvalidItems( bool ignoreInvalid = false );
134
135     /**
136      * Whether the reader is set to ignore invalid log entries.
137      *
138      * \see setIngoreInvalidItems()
139      */
140     bool ignoreInvalidItems() const;
141
142   private:
143     /** Implementation */
144     struct Impl;
145     RW_pointer<Impl,rw_pointer::Scoped<Impl> > _pimpl;
146   };
147
148   /** \relates HistoryLogReader::Options */
149   ZYPP_DECLARE_OPERATORS_FOR_FLAGS( HistoryLogReader::Options );
150
151   ///////////////////////////////////////////////////////////////////
152
153   } // namespace parser
154   /////////////////////////////////////////////////////////////////
155 } // namespace zypp
156 ///////////////////////////////////////////////////////////////////
157
158 #endif /* ZYPP_PARSER_HISTORYLOGREADER_H_ */