tabular

Flexible input tabulator
git clone https://git.sinitax.com/sinitax/tabular
Log | Files | Refs | Submodules | sfeed.txt

test.c (1608B)


      1#include "allocator.h"
      2#include "dvec.h"
      3
      4#include <err.h>
      5#include <stdbool.h>
      6#include <stdio.h>
      7#include <string.h>
      8#include <stdlib.h>
      9
     10static const char *dvec_err[] = {
     11	DVEC_STRERR_INIT
     12};
     13
     14bool
     15str_sort_order(const void *p1, const void *p2, const void *user)
     16{
     17	const char *s1 = *(const char **)p1;
     18	const char *s2 = *(const char **)p2;
     19
     20	return strcmp(s1, s2) <= 0;
     21}
     22
     23int
     24str_search(const void *p, const void *user)
     25{
     26	const char *str = *(const char **)p, *cmp = user;
     27
     28	return strcmp(cmp, str);
     29}
     30
     31int
     32main(int argc, const char **argv)
     33{
     34	struct dvec dvec, *fused;
     35	ssize_t below, above, on;
     36	const char **val, *tmp;
     37	int i, rc;
     38
     39	rc = dvec_init(&dvec, sizeof(const char *), 16, &stdlib_heap_allocator);
     40	if (rc) DVEC_ERR(dvec_err, rc);
     41
     42	for (i = 1; i < argc; i++) {
     43		rc = dvec_add_back(&dvec, 1);
     44		if (rc) DVEC_ERR(dvec_err, rc);
     45		val = dvec_back(&dvec);
     46		*val = argv[i];
     47	}
     48
     49	rc = dvec_quick_sort_ex(&dvec, &stdlib_heap_allocator, &tmp,
     50		false, str_sort_order, NULL);
     51	if (rc < 0) DVEC_ERR(dvec_err, rc);
     52
     53	on = dvec_binary_search(&dvec, str_search, "abc", &below, &above);
     54
     55	for (DVEC_ITER(&dvec, val))
     56		printf("%s\n", *val);
     57
     58	printf("dvec len: %lu\n", dvec.len);
     59
     60	printf("\n");
     61	printf("below: %li\n", below);
     62	printf("on: %li\n", on);
     63	printf("above: %li\n", above);
     64	printf("\n");
     65
     66	fused = dvec_alloc_fused(sizeof(const char *), 0,
     67		&stdlib_heap_allocator, &rc);
     68	if (!fused) DVEC_ERR(dvec_err, rc);
     69
     70	printf("fused: %p %p (+%li)\n", (void *) fused,
     71		fused->data, fused->data - (uint8_t *) fused);
     72
     73	rc = dvec_free(fused);
     74	if (rc) DVEC_ERR(dvec_err, rc);
     75
     76	dvec_deinit(&dvec);
     77}