~brenns10/sc-examples

c9b9f2ab52ad3e30a696474c72e3dc232b436d92 — Stephen Brennan 2 years ago bb2c08b
Add list example
A list-example/README.md => list-example/README.md +16 -0
@@ 0,0 1,16 @@
hash-tester
===========

A simple REPL which can either store a key=value mapping, or retrieve the value
associated with a key. This demonstrates the use of the hash table present in
the sc-collections library. Here is an example session:

    $ build/hash-tester
    > foo=bar
    > foo
    bar
    > foo=baz
    deleting foo=bar
    > foo
    baz
    > exit

A list-example/list-example.c => list-example/list-example.c +43 -0
@@ 0,0 1,43 @@
/*
 * hash-tester.c: simple store or retrieve REPL to demo the hash table
 *
 * usage: hash-tester
 * > key=value
 * > key
 * value
 * > exit
 */
#define _POSIX_C_SOURCE 200809L

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "sc-collections.h"

struct argument {
	char *argstring;
	struct sc_list_head list;
};

int main(int argc, char **argv)
{
	struct sc_list_head list;
	struct argument *tmp, *next;
	int i;

	sc_list_init(&list);
	for (i = 0; i < argc; i++) {
		tmp = malloc(sizeof(struct argument));
		tmp->argstring = argv[i];
		sc_list_insert_end(&list, &tmp->list);
	}

	sc_list_for_each_entry(tmp, &list, list, struct argument) {
		printf("argument: %s\n", tmp->argstring);
	}

	sc_list_for_each_safe(tmp, next, &list, list, struct argument) {
		free(tmp);
	}
}

A list-example/meson.build => list-example/meson.build +17 -0
@@ 0,0 1,17 @@
project('list-example', 'c')

sources = [
	'list-example.c',
]

libsc_collections_dep = dependency(
	'sc-collections',
	fallback : ['sc-collections', 'libsc_collections_dep'],
	version : '>=0.4.0',
)

executable(
	'list-example',
	sources,
	dependencies : libsc_collections_dep,
)

A list-example/subprojects/sc-collections.wrap => list-example/subprojects/sc-collections.wrap +4 -0
@@ 0,0 1,4 @@
[wrap-git]
directory = sc-collections.git
url = https://git.sr.ht/~brenns10/sc-collections
revision = master