this callback is dumb

This commit is contained in:
Chris West (Faux) 2017-07-13 09:36:38 +01:00
parent da91ba6908
commit 4225e76641
4 changed files with 41 additions and 9 deletions

View File

@ -1,4 +1,4 @@
CXXFLAGS=-Wall -Wextra
CXXFLAGS=-Wall -Wextra -g
all: libapt-c.a

View File

@ -1,4 +1,9 @@
#include <sstream>
#include <assert.h>
#include <apt-pkg/pkgcache.h>
#include <apt-pkg/prettyprinters.h>
#include <apt-pkg/cachefile.h>
struct PCache {
@ -10,7 +15,11 @@ extern "C" {
PCache *get_pkg_cache();
void free_pkg_cache(PCache *cache);
int iterate_all_packages(PCache *cache, int (*visit)(const char*));
int iterate_packages(PCache *cache, int (*visit)(pkgCache::PkgIterator *iterator));
const char *pkg_iter_name(pkgCache::PkgIterator *iterator);
// freed by caller
char *pkg_iter_pretty(PCache *cache, pkgCache::PkgIterator *iterator);
}
PCache *get_pkg_cache() {
@ -28,16 +37,33 @@ PCache *get_pkg_cache() {
}
void free_pkg_cache(PCache *cache) {
// TODO: is cache->cache cleaned up with cache->cache_file?
delete cache->cache_file;
delete cache;
}
int iterate_all_packages(PCache *cache, int (*visit)(const char*)) {
int iterate_packages(PCache *cache, int (*visit)(pkgCache::PkgIterator*)) {
for (pkgCache::PkgIterator iter = cache->cache->PkgBegin(); iter != cache->cache->PkgEnd(); ++iter) {
if (!visit(iter.Name())) {
if (!visit(&iter)) {
return false;
}
}
return true;
}
const char *pkg_iter_name(pkgCache::PkgIterator *iterator) {
return iterator->Name();
}
char *pkg_iter_pretty(PCache *cache, pkgCache::PkgIterator *iterator) {
assert(cache);
assert(iterator);
std::stringstream ss;
ss << APT::PrettyPkg(cache->cache_file->GetDepCache(), *iterator);
return strdup(ss.str().c_str());
}
//const void *pkg_iter_name(pkgCache::PkgIterator *iterator) {
// return iterator->VersionList();
//}

View File

@ -8,9 +8,12 @@ mod tests {
use std::ffi::CStr;
extern fn print_arg(arg: *const libc::c_char) -> libc::c_int {
extern fn print_pkg(iter: *mut libc::c_void) -> libc::c_int {
unsafe {
println!("{:?}", CStr::from_ptr(arg).to_str());
println!("{}", CStr::from_ptr(raw::pkg_iter_name(iter)).to_str().expect("package names are always valid utf-8, in that they're always valid low ascii"));
let pretty = raw::pkg_iter_pretty(cache, iter);
println!("{}", CStr::from_ptr(pretty).to_str().expect("package names are always valid utf-8, in that they're always valid low ascii"));
libc::free(pretty as *mut libc::c_void);
return 1;
}
}
@ -19,7 +22,7 @@ mod tests {
fn it_works() {
unsafe {
let cache = raw::get_pkg_cache();
println!("{:?}", raw::iterate_all_packages(cache, print_arg));
println!("{:?}", raw::iterate_packages(cache, print_pkg));
raw::free_pkg_cache(cache);
}
}

View File

@ -9,8 +9,11 @@ extern {
pub fn get_pkg_cache() -> *mut c_void;
pub fn free_pkg_cache(cache: *mut c_void);
pub fn iterate_all_packages(
pub fn iterate_packages(
cache: *mut c_void,
visit: extern fn(name: *const c_char) -> c_int,
visit: extern fn(name: *mut c_void) -> c_int,
) -> c_int;
pub fn pkg_iter_name(iterator: *mut c_void) -> *const c_char;
pub fn pkg_iter_pretty(cache: *mut c_void, iterator: *mut c_void) -> *mut c_char;
}