SBDP02/readidx.c
2018-12-14 19:57:49 +01:00

84 lines
1.6 KiB
C

#include <stdlib.h>
#include <memory.h>
#define OPTPARSE_IMPLEMENTATION
#define OPTPARSE_API static
#include "common.h"
#include "index.h"
typedef struct {
char* index;
} opts_t;
opts_t 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 '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;
for (int i = 0; i < node.header.entries; i++) {
entry = btree_get_entry(&node, i);
print_page(entry->left);
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 indes %s for reading.", options.index);
return 1;
}
print_page(tree.header.root);
return EXIT_SUCCESS;
}