HistoryLogReader::readFrom() and readFromTo() added.
authorJán Kupec <jkupec@suse.cz>
Wed, 28 Jan 2009 13:35:53 +0000 (14:35 +0100)
committerJán Kupec <jkupec@suse.cz>
Wed, 28 Jan 2009 13:35:53 +0000 (14:35 +0100)
zypp/parser/HistoryLogReader.cc
zypp/parser/HistoryLogReader.h

index e339a76..511b7d0 100644 (file)
@@ -42,8 +42,14 @@ namespace zypp
     {}
 
     HistoryItem::Ptr createHistoryItem(HistoryItem::FieldVector & fields);
+    void parseLine(const string & line, unsigned int lineNr);
 
     void readAll(const ProgressData::ReceiverFnc & progress);
+    void readFrom(const Date & date,
+        const ProgressData::ReceiverFnc & progress);
+    void readFromTo(
+        const Date & fromDate, const Date & toDate,
+        const ProgressData::ReceiverFnc & progress);
 
     Pathname _filename;
     ProcessItem _callback;
@@ -92,6 +98,54 @@ namespace zypp
     return HistoryItem::Ptr();
   }
 
+  void HistoryLogReader::Impl::parseLine(const string & line, unsigned int lineNr)
+  {
+    HistoryItem::FieldVector fields;
+    HistoryItem::Ptr item_ptr;
+
+    // parse into fields
+    str::splitEscaped(line, back_inserter(fields), "|", true);
+
+    if (fields.size() <= 2)
+    {
+      ParseException
+        e(str::form("Error in history log on line #%u.", lineNr));
+      e.addHistory(
+          str::form("Bad number of fields. Got %ld, expected more than %d.",
+              fields.size(), 2));
+      ZYPP_THROW(e);
+    }
+
+    try
+    {
+      item_ptr = createHistoryItem(fields);
+    }
+    catch (const Exception & e)
+    {
+      ZYPP_CAUGHT(e);
+      ERR << "Invalid history log entry on line #" << lineNr << ":" << endl
+          << line << endl;
+
+      if (!_ignoreInvalid)
+      {
+        ParseException newe(
+            str::form("Error in history log on line #%u.", lineNr ) );
+        newe.remember(e);
+        ZYPP_THROW(newe);
+      }
+    }
+
+    if (item_ptr)
+      _callback(item_ptr);
+    else if (!_ignoreInvalid)
+    {
+      ParseException
+        e(str::form("Error in history log on line #%u.", lineNr));
+      e.addHistory("Unknown entry type.");
+      ZYPP_THROW(e);
+    }
+  }
+
   void HistoryLogReader::Impl::readAll(const ProgressData::ReceiverFnc & progress)
   {
     InputStream is(_filename);
@@ -101,55 +155,89 @@ namespace zypp
     pd.sendTo( progress );
     pd.toMin();
 
-    HistoryItem::FieldVector fields;
-    HistoryItem::Ptr item_ptr;
-    for (; line; line.next(), pd.tick(), fields.clear(), item_ptr.reset())
+    for (; line; line.next(), pd.tick() )
     {
-      const string & s = *line;
-      if (s[0] == '#') // ignore comments
+      // ignore comments
+      if ((*line)[0] == '#')
         continue;
 
-      // parse fields
-      str::splitEscaped(s, back_inserter(fields), "|", true);
+      parseLine(*line, line.lineNo());
+    }
 
-      if (fields.size() <= 2)
-        ZYPP_THROW(ParseException(
-          str::form("Bad number of fields. Got %ld, expected more than %d.",
-            fields.size(), 2)));
+    pd.toMax();
+  }
 
-      try
-      {
-        item_ptr = createHistoryItem(fields);
-      }
-      catch (const Exception & e)
-      {
-        ZYPP_CAUGHT(e);
-        ERR << "Invalid history log entry on line #" << line.lineNo() << ":" << endl
-            << s << endl;
+  void HistoryLogReader::Impl::readFrom(const Date & date,
+      const ProgressData::ReceiverFnc & progress)
+  {
+    InputStream is(_filename);
+    iostr::EachLine line(is);
 
-        if (!_ignoreInvalid)
-        {
-          ParseException newe(
-              str::form("Error in history log on line #%u.", line.lineNo() ) );
-          newe.remember(e);
-          ZYPP_THROW(newe);
-        }
-      }
+    ProgressData pd;
+    pd.sendTo( progress );
+    pd.toMin();
+
+    bool pastDate = false;
+    for (; line; line.next(), pd.tick())
+    {
+      const string & s = *line;
 
-      if (item_ptr)
-        _callback(item_ptr);
-      else if (!_ignoreInvalid)
+      // ignore comments
+      if (s[0] == '#')
+        continue;
+
+      if (pastDate)
+        parseLine(s, line.lineNo());
+      else
       {
-        ParseException
-          e(str::form("Error in history log on line #%u.", line.lineNo()));
-        e.addHistory("Unknown entry type.");
-        ZYPP_THROW(e);
+        Date logDate(s.substr(0, s.find('|')), HISTORY_LOG_DATE_FORMAT);
+        if (logDate > date)
+        {
+          pastDate = true;
+          parseLine(s, line.lineNo());
+        }
       }
     }
 
     pd.toMax();
   }
 
