libept  0.5.25
apt/apt.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 #ifndef EPT_APT_APT_H
3 #define EPT_APT_APT_H
4 
9 /*
10  * Copyright (C) 2007,2008 Enrico Zini <enrico@enricozini.org>
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  */
26 
27 #include <wibble/exception.h>
28 #include <ept/apt/version.h>
29 #include <ept/core/apt.h>
30 
31 #include <iterator>
32 
33 namespace ept {
34 namespace apt {
35 
36 class Exception : public wibble::exception::Generic
37 {
38 protected:
39  std::string m_message;
40 
41 public:
42  Exception(const std::string& context) throw ();
43  ~Exception() throw () {}
44 
45  virtual const char* type() const throw () { return "Apt"; }
46  virtual std::string desc() const throw () { return m_message; }
47 };
48 
49 class Apt;
50 class AptImplementation;
51 class RecordIteratorImpl;
52 using core::PackageState;
53 
60 class Apt
61 {
62 protected:
63  AptImplementation* impl;
64 
65 public:
66  // Iterate Packages in the Apt cache
67  class Iterator : public std::iterator<std::input_iterator_tag, std::string, void, void, void>
68  {
69  void* cur;
70 
71  protected:
72  // Construct a valid iterator
73  Iterator(void* cur) : cur(cur) {}
74 
75  // Construct and end iterator
76  Iterator() : cur(0) {}
77 
78  public:
79  // Copy constructor
80  Iterator(const Iterator&);
81  ~Iterator();
82  std::string operator*();
84  Iterator& operator=(const Iterator&);
85  bool operator==(const Iterator&) const;
86  bool operator!=(const Iterator&) const;
87 
88  // FIXME: Iterator operator++(int); cannot be easily implemented
89  // because of how Apt's pkgIterator works
90 
91  friend class Apt;
92  };
93 
94  // Iterate Package records in the Apt cache
95  class RecordIterator : public std::iterator<std::input_iterator_tag, std::string, void, void, void>
96  {
97  RecordIteratorImpl* impl;
98  size_t pos;
99  std::string cur;
100  size_t cur_pos;
101 
102  protected:
103  // Construct a valid iterator
104  RecordIterator(RecordIteratorImpl* cur, size_t pos = 0);
105 
106  // Construct and end iterator
107  RecordIterator() : impl(0), pos(0), cur_pos(0) {}
108 
109  public:
110  // Copy constructor
111  RecordIterator(const RecordIterator& r);
112 
113  ~RecordIterator();
114  std::string operator*();
115  std::string* operator->();
118  bool operator==(const RecordIterator&) const;
119  bool operator!=(const RecordIterator&) const;
120 
121  // FIXME: Iterator operator++(int); cannot be easily implemented
122  // because of how Apt's pkgIterator works
123 
124  friend class Apt;
125  };
126 
129 
133  Apt();
134  ~Apt();
135 
136  iterator begin() const;
137  iterator end() const;
138 
139  record_iterator recordBegin() const;
140  record_iterator recordEnd() const;
141 
142 
144  size_t size() const;
145 
150  bool isValid(const std::string& pkg) const;
151 
154  std::string validate(const std::string& pkg) const
155  {
156  if (isValid(pkg))
157  return pkg;
158  return std::string();
159  }
160 
163  Version validate(const Version& ver) const;
164 
166  Version installedVersion(const std::string& pkg) const;
167 
169  Version candidateVersion(const std::string& pkg) const;
170 
175  Version anyVersion(const std::string& pkg) const;
176 
178  PackageState state(const std::string& pkg) const;
179 
186  //template<typename FILTER, typename OUT>
187  //void search(const FILTER& filter, OUT& out);
188 
190  std::string rawRecord(const std::string& pkg) const;
191 
193  std::string rawRecord(const Version& ver) const;
194 
196  time_t timestamp();
197 
204  void checkCacheUpdates();
205 
212  void invalidateTimestamp();
213 };
214 
215 }
216 }
217 
218 // vim:set ts=4 sw=4:
219 #endif
Apt()
Create the Apt data provider.
Definition: apt.cc:433
Definition: core/apt.h:42
Iterator iterator
Definition: apt/apt.h:127
void checkCacheUpdates()
Check if the cache has been changed by another process, and reopen it if that is the case...
Definition: apt.cc:622
record_iterator recordBegin() const
Definition: apt.cc:448
Iterator(void *cur)
Definition: apt/apt.h:73
bool operator!=(const Iterator &) const
Definition: apt.cc:347
bool operator==(const RecordIterator &) const
Definition: apt.cc:423
-*- C++ -*- (c) 2006, 2007 Petr Rockai me@mornfall.net
Definition: apt.cc:43
PackageState state(const std::string &pkg) const
Return state information on a package.
Definition: apt.cc:522
bool isValid(const std::string &pkg) const
Validate a package name, returning trye if it exists in the APT database, or false if it does not...
Definition: apt.cc:468
std::string operator*()
Definition: apt.cc:320
RecordIterator()
Definition: apt/apt.h:107
std::string validate(const std::string &pkg) const
Validate a package name, returning it if it exists in the APT database, or returning the empty string...
Definition: apt/apt.h:154
Version installedVersion(const std::string &pkg) const
Return the installed version for a package.
Definition: apt.cc:497
Definition: apt/apt.h:36
~RecordIterator()
Definition: apt.cc:375
High-level access to the Apt cache, as a data provider for the ept framework.
Definition: apt/apt.h:60
time_t timestamp()
Timestamp of when the apt index was last modified.
Definition: apt.cc:463
RecordIterator & operator=(const RecordIterator &r)
Definition: apt.cc:411
iterator end() const
Definition: apt.cc:443
Definition: apt/apt.h:67
virtual std::string desc() const
Definition: apt/apt.h:46
Version candidateVersion(const std::string &pkg) const
Return the candidate version for a package.
Definition: apt.cc:488
void invalidateTimestamp()
Invalidate the cache timestamp used to track cache updates.
Definition: apt.cc:632
iterator begin() const
Definition: apt.cc:436
record_iterator recordEnd() const
Definition: apt.cc:453
std::string m_message
Definition: apt/apt.h:39
RecordIterator & operator++()
Definition: apt.cc:398
bool operator!=(const RecordIterator &) const
Definition: apt.cc:427
std::string rawRecord(const std::string &pkg) const
Perform a package search.
Definition: apt.cc:569
Iterator()
Definition: apt/apt.h:76
Representation of a package with a version.
Definition: apt/apt.h:95
virtual const char * type() const
Definition: apt/apt.h:45
Iterator & operator=(const Iterator &)
Definition: apt.cc:300
AptImplementation * impl
Definition: apt/apt.h:63
~Exception()
Definition: apt/apt.h:43
~Apt()
Definition: apt.cc:434
~Iterator()
Definition: apt.cc:316
bool operator==(const Iterator &) const
Definition: apt.cc:337
std::string operator*()
Definition: apt.cc:380
Exception(const std::string &context)
Definition: apt.cc:47
Version anyVersion(const std::string &pkg) const
Return the candidate version for a package, if available, or the installed version otherwise...
Definition: apt.cc:507
RecordIterator record_iterator
Definition: apt/apt.h:128
size_t size() const
Return the number of packages in the archive.
Definition: apt.cc:458
Lightweight Version class that represent a package with a version, with very cheap value copy operati...
Definition: apt/version.h:40
std::string * operator->()
Definition: apt.cc:389
Iterator & operator++()
Definition: apt.cc:324