Moved display logic to seperate TU (display.c), added options for display output types (TODO) and (TODO) flags.

Fortune for naka's current commit: Future small blessing − 末小吉
master
Avril 3 years ago
parent a5b3bc79b3
commit 9d0747a5f7
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -4,6 +4,7 @@
#include <macros.h> #include <macros.h>
#include <ints.h> #include <ints.h>
#include <slice.h> #include <slice.h>
#include <map.h>
typedef struct comp_aggregate { typedef struct comp_aggregate {
bool all_matched; bool all_matched;

@ -0,0 +1,35 @@
#ifndef _DISPLAY_H
#define _DISPLAY_H
#include <stdio.h>
#include "macros.h"
#include "ints.h"
#include "slice.h"
typedef enum display_kind {
DISPK_NORMAL = 0,
DISPK_CSV,
DISPK_BINARY,
DISPK_INI,
} dispkind_t;
#define X(n) AS(1lu << (n), usize)
typedef enum display_flags {
DISPF_SHOW_NUMBER = X(0),
DISPF_SHOW_LENGTH = X(1),
DISPF_SHOW_SLICE = X(2),
DISPF_SHOW_FAILURES = X(3),
} dispflags_t;
#define DISPLAY_FLAGS_DEFAULT (DISPF_SHOW_SLICE | DISPF_SHOW_NUMBER | DISPF_SHOW_FAILURES)
#undef X
typedef struct {
slice_t cslice;
const void* base;
usize index;
} dispin_t;
void display_result(FILE* output, dispin_t input, dispkind_t how, dispflags_t flags);
#endif /* _DISPLAY_H */

@ -0,0 +1,50 @@
//! Displaying match data
#include <stdio.h>
#include <stdlib.h>
#include <macros.h>
#include <comp.h>
#include <display.h>
static void _display_normal(FILE* output, dispin_t*pIN input, dispflags_t flags)
{
slice_t cslice = input->cslice;
const void* base = input->base;
usize i = input->index;
#ifdef _DEBUG_MANCALC_OFFSETS
struct {
usize start, end;
} pos = {
.start = AS(cslice.ptr - base, usize),
};
pos.end = pos.start + cslice.len;
#else
let pos = slice_sted((slice_t[]){ SLICE(AS(cslice.ptr - base, usize), cslice.len)});
#endif
INFO("Mapped region slice for %lu, pre-transform (at %p): " SLICE_FORMAT ", len: %lu", i+1, base, SLICE_FORMAT_ARGS(&cslice), cslice.len);
//TODO: Honour `flags`
IGNORE(flags);
fprintf(output, "match: %lu: %lu -> %lu\n", i+1, pos.start, pos.end);
}
void display_result(FILE* output, dispin_t input, dispkind_t how, dispflags_t flags)
{
flags = flags ?: DISPLAY_FLAGS_DEFAULT;
output = output ?: stdout;
debug_assert(input);
TRACE("Outputting as %d to %p with flags 0x%08lx", (int)how, output, (usize)flags);
switch(how)
{
case DISPK_NORMAL:
_display_normal(output, &input, flags);
break;
case DISPK_CSV:
case DISPK_BINARY:
case DISPK_INI:
default: COLD_EXPR(FATAL("unknown or unimplemented display kind: %d", (int)how));
}
}

@ -16,6 +16,7 @@
#include <slice.h> #include <slice.h>
#include <map.h> #include <map.h>
#include <comp.h> #include <comp.h>
#include <display.h>
static const char* _compdate_readable() static const char* _compdate_readable()
{ {
@ -180,18 +181,8 @@ inv_args:
if(!comp_slice_of(&needle, current++, &cslice)) { if(!comp_slice_of(&needle, current++, &cslice)) {
printf("fail: %lu\n", i+1); printf("fail: %lu\n", i+1);
} else { } else {
#ifdef _DEBUG_MANCALC_OFFSETS TRACE("Displaying result %lu (" SLICE_FORMAT ") (base %p)", i, SLICE_FORMAT_ARGS(&cslice), base);
struct { display_result(stdout, (dispin_t){ .base = base, .cslice = cslice, .index = i}, DISPK_NORMAL, 0);
usize start, end;
} pos = {
.start = AS(cslice.ptr - base, usize),
};
pos.end = pos.start + cslice.len;
#else
let pos = slice_sted((slice_t[]){ SLICE(AS(cslice.ptr - base, usize), cslice.len)});
#endif
INFO("Mapped region slice for %lu, pre-transform (at %p): " SLICE_FORMAT ", len: %lu", i+1, base, SLICE_FORMAT_ARGS(&cslice), cslice.len);
printf("match: %lu: %lu -> %lu\n", i+1, pos.start, pos.end);
} }
} }

@ -35,7 +35,6 @@ const char* const _trace_level_descriptions[_TRACE_LEVEL_NUM] = {
"Only show fatal (aborting) errors", "Only show fatal (aborting) errors",
}; };
static bool _trace_lookup(const char*pIN name, enum trace_level *pOUT olevel) static bool _trace_lookup(const char*pIN name, enum trace_level *pOUT olevel)
{ {
debug_assert(name); debug_assert(name);

Loading…
Cancel
Save