+  void HistoryLogReader::Impl::readFromTo(
+      const Date & fromDate, const Date & toDate,
+      const ProgressData::ReceiverFnc & progress)
+  {
+    InputStream is(_filename);
+    iostr::EachLine line(is);
+
+    ProgressData pd;
+    pd.sendTo(progress);
+    pd.toMin();
+
+    bool pastFromDate = false;
+    for (; line; line.next(), pd.tick())
+    {
+      const string & s = *line;
+
+      // ignore comments
+      if (s[0] == '#')
+        continue;
+
+      Date logDate(s.substr(0, s.find('|')), HISTORY_LOG_DATE_FORMAT);
+
+      // past toDate - stop reading
+      if (logDate >= toDate)
+        break;
+
+      // past fromDate - start reading
+      if (!pastFromDate && logDate > fromDate)
+        pastFromDate = true;
+
+      if (pastFromDate)
+        parseLine(s, line.lineNo());
+    }
+
+    pd.toMax();
+  }
 
   /////////////////////////////////////////////////////////////////////
   //
@@ -174,6 +262,15 @@ namespace zypp
   void HistoryLogReader::readAll(const ProgressData::ReceiverFnc & progress)
   { _pimpl->readAll(progress); }
 
+  void HistoryLogReader::readFrom(const Date & date,
+      const ProgressData::ReceiverFnc & progress)
+  { _pimpl->readFrom(date, progress); }
+
+  void HistoryLogReader::readFromTo(
+      const Date & fromDate, const Date & toDate,
+      const ProgressData::ReceiverFnc & progress)
+  { _pimpl->readFromTo(fromDate, toDate, progress); }
+
 
     /////////////////////////////////////////////////////////////////
   } // namespace parser
index 7cc4b29..45ecd27 100644 (file)
@@ -36,7 +36,7 @@ namespace zypp
   //
   /**
    * Reads a zypp history log file and calls the ProcessItem function passed
-   * in the constructor for each item found.
+   * in the constructor for each item read.
    *
    * Example:
    * <code>
@@ -82,18 +82,41 @@ namespace zypp
 
     /**
      * Read the whole log file.
+     *
+     * \param progress An optional progress data receiver function.
      */
     void readAll(
       const ProgressData::ReceiverFnc &progress = ProgressData::ReceiverFnc() );
 
     /**
      * Read log from specified \a date.
+     *
+     * \param date     Date from which to read.
+     * \param progress An optional progress data receiver function.
+     *
+     * \see readFromTo()
      */
     void readFrom( const Date & date,
       const ProgressData::ReceiverFnc &progress = ProgressData::ReceiverFnc() );
 
     /**
      * Read log between \a fromDate and \a toDate.
+     *
+     * The date comparison's precision goes to seconds. Omitted time parts
+     * get replaced by zeroes, so if e.g. the time is not specified at all, the
+     * date means midnight of the specified date. So
+     *
+     * <code>
+     * fromDate = Date("2009-01-01", "%Y-%m-%d");
+     * toDate   = Date("2009-01-02", "%Y-%m-%d");
+     * </code>
+     *
+     * will yield log entries from midnight of January, 1st untill
+     * one second before midnight of January, 2nd.
+     *
+     * \param fromDate Date from which to read.
+     * \param toDate   Date on which to stop reading.
+     * \param progress An optional progress data receiver function.
      */
     void readFromTo( const Date & fromDate, const Date & toDate,
       const ProgressData::ReceiverFnc &progress = ProgressData::ReceiverFnc() );
@@ -107,6 +130,8 @@ namespace zypp
 
     /**
      * Whether the reader is set to ignore invalid log entries.
+     *
+     * \see setIngoreInvalidItems()
      */
     bool ignoreInvalidItems() const;