Add examples
This commit is contained in:
parent
125153076d
commit
86928b4b22
40
examples/Makefile
Normal file
40
examples/Makefile
Normal file
@ -0,0 +1,40 @@
|
||||
# This software is licensed by the MIT License, see LICENSE file
|
||||
# Copyright © 2022 Gregory Lirent
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
BUILD_PATH ?= ./bin
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
CC = clang
|
||||
MKDIR = mkdir -p
|
||||
RMRF = rm -rf
|
||||
AR = ar crs
|
||||
CP = cp
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
examples: modules
|
||||
examples: $(addprefix $(BUILD_PATH)/,$(notdir $(basename $(wildcard ./*.c))))
|
||||
|
||||
$(BUILD_PATH)/%: ./%.c | $(BUILD_PATH)/
|
||||
$(CC) $^ ../bin/release/libcdsb.a ../modules/libunic/bin/libunic.a -o $@ $(CFLAGS) -O2 -Wall
|
||||
|
||||
|
||||
$(BUILD_PATH)/:
|
||||
$(MKDIR) $@
|
||||
$(BUILD_PATH)/obj/: | $(BUILD_PATH)/
|
||||
$(MKDIR) $@
|
||||
|
||||
clean:
|
||||
$(RMRF) ./bin/
|
||||
cd ../ && $(MAKE) clean
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
FORCE:
|
||||
modules: ../bin/release/libcdsb.a
|
||||
|
||||
../bin/release/libcdsb.a: FORCE
|
||||
cd ../ && $(MAKE) release
|
37
examples/array.c
Normal file
37
examples/array.c
Normal file
@ -0,0 +1,37 @@
|
||||
/* This software is licensed by the MIT License, see LICENSE file */
|
||||
/* Copyright © 2022 Gregory Lirent */
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include "../include/extra/array.h"
|
||||
|
||||
typedef vtype_array arr_t;
|
||||
|
||||
int print_value(void* value, ssize_t index, vtype type, void* data) {
|
||||
const char *n = data;
|
||||
vtype_int32 *v = value;
|
||||
|
||||
assert(type == VTYPE_INT32);
|
||||
|
||||
printf("%s %d (index: %ld)\n", n, *v, index);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
arr_t arr;
|
||||
|
||||
array_init(&arr, VTYPE_INT32);
|
||||
|
||||
for (size_t i = 0; i < 28; ++i) {
|
||||
array_push_back(&arr, i);
|
||||
}
|
||||
|
||||
array_get_by_index(&arr, 13, "Get value:", print_value);
|
||||
array_pop_by_index(&arr, 18, "Pop value:", print_value);
|
||||
|
||||
array_foreach(&arr, "Foreach loop:", print_value);
|
||||
|
||||
array_free(&arr);
|
||||
}
|
61
examples/dict.c
Normal file
61
examples/dict.c
Normal file
@ -0,0 +1,61 @@
|
||||
/* This software is licensed by the MIT License, see LICENSE file */
|
||||
/* Copyright © 2022 Gregory Lirent */
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "../include/extra/dict.h"
|
||||
|
||||
typedef vtype_dict dict_t;
|
||||
|
||||
int print_value(const void* key, vtype key_type, void* value, vtype value_type, void* data) {
|
||||
const char *n = data;
|
||||
|
||||
switch (key_type) {
|
||||
default: abort();
|
||||
|
||||
case VTYPE_INT32:
|
||||
printf("%s %d: ", n, *(vtype_int32*)key);
|
||||
break;
|
||||
case VTYPE_FLOAT:
|
||||
printf("%s %f: ", n, *(vtype_float*)key);
|
||||
break;
|
||||
}
|
||||
|
||||
switch (value_type) {
|
||||
default: abort();
|
||||
|
||||
case VTYPE_INT32:
|
||||
printf("%d\n", *(vtype_int32*)value);
|
||||
break;
|
||||
case VTYPE_FLOAT:
|
||||
printf("%f\n", *(vtype_float*)value);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
dict_t dict;
|
||||
vtype_float fl = 0.0;
|
||||
|
||||
dict_init(&dict);
|
||||
|
||||
for (size_t i = 0; i < 28; ++i) {
|
||||
if (i%2) {
|
||||
dict_update(&dict, (vtype_float)fl, (vtype_int32)i);
|
||||
} else {
|
||||
dict_update(&dict, (vtype_int32)i, (vtype_float)fl);
|
||||
}
|
||||
fl += 0.05;
|
||||
}
|
||||
|
||||
dict_get(&dict, 14, "Get value:", print_value);
|
||||
dict_pop(&dict, 0.25, "Pop value:", print_value);
|
||||
|
||||
dict_foreach(&dict, "Foreach loop:", print_value);
|
||||
|
||||
dict_free(&dict);
|
||||
}
|
51
examples/list.c
Normal file
51
examples/list.c
Normal file
@ -0,0 +1,51 @@
|
||||
/* This software is licensed by the MIT License, see LICENSE file */
|
||||
/* Copyright © 2022 Gregory Lirent */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "../include/extra/list.h"
|
||||
|
||||
typedef vtype_list list_t;
|
||||
|
||||
|
||||
int print_value(void* value, ssize_t index, vtype type, void* data) {
|
||||
const char *n = data;
|
||||
|
||||
switch (type) {
|
||||
default: abort();
|
||||
|
||||
case VTYPE_INT32:
|
||||
printf("%s %d (index: %ld)\n", n, *(vtype_int32*)value, index);
|
||||
break;
|
||||
case VTYPE_FLOAT:
|
||||
printf("%s %f (index: %ld)\n", n, *(vtype_float*)value, index);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
list_t list;
|
||||
vtype_float fl = 0.0;
|
||||
|
||||
list_init(&list);
|
||||
|
||||
for (size_t i = 0; i < 28; ++i) {
|
||||
if (i%2) {
|
||||
list_push_back(&list, (vtype_int32)i);
|
||||
} else {
|
||||
list_push_back(&list, (vtype_float)fl);
|
||||
fl += 0.05;
|
||||
}
|
||||
}
|
||||
|
||||
list_get_by_index(&list, 13, "Get value:", print_value);
|
||||
list_pop_by_index(&list, 18, "Pop value:", print_value);
|
||||
|
||||
list_foreach(&list, "Foreach loop:", print_value);
|
||||
|
||||
list_free(&list);
|
||||
}
|
55
examples/map.c
Normal file
55
examples/map.c
Normal file
@ -0,0 +1,55 @@
|
||||
/* This software is licensed by the MIT License, see LICENSE file */
|
||||
/* Copyright © 2022 Gregory Lirent */
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include "../include/extra/map.h"
|
||||
|
||||
typedef vtype_map map_t;
|
||||
|
||||
int print_value(const void* key, vtype key_type, void* value, vtype value_type, void* data) {
|
||||
const char *n = data;
|
||||
vtype_int32 *k = (void*)key;
|
||||
|
||||
assert(key_type == VTYPE_INT32);
|
||||
|
||||
printf("%s %d: ", n, *k);
|
||||
|
||||
switch (value_type) {
|
||||
default: abort();
|
||||
|
||||
case VTYPE_INT32:
|
||||
printf("%d\n", *(vtype_int32*)value);
|
||||
break;
|
||||
case VTYPE_FLOAT:
|
||||
printf("%f\n", *(vtype_float*)value);
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
map_t map;
|
||||
vtype_float fl = 0.0;
|
||||
|
||||
map_init(&map, VTYPE_INT32);
|
||||
|
||||
for (size_t i = 0; i < 28; ++i) {
|
||||
if (i%2) {
|
||||
map_update(&map, i, (vtype_int32)i);
|
||||
} else {
|
||||
map_update(&map, i, (vtype_float)fl);
|
||||
fl += 0.05;
|
||||
}
|
||||
}
|
||||
|
||||
map_get(&map, 13, "Get value:", print_value);
|
||||
map_pop(&map, 18, "Pop value:", print_value);
|
||||
|
||||
map_foreach(&map, "Foreach loop:", print_value);
|
||||
|
||||
map_free(&map);
|
||||
}
|
38
examples/set.c
Normal file
38
examples/set.c
Normal file
@ -0,0 +1,38 @@
|
||||
/* This software is licensed by the MIT License, see LICENSE file */
|
||||
/* Copyright © 2022 Gregory Lirent */
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include "../include/extra/set.h"
|
||||
|
||||
typedef vtype_set vset_t;
|
||||
|
||||
int print_value(const void* value, vtype type, void* data) {
|
||||
const char *n = data;
|
||||
vtype_int32 *v = (void*)value;
|
||||
|
||||
assert(type == VTYPE_INT32);
|
||||
|
||||
printf("%s %d\n", n, *v);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
vset_t set;
|
||||
|
||||
vset_init(&set, VTYPE_INT32);
|
||||
|
||||
for (size_t i = 0; i < 28; ++i) {
|
||||
vset_push(&set, i);
|
||||
}
|
||||
|
||||
vset_get(&set, 13, "Get value:", print_value);
|
||||
vset_pop(&set, 18, "Pop value:", print_value);
|
||||
|
||||
vset_foreach(&set, "Foreach loop:", print_value);
|
||||
|
||||
vset_free(&set);
|
||||
}
|
42
examples/string.c
Normal file
42
examples/string.c
Normal file
@ -0,0 +1,42 @@
|
||||
/* This software is licensed by the MIT License, see LICENSE file */
|
||||
/* Copyright © 2022 Gregory Lirent */
|
||||
|
||||
#include <stdio.h>
|
||||
#include "../include/extra/string.h"
|
||||
#include "../include/extra/array.h"
|
||||
|
||||
typedef vtype_string str_t;
|
||||
typedef vtype_array arr_t;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
|
||||
str_t str;
|
||||
|
||||
string_init(&str, "sed ut perspiciatis");
|
||||
|
||||
string_concat(&str, ", Unde omnis iste natus error sit voluptatem accusantium doloremque laudantium");
|
||||
string_concat(&str, ", Totam rem aperiam eaque ipsa");
|
||||
string_concat(&str, ", Quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt");
|
||||
string_concat(&str, ", Explicabo.");
|
||||
|
||||
string_capitalize(&str);
|
||||
|
||||
string_reverse(&str);
|
||||
|
||||
printf("%s\n", str.buffer);
|
||||
|
||||
arr_t parts = string_split(&str, ',', -1);
|
||||
|
||||
printf("%lu\n", array_size(&parts));
|
||||
|
||||
for (size_t i = 0; i < array_size(&parts); ++i) {
|
||||
str_t* value = array_at(&parts, i);
|
||||
|
||||
string_trim_spaces(value);
|
||||
|
||||
printf("%s (%lu)\n", value->buffer, string_nmemb(value));
|
||||
}
|
||||
|
||||
array_free(&parts);
|
||||
string_free(&str);
|
||||
}
|
Loading…
Reference in New Issue
Block a user