2017-07-13 11:36:38 +03:00
# include <sstream>
# include <assert.h>
2017-07-12 23:18:34 +03:00
# include <apt-pkg/pkgcache.h>
2017-07-13 11:36:38 +03:00
# include <apt-pkg/prettyprinters.h>
2017-07-12 23:18:34 +03:00
# include <apt-pkg/cachefile.h>
struct PCache {
2017-07-13 12:04:18 +03:00
// Owned by us.
2017-07-12 23:18:34 +03:00
pkgCacheFile * cache_file ;
2017-07-13 12:04:18 +03:00
// Borrowed from cache_file.
pkgCache * cache ;
} ;
struct PPkgIterator {
// Owned by us.
pkgCache : : PkgIterator iterator ;
// Borrowed from PCache.
2017-07-12 23:18:34 +03:00
pkgCache * cache ;
} ;
extern " C " {
2017-07-13 13:32:33 +03:00
void init_config_system ( ) ;
2017-07-13 12:04:18 +03:00
PCache * pkg_cache_create ( ) ;
void pkg_cache_release ( PCache * cache ) ;
2017-07-12 23:18:34 +03:00
2017-07-13 12:04:18 +03:00
PPkgIterator * pkg_cache_pkg_iter ( PCache * cache ) ;
void pkg_iter_release ( PPkgIterator * iterator ) ;
2017-07-13 13:03:31 +03:00
void pkg_iter_next ( PPkgIterator * iterator ) ;
bool pkg_iter_end ( PPkgIterator * iterator ) ;
2017-07-13 12:04:18 +03:00
const char * pkg_iter_name ( PPkgIterator * iterator ) ;
2017-07-13 11:36:38 +03:00
// freed by caller
2017-07-13 12:04:18 +03:00
char * pkg_iter_pretty ( PCache * cache , PPkgIterator * iterator ) ;
2017-07-12 23:18:34 +03:00
}
2017-07-13 13:32:33 +03:00
void init_config_system ( ) {
2017-07-12 23:18:34 +03:00
pkgInitConfig ( * _config ) ;
pkgInitSystem ( * _config , _system ) ;
2017-07-13 13:32:33 +03:00
}
2017-07-12 23:18:34 +03:00
2017-07-13 13:32:33 +03:00
PCache * pkg_cache_create ( ) {
2017-07-12 23:18:34 +03:00
pkgCacheFile * cache_file = new pkgCacheFile ( ) ;
pkgCache * cache = cache_file - > GetPkgCache ( ) ;
PCache * ret = new PCache ( ) ;
ret - > cache_file = cache_file ;
ret - > cache = cache ;
return ret ;
}
2017-07-13 12:04:18 +03:00
void pkg_cache_release ( PCache * cache ) {
2017-07-13 11:36:38 +03:00
// TODO: is cache->cache cleaned up with cache->cache_file?
2017-07-12 23:18:34 +03:00
delete cache - > cache_file ;
delete cache ;
}
2017-07-13 12:04:18 +03:00
PPkgIterator * pkg_cache_pkg_iter ( PCache * cache ) {
PPkgIterator * wrapper = new PPkgIterator ( ) ;
wrapper - > iterator = cache - > cache - > PkgBegin ( ) ;
wrapper - > cache = cache - > cache ;
return wrapper ;
}
void pkg_iter_release ( PPkgIterator * wrapper ) {
delete wrapper ;
}
2017-07-12 23:18:34 +03:00
2017-07-13 13:03:31 +03:00
void pkg_iter_next ( PPkgIterator * wrapper ) {
2017-07-13 12:04:18 +03:00
+ + wrapper - > iterator ;
2017-07-13 13:03:31 +03:00
}
bool pkg_iter_end ( PPkgIterator * wrapper ) {
return wrapper - > cache - > PkgEnd ( ) = = wrapper - > iterator ;
2017-07-12 23:18:34 +03:00
}
2017-07-13 11:36:38 +03:00
2017-07-13 12:04:18 +03:00
const char * pkg_iter_name ( PPkgIterator * wrapper ) {
return wrapper - > iterator . Name ( ) ;
2017-07-13 11:36:38 +03:00
}
2017-07-13 12:04:18 +03:00
char * pkg_iter_pretty ( PCache * cache , PPkgIterator * wrapper ) {
2017-07-13 11:36:38 +03:00
assert ( cache ) ;
2017-07-13 12:04:18 +03:00
assert ( wrapper ) ;
2017-07-13 11:36:38 +03:00
std : : stringstream ss ;
2017-07-13 12:04:18 +03:00
ss < < APT : : PrettyPkg ( cache - > cache_file - > GetDepCache ( ) , wrapper - > iterator ) ;
2017-07-13 11:36:38 +03:00
return strdup ( ss . str ( ) . c_str ( ) ) ;
}
//const void *pkg_iter_name(pkgCache::PkgIterator *iterator) {
// return iterator->VersionList();
//}