SBDP02/readidx.c
2018-12-14 21:11:21 +01:00

96 lines
1.9 KiB
C

#include <stdlib.h>
#include <memory.h>
#define OPTPARSE_IMPLEMENTATION
#define OPTPARSE_API static
#include "common.h"
#include "index.h"
#define OPTION_KEYS_ONLY 1
struct {
char* index;
uint8_t flags;
} options;
btree_t tree;
void init_args(int args, char* argv[])
{
optparse_t opts;
optparse_init(&opts, argv);
for (char opt; opt != -1; opt = optparse(&opts, "qv")) {
switch (opt) {
case 'k':
options.flags |= OPTION_KEYS_ONLY;
break;
case 'q':
verbosity--;
break;
case 'v':
verbosity++;
break;
}
}
options.index = optparse_arg(&opts);
}
void help(const char* name) {
printf(
"%s - some help"
,
name
);
}
void print_page(page_t page)
{
if (!page) return;
char buffer[PAGE_SIZE];
char prefix[1024] = {};
file_read(tree.file, page, buffer, PAGE_SIZE);
btree_node_t node;
memcpy(&node.header, buffer, sizeof(node.header));
memcpy(&node.entries, buffer + sizeof(node.header), NODE_SIZE_MAX);
btree_entry_t *entry;
record_t record;
for (int i = 0; i < node.header.entries; i++) {
entry = btree_get_entry(&node, i);
print_page(entry->left);
if (~options.flags & OPTION_KEYS_ONLY) {
btree_read_record(&tree, entry->key, &record);
printf("%u: (%lf, %lf)\n", entry->key, record.x, record.y);
} else {
printf("%u\n", entry->key);
}
}
print_page(entry->right);
}
int main(int argc, char* argv[])
{
if (argc < 2) {
help(argv[0]);
return 0;
}
init_args(argc, argv);
if (btree_open(&tree, options.index) != 0) {
fprintf(stderr, "Cannot open index %s for reading.", options.index);
return 1;
}
print_page(tree.header.root);
return EXIT_SUCCESS;
}