├── .github
└── workflows
│ └── c-cpp.yml
├── .gitignore
├── Doxyfile
├── LICENCE.md
├── README.md
├── _config.yml
├── build.sh
├── compile_flags.txt
├── docs
├── .overview.md
├── LICENCE.md
├── art
│ └── omp_pt_sin.md
├── config.md
├── imgs
│ ├── enums
│ │ ├── enum_binary_tree.png
│ │ ├── enum_expand1.png
│ │ ├── enum_preview1_fn.png
│ │ └── enum_preview1_gen.png
│ ├── example
│ │ ├── enum_binary_tree.png
│ │ ├── enum_webevent.png
│ │ ├── ex3_omp_arr_fill.gif
│ │ ├── example_1.png
│ │ ├── example_2.png
│ │ ├── fnptr_arr.png
│ │ └── pt_pre_900px.gif
│ ├── output.png
│ ├── readme_gs_conf_error_1.png
│ ├── samplepgrm.png
│ └── ztypes_preview0.png
├── in-depth
│ ├── gmath.md
│ ├── mem.md
│ ├── proc
│ │ └── omp.md
│ └── strings.md
├── index.md
├── install.md
├── overview.md
├── prep
│ └── prep.md
├── proc
│ └── proc.md
├── types
│ ├── alg
│ │ ├── alg.md
│ │ ├── enum.md
│ │ ├── enum_webevent.png
│ │ ├── rec.md
│ │ └── tup.md
│ ├── arr
│ │ ├── Arr_preview0.png
│ │ ├── Arr_preview0_op.png
│ │ ├── dynt.md
│ │ ├── fx_arr.md
│ │ ├── gen_arr.md
│ │ ├── index.md
│ │ └── irrg.md
│ ├── base
│ │ ├── base.md
│ │ └── primitive_types.md
│ ├── ds
│ │ ├── LinkedList.md
│ │ ├── hashhoyt.md
│ │ ├── imgs
│ │ │ └── LinkedList
│ │ │ │ ├── z__Link_0.png
│ │ │ │ ├── z__Link_1.png
│ │ │ │ ├── z__Link_10.png
│ │ │ │ ├── z__Link_11.png
│ │ │ │ ├── z__Link_12.png
│ │ │ │ ├── z__Link_13.png
│ │ │ │ ├── z__Link_14.png
│ │ │ │ ├── z__Link_15.png
│ │ │ │ ├── z__Link_16.png
│ │ │ │ ├── z__Link_2.png
│ │ │ │ ├── z__Link_3.png
│ │ │ │ ├── z__Link_4.png
│ │ │ │ ├── z__Link_5.png
│ │ │ │ ├── z__Link_6.png
│ │ │ │ ├── z__Link_7.png
│ │ │ │ ├── z__Link_8.png
│ │ │ │ └── z__Link_9.png
│ │ ├── index.md
│ │ └── tree.md
│ ├── geo
│ │ ├── matrix.md
│ │ └── vector.md
│ ├── misc
│ │ ├── index.md
│ │ ├── option.md
│ │ └── result.md
│ └── string
│ │ └── string.md
└── using.md
├── examples
├── enum
│ └── binarytree.c
├── gen_perlin2D
│ └── main.c
├── hexdump
│ └── main.c
├── shell
│ └── main.c
└── tabtospace
│ └── main.c
├── extra
└── c.vim
├── lib.c
├── mkdocs.yml
├── spec
├── spec-ori.tex
└── spec.tex
└── src
├── bin
└── main.c
├── lib
├── _config_types.h
├── _export.h
├── _typeid.h
├── ansifmt.h
├── arena.h
├── argparse.h
├── arr.h
├── arrfree.h
├── assert.h
├── atomic.h
├── autotype.h
├── bitf.h
├── bytes.h
├── cglm.h
├── contof.h
├── curl.h
├── dynt.h
├── enum.h
├── fio.h
├── fnptr.h
├── forrange.h
├── gmath.h
├── hashset.h
├── htmlfmt.h
├── irrg.h
├── lexer.h
├── llist.h
├── map.h
├── map2d.h
├── map3d.h
├── mapch.h
├── matrix.h
├── obj.h
├── offsetof.h
├── omp.h
├── option.h
├── pairarr.h
├── prep
│ ├── args.h
│ ├── base.h
│ ├── call.h
│ ├── comp
│ │ └── eval.h
│ ├── eval.h
│ ├── gen
│ │ ├── args
│ │ │ ├── applyfxy.h
│ │ │ ├── get.h
│ │ │ ├── getupto.h
│ │ │ ├── skip.h
│ │ │ └── var.h
│ │ └── num
│ │ │ ├── dec_n1024.h
│ │ │ ├── dec_n2048.h
│ │ │ ├── dec_u16.h
│ │ │ ├── dec_u8.h
│ │ │ ├── inc_n1024.h
│ │ │ ├── inc_n2048.h
│ │ │ ├── inc_u16.h
│ │ │ └── inc_u8.h
│ ├── loop.h
│ ├── map.h
│ ├── nm
│ │ ├── assert.h
│ │ ├── cond.h
│ │ ├── ident.h
│ │ ├── pragma.h
│ │ └── string.h
│ └── num.h
├── print.h
├── random.h
├── record.h
├── result.h
├── return.h
├── soarr.h
├── std
│ ├── alloc.h
│ ├── arch.h
│ ├── io.h
│ ├── mem.h
│ └── primitives.h
├── string.h
├── string8.h
├── strto.h
├── subp.h
├── sys.h
├── termio.h
├── test.h
├── tgprint.h
├── thread.h
├── time.h
├── tree.h
├── tui.h
├── tuple.h
├── type.h
├── typegen.h
├── typeid.h
├── typeof.h
├── u8arr.h
├── utf8.h
├── utils.h
└── vector.h
└── testlib
├── arena.c
├── imp
├── argparse.c
├── print.c
└── time.c
├── main.c
├── playground.c
└── types
├── arr.c
├── base.c
├── bit.c
├── hashset.c
├── obj.c
└── string.c
/.github/workflows/c-cpp.yml:
--------------------------------------------------------------------------------
1 | name: C/C++ CI
2 |
3 | on:
4 | push:
5 | branches: [ master ]
6 | pull_request:
7 | branches: [ master ]
8 |
9 | jobs:
10 | build:
11 | strategy:
12 | matrix:
13 | os: [ubuntu-latest, macos-latest, windows-latest]
14 |
15 | runs-on: ${{ matrix.os }}
16 |
17 | steps:
18 | - uses: actions/checkout@v2
19 |
20 | - name: Tests
21 | run: sh build.sh testlib
22 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Prerequisites
2 | *.d
3 |
4 | # Object files
5 | *.o
6 | *.ko
7 | *.obj
8 | *.elf
9 |
10 | # Linker output
11 | *.ilk
12 | *.map
13 | *.exp
14 |
15 | # Precompiled Headers
16 | *.gch
17 | *.pch
18 |
19 | # Libraries
20 | *.lib
21 | *.a
22 | *.la
23 | *.lo
24 |
25 | # Shared objects (inc. Windows DLLs)
26 | *.dll
27 | *.so
28 | *.so.*
29 | *.dylib
30 |
31 | # Executables
32 | *.exe
33 | *.out
34 | *.app
35 | *.i*86
36 | *.x86_64
37 | *.hex
38 |
39 | # Debug files
40 | *.dSYM/
41 | *.su
42 | *.idb
43 | *.pdb
44 |
45 | # Kernel Module Compile Results
46 | *.mod*
47 | *.cmd
48 | .tmp_versions/
49 | modules.order
50 | Module.symvers
51 | Mkfile.old
52 | dkms.confest
53 |
54 | # Self
55 | a.out
56 | .DS_Store
57 | logtest
58 | build
59 | ./build
60 | docs/vid/*
61 | site/
62 | src/prep/gen/num/inc_u16.h
63 | src/prep/gen/num/dec_u16.h
64 |
--------------------------------------------------------------------------------
/_config.yml:
--------------------------------------------------------------------------------
1 | theme: jekyll-theme-merlot
--------------------------------------------------------------------------------
/build.sh:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | # Basic Config, edit these variabl
4 | PROJECT_NAME="z_"
5 | CC=gcc
6 | CCFLAGS="-Wall -Wextra -O2"
7 | LIBFLAGS=""
8 | TESTFLAGS="-ggdb3 -g"
9 | AR=ar
10 | ARFLAGS="-q"
11 | OUT_BIN="z_"
12 | OUT_LIB="libz_.a"
13 | OUT_SOLIB="lib$PROJECT_NAME.so"
14 | OUT_HEAD="z_"
15 | BIN_DIR="/usr/local/bin/"
16 | LIB_DIR="/usr/local/lib/"
17 | INC_DIR="/usr/local/include/"
18 | BIN_DIR="$HOME/../usr/bin/"
19 | LIB_DIR="$HOME/../usr/lib/"
20 | INC_DIR="$HOME/../usr/include/"
21 |
22 | check_mkdir() {
23 | if [ -d "$1" ]; then
24 | continue
25 | else
26 | $(mkdir "$1")
27 | fi
28 | }
29 |
30 | check_build() {
31 | check_mkdir "./build"
32 | check_mkdir "./build/lib"
33 | check_mkdir "./build/include"
34 | check_mkdir "./build/bin"
35 | }
36 |
37 | make_clean() {
38 | clean_what="./build"
39 | casevar=$1
40 | case "$casevar" in
41 | "lib") clean_what="$clean_what/lib"
42 | ;;
43 | "headers") clean_what="$clean_what/include"
44 | ;;
45 | "test") clean_what="$clean_what/test"
46 | ;;
47 | "bin") clean_what="$clean_what/bin"
48 | ;;
49 | "ofiles") clean_what="*.o"
50 | ;;
51 | "all") clean_what="./build/* *.o"
52 | ;;
53 | *) echo "Unknown option '$*'"
54 | return
55 | ;;
56 | esac
57 |
58 | $(rm -rf $clean_what)
59 | }
60 |
61 | make_lib_header() {
62 | name=$OUT_HEAD
63 | $(find "./src/lib" -name '*.h' | cpio -pdm ./build/include)
64 | $(rm -rf ./build/$name)
65 | $(mv ./build/include/src/lib ./build/include/$name)
66 | $(rm -rf ./build/include/src)
67 | echo "Made headers as $name in ./build/include"
68 | }
69 |
70 | make_lib(){
71 | extraflags="$1"
72 | FILES=$(find "./src/lib" -name '*.c')
73 | $($CC $CCFLAGS $extraflags -c $FILES)
74 | $($AR $ARFLAGS $OUT_LIB *.o)
75 | make_clean "all"
76 | check_build
77 | $(mv $OUT_LIB ./build/lib)
78 | }
79 |
80 | make_bin(){
81 | make_lib
82 | FILES=$(find "./src/bin" -name '*.c')
83 | $(mkdir ./build/bin)
84 | $($CC $CCFLAGS $LIBFLAGS $FILES ./build/lib/$OUT_LIB -o ./build/bin/$OUT_BIN)
85 | }
86 |
87 | make_install_lib() {
88 | make_lib
89 | $(mv -ni ./build/lib/$OUT_LIB "$LIB_DIR")
90 | make_lib_header
91 | $(cp -Rf ./build/include/$OUT_HEAD "$INC_DIR")
92 | $(rm -rf ./build/include/$OUT_HEAD)
93 | }
94 |
95 | make_install_bin() {
96 | make_bin
97 | $(mv -ni ./build/bin/$OUT_BIN "$BIN_DIR")
98 | }
99 |
100 | make_install() {
101 | case "$1" in
102 | "bin") make_install_bin
103 | ;;
104 | "lib") make_install_lib
105 | ;;
106 | *) echo "Unknown option '$1', either use 'bin' or 'lib'"
107 | ;;
108 | esac
109 | }
110 |
111 | make_test_lib(){
112 | make_lib $TESTFLAGS
113 | FILES=$(find "./src/testlib" -name '*.c')
114 | $($CC $CCFLAGS $TESTFLAGS $FILES ./build/lib/$OUT_LIB -o "./build/testlib")
115 |
116 | case "$1" in
117 | "run") $(./build/testlib)
118 | ;;
119 | *) echo "Use './build.sh testlib run' to build & run the test"
120 | ;;
121 | esac
122 | }
123 |
124 | make() {
125 | check_build
126 | case "$1" in
127 | "lib") make_lib
128 | ;;
129 | "headers") make_lib_header
130 | ;;
131 | "bin") make_bin
132 | ;;
133 | "install") make_install $2
134 | ;;
135 | "testlib") make_test_lib $2
136 | ;;
137 | "testbin") make_test_bin $2
138 | ;;
139 | "clean")
140 | make_clean $2
141 | ;;
142 | *)
143 | echo "Unknown option '$1'"
144 | echo "Usage: $0 [ lib | headers | bin | testlib | testbin | install[lib|bin]]"
145 | ;;
146 | esac
147 |
148 | }
149 |
150 | make $*
151 |
--------------------------------------------------------------------------------
/compile_flags.txt:
--------------------------------------------------------------------------------
1 | -std=c23
2 | -Wall
3 | -Wextra
4 | -Wpedantic
5 | -Winline
6 | -Wswitch
7 | -Warith-conversion
8 | -DZ__IMPLEMENTATION
9 |
--------------------------------------------------------------------------------
/docs/art/omp_pt_sin.md:
--------------------------------------------------------------------------------
1 | # OpenMP vs. Pthread vs. Single Thread
2 |
3 | This file documents the Comparism between the different Multi-thread library in The C Programing Language.
4 |
5 | ## Introduction
6 |
7 |
8 | ### We will go through
9 |
10 | * Performance
11 | * Ease of Use
12 | * Safety
13 |
14 |
15 | ## Benchmark
16 |
17 | ### Tools & Process
18 |
19 | We will be creating a 10000x10000 array and filling them with 2D perlin noise geration.
20 |
21 |
22 |
23 | There will will be 3 Benchmarks for 3 ways to create
24 |
25 | #### Single-Thread
26 |
27 | #### Pthread
28 |
29 | #### OpenMP
30 |
31 | ### Result
32 |
33 | Average Time.
34 |
35 | | | Single Thread| OpenMP| Pthread|
36 | |:---------|---------------:|-------------:|--------------:|
37 | |Time (Avg)|11.47120 seconds|4.1330 seconds|4.08920 seconds|
38 | |Increase | NIL| 64.35290%| 63.97065%|
39 |
40 | ## Evaluation
41 |
42 | ## Conclution
43 |
44 | ## Appendix
45 |
46 | The source code can be found on [examples/gen_perlin2D](../../examples/gen_perlin2D) in the parent directory.
47 |
48 |
49 |
--------------------------------------------------------------------------------
/docs/config.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/config.md
--------------------------------------------------------------------------------
/docs/imgs/enums/enum_binary_tree.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/imgs/enums/enum_binary_tree.png
--------------------------------------------------------------------------------
/docs/imgs/enums/enum_expand1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/imgs/enums/enum_expand1.png
--------------------------------------------------------------------------------
/docs/imgs/enums/enum_preview1_fn.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/imgs/enums/enum_preview1_fn.png
--------------------------------------------------------------------------------
/docs/imgs/enums/enum_preview1_gen.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/imgs/enums/enum_preview1_gen.png
--------------------------------------------------------------------------------
/docs/imgs/example/enum_binary_tree.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/imgs/example/enum_binary_tree.png
--------------------------------------------------------------------------------
/docs/imgs/example/enum_webevent.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/imgs/example/enum_webevent.png
--------------------------------------------------------------------------------
/docs/imgs/example/ex3_omp_arr_fill.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/imgs/example/ex3_omp_arr_fill.gif
--------------------------------------------------------------------------------
/docs/imgs/example/example_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/imgs/example/example_1.png
--------------------------------------------------------------------------------
/docs/imgs/example/example_2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/imgs/example/example_2.png
--------------------------------------------------------------------------------
/docs/imgs/example/fnptr_arr.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/imgs/example/fnptr_arr.png
--------------------------------------------------------------------------------
/docs/imgs/example/pt_pre_900px.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/imgs/example/pt_pre_900px.gif
--------------------------------------------------------------------------------
/docs/imgs/output.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/imgs/output.png
--------------------------------------------------------------------------------
/docs/imgs/readme_gs_conf_error_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/imgs/readme_gs_conf_error_1.png
--------------------------------------------------------------------------------
/docs/imgs/samplepgrm.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/imgs/samplepgrm.png
--------------------------------------------------------------------------------
/docs/imgs/ztypes_preview0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/imgs/ztypes_preview0.png
--------------------------------------------------------------------------------
/docs/in-depth/gmath.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/in-depth/gmath.md
--------------------------------------------------------------------------------
/docs/in-depth/mem.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/in-depth/mem.md
--------------------------------------------------------------------------------
/docs/in-depth/proc/omp.md:
--------------------------------------------------------------------------------
1 | # Multi-Threading with OpenMP in z_
2 |
3 | This File Documents the Parallel computing implentation/features done and provided by `z_`, using OpenMP.
4 |
5 | ## The Basics
6 |
7 | OpenMP is called with `z__omp(ARGS)` macro, which is just a wrapper that translates into,
8 | ```c
9 | #pragma omp ARGS
10 | ```
11 | > `ARGS`: Comma seperated Arguments that will places after `omp`.
12 |
13 | ### 1. Defination
14 |
15 | ### 2. Base Operations
16 |
17 | ### 3. Implemention
18 |
19 | ### 4. More
20 |
21 | ## Usage
22 |
23 | ### Creating an Implemention
24 |
25 | ## End Note
26 |
--------------------------------------------------------------------------------
/docs/in-depth/strings.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/in-depth/strings.md
--------------------------------------------------------------------------------
/docs/index.md:
--------------------------------------------------------------------------------
1 | # z_
2 |
3 | Guide & Documentation for `z_` Library
4 |
5 | **`#include `**
6 |
7 | ## Table of Contents
8 |
9 | - **Types**
10 | - [Primitive Types & Base Utils](types/base/base.md)
11 | - [Memory]()
12 | - [String Types]()
13 | - [Array Types](types/arr/index.md)
14 | - [Algebraic Types](types/alg/alg.md)
15 | - [Geometric Types]()
16 | - [Data Structues]()
17 | - [Miscellaneous](types/misc/index.md)
18 | - **Proc**
19 | - [Pthread]()
20 | - [Threads]()
21 | - [OpenMP]()
22 | - **IMP**
23 | - [File IO]()
24 | - [Time]()
25 | - [System]()
26 | - [Terminal Text Formating]()
27 | - [Generic Print]()
28 | - **PreP**
29 |
30 |
--------------------------------------------------------------------------------
/docs/install.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/install.md
--------------------------------------------------------------------------------
/docs/overview.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/overview.md
--------------------------------------------------------------------------------
/docs/prep/prep.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/prep/prep.md
--------------------------------------------------------------------------------
/docs/proc/proc.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/proc/proc.md
--------------------------------------------------------------------------------
/docs/types/alg/alg.md:
--------------------------------------------------------------------------------
1 | # Algebraic Types
2 |
3 | [From Wikipedia](https://en.wikipedia.org/wiki/Algebraic_data_type): In computer programming, especially functional programming and type theory, an algebraic data type is a kind of composite type, i.e., a type formed by combining other types.
4 |
5 | Two common classes of algebraic types are product types (i.e., tuples and records) and sum types (i.e., tagged or disjoint unions, coproduct types or variant types
6 |
7 | This Library Provides
8 |
9 | - [Tuples](./tup.md)
10 | - [Records](./rec.md)
11 | - [Sum Types](./enum.md)
12 |
13 |
14 |
--------------------------------------------------------------------------------
/docs/types/alg/enum.md:
--------------------------------------------------------------------------------
1 | # Sum Types
2 |
3 | **`#include `**
4 |
5 | Sum Types are DataType that can hold value of several but fixed types. Only one type can be used at a time.
6 | The C language already something similar called `unions` but in unions themselves never declare which type is being used. Thus, it is called a `untaged-union`.
7 |
8 | Sum-Types also known as `Taged Unions`, `Choice Type`, `Variant` etc. Holds a tag that explicitly indicates which field is in use.
9 |
10 | `z_` provides Sum Types as `z__Enum`, `z__EnumDef` in `z_/types/enum.h` Header.
11 |
12 |
13 | ## Usage
14 |
15 | To Create a new Sum Type use the `z__Enum`
16 | ```c
17 | z__Enum(
18 | ,
19 | (, ),
20 | ...
21 | );
22 | ```
23 |
24 | * `Name` is the new Enum Type.
25 | * `variant` is the Variant of that enum type. Followed by a product-type; Tuple or Record. If left blank, that variant is taken as unit type.
26 |
27 | Example:
28 |
29 |

30 |
31 |
32 | Each `variant` is also generates a function of the same name, which can be used to initiazed a Sum Type. As shown in the Example.
33 |
34 | #### Assignment without Function calling
35 |
36 | If we do not want to use a function we have `z__Enum_chip(&en, sl, ...)` & `z__Enum_grave(Enum_T, sl, ...)` macros which do the same with out calling a function. In fact all the variant function themselves have `z__Enum_chip()` in them.
37 |
38 | ##### With Function Calling
39 |
40 | ```c
41 | z__Enum(
42 | Result,
43 | (Ok, void *, size_t),
44 | (Err, char *)
45 | );
46 |
47 | Result safe_malloc(size_t len) {
48 | void *data = malloc(len);
49 | Result result;
50 | if (data) {
51 | return Ok(data, len);
52 | } else {
53 | return Err("Out of Memory!!");
54 | }
55 | }
56 |
57 | ```
58 |
59 | ##### Using `z__Enum_chip`
60 |
61 | ```c
62 | z__Enum(
63 | Result,
64 | (Ok, void *, size_t),
65 | (Err, char *)
66 | );
67 |
68 | Result safe_malloc(size_t len) {
69 | void *data = malloc(len);
70 | Result result;
71 | if (data) {
72 | z__Enum_chip(&result, Ok, data, len);
73 | } else {
74 | z__Enum_chip(&result, Err, "Out of Memory!!");
75 | }
76 |
77 | return result;
78 | }
79 |
80 | ```
81 |
82 | ##### Using `z__Enum_grave`
83 |
84 | ```c
85 | z__Enum(
86 | Result,
87 | (Ok, void *, size_t),
88 | (Err, char *)
89 | );
90 |
91 | Result safe_malloc(size_t len) {
92 | void *data = malloc(len);
93 | if (data) {
94 | return z__Enum_grave(Result, Ok, data, len);
95 | } else {
96 | return z__Enum_grave(Result, Err, "Out of Memory!!");
97 | }
98 | }
99 |
100 | ```
101 |
102 | #### Advantages
103 |
104 | * `z__Enum_grave` & `z__Enum_chip` does not do any function call.
105 | * Paired with `z__EnumDef` which does not create/generate any variant-related function. Makes a useful versatile and light-weight alternative to the bloated `z__Enum`.
106 |
107 | Using this I've able to create new types such as [result.h](../../src/types/result.h) and [option.h](../../src/types/option.h).
108 |
109 | #### Match Statement
110 |
111 | Using `match` or `z__Enum_match` you can evaluate which variant is in use and act accordingly.
112 | ```c
113 | match(result) {
114 | // ...
115 | }
116 | ```
117 |
118 | - Each variant is represented through `slot` or `z__Enum_slot`
119 | - `unslot` is to close off a `slot`
120 |
121 |
122 | ##### `slot` & `unslot`
123 |
124 | ```c
125 | match(result) {
126 | slot(Ok, data, size) do_something(*data, *size); unslot;
127 | slot(Err, msg) log_error(*msg); unslot;
128 | }
129 | ```
130 |
131 | If you already familiar with Tuples and records it would be easier to understand whats happening.
132 | You can read about them from [here](./alg.md).
133 |
134 | ---
135 |
136 | Here is a quick run-down for using `slot`:
137 | - First argument of `slot()` is the variant name, Followed by the reffernce names.
138 | - Reffrences are created exactly how `z__Tuple_toRefernces` & `z__Record_toRefernces` does.
139 | - `slot()`; empty slot acts as the `default` case. i.e If none of the variants are matched, default case is used.
140 | - `slot(var, _x, _y)` is used for Tuple Variants
141 | - `slot(var, (_x, member1), (_y, member2))` for Record Variants
142 | - `slot(var, ((reff)))` Creates a Reffernce to the Tuple or Record Variant
143 | - `()` can be used to skip the members in for tuple variants, such as `slot(var, _x, (), _z)`
144 |
145 |
146 | [Back to Algebraic Type](./alg.md)
147 |
148 |
--------------------------------------------------------------------------------
/docs/types/alg/enum_webevent.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/alg/enum_webevent.png
--------------------------------------------------------------------------------
/docs/types/alg/rec.md:
--------------------------------------------------------------------------------
1 | # Records
2 |
3 | **`#include `**
4 |
5 | Create a Record using `z__Record(...)`
6 | ```c
7 | z__Record(
8 | ,
9 | (, , .. ),
10 | ...
11 | ) ;
12 | ```
13 |
14 | * `Name` is the new datatype. Followed by their fields.
15 | * Each field is enclosed inside a parentheses.
16 | * First Argument is reserved for the `typename`, following which are its member of that `type`.
17 |
18 | Example:
19 | ```c
20 | z__Record(
21 | Map2D,
22 | (int, height, width),
23 | (char *, data)
24 | );
25 |
26 | struct Map2D map;
27 | ```
28 |
29 | Creating a Anonymous Record
30 | ```c
31 | // This time no Name Required.
32 | z__RecordX(
33 | (int, height, width),
34 | (char *, data)
35 | ) map;
36 | ```
37 |
38 | #### Creating Bit-Fields
39 |
40 | Records has built-in support for generating bit-fields.
41 | ```c
42 | z__RecordX(
43 | (int, id),
44 | (char, (1, // <= Specify how many bits each field has access to
45 | attr_is_toxic,
46 | attr_is_liquid,
47 | attr_is_invisible))
48 | ) TileData;
49 | ```
50 |
51 | The above code generates a structure with four members `(id, attr_is_toxic, attr_is_liquid, attr_is_invisible)`.
52 | Performing a `sizeof` on TileData will yield `5` bytes.
53 | - `4` bytes for `id`.
54 | - `1` bytes for `(attr_is_toxic, attr_is_liquid, attr_is_invisible)`
55 | - `(attr_is_toxic, attr_is_liquid, attr_is_invisible)` Has access to each bit of a `char` which is of 8 bytes.
56 |
57 |
58 | #### Nested Records
59 |
60 | Records Can be nested; Declare a record inside a record. And also with Tuples too!!
61 | ```c
62 | z__Record(
63 | Player,
64 | (z__RecordX((float, x, y)), pos),
65 | (z__Tuple(float, float), HP, MP, AP )
66 | );
67 | ```
68 |
69 | #### Accessing Member
70 |
71 | Accessing a Record Member is same as struct
72 | ```c
73 | Player player1; // Using The record from previous example
74 | player1.pos.x = 10.0f;
75 | ```
76 |
77 | #### Assignment
78 |
79 | Assignment can be done in a bulk using `z__Record_assign`
80 | ```c
81 | z__RecordX(
82 | (int, height, width),
83 | (char *, data)
84 | ) map;
85 |
86 | z__Record_assign(&map,
87 | (height, 10),
88 | (width, 10),
89 | (data, calloc(sizeof(char), map.height * map.width)));
90 | ```
91 |
92 | #### Create References
93 |
94 | Similar to Tuples Reffrences can be created using `z__Record_toReference`
95 | ```c
96 | z__Record_toReference(&map,(height, h), (width, w));
97 | printf("%d %d", *h, *w);
98 | ```
99 |
100 | ---
101 |
102 | Move on to:
103 | * [Sum Types](./enum.md)
104 | * [Tuples](./tup.md)
105 |
106 |
--------------------------------------------------------------------------------
/docs/types/alg/tup.md:
--------------------------------------------------------------------------------
1 | # Tuples
2 |
3 | **`#include `**
4 |
5 | Tuples are structures holding values of different types, where each of its members are not named or specified by the user.
6 |
7 | To Create a basic anonymous Tuple use:
8 | ```c
9 | z__TupleX(T1, T2, ...) ;
10 | ```
11 | > Here we create a anonymous structure where each argument is the type of their respective index.
12 | Example:
13 | ```c
14 | z__TupleX(int, int, int) color_rgb;
15 | z__TupleX(char *, size_t) message;
16 | ```
17 |
18 | We can also create Named Structure
19 | ```c
20 | z__Tuple(
21 | Clr_rgb,
22 | int, int, int
23 | );
24 | struct Clr_rgb color_rgb;
25 | ```
26 |
27 | #### Accessing Variables
28 |
29 | Each value are index from `_0` to `_n`.
30 |
31 | To access one
32 | ```c
33 | z__TupleX(int, int, int) color_rgb;
34 | color_rgb._0 = 23; // First Member
35 | color_rgb._1 = 54; // Second Member
36 | color_rgb._2 = 211; // Third Member
37 | ```
38 |
39 | Or we can use `z__Tuple_get(tup, n)`
40 | ```c
41 | return z__Tuple_get(color_rgb, 1); // Return the second element
42 | ```
43 |
44 | #### Assignment
45 |
46 | For quick assignment you can use the Accessing Variables method or we can use the `z__Tuple_assign(&tup, ...)` Macro
47 | It'll automatically assign the values corresponding to the index.
48 | ```c
49 | z__TupleX(int, int, int) color_rgb;
50 | z__Tuple_assign(&color_rgb, 23, 54, 211);
51 | ```
52 |
53 | #### Create References
54 |
55 | `z__Tuple_toReference(tup, ...)` Creates refferences to a tuples's members.
56 |
57 | ```c
58 | z__TupleX(int, int, int) color_rgb;
59 | z__Tuple_toReference(color_rgb, r, g, b);
60 | *r = 23;
61 | *g = 54;
62 | *b = 211;
63 | ```
64 |
65 | If you want the skip a or multiple indexs use `()` in their place
66 | ```c
67 | z__TupleX(int, int, int) color_rgb;
68 | z__Tuple_toReference(color_rgb, r, (), b);
69 | *r = 23;
70 | *b = 211;
71 | // g pointer is never created.
72 | ```
73 |
74 | #### Create Member Prototypes
75 |
76 | You can Declare Variables Resembling the exact type of a members in a tuple
77 | ```c
78 | z__TupleX(time_t, char *, size_t) types_tmp;
79 | z__Tuple_toVarProto(types_tmp,
80 | time, // Type: time_t
81 | message, // Type: char *
82 | length // Type: size_t
83 | );
84 |
85 | // `()` can be use to skip a member.
86 | z__Tuple_toVarProto(types_tmp, (), message2, size2);
87 | ```
88 |
89 | ---
90 |
91 | Thats it!! You can also see other Algebraic Types below:
92 | * [Records](./rec.md)
93 | * [Sum Types](./enum.md)
94 |
95 |
--------------------------------------------------------------------------------
/docs/types/arr/Arr_preview0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/arr/Arr_preview0.png
--------------------------------------------------------------------------------
/docs/types/arr/Arr_preview0_op.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/arr/Arr_preview0_op.png
--------------------------------------------------------------------------------
/docs/types/arr/dynt.md:
--------------------------------------------------------------------------------
1 | # Typeless Array (Same Size)
2 |
3 | **`#include `**
4 |
5 |
--------------------------------------------------------------------------------
/docs/types/arr/fx_arr.md:
--------------------------------------------------------------------------------
1 | # Fixed Size Arrays
2 |
3 | **`#include `**
4 |
5 |
--------------------------------------------------------------------------------
/docs/types/arr/gen_arr.md:
--------------------------------------------------------------------------------
1 | # Generic Array Type
2 |
3 | **`#include `**
4 |
5 | *z_* provides type generic arrays, similar to vectors in C++. This is an in-depth explanation of `z__Arr`.
6 |
7 | ## About
8 | Generic arrays are totally constructed with macros and macro functions, however
9 | there is a Funtion Generator inspired by P99_NEW, more on that later.
10 |
11 | ## Getting Started
12 | In order to construct a new Array we have to use `z__Arr(T) name` Macro to do so.
13 | ```c
14 | // Example: Creating a new Array
15 | z__Arr(int) myvar;
16 | ```
17 | Will create a new variable `myvar` holding three values (NOTE: z__Arr(T) is just a struct in disguise).
18 | ```c
19 | myvar.data // Array Holding Data
20 | myvar.len // Total length; Maximum amount of scaler data the array can hold.
21 | myvar.lenUsed // The Total space already in use.
22 | ```
23 | ```c
24 | typedef z__Arr(int) myarrtype;
25 | myarrtype myvar; // We can also create a typedef for multiple usecase
26 | ```
27 |
28 | ### Pre-requisites
29 | * If `len` is 0, then that means Array is not initialized.
30 | * If `len` is 0 and `lenUsed` is > 0 then its assumed the z__Arr is soft copy; Linked with some other array.
31 |
32 |
33 | ## Constructing an Array
34 | We have created our array but before we start using it, we have to initialize it first.
35 | ```c
36 | int space = 13;
37 | z__Arr_new(myvar, space);
38 | // @var: Variable name
39 | // @space: The amount of space to be initialized at start.
40 | ```
41 | > Thats it, Now we can use we our Array.
42 |
43 | There are multiple ways to access and add or delete values from the array.
44 | Like the Direct Spartan way.
45 | ```c
46 | myvar.data[myvar.lenUsed] = 23;
47 | myvar.lenUsed++;
48 | ```
49 |
50 | Now this is bit dirty so there is a function to it in a more clean manner.
51 | ```c
52 | z__Arr_push(&myvar, 23);
53 | ```
54 | This always add the value at the first unused space of the Array and increments the `lenUsed` by 1. Not only that if the array is filled up then it will reallocate the space, and update the size.
55 |
56 | Now then, here we set `int` type as the datatype which the array will hold. We can ofcourse have any datatype which is valid in c.
57 |
58 | Such As:
59 | ```c
60 | struct Point {int x, int y};
61 | struct Point p1 = {32 , 4};
62 |
63 | z__Arr(struct Point) Px; // Defining a new Array.
64 | z__Arr_new(&Px, 8); // Initializing the Array.
65 |
66 | z__Arr_push(&Px, (struct Point){12, 32} ); // Pushing At Top index[0].
67 | z__Arr_push(&Px, p1); // Pushing At Top index[1].
68 |
69 | ```
70 |
71 | ## Popping & Deleting
72 |
73 | ### Popping
74 |
75 | Popping funtion for z__Arr is pretty straight forward.
76 | ```c
77 | z__Arr_pop(&Px); // Deletes the last pushed value.
78 | ```
79 | It also checks if too many unused memory is allocated, if yes it'll resize it,
80 | leaving enough spaces determined by a threshold.
81 |
82 | ### Deletion
83 |
84 | Remember `z__Arr` is allocated on heap, so we have to manually delete the variable we have created.
85 |
86 | ```c
87 | z__Arr_delete(&Px); // Free up allocated memory and set len & lenUsed to 0.
88 | ```
89 | This Cleans up your array.
90 |
91 |
92 | ## Previews
93 |
94 |

95 |
Output
96 |

97 |
98 |
--------------------------------------------------------------------------------
/docs/types/arr/index.md:
--------------------------------------------------------------------------------
1 | # Arrays
2 |
3 | #### Type Generic Arrays
4 |
5 | * [Fixed Length](./fx_arr.md)
6 | * [Dynamic](./gen_arr.md)
7 |
8 | #### Typeless Arrays
9 |
10 | * [Same-sized Elements](./dynt.md)
11 | * [Varied-sized Elements](./irrg.md)
12 | > Both are Dynamic Arrays
13 |
--------------------------------------------------------------------------------
/docs/types/arr/irrg.md:
--------------------------------------------------------------------------------
1 | # Typeless Array (Varied Size)
2 |
3 | **`#include `**
4 |
5 |
--------------------------------------------------------------------------------
/docs/types/base/base.md:
--------------------------------------------------------------------------------
1 | # Base Utils Of Types
2 |
3 | * [Primitive Types](./primitive_types.md)
4 | * [Type Of Operations & Type IDs]()
5 | * [Raw Bif-Fields]()
6 | * [Util Macros]()
7 |
8 |
--------------------------------------------------------------------------------
/docs/types/base/primitive_types.md:
--------------------------------------------------------------------------------
1 | # Primitive Types And Basics
2 |
3 | **`#include `**
4 |
5 | `ztypes` includes type defination of primitive types (a basic wrapper around what standard library provides).
6 |
7 | ## Showcase
8 |
9 | **Fixed Sized Types** : These Types will be of same size *always*.
10 |
11 | | Type |Description|
12 | |------------------------------------------------------------|-----------|
13 | | **z__u\** | An unsigned 8-bit, 16-bit, 32-bit, 64-bit Integer.|
14 | | **z__i\** | A signed 8-bit, 16-bit, 32-bit, 64-bit Integer.|
15 | | **z__f\** | 32-bit or 64-bit floating Number.|
16 | | **z__bool** | Boolean |
17 | | **z__byte** | Same as 8-bit Integer.|
18 |
19 | ---
20 | > \ is the the bit-space of that type, i.e. z__u32 is a 32=bit integer.
21 | > For Integer They are: 8, 16, 32, 64
22 | > For Floats They are: 32, 64
23 |
24 | - Any Integer type suffixed with _f will inidicate its a _fast_ variant of that datatype. Example being `z__u32_f` or `z__i8_f`
25 |
26 | ---
27 |
28 | There is also un-specified
29 | |||
30 | |---|---|
31 | | **z__int** | System Specific Integer Type |
32 | | **z__float** | System Specific Float Type |
33 |
34 |
35 | Some Other Miscellaneous Types
36 | |||
37 | |---|---|
38 | | **z__ptr** | System Specific Pointer Type |
39 | | **z__imax** | Max Integer width type |
40 | | **z__umax** | Max Integer width type (unsigned) |
41 | | **z__iptr** | Pointer for Ptr arithmatic |
42 | | **z__uptr** | Pointer for Ptr arithmatic (unsigned) |
43 | | **z__ptddiff** | For Result of pointer subtraction (difference) |
44 | | **z__size** | Result for `sizeof` or `z__sizeof` operator |
45 | > Due the Architecture Difference in the _CPUs_ the pointer size also changes;
46 | > On a 32-bit machine, a pointer is of a 32-bit in size and in 64-bit machine it is of 64-bit in size.
47 |
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/docs/types/ds/hashhoyt.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/hashhoyt.md
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_0.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_0.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_1.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_10.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_10.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_11.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_11.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_12.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_12.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_13.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_13.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_14.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_14.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_15.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_15.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_16.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_16.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_2.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_3.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_4.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_4.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_5.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_5.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_6.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_6.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_7.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_7.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_8.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_8.png
--------------------------------------------------------------------------------
/docs/types/ds/imgs/LinkedList/z__Link_9.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/imgs/LinkedList/z__Link_9.png
--------------------------------------------------------------------------------
/docs/types/ds/index.md:
--------------------------------------------------------------------------------
1 | # Data Structures
2 |
3 | Here lies the Various collections of Data Structure.
4 |
5 | * [Linked List](./LinkedList.md)
6 | * [Hash Table]()
7 |
8 | ---
9 |
10 | [Back to TOCs](../../index.md)
11 |
12 |
--------------------------------------------------------------------------------
/docs/types/ds/tree.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/ds/tree.md
--------------------------------------------------------------------------------
/docs/types/geo/matrix.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/geo/matrix.md
--------------------------------------------------------------------------------
/docs/types/geo/vector.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/geo/vector.md
--------------------------------------------------------------------------------
/docs/types/misc/index.md:
--------------------------------------------------------------------------------
1 | # Miscellaneous
2 |
3 | For miscellaneous, uncategorized stuff
4 |
5 | * [Result Type](./result.md)
6 | * [Option Type](./option.md)
7 |
--------------------------------------------------------------------------------
/docs/types/misc/option.md:
--------------------------------------------------------------------------------
1 | # Option Type
2 |
3 | **`#include `**
4 |
5 | Option Type types provides optional values for handling exceptions and such.
6 |
7 | ---
8 |
9 | A Option Type is created with the prototype shown below:
10 | ```c
11 | z__Option(Tok, T...);
12 | ```
13 | `Tok` is the token name that does not pollute namespaces. `T...` is tuple/list of generic types.
14 |
15 | `z__OptionType(Tok)` is used to retrieve the structure of the Option Type. `z__Some(Tok, val...)` and `z__None(Tok)`.
16 |
17 | ##### Example
18 |
19 | ---
20 |
21 | * [Go Back](./index.md)
22 | * [Homepage](../../index.md)m
23 |
--------------------------------------------------------------------------------
/docs/types/misc/result.md:
--------------------------------------------------------------------------------
1 | # Result Type
2 |
3 | **`#include `**
4 |
5 | Result Type provides a definitive way handle recoverable errors.
6 |
7 | ---
8 |
9 | A Result Type is created with the prototype shown below:
10 | ```c
11 | z__Result(Tok, T, E);
12 | ```
13 | `Tok` is the token name that does not pollute namespaces. `T` and `E` are generic type parameters; With `T` representing the type value that is to be returned on success and `E` is the Error type to be returned.
14 |
15 | `z__ResultType(Tok)` is used to retrieve the structure of the Result Type. `z__Ok(Tok, val)` and `z__Err(Tok, val)` is used for returning the result on success or failure of type `T` and `E` respectively.
16 |
17 | ##### Example
18 | ```c
19 | z__Result(Mem, void *, char *);
20 | z__ResultType(Mem) safe_malloc(size_t size)
21 | {
22 | void *_tmp = malloc(size);
23 | if(_tmp) return z__Ok(Mem, _tmp);
24 | return z__Err(Mem, "Memory Allocation Failed");
25 | }
26 | ```
27 |
28 | #### Evaluating Errors
29 |
30 | For evaluation `match` and `IfSlot` statement is used similar to `z__Enum`.
31 |
32 | ```c
33 | z__Result(Mem, void *, char *);
34 | z__ResultType(Mem) safe_malloc(size_t size)
35 | {
36 | void *_tmp = malloc(size);
37 | if(_tmp) return z__Ok(Mem, _tmp);
38 | return z__Err(Mem, "Memory Allocation Failed");
39 | }
40 |
41 | int main(void)
42 | {
43 | z__auto data = safe_malloc(1000);
44 | void *mem;
45 | ifSlot(data, Err, why) print("%s", *why) unslot;
46 |
47 | z__auto data_2 = safe_malloc(102424);
48 | void *mem_2;
49 | match(data_2) {
50 | slot(Ok, gt) mem = *gt; unslot;
51 | slot(Err, why) printf("%s", *why); unslot;
52 | }
53 | }
54 | ```
55 |
56 | #### Set Default value
57 |
58 | Using `z__Result_unwrap_or(R_T, def_val)` you unwrap, evaluate and choose between the retrieve and default value.
59 |
60 | ```c
61 | z__Result(Num, int, char *);
62 | z__ResultType(Num) get_number(void)
63 | {
64 | if (0) return z__Ok(10);
65 | z__Err("No Number for you");
66 | }
67 |
68 | int main(void)
69 | {
70 | int a = z__Result_unwrap_or(get_number(), 25);
71 | printf("%d\n", a); // Will print 25.
72 | }
73 | ```
74 |
75 | ---
76 |
77 | * [Go Back](./index.md)
78 | * [Homepage](../../index.md)
79 |
--------------------------------------------------------------------------------
/docs/types/string/string.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/types/string/string.md
--------------------------------------------------------------------------------
/docs/using.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/docs/using.md
--------------------------------------------------------------------------------
/examples/enum/binarytree.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | z__Enum(
5 | BinaryTree,
6 | (Leaf, int),
7 | (Node, BinaryTree *, int, BinaryTree *)
8 | );
9 |
10 | int sum(const BinaryTree *tree) { /* 6 */
11 | match(*tree) { /* /\ */
12 | slot(Leaf, x) /* 2 7 */
13 | return *x; unslot; /* /\ */
14 | slot(Node, lhs, x, rhs) /* 1 4 */
15 | return sum(*lhs) + *x + sum(*rhs); unslot; /* /\ */
16 | } /* 3 5 */
17 | return 0;
18 | }
19 |
20 | #define TREE(tree) ((BinaryTree *)(BinaryTree[]){tree})
21 | #define NODE(left, numbe30, right) TREE(Node(left, numbe30, right))
22 | #define LEAF(number) TREE(Leaf(number))
23 |
24 | int main(int argc, char ** argv)
25 | {
26 | const BinaryTree *tree =
27 | NODE(NODE(LEAF(1), 2, NODE(LEAF(3), 4, LEAF(5))), 6, LEAF(7));
28 |
29 | printf("%d\n", sum(tree)); // 28
30 | }
31 |
32 |
--------------------------------------------------------------------------------
/examples/hexdump/main.c:
--------------------------------------------------------------------------------
1 | /**
2 | * z_ Example: hexdump
3 | * Prints out byte sequence of any file.
4 | * Flags:
5 | * -f [FILENAME] -> Specify the file name/path. REQUIRED
6 | * -w [WIDTH] -> Specify how long the each line should be in bytes, default: 16
7 | */
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 | #include
14 |
15 | #include
16 | #include
17 | #include
18 |
19 | struct Config {
20 | z__size width;
21 | z__size from;
22 | z__String filename;
23 | };
24 |
25 | void print_hex_line(
26 | z__size const line
27 | , z__byte const *ori_bt
28 | , z__size size
29 | , z__size width)
30 | {
31 | z__size i = line;
32 | z__size const len = z__util_min_unsafe(width+i, size);
33 | z__byte const *iter = ori_bt + i;
34 |
35 | printf("%010lx:", line);
36 | for (; i < len; i++, iter++) {
37 | printf(" %02hhx", *iter);
38 | }
39 |
40 | i = line;
41 | iter = ori_bt + i;
42 | fputs(" | ", stdout);
43 | for (; i < len; i++, iter++) {
44 | if(isprint(*iter)) {
45 | putchar(*iter);
46 | } else {
47 | putchar('.');
48 | }
49 | }
50 | putc('\n', stdout);
51 | }
52 |
53 | int main (int argc, char *argv[])
54 | {
55 | struct Config config = {
56 | .width = 0x10
57 | , .filename = NULL
58 | , .from = 0
59 | };
60 |
61 | z__argp_start(argv, 1, argc) {
62 | z__argp_ifarg(&config.width, "-w");
63 | z__argp_ifarg(&config.from, "-s");
64 | z__argp_ifarg_custom("-f") {
65 | z__argp_next();
66 | config.filename = z__String_newFromStr(z__argp_get(), -1);
67 | }
68 | }
69 |
70 | if(config.width == 0) config.width = 0x10;
71 | if(config.filename.data == NULL) {
72 | perror("File name not specified, please use the " "-f" "flags to do so.");
73 | return 0;
74 | }
75 |
76 | z__byteArr filedata = {0};
77 | z__fio_Arr_newFromFile(&filedata, config.filename.data);
78 |
79 | if(filedata.data == NULL) {
80 | perror("FILE CANT BE LOAD");
81 | return 0;
82 | }
83 |
84 | printf("%s %ubytes\n", config.filename.data, filedata.lenUsed);
85 |
86 | z__size i = config.from;
87 | for (; i < filedata.lenUsed; i += config.width) {
88 | print_hex_line(i
89 | , filedata.data
90 | , filedata.lenUsed * sizeof(*filedata.data)
91 | , config.width);
92 | }
93 |
94 | z__Arr_delete(&filedata);
95 | z__String_delete(&config.filename);
96 |
97 | return 0;
98 | }
99 |
--------------------------------------------------------------------------------
/examples/shell/main.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 | #include
6 | #include
7 | #include
8 | #include
9 | #include
10 |
11 | #include
12 | #include
13 | #include
14 |
15 | #include
16 | #include
17 |
18 | #include
19 |
20 | #define PROMT_L ""
21 |
22 | typedef struct chs_State chs_State;
23 | typedef void (* BuiltinFn)(chs_State *, void **);
24 | typedef z__HashHoyt(BuiltinFn) Functions;
25 |
26 | typedef struct chs_State {
27 | char *cmd;
28 | char *args;
29 |
30 | int cmd_len;
31 | int arg_len;
32 |
33 | z__String dir;
34 | Functions fn;
35 | } chs_State;
36 |
37 | void die(int _code)
38 | {
39 | exit(_code);
40 | }
41 |
42 | static void _cd_back(chs_State *c)
43 | {
44 | if(c->dir.lenUsed > 1) {
45 | int i = c->dir.lenUsed-2;
46 | while(i > 1 && c->dir.data[i] != '/') i--;
47 | c->dir.lenUsed = i;
48 | c->dir.data[i] = 0;
49 | }
50 | return;
51 | }
52 |
53 | static void _cd(chs_State *c, void **m)
54 | {
55 | const char *args = c->args;
56 | if(args[0] == 0) z__String_replace(&c->dir, "/");
57 | if(args[0] == '.' && args[1] == '.') _cd_back(c);
58 |
59 | if(args[0] == '/') {
60 | if(z__fio_directoryExist(args)) {
61 | z__String_replace(&c->dir, "%s", args);
62 | } else {
63 | z__log_cl256(1, "Folder %s Does not exist!\n", args);
64 | }
65 | return;
66 | }
67 |
68 | z__String_append(&c->dir, 0, '0', "%s/", args);
69 | if (z__fio_directoryExist(c->dir.data)) {
70 | } else {
71 | z__log_cl256(1, "Folder %s Does not exist!\n", c->dir.data);
72 | _cd_back(c);
73 | }
74 | }
75 |
76 | static void _ls(chs_State *c, void **m)
77 | {
78 | if(c->args[0] == 0) c->args = "./";
79 | const char *args = c->args;
80 |
81 | z__String tmp = z__String_newCopy(c->dir);
82 | _cd(c, 0);
83 |
84 | DIR *drip = opendir(c->dir.data);
85 | if(drip == NULL) goto _L_return;
86 |
87 | struct dirent *d;
88 | int items = 0;
89 | while((d = readdir(drip)) != NULL) {
90 | printf("%s\n", d->d_name);
91 | }
92 | closedir(drip);
93 |
94 | _L_return:
95 | z__String_delete(&c->dir);
96 | c->dir = tmp;
97 | }
98 |
99 | void chs_State_addfn(chs_State *c, const char *name, BuiltinFn fn)
100 | {
101 | z__HashHoyt_set(&c->fn, name, fn);
102 | }
103 |
104 | chs_State *chs_State_new(void)
105 | {
106 | chs_State *c = z__New(*c, 1);
107 | c->cmd = "";
108 | c->args = "";
109 |
110 | c->dir = z__String_new(128);
111 | z__String_replace(&c->dir, "/");
112 |
113 | z__HashHoyt_new(&c->fn);
114 | chs_State_addfn(c, "cd", _cd);
115 | chs_State_addfn(c, "ls", _ls);
116 | return c;
117 | }
118 |
119 | void chs_State_delete(chs_State *c)
120 | {
121 | z__String_delete(&c->dir);
122 | z__HashHoyt_delete(&c->fn);
123 | }
124 |
125 | void* chs_State_tryrunfn(chs_State *c)
126 | {
127 | BuiltinFn *f;
128 | z__HashHoyt_getreff(&c->fn, c->cmd, f);
129 | z__fnptr_call(f, 0, c, NULL);
130 | return f;
131 | }
132 |
133 | #define chs_readline(c, promt)\
134 | {\
135 | (c)->cmd = readline(promt);\
136 | add_history((c)->cmd);\
137 | (c)->cmd_len = strlen((c)->cmd);\
138 | (c)->args = (char *)z__str_skipget_ws((c)->cmd, (c)->cmd_len);\
139 | (c)->arg_len = ((c)->args - (c)->cmd);\
140 | (c)->cmd_len = (c)->cmd_len - (c)->arg_len;\
141 | *(c)->args = 0;\
142 | (c)->args += 1;\
143 | }
144 |
145 | #define chs_linefree(c)\
146 | { z__FREE((c)->cmd); (c)->cmd = ""; (c)->args = ""; }
147 |
148 | static void chs_run(void)
149 | {
150 | chs_State *c = chs_State_new();
151 | int quit = 0;
152 | while(!quit) {
153 | printf("%s >>> ", c->dir.data);
154 | chs_readline(c, PROMT_L);
155 | if(chs_State_tryrunfn(c) == NULL) {
156 | c->args[-1] = ' ';
157 | z__sub_spawn(c->cmd);
158 | }
159 | chs_linefree(c);
160 | }
161 | }
162 |
163 | int main(int argc, char const *argv[])
164 | {
165 | signal(SIGINT, die);
166 | chs_run();
167 | return 0;
168 | }
169 |
--------------------------------------------------------------------------------
/examples/tabtospace/main.c:
--------------------------------------------------------------------------------
1 | /**
2 | * z_ Example: tabtospace
3 | * Reads in a file, converts tabs to spaces.
4 | * Requies two argument:
5 | * [FILE] -> file name/path that will be read.
6 | * [FILE_OUT] -> file to written in.
7 | */
8 |
9 | #include
10 | #include
11 | #include
12 | #include
13 |
14 | int main(int argc, char const *argv[])
15 | {
16 | if(argc < 2) {
17 | fprintf(stderr, "%s requires two arguments [FILE] [FILE_OUT]", argv[0]);
18 | }
19 |
20 | z__String str = z__String_newFromFile(argv[1]);
21 | if(str.data == NULL) {
22 | fprintf(stderr, "Cant Load File %s", argv[1]);
23 | goto _L_return_ERR;
24 | }
25 |
26 | z__String str_out = z__String_new(32);
27 |
28 | for (size_t i = 0; i < str.lenUsed; i++) {
29 | if(str.data[i] == '\t') {
30 | for (size_t i = 0; i < 4; i++) {
31 | z__String_pushChar(&str_out, ' ');
32 | }
33 | } else {
34 | z__String_pushChar(&str_out, str.data[i]);
35 | }
36 | }
37 |
38 | FILE *fp = fopen(argv[2], "w");
39 | if(fp == NULL) {
40 | fprintf(stderr, "Cant write to file %s\n", argv[2]);
41 | goto _L_return_ERR;
42 | }
43 | fwrite(str_out.data, sizeof(*str_out.data), str_out.lenUsed-1, fp);
44 | fclose(fp);
45 |
46 | z__String_delete(&str);
47 | z__String_delete(&str_out);
48 | return EXIT_SUCCESS;
49 |
50 | _L_return_ERR: {
51 | z__String_delete(&str);
52 | z__String_delete(&str_out);
53 | return EXIT_FAILURE;
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/extra/c.vim:
--------------------------------------------------------------------------------
1 | " Vim Syntax File
2 | " Language: C, ztypes-Extention
3 | " Author: Zakarouf
4 |
5 | " This File is merely an extention for C syntax to be used for my self defined
6 | " types. Do not use This as a standalone syntax file.
7 | " Put it in your `after/syntax/` as `c.vim` folder or append these lines to your
8 | " pre-exting syntax extention file
9 |
10 |
11 | " BASIC START
12 | syn keyword cType z__int z__float z__bool z__ptr z__byte
13 | syn keyword cType z__iptr z__uptr z__umax z__imax
14 | syn keyword cType z__size z__ptrdiff
15 |
16 | syn keyword cType z__i8 z__i16 z__i32 z__i64
17 | syn keyword cType z__i8_f z__i16_f z__i32_f z__i64_f
18 |
19 | syn keyword cType z__u8 z__u16 z__u32 z__u64
20 | syn keyword cType z__u8_f z__u16_f z__u32_f z__u64_f
21 |
22 | syn keyword cType z__f32 z__f64
23 |
24 | syn keyword cType z__Bytes z__Dynt z__Irrg z__voidArr z__byteArr
25 | syn keyword cType z__i8Arr z__i16Arr z__i32Arr z__i64Arr
26 | syn keyword cType z__u8Arr z__u16Arr z__u32Arr z__u64Arr
27 | syn keyword cType z__ptrArr z__boolArr z__f32Arr z__f64Arr
28 |
29 | syn keyword cType z__char z__CStr z__Str
30 | syn keyword cType z__String z__StringList z__StringListArr
31 |
32 | " Types
33 | syn keyword cType z__ivec2 z__ivec3 z__ivec4
34 | syn keyword cType z__vec2 z__vec3 z__vec4
35 | syn keyword cType z__mat2 z__mat3 z__mat4
36 | syn keyword cType z__versor;
37 |
38 | syn keyword cType z__Vector2 z__Vector3 z__Vector4 z__Versor
39 | syn keyword cType z__Vint2 z__Vint3 z__Vint4
40 |
41 | syn keyword cType z__i8Vec2 z__i8Vec3 z__i8Vec4
42 | syn keyword cType z__i16Vec2 z__i16Vec3 z__i16Vec4
43 | syn keyword cType z__i32Vec2 z__i32Vec3 z__i32Vec4
44 | syn keyword cType z__i64Vec2 z__i64Vec3 z__i64Vec4
45 |
46 | syn keyword cType z__u8Vec2 z__u8Vec3 z__u8Vec4
47 | syn keyword cType z__u16Vec2 z__u16Vec3 z__u16Vec4
48 | syn keyword cType z__u32Vec2 z__u32Vec3 z__u32Vec4
49 | syn keyword cType z__u64Vec2 z__u64Vec3 z__u64Vec4
50 |
51 | syn keyword cType z__Matrix2 z__Matrix3 z__Matrix4
52 | syn keyword cType z__Mint2 z__Mint3 z__Mint4
53 |
54 | syn keyword cStructure z__Vector z__Arr z__Martix z__VectorSt
55 | syn keyword cStructure z__ArrFx z__ArrSx z__ArrSxDef z__ArrSxI z__ArrSxT
56 | syn keyword cStructure z__Link z__LinkType z__LList
57 |
58 | syn keyword cStructure z__Tuple z__TupleX z__Record z__RecordX record z__Enum z__EnumDef
59 | syn keyword cStatement match slot unslot ifSlot emptyslot
60 |
61 | syn keyword cStructure z__Result z__Option
62 |
63 | syn keyword cType z__auto
64 | syn keyword cOperator z__typeof z__sizeof z__typeID z__cont_typeof
65 |
66 | "Objfn & Closure
67 | syn keyword cStatement z__objcl_call
68 | syn keyword cFunction z__objfn_cl
69 | syn keyword cStructure z__objcl
70 |
71 | "Mem
72 | syn keyword cStatement z__autofree z__Arrfree
73 |
74 | "OpenMP
75 | syn keyword cStatement z__omp z__omp_parallel z__omp_for
76 |
77 | "Pthread
78 |
79 | "syn keyword cType z__pt_Thread z__pt_MetaData z__pt_UniqueData z__pt_Thread_Attr
80 | "syn keyword cStructure z__pt_DataContext z__pt_DataContextType
81 | "Misc
82 |
83 | syn keyword cStatement zpp__pragma
84 |
85 | "syn match cFunction "\<[a-zA-Z_][a-zA-Z_0-9]*\>[^()]*)("me=e-2
86 | "syn match cFunction "\<[a-zA-Z_][a-zA-Z_0-9]*\>\s*("me=e-1
87 | "hi cFunction gui=NONE guifg=#aff232
88 |
89 |
--------------------------------------------------------------------------------
/lib.c:
--------------------------------------------------------------------------------
1 | #include "src/lib/z__dynt.c"
2 | #include "src/lib/z__u8arr.c"
3 | #include "src/lib/z__arena.c"
4 | #include "src/lib/z__fio.c"
5 | #include "src/lib/z__map.c"
6 | #include "src/lib/z__sys.c"
7 | #include "src/lib/z__irrg.c"
8 | #include "src/lib/z__subp.c"
9 | #include "src/lib/z__time.c"
10 | #include "src/lib/z__bytes.c"
11 | #include "src/lib/z__lexer.c"
12 | #include "src/lib/z__map2D.c"
13 | #include "src/lib/z__map.c"
14 | #include "src/lib/z__mapch.c"
15 | #include "src/lib/z__print.c"
16 | #include "src/lib/z__string.c"
17 | #include "src/lib/z__termio.c"
18 | #include "src/lib/z__thread.c"
19 | #include "src/lib/z__arrfree.c"
20 | #include "src/lib/z__tgprint.c"
21 | #include "src/lib/z__tui_term.c"
22 | #include "src/lib/z__tui_window.c"
23 | #include "src/lib/z__tui_display.c"
24 |
25 |
--------------------------------------------------------------------------------
/mkdocs.yml:
--------------------------------------------------------------------------------
1 | site_name: z_
2 | site_url: https://zakarouf.github.io/z_
3 |
4 | theme:
5 | name: material
6 | language: en
7 | features:
8 | - content.code.annotate
9 | # - content.tabs.link
10 | # - header.autohide
11 | # - navigation.expand
12 | - navigation.indexes
13 | # - navigation.instant
14 | - navigation.sections
15 | - navigation.tabs
16 | # - navigation.tabs.sticky
17 | - navigation.top
18 | - navigation.tracking
19 | - search.highlight
20 | - search.share
21 | - search.suggest
22 | # - toc.integrate
23 | palette:
24 | scheme: slate
25 | primary: amber
26 | accent: amber
27 |
28 | markdown_extensions: [fenced_code]
29 |
30 | # Extensions
31 | markdown_extensions:
32 | - admonition
33 | - abbr
34 | - attr_list
35 | - def_list
36 | - footnotes
37 | - meta
38 | - md_in_html
39 | - toc:
40 | permalink: true
41 | - pymdownx.arithmatex:
42 | generic: true
43 | - pymdownx.betterem:
44 | smart_enable: all
45 | - pymdownx.caret
46 | - pymdownx.details
47 | - pymdownx.emoji:
48 | emoji_index: !!python/name:materialx.emoji.twemoji
49 | emoji_generator: !!python/name:materialx.emoji.to_svg
50 | - pymdownx.highlight:
51 | anchor_linenums: true
52 | - pymdownx.inlinehilite
53 | - pymdownx.keys
54 | - pymdownx.mark
55 | - pymdownx.smartsymbols
56 | - pymdownx.superfences:
57 | custom_fences:
58 | - name: mermaid
59 | class: mermaid
60 | format: !!python/name:pymdownx.superfences.fence_code_format
61 | - pymdownx.tabbed:
62 | alternate_style: true
63 | - pymdownx.tasklist:
64 | custom_checkbox: true
65 | - pymdownx.tilde
66 | - fenced_code
67 |
68 | nav:
69 | - Home: index.md
70 | - Getting Started:
71 | - install.md
72 | - using.md
73 | - config.md
74 | - Overview: overview.md
75 | - Types:
76 | - Primitive Types: types/base/primitive_types.md
77 | - String: types/string/string.md
78 | - Generic Arrays: types/arr/gen_arr.md
79 | - Object Arrays: types/arr/dynt.md
80 | - Object Lists: types/arr/irrg.md
81 | - Vector: types/geo/vector.md
82 | - Matrix: types/geo/matrix.md
83 | - Record: types/alg/rec.md
84 | - Tuple: types/alg/tup.md
85 | - Sum Type: types/alg/enum.md
86 | - Optional: types/misc/option.md
87 | - Result: types/misc/result.md
88 | - Hash Table: types/ds/hashhoyt.md
89 | - Linked List: types/ds/LinkedList.md
90 | - Tree: types/ds/tree.md
91 |
92 | # - Type System:
93 | # - Type of
94 | # - Type id
95 | # - Auto Type
96 | # - Type Generic
97 | # - Type Reflection
98 | # - Memory:
99 | # - Basic Allocaters
100 | # - Wrapper
101 | # - Deconstrutor with z__Autofree
102 | # - Memory Logging
103 | #- Multi-Threading:
104 | # - Multi-Threading Model
105 | # - Using Pthreads
106 | # - Using OpenMP
107 | #- Standard Implementations:
108 | # - File System
109 | # - Time System
110 | # - Argument Parser
111 | # - ANSI Escape Code Formater
112 | # - Generic Print
113 | # - String Caster
114 | # - Logger
115 | # - Terminal IO
116 | #- Preprocessor:
117 | # - Introduction
118 | # - Programming Using C Macros
119 | # - Argument Selection
120 | # - Argument Mapping
121 | # - Conditionals
122 | - Licence: LICENCE.md
123 |
--------------------------------------------------------------------------------
/spec/spec-ori.tex:
--------------------------------------------------------------------------------
1 | \documentclass[12pt]{article}
2 |
3 | \usepackage{hyperref}
4 | \usepackage{float}
5 | \usepackage{bussproofs}
6 | \usepackage{amssymb}
7 | \usepackage[standard,thmmarks]{ntheorem}
8 | \usepackage{biblatex}
9 | \usepackage{csquotes}
10 | \usepackage{mathtools}
11 | \usepackage{tabularx}
12 | \usepackage[english]{babel}
13 |
14 | \theoremstyle{break}
15 | \newtheorem{notation}{Notation}
16 |
17 | \addbibresource{references.bib}
18 |
19 | \floatstyle{boxed}
20 | \restylefloat{figure}
21 | \allowdisplaybreaks
22 |
23 | \begin{document}
24 |
25 | \title{z\_ Module Specification (v1.58.0)}
26 | \data{\today}
27 | \author{Zakarouf \\ e-mail: \href{mailto:zak.d.official@gmail.com}{zak.d.official@gmail.com}}
28 | \maketitle
29 |
30 | \begin{abstract}
31 | This paper formally describes the syntax and semantics of various submodules of Types, Meta-Programs & Functions written in z\_. A super-sect of the C Standard Library that aims to bring user-friendly high-level abstraction and functions to C. To learn about and have a overview on z\_, please see the official repository \cite{z_}.
32 | \end{abstract}
33 |
34 | \tableofcontents
35 |
36 | \newpage
37 |
38 | \section{EBNF Grammer}
39 | \begin{figure}[H]
40 | \caption{Grammer Rules}
41 |
42 |
43 | \end{figure}
44 | \end{document}
45 |
--------------------------------------------------------------------------------
/spec/spec.tex:
--------------------------------------------------------------------------------
1 | \documentclass[12pt]{article}
2 |
3 | \usepackage{hyperref}
4 | \usepackage{float}
5 |
6 | \theoremstyle{break}
7 | \newtheorem{notation}{Notation}
8 |
9 | \addbibresource{references.bib}
10 |
11 | \floatstyle{boxed}
12 | \restylefloat{figure}
13 | \allowdisplaybreaks
14 |
15 | \begin{document}
16 |
17 | \title{z\_ Module Specification (v1.58.0)}
18 | \data{\today}
19 | \author{Zakarouf \\ e-mail: \href{mailto:zak.d.official@gmail.com}{zak.d.official@gmail.com}}
20 | \maketitle
21 |
22 | \begin{abstract}
23 | This paper formally describes the syntax and semantics of various submodules of Types, Meta-Programs & Functions written in z\_. A super-sect of the C Standard Library that aims to bring user-friendly high-level abstraction and functions to C. To learn about and have a overview on z\_, please see the official repository \cite{z_}.
24 | \end{abstract}
25 |
26 | \tableofcontents
27 |
28 | \newpage
29 |
30 | \section{EBNF Grammer}
31 | \begin{figure}[H]
32 | \caption{Grammer Rules}
33 |
34 |
35 | \end{figure}
36 | \end{document}
37 |
--------------------------------------------------------------------------------
/src/bin/main.c:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | int main(int argc, char *argv[])
4 | {
5 |
6 | return EXIT_SUCCESS;
7 | }
8 |
--------------------------------------------------------------------------------
/src/lib/_config_types.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__DYNAMIC_TYPE__CONFIG_H
2 | #define ZAKAROUF__DYNAMIC_TYPE__CONFIG_H
3 |
4 | /*
5 | * Z__TYPES_CONFIG
6 | * ---------------------
7 | * - Indentions refers to sub-config of the previous one:
8 | * EXAMPLE: #define foo
9 | * #define bar
10 | * ^
11 | * Here `bar` is sub-config of `foo`
12 | * Note: Sub-config generally imply that the parent config
13 | * is Active, and if the there is #if #elif #else type of deal then
14 | * `//##ELIF` and `//##ELSE` is used in the same indentation as config its
15 | * part of
16 | *
17 | * This type of Config Defination and Used in-order to easily specify which type
18 | * of config it is? And how it is going to effect the compilation process.
19 | * Many of these parent Config directly effects the weight of the code Base. So it
20 | * is advised to turn off those config definations which are not needed by commentng
21 | * them out. Such as CGLM or Vector Types etc.
22 | */
23 |
24 | #include "_export.h"
25 |
26 |
27 | /* Config Start */
28 | /*------------------------------------------*/
29 |
30 | /* General */
31 | /* Pretty self Explanatory, Edit them with Caution */
32 | #define Z___TYPE_REALLOC_RESIZE_BY_DEFAULT 8
33 |
34 | #define Z___TYPE_CONFIG__ARR__GROWTH_FACTOR__NUM 8
35 | #define Z___TYPE_CONFIG__ARR__GROWTH_FACTOR__OPERATOR +
36 | #define Z___TYPE_CONFIG__ARR__SHRINK_FACTOR__NUM 8
37 |
38 | #define Z___TYPE_STRINGLINES_REALLOC_RESIZE_BY_DEFAULT 8
39 |
40 | /*
41 | * Used Dollar-sign at the end of macros to explicitly tell they are indeed macros
42 | * Currently not all macros are aliased sadly.
43 | * z__Arr_new$() <- is a macros
44 | */
45 | //#define Z___TYPE_CONFIG__ALIAS_DOLLARSIGN_FOR_MACROS
46 |
47 |
48 | /* CGLM LIBRARY for graphics maths */
49 | //#define Z___TYPE_INCLUED_CGLM
50 | //#define Z___TYPE_CONFIG__CGLM_ALIAS_TYPE_VECTOR
51 | //#define Z___TYPE_CONFIG__CGLM_ALIAS_TYPE_MATRIX
52 |
53 | /* Used String Type */
54 | #define Z___TYPE_CONFIG__USE_STRING
55 | #define Z___TYPE_CONFIG__STRING__DEFAULT_SIZE 32
56 |
57 | /* Array Types, Use Pre-defined typed arrays? */
58 | #define Z___TYPE_CONFIG__USE_TYPE_ARR_PREDEFINED
59 | //#define Z___TYPE_CONFIG__USE_ARR_PREDEFINED_FUNCS
60 | //#define Z___TYPE_CONFIG__USE_ARR_FUNTION_GENERATION_TEMPLATE
61 |
62 | /* Linked List Type */
63 | #define Z___TYPE_CONFIG__USE_TYPE_LINKEDLIST
64 |
65 | /* Dynamic Arrays */
66 | #define Z___TYPE_CONFIG__USE_ARR_ARRAYTYPE
67 |
68 | /* Use Irregular Object holder */
69 | #define Z___TYPE_CONFIG__USE_IRREGULAR_ARRAYTYPE
70 | #define Z___TYPE_CONFIG__USE_DYNT_ARRAYTYPE
71 |
72 | /* Use Enum Type */
73 | //#define Z___TYPE_CONFIG__USE_ENUM_PRIMITIVE
74 | #define Z___TYPE_CONFIG__USE_ENUM
75 | #define Z___TYPE_CONFIG__USE_ENUM_ALIAS_MATCH_STATEMENT
76 |
77 | /* Use Tuple Type */
78 | #define Z___TYPE_CONFIG__USE_TUPLE
79 |
80 | /* Use Record Type */
81 | #define Z___TYPE_CONFIG__USE_RECORD
82 |
83 | /* Memory Management */
84 | //#define Z___TYPE_CONFIG__USE_MEM /* Custom Mem is always included, this config is useless */
85 | //#define Z___TYPE_CONFIG__USE_MEM__VTRACK
86 | //#define Z___TYPE_CONFIG__USE_MEM__TRACK
87 | //#define Z___TYPE_CONFIG__USE_MEM__TRACK_AS_DEFAULT
88 |
89 | /* Use Vectors and stuff */
90 | #define Z___TYPE_CONFIG__USE_GMATHTYPE
91 | //#define Z___TYPE_CONFIG__USE_f64_FOR_VECTOR_STRUCT
92 | //#define Z___TYPE_CONFIG__USE_i64_FOR_INT_VECTOR_STRUCT
93 |
94 | #define Z___TYPE_CONFIG__USE_EXTENDED_FEATURES
95 | #define Z___TYPE_CONFIG__USE_TYPE_IDENTIFIER
96 |
97 |
98 |
99 | /* Config End */
100 | /*------------------------------------------*/
101 | /* Checks of Conflicting Configs and Errors */
102 |
103 | #ifdef Z___TYPE_CONFIG__ALIAS_DOLLARSIGN_FOR_MACROS
104 | #ifndef __GNUC__
105 | #error "z_:types: Used of Gnu Extention `Dollar-sign` but not availiable!!!\n"
106 | #endif
107 | #endif
108 |
109 | #endif
110 |
111 |
--------------------------------------------------------------------------------
/src/lib/_export.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z__EXPORT_H
2 | #define ZAKAROUF_Z__EXPORT_H
3 |
4 | #define Z__VERSION_STR "2.0.0"
5 | #define Z__VERSION_MAJOR 2
6 | #define Z__VERSION_MINOR 0
7 | #define Z__VERSION_PATCH 0
8 |
9 | #if defined(_MSC_VER)
10 | # ifdef Z__STATIC
11 | # define Z__EXPORT
12 | # elif defined(Z__EXPORTS)
13 | # define Z__EXPORT __declspec(dllexport)
14 | # else
15 | # define Z__EXPORT __declspec(dllimport)
16 | # endif
17 | # define Z__INLINE __forceinline
18 | #else
19 | # define Z__EXPORT __attribute__((visibility("default")))
20 | # define Z__INLINE static inline __attribute((always_inline))
21 | #endif
22 |
23 | #endif
24 |
25 |
--------------------------------------------------------------------------------
/src/lib/_typeid.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPEID_H
2 | #define ZAKAROUF__ZTYPES_TYPEID_H
3 |
4 | #include "std/primitives.h"
5 | #include "vector.h"
6 | #include "matrix.h"
7 | #include "arr.h"
8 | #include "string.h"
9 | #include "dynt.h"
10 | #include "irrg.h"
11 |
12 | #define __builtin_choose_expr __builtin_choose_expr
13 | #define z__typeID(T) \
14 | __builtin_choose_expr(z__type_isequal(T, z__i16), 1, \
15 | __builtin_choose_expr(z__type_isequal(T, z__u16), 2, \
16 | __builtin_choose_expr(z__type_isequal(T, z__i32), 3, \
17 | __builtin_choose_expr(z__type_isequal(T, z__u32), 4, \
18 | __builtin_choose_expr(z__type_isequal(T, z__i64), 5, \
19 | __builtin_choose_expr(z__type_isequal(T, z__u64), 6, \
20 | \
21 | __builtin_choose_expr(z__type_isequal(T, z__f32), 7, \
22 | __builtin_choose_expr(z__type_isequal(T, z__f64), 8, \
23 | \
24 | __builtin_choose_expr(z__type_isequal(T, z__i8Arr), 9, \
25 | __builtin_choose_expr(z__type_isequal(T, z__u8Arr), 10, \
26 | __builtin_choose_expr(z__type_isequal(T, z__i16Arr), 11, \
27 | __builtin_choose_expr(z__type_isequal(T, z__u16Arr), 12, \
28 | __builtin_choose_expr(z__type_isequal(T, z__i32Arr), 13, \
29 | __builtin_choose_expr(z__type_isequal(T, z__u32Arr), 14, \
30 | __builtin_choose_expr(z__type_isequal(T, z__i64Arr), 15, \
31 | __builtin_choose_expr(z__type_isequal(T, z__u64Arr), 16, \
32 | \
33 | __builtin_choose_expr(z__type_isequal(T, z__f32Arr), 17, \
34 | __builtin_choose_expr(z__type_isequal(T, z__f64Arr), 18, \
35 | \
36 | \
37 | __builtin_choose_expr(z__type_isequal(T, z__char), 19, \
38 | __builtin_choose_expr(z__type_isequal(T, z__String), 20, \
39 | __builtin_choose_expr(z__type_isequal(T, z__StringList), 21, \
40 | __builtin_choose_expr(z__type_isequal(T, z__StringListArr), 22, \
41 | \
42 | __builtin_choose_expr(z__type_isequal(T, z__ptr), 23, \
43 | __builtin_choose_expr(z__type_isequal(T, z__Dynt), 24, \
44 | \
45 | __builtin_choose_expr(z__type_isequal(T, char[]), 25, \
46 | __builtin_choose_expr(z__type_isequal(T, const char*), 26, \
47 | \
48 | __builtin_choose_expr(z__type_isequal(T, z__Vector2), 31, \
49 | __builtin_choose_expr(z__type_isequal(T, z__Vector3), 32, \
50 | __builtin_choose_expr(z__type_isequal(T, z__Vector4), 33, \
51 | \
52 | __builtin_choose_expr(z__type_isequal(T, z__Vint2), 34, \
53 | __builtin_choose_expr(z__type_isequal(T, z__Vint3), 35, \
54 | __builtin_choose_expr(z__type_isequal(T, z__Vint4), 36, \
55 | \
56 | __builtin_choose_expr(z__type_isequal(T, z__Matrix2), 37, \
57 | __builtin_choose_expr(z__type_isequal(T, z__Matrix3), 38, \
58 | __builtin_choose_expr(z__type_isequal(T, z__Matrix4), 39, \
59 | \
60 | __builtin_choose_expr(z__type_isequal(T, z__Mint2), 40, \
61 | __builtin_choose_expr(z__type_isequal(T, z__Mint3), 41, \
62 | __builtin_choose_expr(z__type_isequal(T, z__Mint4), 42, \
63 | \
64 | __builtin_choose_expr(z__type_isequal(T, z__ivec2), 51, \
65 | __builtin_choose_expr(z__type_isequal(T, z__ivec3), 52, \
66 | __builtin_choose_expr(z__type_isequal(T, z__ivec4), 53, \
67 | __builtin_choose_expr(z__type_isequal(T, z__vec2), 54, \
68 | __builtin_choose_expr(z__type_isequal(T, z__vec3), 55, \
69 | __builtin_choose_expr(z__type_isequal(T, z__vec4), 56, \
70 | __builtin_choose_expr(z__type_isequal(T, z__versor), 57, \
71 | __builtin_choose_expr(z__type_isequal(T, z__mat2), 58, \
72 | __builtin_choose_expr(z__type_isequal(T, z__mat3), 59, \
73 | __builtin_choose_expr(z__type_isequal(T, z__mat4), 60, \
74 | \
75 | __builtin_choose_expr(sizeof(T) == 1, 19, \
76 | __builtin_choose_expr(sizeof(T) == 2, 1, \
77 | (0) )))))))))))))))))))))))))))))))))))))))))))))))))) // wtf? just no
78 |
79 |
80 | #endif
81 |
82 |
--------------------------------------------------------------------------------
/src/lib/arena.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__Z__ARENA_ALLOCATOR_H
2 | #define ZAKAROUF__Z__ARENA_ALLOCATOR_H
3 |
4 | #include "std/primitives.h"
5 |
6 | typedef struct z__Arena z__Arena;
7 | typedef struct z__ArenaRegion z__ArenaRegion;
8 | struct z__ArenaRegion {
9 | z__byte *pool;
10 | z__size capacity;
11 | z__size used;
12 | z__ArenaRegion *next;
13 | };
14 |
15 | struct z__Arena {
16 | z__ArenaRegion
17 | *begin, *end;
18 | };
19 |
20 | z__Arena* z__Arena_new(z__size pool_capacity);
21 | void z__Arena_delete(z__Arena *a);
22 | void* z__Arena_alloc(z__Arena *a, z__size size);
23 | z__size z__Arena_total_used(z__Arena *a);
24 |
25 |
26 | #ifdef Z__IMPLEMENTATION
27 | #include
28 | #include "std/mem.h"
29 | #include "std/primitives.h"
30 |
31 | static z__ArenaRegion *ArenaRegion_new(z__size max_capacity)
32 | {
33 | z__ArenaRegion *region = z__MALLOC(sizeof(*region) + max_capacity);
34 | region->pool = z__cast(void *, region + 1);
35 | region->capacity = max_capacity;
36 | region->next = NULL;
37 | region->used = 0;
38 | return region;
39 | }
40 |
41 | static z__bool ArenaRegion_check(z__ArenaRegion *r)
42 | {
43 | if(!r->capacity) return 0;
44 | if(r->used > r->capacity) return 0;
45 | if(!r->pool) return 0;
46 |
47 | return 1;
48 | }
49 |
50 | z__Arena *z__Arena_new(z__size pool_capacity)
51 | {
52 | z__ArenaRegion *reg = ArenaRegion_new(pool_capacity + sizeof(z__Arena));
53 | z__Arena *arena = z__cast(void *, reg->pool);
54 | reg->used += sizeof(z__Arena);
55 |
56 | arena->begin = reg;
57 | arena->end = reg;
58 |
59 | return arena;
60 | }
61 |
62 | void z__Arena_delete(z__Arena *arena)
63 | {
64 | z__ArenaRegion *reg = arena->begin;
65 | z__ArenaRegion *reg_next = reg->next;
66 | z__FREE(reg);
67 | reg = reg_next;
68 | while(reg) {
69 | reg->next = reg;
70 | z__FREE(reg);
71 | reg = reg->next;
72 | }
73 | }
74 |
75 | void *z__Arena_alloc(z__Arena *arena, z__size size)
76 | {
77 | z__ArenaRegion *r = arena->end;
78 | z__size availiable = r->capacity - r->used;
79 | if(availiable < size) {
80 | z__ArenaRegion *new_r = ArenaRegion_new(size > r->capacity?
81 | size : r->capacity);
82 | r->next = new_r;
83 | arena->end = r->next;
84 | r = new_r;
85 | }
86 |
87 | void *mem = r->pool + r->used;
88 | r->used += size;
89 | return mem;
90 | }
91 |
92 | z__size z__Arena_total_used(z__Arena *arena)
93 | {
94 | z__ArenaRegion *r = arena->begin;
95 | z__size total = r->used;
96 | r = r->next;
97 | while(r != NULL) {
98 | total += r->used;
99 | r = r->next;
100 | }
101 | return total;
102 | }
103 |
104 | z__size z__Arena_total(z__Arena *arena)
105 | {
106 | z__ArenaRegion *r = arena->begin;
107 | z__size total = r->capacity;
108 | r = r->next;
109 | while(r != NULL) {
110 | total += r->capacity;
111 | r = r->next;
112 | }
113 | return total;
114 | }
115 |
116 | z__bool z__Arena_check(z__Arena *arena)
117 | {
118 | if(!arena) return 0;
119 | z__bool line_check = 0;
120 |
121 | z__ArenaRegion *r = arena->begin;
122 | if(!ArenaRegion_check(r)) return 0;
123 |
124 | r = r->next;
125 | while(r != NULL) {
126 | if(!ArenaRegion_check(r)) return 0;
127 | r = r->next;
128 | if(r == arena->end) {
129 | if(r->next == NULL) {
130 | line_check = 1;
131 | }
132 | }
133 | }
134 |
135 | return line_check;
136 | }
137 |
138 | #endif //Z__IMPLEMENTATION
139 |
140 | #endif
141 |
142 |
--------------------------------------------------------------------------------
/src/lib/argparse.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__ARGPARSE_H
2 | #define ZAKAROUF_Z_IMP__ARGPARSE_H
3 |
4 | #include "strto.h"
5 |
6 | #define z__argp_next() {_priv_argp_chptr += 1; if (_priv_argp_chptr >= _priv_argp_chptrEnd)break;}
7 | #define z__argp_get() ({*_priv_argp_chptr;})
8 | #define z__argp_breakcheck
9 | #define z__argp_isatend() ((_priv_argp_chptr + 1) >= _priv_argp_chptrEnd)
10 |
11 | #define z__argp_start(argv, from, upto)\
12 | for(char const **_priv_argp_chptr = &argv[from]\
13 | , **_priv_argp_chptrEnd = &argv[upto];\
14 | _priv_argp_chptr < _priv_argp_chptrEnd;\
15 | _priv_argp_chptr += 1)
16 |
17 | #ifdef Z__ARGPARSE_USE_STRCMP
18 | # define z__PRIV__argp_arg_cmp(x) (strcmp(x, *_priv_argp_chptr) == 0)
19 | #else
20 | # define z__PRIV__argp_arg_cmp(x)\
21 | (strncmp(zpp__PRIV__Args_get_1 x\
22 | , *_priv_argp_chptr\
23 | , zpp__PRIV__Args_get_2 x) == 0)
24 | #endif
25 |
26 | #define z__PRIV__argp_arg_check_empty_1(a)
27 | #define z__PRIV__argp_arg_check_empty_0(a)\
28 | || z__PRIV__argp_arg_cmp(a)
29 |
30 | #define z__PRIV__argp_arg_check(a)\
31 | zpp__CAT(z__PRIV__argp_arg_check_empty_, zpp__Args_IS_EMPTY(a))(a)
32 |
33 | #define z__argp_ifarg_custom(...)\
34 | if(z__PRIV__argp_arg_cmp(zpp__PRIV__Args_get_1(__VA_ARGS__)) \
35 | zpp__Args_map(z__PRIV__argp_arg_check, zpp__Args_skip_1(__VA_ARGS__)) )
36 |
37 | #define z__argp_elifarg_custom(...)\
38 | else if(z__PRIV__argp_arg_cmp(zpp__PRIV__Args_get_1(__VA_ARGS__)) \
39 | zpp__Args_map(z__PRIV__argp_arg_check, zpp__Args_skip_1(__VA_ARGS__)) )
40 |
41 | #define z__argp_ifarg(v, ...)\
42 | z__argp_ifarg_custom(__VA_ARGS__) { \
43 | z__argp_next(); \
44 | _Static_assert(sizeof(z__PRIV__strto_scan_typegen(v)) > 1 \
45 | , "Argparse: Strto Doesn't Support this type"); \
46 | z__strto(*_priv_argp_chptr, v); \
47 | }
48 |
49 | #define z__argp_elifarg(v, ...)\
50 | z__argp_elifarg_custom(__VA_ARGS__) \
51 | z__argp_next(); \
52 | _Static_assert(sizeof(z__PRIV__strto_scan_typegen(v)) > 1 \
53 | , "Argparse: Strto Doesn't Support this type"); \
54 | z__strto(*_priv_argp_chptr, v); \
55 | }
56 |
57 | #endif
58 |
59 |
--------------------------------------------------------------------------------
/src/lib/arrfree.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__ARRFREE_H
2 | #define ZAKAROUF__ZTYPES_TYPES__ARRFREE_H
3 |
4 | void z__mem_Arr_clean(void * arr);
5 | #define z__Arrfree __attribute__((cleanup(z__mem_Arr_clean)))
6 |
7 | #ifdef Z__IMPLEMENTATION
8 | #include
9 | #include "std/mem.h"
10 | #include "arr.h"
11 |
12 | void z__mem_Arr_clean(void * arr)
13 | {
14 | z__voidArr *temp = arr;
15 | //printf("%d, %p\n", temp->len, temp->data); // for Debug
16 | z__Arr_delete(temp);
17 | }
18 | #endif //Z__IMPLEMENTATION
19 |
20 | #endif
21 |
--------------------------------------------------------------------------------
/src/lib/assert.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__Z__ASSERT_H
2 | #define ZAKAROUF__Z__ASSERT_H
3 |
4 | #include "prep/nm/assert.h"
5 | #include "sys.h"
6 |
7 | #define z__assert(exp)\
8 | (zpp__assert_construct(exp \
9 | , z__exit, -1, __FILE__":%d:%s\n" \
10 | "Assersion Failed: '%s'\n", __LINE__, __func__, #exp))
11 |
12 | #define z__assert_eq(a, b) z__assert(a == b)
13 | #define z__assert_noteq(a, b) z__assert(!(a == b))
14 | #define z__assert_nullptr(a) z__assert((a == NULL))
15 |
16 | #endif
17 |
18 |
--------------------------------------------------------------------------------
/src/lib/atomic.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__Z__PROC_PT_ATOMIC_H
2 | #define ZAKAROUF__Z__PROC_PT_ATOMIC_H
3 |
4 | #define z__Atomic(T) _Atomic T
5 |
6 | #if __STDC_VERSION__ < 201112L || __STDC_NO_ATOMICS__ == 1
7 | typedef z__Atomic(int) z__atomic_int;
8 | #else
9 | #include
10 | typedef atomic_int z__atomic_int;
11 | #endif
12 |
13 | #endif
14 |
15 |
--------------------------------------------------------------------------------
/src/lib/autotype.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__AUTOTYPE_H
2 | #define ZAKAROUF__ZTYPES_TYPES__AUTOTYPE_H
3 |
4 | #define z__auto __auto_type
5 |
6 | #endif
7 |
8 |
--------------------------------------------------------------------------------
/src/lib/bitf.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__BITF_H
2 | #define ZAKAROUF__ZTYPES_TYPES__BITF_H
3 |
4 | #include "std/primitives.h"
5 |
6 | /**
7 | * Set the corresponding bit as 1 or True.
8 | * If the bit was already set as 1 or True, nothing happens.
9 | */
10 | #define z__bit_set(b, offset) b |= 1 << (offset)
11 |
12 | /**
13 | * Set the corresponding bit as 0 or False.
14 | * If the bit was already set as 0 or False, nothing happens.
15 | */
16 | #define z__bit_clear(b, offset) b &= ~(1 << (offset))
17 |
18 | /**
19 | * Toggle the corresponding bit value;
20 | * If the value is True, set it to False and vice versa.
21 | */
22 | #define z__bit_toggle(b, offset) b ^= 1 << (offset)
23 |
24 | /**
25 | * Get the value of the number as a whole with only the offset bit set to 1,
26 | * if the corresponding bit is set to True.
27 | */
28 | #define z__bit_is(b, offset) b & (1 << (offset))
29 |
30 | /**
31 | * Check if the corresponding bit is set to True.
32 | */
33 | #define z__bit_has(b, offset) (((b) >> (offset)) & 1)
34 |
35 | /**
36 | * Check if value of bit in a given offset is that of the offset itself.
37 | */
38 | #define z__bit_iseq(b, offset) ((b)&(offset) == offset)
39 |
40 |
41 | /** Bitf Interface **/
42 |
43 |
44 | /**
45 | * Set the corresponding bit as 1 or True.
46 | * If the bit was already set as 1 or True, nothing happens.
47 | */
48 | #define z__bitf_set(x, bit_num)\
49 | z__bit_set(z__PRIV__BITMAKE_U8IDX(x, bit_num), bit_num & 7)
50 |
51 | /**
52 | * Toggle the corresponding bit value;
53 | * If the value is True, set it to False and vice versa.
54 | */
55 | #define z__bitf_toggle(x, bit_num)\
56 | z__bit_toggle(z__PRIV__BITMAKE_U8IDX(x, bit_num), bit_num & 7)
57 | /**
58 | * Set the corresponding bit as 0 or False.
59 | * If the bit was already set as 0 or False, nothing happens.
60 | */
61 | #define z__bitf_clear(x, bit_num)\
62 | z__bit_clear(z__PRIV__BITMAKE_U8IDX(x, bit_num), bit_num & 7)
63 | #define z__bitf_unset(x, bit_num) z__bitf_clear(x, bit_num)
64 |
65 | /**
66 | * Check if the corresponding bit is set to True.
67 | */
68 | #define z__bitf_has(x, bit_num)\
69 | z__bit_has(z__PRIV__BITMAKE_U8IDX(x, bit_num), bit_num & 7)
70 |
71 | #define z__PRIV__BITMAKE_U8IDX(x, bit_num) *(z__cast(z__u8 *, x) + (bit_num >> 3))
72 |
73 | /* */
74 |
75 | #define z__BitFieldIndex__PRIV__gen(x) x
76 |
77 | #define z__BitField_idTagIn(Tag, in) zpp__CAT(z__BitField_idTag(Tag), in)
78 | #define z__BitField_idTag(Tag) BF_##Tag##_
79 |
80 | #define z__BitField_id(Tag, ...)\
81 | enum{ zpp__Args_maplist_fn_Pattern(z__BitFieldIndex__PRIV__gen, z__BitField_idTag(Tag),, __VA_ARGS__) }
82 |
83 | #define z__BitField_set(of, tag, bitIndex) z__bitf_set(of, z__BitField_idTagIn(tag, bitIndex))
84 | #define z__BitField_clear(of, tag, bitIndex) z__bitf_clear(of, z__BitField_idTagIn(tag, bitIndex))
85 | #define z__BitField_toggle(of, tag, bitIndex) z__bitf_toggle(of, z__BitField_idTagIn(tag, bitIndex))
86 | #define z__BitField_is(of, tag, bitIndex) z__bitf_is(of, z__BitField_idTagIn(tag, bitIndex))
87 | #define z__BitField_iseq(of, tag, bitIndex) z__bitf_iseq(of, z__BitField_idTagIn(tag, bitIndex))
88 |
89 |
90 |
91 | #endif
92 |
93 |
--------------------------------------------------------------------------------
/src/lib/bytes.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__BYTES_H
2 | #define ZAKAROUF_Z_IMP__BYTES_H
3 |
4 | #include "arr.h"
5 |
6 | /**
7 | * z__Bytes
8 | * Handling Byte Stream; raw data.
9 | */
10 | typedef struct z__Bytes {
11 |
12 | /* Byte Data */
13 | z__u8Arr bytes;
14 |
15 | /* Byte Cursor (Index) */
16 | z__size cursor;
17 |
18 | /* Last Position of Byte Cursor (Index) */
19 | z__size cursor_last;
20 | } z__Bytes;
21 |
22 | #define z__bytes_get(data, T, idx) (z__typeof(T) *)(((z__byte *)data) + (sizeof(T) * idx))
23 |
24 |
25 |
26 | /**
27 | * Create a New Byte Stream Type.
28 | */
29 | void z__Bytes_new(z__Bytes *, z__u32 const);
30 |
31 | /**
32 | * Create a New Byte Stream and Loads up data;
33 | */
34 | void z__Bytes_newFromPtr(z__Bytes *bt, const z__u8 *src, z__size _size);
35 |
36 | /**
37 | * Deconstructor
38 | */
39 | void z__Bytes_delete(z__Bytes *bt);
40 |
41 | /**
42 | * Push a singular byte into the stream
43 | */
44 | void z__Bytes_push(z__Bytes *, z__u8 const);
45 |
46 | /**
47 | * Push an array of bytes into the stream
48 | */
49 | void z__Bytes_pushStream(z__Bytes *, void const * const, z__size const);
50 |
51 | /**
52 | * Push a z__Bytes type's data in a range
53 | */
54 | void z__Bytes_pushBytes_range(z__Bytes *bt, z__Bytes const *src, z__u64 from, z__u64 len);
55 |
56 | /**
57 | * Push a z__Bytes type's data as a whole
58 | */
59 | void z__Bytes_pushBytes(z__Bytes *bt, z__Bytes const *src);
60 |
61 | /**
62 | * Delete the last pushed byte off the stream
63 | */
64 | void z__Bytes_pop(z__Bytes *);
65 |
66 | /**
67 | * Delete the last `n` pushed bytes off the stream
68 | */
69 | void z__Bytes_popStream(z__Bytes *, z__size const);
70 |
71 | /**
72 | * Resize the stream
73 | */
74 | void z__Bytes_resize(z__Bytes *, z__size const);
75 |
76 | /**
77 | * Compare Function for z__Bytes
78 | */
79 | int z__Bytes_cmp(z__Bytes *bt1, z__Bytes *bt2);
80 |
81 | /**
82 | * Check if data from two Byte Stream is equal;
83 | * Return Either True or False if the data is equal or unequal respectively
84 | */
85 | int z__Bytes_isdataequal(z__Bytes *bt1, z__Bytes *bt2);
86 |
87 | /**
88 | * Moves the Byte Cursor Forward by `n` bytes
89 | */
90 | z__u32 z__Bytes_setCur_next(z__Bytes *bt, z__size by);
91 |
92 | #define z__Bytes_get(T, Bytes, i) (*(T *)(&(Bytes)->bytes.data[i]))
93 | #define z__Bytes_getCur(T, Bytes) (*(T *)(&(Bytes)->bytes.data[(Bytes)->cursor]))
94 | #define z__Bytes_getCurL(T, Bytes) (*(T *)(&(Bytes)->bytes.data[(Bytes)->cursor_last]))
95 | #define z__Bytes_getTop(T, Bytes) (*(T *)(&(Bytes)->bytes.data[(Bytes)->bytes.lenUsed-1]))
96 |
97 | #define z__Bytes_getCur_setnext(T, Bytes) ({ \
98 | z__Bytes_setCur_next(Bytes, sizeof(T)); \
99 | z__Bytes_getCurL(T, Bytes); \
100 | })
101 |
102 | #define z__Bytes_resetCur(Bytes)\
103 | { \
104 | (Bytes)->cursor = 0; \
105 | (Bytes)->cursor_last = 0; \
106 | }
107 |
108 | #define z__Bytes_pushV(Bytes, t) z__Bytes_pushStream(Bytes, &(t), sizeof(t))
109 |
110 | /**/
111 |
112 | #ifdef Z__IMPLEMENTATION
113 | #include
114 | #include
115 |
116 | void z__Bytes_new(z__Bytes *bt, z__u32 len)
117 | {
118 | z__Arr_new(&bt->bytes, len);
119 | bt->cursor = 0;
120 | bt->cursor_last = 0;
121 |
122 | }
123 |
124 | void z__Bytes_newFromPtr(z__Bytes *bt, const z__u8 *src, z__size len)
125 | {
126 | z__Bytes_new(bt, len + 8);
127 | memcpy(bt->bytes.data, src, len);
128 | bt->bytes.lenUsed = len;
129 | }
130 |
131 | void z__Bytes_delete(z__Bytes *bt)
132 | {
133 | z__Arr_delete(&bt->bytes);
134 | bt->cursor = 0;
135 | bt->cursor_last = 0;
136 | }
137 |
138 | void z__Bytes_push(z__Bytes *bt, z__u8 const val)
139 | {
140 | z__Arr_push(&bt->bytes, val);
141 |
142 | }
143 |
144 | void z__Bytes_pushStream(z__Bytes *bt, void const * const data, z__size data_size)
145 | {
146 | if(((bt->bytes.lenUsed) * sizeof(&bt->bytes.data) + data_size)
147 | >= bt->bytes.len * sizeof(&bt->bytes.data) ) {
148 | z__Arr_resize(
149 | &bt->bytes,
150 | ((bt->bytes.lenUsed) * sizeof(&bt->bytes.data)
151 | + data_size + 32));
152 | }
153 | memcpy(&bt->bytes.data[bt->bytes.lenUsed], data, data_size);
154 | bt->bytes.lenUsed += data_size;
155 | }
156 |
157 | void z__Bytes_pushBytes_range(z__Bytes *bt, z__Bytes const *src, z__u64 from, z__u64 len)
158 | {
159 | z__Bytes_pushStream(bt, src->bytes.data + from, len);
160 | }
161 |
162 | void z__Bytes_pushBytes(z__Bytes *bt, z__Bytes const *src)
163 | {
164 | z__Bytes_pushStream(bt, src->bytes.data, src->bytes.lenUsed);
165 | }
166 |
167 | int z__Bytes_cmp(z__Bytes *bt1, z__Bytes *bt2)
168 | {
169 | if(bt1->bytes.lenUsed != bt2->bytes.lenUsed) {
170 | return (z__i64)bt1->bytes.lenUsed - (z__i64)bt2->bytes.lenUsed;
171 | }
172 | return memcmp(bt1->bytes.data, bt2->bytes.data, bt1->bytes.lenUsed);
173 | }
174 |
175 | int z__Bytes_isdataequal(z__Bytes *bt1, z__Bytes *bt2)
176 | {
177 | if(bt1->bytes.lenUsed != bt2->bytes.lenUsed) return 0;
178 | return !memcmp(bt1->bytes.data, bt2->bytes.data, bt1->bytes.lenUsed);
179 | }
180 |
181 | void z__Bytes_pop(z__Bytes *bt)
182 | {
183 | z__Arr_pop_nocheck(&bt->bytes);
184 | if(bt->cursor >= bt->bytes.lenUsed) bt->cursor--;
185 | if(bt->cursor_last >= bt->bytes.lenUsed) bt->cursor_last--;
186 | }
187 |
188 | void z__Bytes_popStream(z__Bytes *bt, z__size sz)
189 | {
190 | if(sz >= bt->bytes.lenUsed) sz = bt->bytes.lenUsed;
191 | bt->bytes.lenUsed -= sz;
192 |
193 | if(bt->cursor >= bt->bytes.lenUsed) bt->cursor = bt->bytes.lenUsed;
194 | if(bt->cursor_last >= bt->bytes.lenUsed) bt->cursor_last = bt->bytes.lenUsed;
195 | }
196 |
197 | void z__Bytes_resize(z__Bytes *bt, z__size sz)
198 | {
199 | z__Arr_resize(&bt->bytes, sz);
200 |
201 | if(bt->cursor >= bt->bytes.lenUsed) bt->cursor = bt->bytes.lenUsed;
202 | if(bt->cursor_last >= bt->bytes.lenUsed) bt->cursor_last = bt->bytes.lenUsed;
203 | }
204 |
205 | z__u32 z__Bytes_setCur_next(z__Bytes *bt, z__size by)
206 | {
207 | (bt)->cursor_last = (bt)->cursor;
208 | if((bt)->cursor + by < (bt)->bytes.lenUsed) {
209 | (bt)->cursor += by;
210 | return true;
211 | }
212 | return false;
213 | }
214 |
215 | #endif //Z__IMPLEMENTATION
216 |
217 | #endif
218 |
--------------------------------------------------------------------------------
/src/lib/cglm.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__CGLM_H
2 | #define ZAKAROUF__ZTYPES_TYPES__CGLM_H
3 |
4 | #include "_config_types.h"
5 |
6 | #include
7 | #include
8 |
9 | #endif
10 |
11 |
--------------------------------------------------------------------------------
/src/lib/contof.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_CONTOF_H
2 | #define ZAKAROUF__ZTYPES_CONTOF_H
3 |
4 | #include "typeof.h"
5 | #include "offsetof.h"
6 |
7 | #define z__contof(ptr, T, M) ({ \
8 | const z__typeof(((T *)0)->M) * __mptr = (ptr); \
9 | (T *)((char *)__mptr - z__offsetof(T, M)); })
10 |
11 |
12 | #endif
13 |
14 |
--------------------------------------------------------------------------------
/src/lib/curl.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_CURLWRAP_H
2 | #define ZAKAROUF_CURLWRAP_H
3 |
4 | typedef struct z__Curl z__Curl;
5 | z__Curl *z__Curl_new(void);
6 | void z__Curl_delete(z__Curl *zc);
7 | void z__Curl_request(z__Curl *zc, const char *link_nullterm);
8 |
9 | #ifdef Z__IMPLEMENTATION
10 | #include
11 | #include
12 |
13 | #include
14 | #include
15 |
16 | struct z__Curl {
17 | CURL *handle;
18 | z__byteArr data;
19 | CURLcode code;
20 | };
21 |
22 | static size_t _WriteMemoryCallback(
23 | void *contents, size_t size
24 | , size_t nmemb, void *userp)
25 | {
26 | size_t realsize = size * nmemb;
27 | z__byteArr *mem = (z__byteArr *)userp;
28 |
29 | z__Arr_expand_ifneeded(mem, realsize+1);
30 |
31 | memcpy(&(mem->data[mem->lenUsed]), contents, realsize);
32 | mem->lenUsed += realsize;
33 | mem->data[mem->lenUsed] = 0;
34 | printf("hi %zu %zu\n", size, nmemb);
35 | return realsize;
36 | }
37 |
38 | z__Curl *z__Curl_new(void)
39 | {
40 | z__Curl *zc = z__MALLOC(sizeof(*zc));
41 | zc->handle = curl_easy_init();
42 | z__Arr_new(&zc->data, 1024);
43 | return zc;
44 | }
45 |
46 | void z__Curl_delete(z__Curl *zc)
47 | {
48 | z__Arr_delete(&zc->data);
49 | curl_easy_cleanup(zc->handle);
50 | z__FREE(zc);
51 | }
52 |
53 | void z__Curl_request(z__Curl *zc, const char *link_nullterm)
54 | {
55 | CURL *curl_handle = zc->handle;
56 | zc->data.lenUsed = 0;
57 | curl_easy_setopt(curl_handle, CURLOPT_URL, link_nullterm);
58 | curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
59 | curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, _WriteMemoryCallback);
60 | curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&zc->data);
61 | curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
62 |
63 | zc->code = curl_easy_perform(curl_handle);
64 | }
65 |
66 | #endif
67 | #endif
68 |
--------------------------------------------------------------------------------
/src/lib/dynt.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__DYNT_H
2 | #define ZAKAROUF_Z_IMP__DYNT_H
3 |
4 | #include "std/primitives.h"
5 |
6 | /* Unknown Similar size Unsafe type Arrays */
7 | typedef struct z__Dynt
8 | {
9 | z__ptr data;
10 | char *comment;
11 | z__u32 commentLen;
12 | z__size unitsize;
13 | z__u32 len, lenUsed;
14 | z__u8 typeID;
15 |
16 | }z__Dynt;
17 |
18 |
19 | #define z__Dynt_getUnitSize(arr) ((arr).unitsize)
20 | #define z__Dynt_getRawData(arr) ((arr).data)
21 | #define z__Dynt_getComment(arr) ((arr).comment)
22 | #define z__Dynt_getLen(arr) ((arr).len)
23 | #define z__Dynt_getUsed(arr) ((arr).lenUsed)
24 |
25 | #define z__Dynt_getAddress(arr, index) (z__Dynt_getRawData(arr) + (index * z__Dynt_getUnitSize(arr)))
26 | #define z__Dynt_getTop(arr, T) ( *(T*)( z__Dynt_getRawData(arr) + ((z__Dynt_getUsed(arr) - 1) * z__Dynt_getUnitSize(arr))) )
27 | #define z__Dynt_getVal(arr, index , T) ( *(T*)( z__Dynt_getRawData(arr) + (index * z__Dynt_getUnitSize(arr))) )
28 |
29 |
30 | z__Dynt z__Dynt_new(z__size unitsize, z__u32 len, z__u8 typeID, const char *comment, z__i32 commentLength);
31 | void z__Dynt_newFromRaw(z__Dynt *obj, void const *ptr, z__size unitsize, z__size len, z__u8 typeID, char const *comment, z__i32 commentLen);
32 |
33 | void z__Dynt_delete(z__Dynt *arrt);
34 | void z__Dynt_push( z__Dynt *arrt, void *val);
35 | void z__Dynt_pop( z__Dynt *arrt);
36 | void z__Dynt_resize(z__Dynt *arrt, z__u32 newsize);
37 | z__Dynt z__Dynt_makeCopy(const z__Dynt arrt);
38 | int z__Dynt_isdataequal(const z__Dynt *d1, const z__Dynt *d2);
39 | int z__Dynt_isduplicate(const z__Dynt *d1, const z__Dynt *d2);
40 |
41 |
42 | #define z__Dynt_newFromArr(arrt, arr, id, comment, commentLen)\
43 | z__Dynt_newFromRaw( \
44 | arrt \
45 | , (arr).data \
46 | , sizeof(*(arr).data) \
47 | , (arr).lenUsed \
48 | , id \
49 | , comment, commentLen); \
50 |
51 |
52 |
53 | #ifdef Z__IMPLEMENTATION
54 |
55 | #include
56 | #include
57 | #include
58 |
59 | #include "std/mem.h"
60 |
61 | z__Dynt z__Dynt_new(z__size unitsize, z__u32 len, z__u8 typeID, const char *comment, z__i32 commentLength)
62 | {
63 | z__Dynt arrt = {
64 | .data = z__MALLOC(unitsize * len),
65 | .unitsize = unitsize,
66 | .len = len,
67 | .lenUsed = 0,
68 | .typeID = typeID
69 | };
70 |
71 | if (commentLength == 0) {
72 | arrt.comment = NULL;
73 | arrt.commentLen = commentLength;
74 | } else if (commentLength == -1) {
75 | arrt.comment = z__MALLOC(sizeof(char) * strnlen(comment, 1024));
76 | snprintf(arrt.comment, 1024, "%s", comment);
77 | arrt.commentLen = 1024;
78 | } else {
79 | arrt.comment = z__MALLOC(sizeof(char) * commentLength);
80 | memcpy(arrt.comment, comment, commentLength);
81 | arrt.comment[commentLength-1] = 0;
82 | arrt.commentLen = commentLength;
83 | }
84 |
85 | return arrt;
86 | }
87 |
88 | void z__Dynt_newFromRaw(z__Dynt *obj, void const *ptr, z__size unitsize, z__size len, z__u8 typeID, char const *comment, z__i32 commentLen)
89 | {
90 | *obj = z__Dynt_new(unitsize, len, typeID, comment, commentLen);
91 | memcpy(obj->data, ptr, unitsize * len);
92 | }
93 |
94 | void z__Dynt_push( z__Dynt *arrt, void *val)
95 | {
96 | if (arrt->lenUsed >= arrt->len)
97 | {
98 | arrt->len += arrt->len * 2;
99 | arrt->data = z__REALLOC_SAFE(arrt->data, arrt->unitsize * (arrt->len) );
100 | }
101 |
102 | void *tmpptr = ((z__byte*)(arrt->data)) + (arrt->lenUsed * arrt->unitsize);
103 | memcpy(tmpptr, val, arrt->unitsize);
104 | arrt->lenUsed += 1;
105 | }
106 |
107 | void z__Dynt_pop( z__Dynt *arrt)
108 | {
109 | arrt->lenUsed -= 1;
110 | if ((arrt->len - arrt->lenUsed) >= 8)
111 | {
112 | arrt->len -= 8;
113 | arrt->data = z__REALLOC_SAFE(arrt->data, arrt->unitsize * (arrt->len) );
114 | }
115 | }
116 |
117 | inline void z__Dynt_resize(z__Dynt *arrt, z__u32 newsize)
118 | {
119 | arrt->len = newsize;
120 | if (arrt->lenUsed > arrt->len)
121 | {
122 | arrt->lenUsed = arrt->len;
123 | }
124 |
125 | arrt->data = z__REALLOC_SAFE(arrt->data, arrt->unitsize * (arrt->len));
126 | }
127 |
128 | z__Dynt z__Dynt_makeCopy(const z__Dynt arrt)
129 | {
130 | z__Dynt arrtCopy = {
131 | .data = z__MALLOC(arrt.len * arrt.unitsize),
132 | .len = arrt.len,
133 | .lenUsed = arrt.lenUsed,
134 | .unitsize = arrt.unitsize,
135 | .commentLen = arrt.commentLen
136 | };
137 |
138 | z__u32 namelen = arrt.commentLen;
139 | if (namelen > 0)
140 | {
141 | arrtCopy.comment = z__MALLOC(sizeof(char) * namelen);
142 | memcpy(arrtCopy.comment, arrt.comment, namelen);
143 | } else {
144 | arrtCopy.comment = NULL;
145 | }
146 |
147 | return arrtCopy;
148 |
149 | }
150 |
151 | int z__Dynt_isdataequal(const z__Dynt *d1, const z__Dynt *d2)
152 | {
153 | if(d1->unitsize * d1->lenUsed != d2->unitsize * d2->lenUsed) return 0;
154 | return !memcmp(d1->data, d2->data, d1->lenUsed * d1->unitsize);
155 | }
156 |
157 | int z__Dynt_isduplicate(const z__Dynt *d1, const z__Dynt *d2)
158 | {
159 | if(d1->commentLen != d2->commentLen) return 0;
160 | if(strncmp(d1->comment, d2->comment, d1->commentLen) != 0) return 0;
161 | return z__Dynt_isdataequal(d1, d2);
162 | }
163 |
164 |
165 | void z__Dynt_delete(z__Dynt* arrt)
166 | {
167 | z__FREE(arrt->data);
168 | arrt->len = 0;
169 | arrt->lenUsed = 0;
170 | arrt->unitsize = 0;
171 |
172 | if (arrt->commentLen > 0){
173 | z__FREE(arrt->comment);
174 | }
175 | arrt->commentLen = 0;
176 | }
177 | #endif // Z__IMPLEMENTATION
178 |
179 | #endif
180 |
--------------------------------------------------------------------------------
/src/lib/fnptr.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__FNPTR_H
2 | #define ZAKAROUF__ZTYPES_TYPES__FNPTR_H
3 |
4 | #include "typeof.h"
5 |
6 | #define z__fnptr_fn(fn) z__typeof(fn)*
7 | #define z__fnptr(Name, T, ...) T(*Name)(__VA_ARGS__)
8 | #define z__fnptr_call(fptr, defrval, ...) (fptr != NULL? (*fptr)(__VA_ARGS__) : defrval)
9 |
10 | #endif
11 |
12 |
--------------------------------------------------------------------------------
/src/lib/forrange.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__FORRANGE_H
2 | #define ZAKAROUF_Z_IMP__FORRANGE_H
3 |
4 | #include "typeof.h"
5 | #include "prep/args.h"
6 |
7 | #define z__PRIV__forrange_5(iterator, init, upto, step, T)\
8 | for (T iterator = (init); iterator < (upto); iterator = step)
9 |
10 | #define z__PRIV__forrange_4(iterator, init, upto, step)\
11 | z__PRIV__forrange_5(iterator, init, upto, step, size_t)
12 |
13 | #define z__PRIV__forrange_3(iterator, init, upto)\
14 | z__PRIV__forrange_4(iterator, init, upto, (iterator + 1))
15 |
16 | #define z__PRIV__forrange_2(iterator, upto)\
17 | z__PRIV__forrange_3(iterator, 0, upto)
18 |
19 | /* Reverse */
20 | #define z__PRIV__forrange_r_4(iterator, from, upto, step)\
21 | for (size_t iterator = from; iterator >= upto; iterator -= step)
22 |
23 | #define z__PRIV__forrange_r_3(iterator, from, upto)\
24 | z__PRIV__forrange_r_4(iterator, from, upto, -1)
25 |
26 |
27 | #define z__forrange(iterator, upto, ...)\
28 | zpp__Args_Overload(z__PRIV__forrange_, iterator, upto ,##__VA_ARGS__)
29 |
30 | #define z__forrange_r(...) zpp__Args_Overload(z__PRIV__forrange_r_, __VA_ARGS__)
31 | #define z__forrange_t(iterator, init, upto)\
32 | z__forrange(iterator, init, upto, (iterator + 1), z__typeof(init))
33 |
34 | #endif
35 |
--------------------------------------------------------------------------------
/src/lib/gmath.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__GMATH_H
2 | #define ZAKAROUF__ZTYPES_TYPES__GMATH_H
3 |
4 | #include "_config_types.h"
5 | #include "std/primitives.h"
6 |
7 | #include "vector.h"
8 | #include "matrix.h"
9 |
10 | #endif
11 |
--------------------------------------------------------------------------------
/src/lib/htmlfmt.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__HTMLFMT_H
2 | #define ZAKAROUF_Z_IMP__HTMLFMT_H
3 |
4 | #include "prep/base.h"
5 | #include "prep/args.h"
6 | #include "prep/map.h"
7 |
8 | /* Create a Single Unclosed Tag */
9 | #define z__html_fmt_tag(tag) "<" z__PRIV__html_tag_open(tag) ">"
10 |
11 | /* Creates a tag encapsulates data */
12 | #define z__html_fmt(tag_data, ...)\
13 | z__html_fmt_tag(tag_data)\
14 | __VA_ARGS__\
15 | "" z__PRIV__html_tag_close(tag_data) ">"
16 |
17 |
18 |
19 | /*! PRIV STUFFF !*/
20 |
21 | /*-----------------HTML---------------*/
22 | #define z__PRIV__html_tag_open__IS_PAREN_0(tag) zpp__TO_STRING(tag)
23 | #define z__PRIV__html_tag_open__IS_PAREN_1(tag) zpp__TO_STRING(zpp__PRIV__Args_get_1 tag) " " zpp__TO_STRING(zpp__Args_skip_1 tag)
24 |
25 | #define z__PRIV__html_tag_open(tag) zpp__CAT(z__PRIV__html_tag_open__IS_PAREN_, zpp__IS_PAREN(tag))(tag)
26 |
27 |
28 | #define z__PRIV__html_tag_close(tag) zpp__CAT(z__PRIV__html_tag_close__IS_PAREN_, zpp__IS_PAREN(tag))(tag)
29 | #define z__PRIV__html_tag_close__IS_PAREN_0(tag) zpp__TO_STRING(tag)
30 | #define z__PRIV__html_tag_close__IS_PAREN_1(tag) zpp__TO_STRING(zpp__PRIV__Args_get_1 tag)
31 |
32 | #endif
33 |
34 |
--------------------------------------------------------------------------------
/src/lib/irrg.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__IRRG_IMPL_H
2 | #define ZAKAROUF_Z_IMP__IRRG_IMPL_H
3 |
4 | #include "std/primitives.h"
5 |
6 | /* Unsafe Irregular Object Holder array */
7 | typedef struct __ZAKAROUF__IRREGULAR_TYPE_STRUCT
8 | {
9 | z__ptr *data;
10 | z__size *size;
11 | z__u8 *typeID;
12 | z__i32 len;
13 | z__i32 lenUsed;
14 | char** comment;
15 |
16 | }z__Irrg;
17 |
18 | #define z__Irrg_getValSize(arr, of) (arr.size[of])
19 | #define z__Irrg_gettypeID(arr, of) (arr.typeID[of])
20 | #define z__Irrg_getComment(arr, of) (arr.comment[of])
21 | #define z__Irrg_getVal(arr, of, T) ( *( T *)(*arr.data[of]) )
22 | #define z__Irrg_getValTop(arr, T) ( *( T *)(*arr.data[arr.lenUsed-1]) )
23 | #define z__Irrg_getAddress(arr, of) (*arr.data[of])
24 | #define z__Irrg_getLen(arr) (arr.len)
25 | #define z__Irrg_getUsed(arr) (arr.lenUsed)
26 |
27 |
28 | z__Irrg z__Irrg_new(z__u32 len);
29 | void z__Irrg_resize(z__Irrg *irgt, z__size newsize);
30 | void z__Irrg_push(z__Irrg *irgt, void *val, z__size size, z__u8 typeID, const char *comment, z__i32 commentLength);
31 | void z__Irrg_pop(z__Irrg *irgt);
32 | void z__Irrg_delete(z__Irrg *irgt);
33 |
34 |
35 | #ifdef Z__IMPLEMENTATION
36 | #include
37 | #include
38 | #include
39 |
40 | #include "std/mem.h"
41 |
42 | z__Irrg z__Irrg_new(z__u32 len)
43 | {
44 | return (z__Irrg) {
45 | .data = z__MALLOC(sizeof(z__ptr*)*len),
46 | .size = z__MALLOC(sizeof(z__size*)*len),
47 | .typeID = z__MALLOC(sizeof(z__u8)*len),
48 | .len = len,
49 | .lenUsed = 0,
50 | .comment = z__MALLOC(sizeof(char*)*len)
51 | };
52 | }
53 |
54 | void z__Irrg_resize(z__Irrg *irgt, z__size newsize)
55 | {
56 | irgt->size = z__REALLOC_SAFE(irgt->size, sizeof(z__size)*newsize );
57 | irgt->typeID = z__REALLOC_SAFE(irgt->typeID, sizeof(z__u8)*newsize);
58 |
59 | // Free memory if _resize is not called from _push or pop
60 | for (z__size i = irgt->lenUsed; i > newsize; --i)
61 | {
62 | //printf("==%d\n", i);
63 | z__FREE(irgt->data[i]);
64 | z__FREE(irgt->comment[i]);
65 | irgt->lenUsed -= 1;
66 | }
67 |
68 | irgt->data = z__REALLOC_SAFE(irgt->data, sizeof(z__ptr*)*newsize);
69 |
70 | irgt->comment = z__REALLOC_SAFE(irgt->comment, sizeof(char*)*newsize);
71 |
72 | irgt->len = newsize;
73 | }
74 |
75 | void z__Irrg_push
76 | (
77 | z__Irrg *irgt
78 | , void *val
79 | , z__size size
80 | , z__u8 typeID
81 | , const char *comment
82 | , z__i32 commentLength
83 | )
84 | {
85 | if (irgt->lenUsed == irgt->len)
86 | {
87 | z__size news = irgt->len * 2;
88 | z__Irrg_resize(irgt, news);
89 |
90 | }
91 |
92 | irgt->data[irgt->lenUsed] = z__MALLOC(size);
93 | memcpy(irgt->data[irgt->lenUsed], val, size);
94 |
95 | irgt->size[irgt->lenUsed] = size;
96 | irgt->typeID[irgt->lenUsed] = typeID;
97 |
98 | if (commentLength == 0) {
99 | irgt->comment[irgt->lenUsed] = NULL;
100 | } else if (commentLength == -1) {
101 | irgt->comment[irgt->lenUsed] = z__MALLOC(sizeof(char) * strnlen(comment, 1024));
102 | snprintf(irgt->comment[irgt->lenUsed], 1024, "%s", comment);
103 | } else {
104 | irgt->comment[irgt->lenUsed] = z__MALLOC(sizeof(char) * commentLength);
105 | memcpy(irgt->comment[irgt->lenUsed], comment, commentLength);
106 | }
107 |
108 | irgt->lenUsed += 1;
109 |
110 | }
111 |
112 | void z__Irrg_pop(z__Irrg *irgt)
113 | {
114 | if (irgt->lenUsed-1 >= 0)
115 | {
116 | z__FREE(irgt->data[irgt->lenUsed-1]);
117 | irgt->lenUsed -= 1;
118 |
119 | if ((irgt->len - irgt->lenUsed) > 8)
120 | {
121 | z__size news = irgt->len - 8;
122 | z__Irrg_resize(irgt, news);
123 | }
124 | }
125 |
126 | }
127 |
128 | void z__Irrg_delete(z__Irrg *irgt)
129 | {
130 | for (int i = irgt->lenUsed; i >= 0; --i)
131 | {
132 | z__FREE(irgt->data[i]);
133 | z__FREE(irgt->comment[i]);
134 |
135 | }
136 | z__FREE(irgt->size);
137 | z__FREE(irgt->typeID);
138 |
139 | z__FREE(irgt->data);
140 | z__FREE(irgt->comment);
141 |
142 | irgt->len = 0;
143 | irgt->lenUsed = 0;
144 | }
145 |
146 | #endif //Z__IMPLEMENTATION
147 |
148 | #endif
149 |
150 |
--------------------------------------------------------------------------------
/src/lib/map.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__PLOTMAP_H
2 | #define ZAKAROUF_Z_IMP__PLOTMAP_H
3 |
4 | #include "std/primitives.h"
5 | #include "vector.h"
6 |
7 | #include "u8arr.h"
8 |
9 | /**
10 | * z__Map()
11 | * Tileset Oriented Map Type with layers or z-dimention.
12 | * size: Dimention of Map
13 | * layers: Total no. of z layers used
14 | * layers_capacity: Total no. of z layers allocated
15 | */
16 | #define z__Map(Name, T, ...) \
17 | typedef struct Name Name; \
18 | struct Name { \
19 | z__u32Vec2 size; \
20 | z__u32 layers; \
21 | z__u32 layers_capacity; \
22 | T **plots; \
23 | __VA_ARGS__ \
24 | }
25 |
26 | void z__Map_new__raw(void *map, z__u64 plotsize, z__u32 x, z__u32 y, z__u32 layers);
27 | void z__Map_delete__raw(void *map);
28 |
29 | int z__Map_new_load__raw(void *_map_, z__u64 plotsize, z__u8Arr *data, z__size from);
30 | void z__Map_dump__raw(void *map, z__u64 plotsize, z__u8Arr *data);
31 |
32 | z__u32 z__Map_layer_push__raw(void *map, z__u64 plotsize);
33 | void z__Map_layer_pop__raw(void *map);
34 | void z__Map_layer_clear__raw(void *map, z__u64 plotsize, z__u32 layer);
35 |
36 | int z__Map_layer_copy__raw(void *_map_, z__u64 plotsize, z__u32 dest, z__u32 src);
37 | int z__Map_layer_push_copy__raw(void *_map_, z__u64 plotsize, z__u32 src);
38 |
39 | int z__Map_layer_swap__raw(void *map, z__u32 l1, z__u32 l2);
40 | int z__Map_layer_swap_at_top__raw(void *map, z__u32 layer);
41 |
42 | #ifdef Z__IMPLEMENTATION
43 | #include
44 | #include
45 | #include "utils.h"
46 | #include "u8arr.h"
47 | #include "forrange.h"
48 |
49 | z__Map(VoidMapNormal, void);
50 | #define self VoidMapNormal *map = _map_;
51 |
52 | void z__Map_new__raw(void *_map_, z__u64 plotsize, z__u32 x, z__u32 y, z__u32 layers)
53 | {
54 | self;
55 |
56 | map->size = (z__u32Vec2){ .x = x, .y = y};
57 | map->layers = layers;
58 | map->layers_capacity = layers + 8;
59 |
60 | map->plots = z__CALLOC(sizeof(*map->plots), map->layers_capacity);
61 | z__forrange(i, layers) {
62 | map->plots[i] = z__MALLOC(plotsize * x * y);
63 | }
64 | }
65 |
66 | void z__Map_delete__raw(void *_map_)
67 | {
68 | self;
69 |
70 | z__forrange(i, map->layers) {
71 | z__FREE(map->plots[i]);
72 | }
73 | z__FREE(map->plots);
74 |
75 | memset(map, 0, sizeof(*map));
76 | }
77 |
78 | void z__Map_dump__raw(void *_map_, z__u64 plotsize, z__u8Arr *data)
79 | {
80 | self;
81 |
82 | z__u8Arr_pushStream(data, &map->size, sizeof(map->size));
83 | z__u8Arr_pushStream(data, &map->layers, sizeof(map->layers));
84 | z__u8Arr_pushStream(data, &plotsize, sizeof(plotsize));
85 |
86 | z__forrange(i, map->layers) {
87 | z__u8Arr_pushStream(data, map->plots[i], plotsize * map->size.x * map->size.y);
88 | }
89 | }
90 |
91 | int z__Map_new_load__raw(void *_map_, z__u64 plotsize, z__u8Arr *data, z__size from)
92 | {
93 | self;
94 |
95 | z__u8Arr_loadStream(data, from, sizeof(map->size), &map->size); from += sizeof(map->size);
96 | z__u8Arr_loadStream(data, from, sizeof(map->layers), &map->layers); from += sizeof(map->layers);
97 |
98 | z__u64 _plotsize = 0;
99 | z__u8Arr_loadStream(data, from, sizeof(_plotsize), &_plotsize); from += sizeof(_plotsize);
100 |
101 | if(_plotsize != plotsize) return 0;
102 |
103 | z__Map_new__raw(map, _plotsize, map->size.x, map->size.y, map->layers);
104 | z__size const size = _plotsize * map->size.x * map->size.y;
105 | z__forrange(i, map->layers) {
106 | z__u8Arr_loadStream(data, from, size, map->plots[i]); from += size;
107 | }
108 | return 1;
109 | }
110 |
111 | z__u32 z__Map_layer_push__raw(void *_map_, z__u64 plotsize)
112 | {
113 | self;
114 | if(map->layers >= map->layers_capacity) {
115 | map->layers_capacity *= 2;
116 | map->plots = z__REALLOC_SAFE(map->plots, sizeof(*map->plots) * map->layers_capacity);
117 | }
118 | map->plots[map->layers] = z__MALLOC(plotsize * map->size.x * map->size.y);
119 | map->layers += 1;
120 | return map->layers;
121 | }
122 |
123 | void z__Map_layer_clear__raw(void *_map_, z__u64 plotsize, z__u32 layer)
124 | {
125 | self;
126 | if(layer >= map->layers) return;
127 | memset(map->plots[layer], 0, plotsize * map->size.x * map->size.y);
128 | }
129 |
130 | void z__Map_layer_pop__raw(void *_map_)
131 | {
132 | self;
133 | z__FREE(map->plots[map->layers-1]);
134 | map->layers -= 1;
135 | }
136 |
137 | int z__Map_layer_swap__raw(void *_map_, z__u32 l1, z__u32 l2)
138 | {
139 | self;
140 | if(l1 > map->layers) return 0;
141 | if(l2 > map->layers) return 0;
142 |
143 | z__swap(map->plots[l1], map->plots[l2]);
144 | return 1;
145 | }
146 |
147 | int z__Map_layer_swap_at_top__raw(void *_map_, z__u32 layer)
148 | {
149 | self;
150 | return z__Map_layer_swap__raw(map, map->layers-1, layer);
151 | }
152 |
153 | int z__Map_layer_copy__raw(void *_map_, z__u64 plotsize, z__u32 dest, z__u32 src)
154 | {
155 | self;
156 | if(src >= map->layers) return 0;
157 | if(dest >= map->layers) return 0;
158 | if(src == dest) return 1;
159 | memcpy(map->plots[dest], map->plots[src], plotsize * map->size.x * map->size.y);
160 | return 1;
161 | }
162 |
163 | int z__Map_layer_push_copy__raw(void *_map_, z__u64 plotsize, z__u32 src)
164 | {
165 | z__u32 dest = z__Map_layer_push__raw(_map_, plotsize);
166 | return z__Map_layer_copy__raw(_map_, plotsize, dest, src);
167 | }
168 |
169 | #undef self
170 | #endif //Z__IMPLEMENTATION
171 |
172 | #endif
173 |
--------------------------------------------------------------------------------
/src/lib/map2d.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__MAP2D_H
2 | #define ZAKAROUF_Z_IMP__MAP2D_H
3 |
4 | #include "std/primitives.h"
5 | #include "vector.h"
6 |
7 | #include "u8arr.h"
8 |
9 | /**
10 | * z__Map2D()
11 | * Tileset Oriented Map Type with layers or z-dimention.
12 | * size: Dimention of Map
13 | * layers: Total no. of z layers used
14 | * layers_capacity: Total no. of z layers allocated
15 | */
16 | #define z__Map2D(Name, T, ...) \
17 | typedef struct Name Name; \
18 | struct Name { \
19 | z__u32Vec2 size; \
20 | z__size plotsize; \
21 | T *plots; \
22 | __VA_ARGS__ \
23 | }
24 |
25 | void z__Map2D_new__raw(void *_map, z__size plotsize, const z__u32 x, z__u32 const y);
26 | void z__Map2D_delete__raw(void *_map);
27 |
28 | #define z__Map2D_new(map, x, y) z__Map2D_new__raw(map, sizeof(*(map)->plots), x, y)
29 | #define z__Map2D_delete(map) { (void)(map)->plots; z__Map2D_delete__raw(map); }
30 |
31 | #ifdef Z__IMPLEMENTATION
32 | #include
33 | #include "std/mem.h"
34 |
35 | z__Map2D(VoidMap2D, void);
36 | #define self VoidMap2D *map = _map_;
37 |
38 | void z__Map2D_new__raw(void *_map_, z__size plotsize, z__u32 const x, z__u32 const y)
39 | {
40 | self;
41 | map->plots = z__MALLOC(plotsize * x * y);
42 | map->plotsize = plotsize;
43 | map->size.x = x;
44 | map->size.y = y;
45 | }
46 |
47 | void z__Map2D_delete__raw(void *_map_)
48 | {
49 | self;
50 | z__FREE(map->plots);
51 | }
52 |
53 | #undef self
54 | #endif //Z__IMPLEMENTATION
55 |
56 | #endif
57 |
58 |
--------------------------------------------------------------------------------
/src/lib/map3d.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z__MAP3D_H
2 | #define ZAKAROUF_Z__MAP3D_H
3 |
4 | #include "std/primitives.h"
5 | #include "vector.h"
6 | #include "utils.h"
7 |
8 | #include "u8arr.h"
9 |
10 | /**
11 | * z__Map3D()
12 | * Tileset Oriented Map Type with layers or z-dimention.
13 | * size: Dimention of Map
14 | * layers: Total no. of z layers used
15 | * layers_capacity: Total no. of z layers allocated
16 | */
17 | #define z__Map3D(Name, T, ...) \
18 | typedef struct Name Name; \
19 | struct Name { \
20 | z__u32Vec3 size; \
21 | z__u32 size_total; \
22 | T *plots; \
23 | __VA_ARGS__; \
24 | }
25 |
26 | #define z__Map3D_get(map, x, y, z) (map).plots[z__xyz3Dto1D(x, y, z, (map).size.x, (map).size.y)]
27 | #define z__Map3D_set(map, x, y, z, value) { z__Map3D_get(map, x, y, z) = value; }
28 |
29 | #define z__Map3D_new(map, _x, _y, _z, ...)\
30 | { \
31 | (map)->size = (z__u32Vec3) { .x = _x, .y = _y, .z = _z }; \
32 | (map)->size_total = _x * _y * _z; \
33 | (map)->plots = z__MALLOC( sizeof(*(map)->plots) * (map)->size_total );\
34 | __VA_ARGS__;\
35 | }
36 |
37 | #define z__Map3D_delete(map, ...)\
38 | { \
39 | __VA_ARGS__; \
40 | z__FREE((map)->plots); \
41 | (map)->size = (z__u32Vec3){0}; \
42 | (map)->size_total = 0; \
43 | } \
44 |
45 | #endif
46 |
47 |
--------------------------------------------------------------------------------
/src/lib/matrix.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__GMATH_MATRIX_H
2 | #define ZAKAROUF__ZTYPES_TYPES__GMATH_MATRIX_H
3 |
4 | #include "std/primitives.h"
5 |
6 | #define z__Matrix(T, x, y, ...) \
7 | union { \
8 | T raw[y][x]; \
9 | T col[y][x]; \
10 | struct { T __VA_ARGS__;}; \
11 | }
12 |
13 | #ifndef Z__CONFIG_VECTOR_CGLM_ALIAS_TYPE_MATRIX
14 |
15 | typedef z__f32 z__mat2[2][2];
16 | typedef z__f32 z__mat3[3][3];
17 | typedef z__f32 z__mat4[4][4];
18 |
19 | typedef z__Matrix(z__f32, 2, 2
20 | , m00, m01
21 | , m10, m11 ) z__Matrix2;
22 |
23 | typedef z__Matrix(z__f32, 3, 3
24 | , m00, m01, m02
25 | , m10, m11, m12
26 | , m20, m21, m22 ) z__Matrix3;
27 |
28 | typedef z__Matrix(z__f32, 4, 4
29 | , m00, m01, m02, m03
30 | , m10, m11, m12, m13
31 | , m20, m21, m22, m23
32 | , m30, m31, m32, m33 ) z__Matrix4;
33 |
34 | #else
35 | #include "cglm.h"
36 |
37 | typedef mat2 z__mat2;
38 | typedef mat3 z__mat3;
39 | typedef mat4 z__mat4;
40 |
41 |
42 | typedef mat2s z__Matrix2;
43 | typedef mat3s z__Matrix3;
44 | typedef mat4s z__Matrix4;
45 | #endif
46 |
47 | // Integer
48 | typedef z__Matrix(z__i32, 2, 2
49 | , m00, m01
50 | , m10, m11 ) z__Mint2;
51 |
52 | typedef z__Matrix(z__i32, 3, 3
53 | , m00, m01, m02
54 | , m10, m11, m12
55 | , m20, m21, m22 ) z__Mint3;
56 |
57 | typedef z__Matrix(z__i32, 4, 4
58 | , m00, m01, m02, m03
59 | , m10, m11, m12, m13
60 | , m20, m21, m22, m23
61 | , m30, m31, m32, m33 ) z__Mint4;
62 |
63 |
64 |
65 | #define z__Matrix2_A(a, b, operator, dest)\
66 | { \
67 | (dest)->raw[0][0] = (a).raw[0][0] operator (b).raw[0][0]; /**/ (dest)->raw[0][1] = (a).raw[0][1] operator (b).raw[0][1]; \
68 | (dest)->raw[1][0] = (a).raw[1][0] operator (b).raw[1][0]; /**/ (dest)->raw[1][1] = (a).raw[1][1] operator (b).raw[1][1]; \
69 | }
70 |
71 | #define z__Matrix3_A(a, b, operator, dest)\
72 | { \
73 | (dest)->raw[0][0] = (a).raw[0][0] operator (b).raw[0][0]; /**/ (dest)->raw[0][1] = (a).raw[0][1] operator (b).raw[0][1]; /**/ (dest)->raw[0][2] = (a).raw[0][2] operator (b).raw[0][2]; \
74 | (dest)->raw[1][0] = (a).raw[1][0] operator (b).raw[1][0]; /**/ (dest)->raw[1][1] = (a).raw[1][1] operator (b).raw1][1]; /**/ (dest)->raw[1][2] = (a).raw[1][2] operator (b).raw[1][2]; \
75 | (dest)->raw[2][0] = (a).raw[2][0] operator (b).raw[2][0]; /**/ (dest)->raw[2][1] = (a).raw[2][1] operator (b).raw[2][1]; /**/ (dest)->raw[2][2] = (a).raw[2][2] operator (b).raw[2][2]; \
76 | }
77 | #define z__Matrix4_A(a, b, operator, dest)\
78 | { \
79 | (dest)->raw[0][0] = (a).raw[0][0] operator (b).raw[0][0]; /**/ (dest)->raw[0][1] = (a).raw[0][1] operator (b).raw[0][1]; /**/ (dest)->raw[0][2] = (a).raw[0][2] operator (b).raw[0][2]; /**/ (dest)->raw[0][3] = (a).raw[0][3] operator (b).raw[0][3]; \
80 | (dest)->raw[1][0] = (a).raw[1][0] operator (b).raw[1][0]; /**/ (dest)->raw[1][1] = (a).raw[1][1] operator (b).raw[1][1]; /**/ (dest)->raw[1][2] = (a).raw[1][2] operator (b).raw[1][2]; /**/ (dest)->raw[1][3] = (a).raw[1][3] operator (b).raw[1][3]; \
81 | (dest)->raw[2][0] = (a).raw[2][0] operator (b).raw[2][0]; /**/ (dest)->raw[2][1] = (a).raw[2][1] operator (b).raw[2][1]; /**/ (dest)->raw[2][2] = (a).raw[2][2] operator (b).raw[2][2]; /**/ (dest)->raw[2][3] = (a).raw[2][3] operator (b).raw[2][3]; \
82 | (dest)->raw[3][0] = (a).raw[3][0] operator (b).raw[3][0]; /**/ (dest)->raw[3][1] = (a).raw[3][1] operator (b).raw[3][1]; /**/ (dest)->raw[3][2] = (a).raw[3][2] operator (b).raw[3][2]; /**/ (dest)->raw[3][3] = (a).raw[3][3] operator (b).raw[3][3]; \
83 | }
84 |
85 | #define z__Matrix2_det(A)\
86 | ( ((A).raw[0][0] * (A).raw[1][1]) - ((A).raw[0][1] * (A).raw[1][0]) )
87 |
88 | #endif
89 |
--------------------------------------------------------------------------------
/src/lib/obj.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__OBJ_H
2 | #define ZAKAROUF__ZTYPES_TYPES__OBJ_H
3 |
4 | #include "prep/args.h"
5 | #include "record.h"
6 | #include "fnptr.h"
7 |
8 | #define z__objfn_clself(T) T* self = (_self_)
9 |
10 | /* Object Function Namespace */
11 | #define z__objfn(T, name)\
12 | zpp__CAT(zpp__CAT(T, _), name)
13 |
14 | #define z__objfn_cl(_A, name)\
15 | z__objfn(zpp__PRIV__Args_get_1 _A, zpp__CAT(name, zpp__CAT(__FOR_, zpp__PRIV__Args_get_2 _A)))
16 |
17 | /* Definations */
18 | #define z__objfn_def(T, name, ret, ...)\
19 | ret z__objfn(T, name) (T* _self_,##__VA_ARGS__)
20 |
21 | #define z__objfn_defcl(T, for, name, ret, ...)\
22 | ret z__objfn_cl((T, for), name)(void *_self_, ##__VA_ARGS__)
23 |
24 | /* Vtables */
25 | #define z__objvt_type(T, alias) zpp__CAT4(T, _, alias, __vt_)
26 |
27 | #define z__PRIV__objvt_sin(obj_T, fn_name, ret, ...) z__fnptr(fn_name, ret, obj_T *_self_,##__VA_ARGS__);
28 | #define z__PRIV__objvt_sinunpack(x, y) z__PRIV__objvt_sin(x, y)
29 | #define z__PRIV__objvt_sinfxy(x, y) z__PRIV__objvt_sinunpack(x, zpp__EXPAND y)
30 |
31 | #define z__objvt(T, alias, ...) /* x E __VA_ARGS__ : where x => (fn_name, ret, ...) */\
32 | typedef struct z__objvt_type(T, alias) {\
33 | zpp__Args_applyfxy(z__PRIV__objvt_sinfxy, T, __VA_ARGS__);\
34 | } z__objvt_type(T, alias)
35 |
36 | /* Hooking Vtables */
37 | #define z__PRIV__objvt_bind_sin(vt, T, v_name, fn_name) (vt)->v_name = &z__objfn(T, fn_name)
38 | #define z__PRIV__objvt_bind_sinunpack(x, y) z__PRIV__objvt_bind_sin(x, y)
39 | #define z__PRIV__objvt_bind_sinfxy(x, y) z__PRIV__objvt_bind_sinunpack(zpp__EXPAND x, zpp__EXPAND y);
40 |
41 | #define z__objvt_bind(vt, T, ...)\
42 | {\
43 | zpp__Args_applyfxy(z__PRIV__objvt_bind_sinfxy, (vt, T), __VA_ARGS__);\
44 | }
45 |
46 | #define z__PRIV__objvt_impl_proto(T, alias) zpp__CAT3(T, _vt_impl_for_, alias)
47 | #define z__objvt_impl(T, alias, ...)\
48 | void z__PRIV__objvt_impl_proto(T, alias) (z__objvt_type(T, alias) *_self_)\
49 | z__objvt_bind(_self_, T, __VA_ARGS__)
50 |
51 | #define z__objvt_get(T, alias)\
52 | z__PRIV__objvt_impl_proto(T, alias)
53 |
54 | /**
55 | * Closure Implementation
56 | */
57 | #define z__objcl(T, cnt, ...)\
58 | typedef struct T { \
59 | void *_obj_; \
60 | zpp__Args_applyfxy(z__PRIV__objvt_sinfxy, void, __VA_ARGS__); \
61 | zpp__EXPAND cnt; \
62 | } T
63 |
64 | #define z__PRIV__objcl_bind_sin(v, obj_T, cl_T, method) (v)->method = z__objfn_cl((obj_T, cl_T), method);
65 | #define z__PRIV__objcl_bind_sinunpack(va, x) z__PRIV__objcl_bind_sin(va, x)
66 | #define z__PRIV__objcl_bind_sinfn(va, x) z__PRIV__objcl_bind_sinunpack(zpp__EXPAND va, x)
67 |
68 | #define z__objcl_bind_raw(cl, cl_T, obj_T, obj, ...)\
69 | {\
70 | (cl)->_obj_ = obj;\
71 | zpp__Args_applyfxy(z__PRIV__objcl_bind_sinfn, (cl, obj_T, cl_T), __VA_ARGS__);\
72 | }
73 |
74 | #define z__objcl_proto(cl_T, obj_T)\
75 | zpp__CAT(cl_T, zpp__CAT(__FOR__, obj_T))
76 |
77 | #define z__objcl_impl(cl_T, obj_T, ...)\
78 | void z__objcl_proto(cl_T, obj_T) (cl_T *cl, obj_T *obj)\
79 | z__objcl_bind_raw(cl, cl_T, obj_T, obj, __VA_ARGS__)\
80 |
81 | #define z__objcl_implext(cl_T, obj_T)\
82 | extern void z__objcl_proto(cl_T, obj_T) (cl_T *cl, obj_T *obj)
83 |
84 | #define z__objcl_set(cl, cl_T, obj, obj_T)\
85 | z__objcl_proto(cl_T, obj_T)(cl, obj)
86 |
87 | #define z__objcl_call(v, method, ...)\
88 | (v)->method((v)->_obj_,##__VA_ARGS__)
89 |
90 | #endif
91 |
92 |
--------------------------------------------------------------------------------
/src/lib/offsetof.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_OFFSETOF_H
2 | #define ZAKAROUF__ZTYPES_OFFSETOF_H
3 |
4 | #if defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 5 || __GNUC__ > 3)
5 | #define z__offsetof(T, field) __builtin_offsetof(T, field)
6 | #else /* !(gcc >= 3.5) */
7 | #define z__offsetof(T, field) ((size_t)(&((T *)0)->field))
8 | #endif /* (gcc >= 3.5) */
9 |
10 | #endif
11 |
12 |
--------------------------------------------------------------------------------
/src/lib/omp.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__Z__PROC_OMP_H
2 | #define ZAKAROUF__Z__PROC_OMP_H
3 |
4 | #include "prep/nm/pragma.h"
5 | #include
6 |
7 | #define z__omp(...) zpp__pragma(omp __VA_ARGS__)
8 |
9 | #define z__omp_parallel(...) z__omp(parallel __VA_ARGS__)
10 | #define z__omp_for(...) z__omp(for __VA_ARGS__)
11 |
12 |
13 |
14 | #endif
15 |
--------------------------------------------------------------------------------
/src/lib/option.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__OPTION_H
2 | #define ZAKAROUF__ZTYPES_TYPES__OPTION_H
3 |
4 | #include "enum.h"
5 |
6 | typedef z__Enum__PRIV__mapArgGlobal_to_C_Enum((Some), (None)) Option__Tags;
7 | #define z__Option(Name, ...)\
8 | z__Enum__PRIV__onlytype(\
9 | Option__##Name,\
10 | Option__Tags,\
11 | (Some, __VA_ARGS__),\
12 | (None)\
13 | )
14 |
15 | #define z__OptionType(Name) Option__##Name
16 | #define z__Some(Name, ...) z__Enum_grave(Option__##Name, Some, __VA_ARGS__)
17 | #define z__None(Name) z__Enum_grave(Option__##Name, None)
18 |
19 | #define z__SomeChip(Op, ...) z__Enum_chip(Op, Some, __VA_ARGS__)
20 | #define z__NoneChip(Op, ...) z__Enum_chip(Op, None, __VA_ARGS__)
21 |
22 | #define z__Option_unwrap_or(op, or)\
23 | ({\
24 | z__typeof(res) _z__unwrap_or__var__R = op;\
25 | (z__Enum_matches(_z__unwrap_or__var__R, Some)? _z__unwrap_or__var__R.data.Some._0 : or);\
26 | })
27 |
28 | #endif
29 |
30 |
--------------------------------------------------------------------------------
/src/lib/pairarr.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__PAIRARR_H
2 | #define ZAKAROUF__ZTYPES_TYPES__PAIRARR_H
3 |
4 | #include "std/primitives.h"
5 | #include "std/mem.h"
6 |
7 | #define z__PairArr(Ta, Tb)\
8 | struct {\
9 | Ta *data_a;\
10 | Tb *data_b;\
11 | z__u32 len, lenUsed;\
12 | }
13 |
14 |
15 | #define z__PairArr_new(pa, _len)\
16 | {\
17 | (pa)->len = _len;\
18 | (pa)->data_a = z__MALLOC(sizeof(*(pa)->data_a) * (pa)->len);\
19 | (pa)->data_b = z__MALLOC(sizeof(*(pa)->data_b) * (pa)->len);\
20 | (pa)->lenUsed = 0;\
21 | }
22 |
23 | #define z__PairArr_delete(pa)\
24 | {\
25 | z__FREE((pa)->data_a);\
26 | (pa)->lenUsed = 0;\
27 | (pa)->len = 0;\
28 | }
29 |
30 | #define z__PairArr_expand(pa, by)\
31 | {\
32 | (pa)->len += by;\
33 | (pa)->data_a = z__REALLOC((pa)->data_a, sizeof(*(pa)->data_a) * (pa)->len);\
34 | (pa)->data_b = z__REALLOC((pa)->data_b, sizeof(*(pa)->data_b) * (pa)->len);\
35 | }
36 |
37 | #define z__PairArr_expand_iffull(pa)\
38 | {\
39 | if((pa)->lenUsed >= (pa)->len) { z__PairArr_expand(pa, (pa)->len); }\
40 | }
41 |
42 | #define z__PairArr_push_nocheck(pa, Va, Vb) \
43 | { \
44 | (pa)->data_a[(pa)->lenUsed] = Va; \
45 | (pa)->data_b[(pa)->lenUsed] = Vb; \
46 | (pa)->lenUsed += 1; \
47 | }
48 |
49 | #define z__PairArr_push(pa, Va, Vb) \
50 | { \
51 | z__PairArr_expand_iffull(pa); \
52 | z__PairArr_push_nocheck(pa, Va, Vb); \
53 | }
54 |
55 |
56 | #endif
57 |
58 |
--------------------------------------------------------------------------------
/src/lib/prep/base.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZPREP_BASE_H
2 | #define ZAKAROUF__ZPREP_BASE_H
3 |
4 | #define zpp__PRIMITIVE_CAT(a, b) a##b
5 | #define zpp__PRIMITIVE_CAT3(a, b, c) a##b##c
6 | #define zpp__PRIMITIVE_CAT4(a, b, c, d) a##b##c##d
7 | #define zpp__PRIMITIVE_CAT5(a, b, c, d, e) a##b##c##d##e
8 |
9 | #define zpp__CAT(_1, _2) zpp__PRIMITIVE_CAT(_1, _2)
10 | #define zpp__CAT3(_1, _2, _3) \
11 | zpp__CAT(zpp__CAT(_1, _2), _3)
12 | #define zpp__CAT4(_1, _2, _3, _4) \
13 | zpp__CAT(zpp__CAT3(_1, _2, _3), _4)
14 | #define zpp__CAT5(_1, _2, _3, _4, _5) \
15 | zpp__CAT(zpp__CAT4(_1, _2, _3, _4), _5)
16 | #define zpp__CAT6(_1, _2, _3, _4, _5, _6) \
17 | zpp__CAT(zpp__CAT5(_1, _2, _3, _4, _5), _6)
18 |
19 | #define zpp__PRIV_TO_STRING(...) #__VA_ARGS__
20 |
21 | #define zpp__TO_STRING(...) zpp__PRIV_TO_STRING(__VA_ARGS__)
22 |
23 | #define zpp__PRIV_COMMA(...) ,
24 | #define zpp__PRIV__SEMI_COLON(...) ;
25 | #define zpp__IGNORE(...)
26 | #define zpp__EXPAND(...) __VA_ARGS__
27 | #define zpp__EMPTY
28 | #define zpp__ARG_FLUSH(v) v
29 |
30 | #define zpp__if_then(cond, x, y) zpp__CAT(zpp__if_then_, cond)(x, y)
31 | #define zpp__if_then_0(x, y) y
32 | #define zpp__if_then_1(x, y) x
33 |
34 | #define zpp__COMPL(exp) zpp__CAT(zpp__COMPL_, exp)
35 | #define zpp__COMPL_0 1
36 | #define zpp__COMPL_1 0
37 |
38 | #define zpp__AND(exp) zpp__CAT(zpp__AND_, exp)
39 | #define zpp__AND_0(exp) 0
40 | #define zpp__AND_1(exp) exp
41 |
42 | #define z__BOOL(x) zpp__COMPL(zpp__NOT(x))
43 |
44 | #define zpp__DETECT_check_n(x, n, ...) n
45 | #define zpp__DETECT_check(...) zpp__DETECT_check_n(__VA_ARGS__, 0,)
46 | #define zpp__DETECT_probe(x) x, 1,
47 |
48 |
49 | #define zpp__NOT(exp) zpp__DETECT_check(zpp__PRIMITIVE_CAT(zpp_NOT_, exp))
50 | #define zpp__NOT_0 zpp__DETECT_probe(~)
51 |
52 | #define zpp__DETECT_paren_probe(...) zpp__DETECT_probe(~)
53 | #define zpp__DETECT_paren(x) zpp__DETECT_check(zpp__DETECT_paren x)
54 |
55 | #define zpp__IS_PAREN_PROBE(...) zpp__DETECT_probe(~)
56 | #define zpp__IS_PAREN(x) zpp__DETECT_check(zpp__IS_PAREN_PROBE x)
57 |
58 | #define zpp__UNPAREN(x) zpp__EXPAND x
59 | #define zpp__PAREN(...) (__VA_ARGS__)
60 |
61 | #define zpp__APPENDL(a, b) (zpp__EXPAND a, zpp__EXPAND b)
62 | #define zpp__APPENDLUP(a) zpp__APPENDL a
63 |
64 | #define zpp__From(...) ({ __VA_ARGS__ })
65 |
66 |
67 | #endif
68 |
69 |
--------------------------------------------------------------------------------
/src/lib/prep/call.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZPREP_CALL_H
2 | #define ZAKAROUF__ZPREP_CALL_H
3 |
4 | #include "args.h"
5 |
6 | #define zpp__call_raw(x, ...) x(__VA_ARGS__)
7 | #define zpp__call_rawalt(f, x) f x
8 | #define zpp__call_name(x1, x, x2) zpp__CAT3(x1 ,x, x2)
9 |
10 | #ifndef ZPP__CONFIG__NO_DOLLAR_SIGN
11 | #define $raw(...) zpp__call_raw(__VA_ARGS__)
12 | #define $rawt(f, x) zpp__call_rawalt(f, x)
13 | #define $name(x1, x, x2) zpp__call_name(x1, x, x2)
14 | #endif
15 |
16 | #endif
17 |
18 |
--------------------------------------------------------------------------------
/src/lib/prep/comp/eval.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZPREP_ASSERT_COMPILER_H
2 | #define ZAKAROUF__ZPREP_ASSERT_COMPILER_H
3 |
4 | #define zpp__choose_expr __builtin_choose_expr
5 |
6 |
7 | #endif
8 |
9 |
--------------------------------------------------------------------------------
/src/lib/prep/eval.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZPREP_EVAL_H
2 | #define ZAKAROUF__ZPREP_EVAL_H
3 |
4 | #define zpp__PRIV__EVAL0(...) __VA_ARGS__
5 | #define zpp__PRIV__EVAL1(...) zpp__PRIV__EVAL0(zpp__PRIV__EVAL0(zpp__PRIV__EVAL0(__VA_ARGS__)))
6 | #define zpp__PRIV__EVAL2(...) zpp__PRIV__EVAL1(zpp__PRIV__EVAL1(zpp__PRIV__EVAL1(__VA_ARGS__)))
7 | #define zpp__PRIV__EVAL3(...) zpp__PRIV__EVAL2(zpp__PRIV__EVAL2(zpp__PRIV__EVAL2(__VA_ARGS__)))
8 | #define zpp__PRIV__EVAL4(...) zpp__PRIV__EVAL3(zpp__PRIV__EVAL3(zpp__PRIV__EVAL3(__VA_ARGS__)))
9 | #define zpp__PRIV__EVAL(...) zpp__PRIV__EVAL4(zpp__PRIV__EVAL4(zpp__PRIV__EVAL4(__VA_ARGS__)))
10 |
11 | #define zpp__eval(...) zpp__PRIV__EVAL(__VA_ARGS__)
12 |
13 |
14 | #endif
15 |
--------------------------------------------------------------------------------
/src/lib/prep/gen/args/var.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZPREP_GEN_ARGS_VAR_H
2 | #define ZAKAROUF__ZPREP_GEN_ARGS_VAR_H
3 |
4 | #define zpp__Args_MAX_ARGS 158
5 |
6 | #define zpp__PRIV__VARIADICS_COUNT_AUX( \
7 | _1, _2, _3, _4, _5, _6, _7, _8, \
8 | _9, _10, _11, _12, _13, _14, _15, _16, \
9 | _17, _18, _19, _20, _21, _22, _23, _24, \
10 | _25, _26, _27, _28, _29, _30, _31, _32, \
11 | _33, _34, _35, _36, _37, _38, _39, _40, \
12 | _41, _42, _43, _44, _45, _46, _47, _48, \
13 | _49, _50, _51, _52, _53, _54, _55, _56, \
14 | _57, _58, _59, _60, _61, _62, _63, _64, \
15 | _65, _66, _67, _68, _69, _70, _71, _72, \
16 | _73, _74, _75, _76, _77, _78, _79, _80, \
17 | _81, _82, _83, _84, _85, _86, _87, _88, \
18 | _89, _90, _91, _92, _93, _94, _95, _96, \
19 | _97, _98, _99, _100, _101, _102, _103, _104, \
20 | _105, _106, _107, _108, _109, _110, _111, _112, \
21 | _113, _114, _115, _116, _117, _118, _119, _120, \
22 | _121, _122, _123, _124, _125, _126, _127, _128, \
23 | _129, _130, _131, _132, _133, _134, _135, _136, \
24 | _137, _138, _139, _140, _141, _142, _143, _144, \
25 | _145, _146, _147, _148, _149, _150, _151, _152, \
26 | _153, _154, _155, _156, _157, _158, _159, \
27 | ...) _159
28 |
29 | #define zpp__PRIV__VARIADICS_COUNT(...) \
30 | zpp__PRIV__VARIADICS_COUNT_AUX( \
31 | __VA_ARGS__, 158, 157, 156, 155, 154, 153, 152, 151, 150, 149, 148 \
32 | , 147, 146, 145, 144, 143, 142, 141, 140, 139, 138, 137, 136, 135, 134, 133 \
33 | , 132, 131, 130, 129, 128, 127, 126, 125, 124, 123, 122, 121, 120, 119, 118 \
34 | , 117, 116, 115, 114, 113, 112, 111, 110, 109, 108, 107, 106, 105, 104, 103 \
35 | , 102, 101, 100, 99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85 \
36 | , 84, 83, 82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66\
37 | , 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49 \
38 | , 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31 \
39 | , 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13 \
40 | , 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, ~)
41 |
42 |
43 | #endif
44 |
--------------------------------------------------------------------------------
/src/lib/prep/nm/assert.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZPREP_NM_ASSERT_H
2 | #define ZAKAROUF__ZPREP_NM_ASSERT_H
3 |
4 | #include "../base.h"
5 |
6 | #define zpp__assert_exp(exp)\
7 | ((void) ((exp) ? 1 : 0)
8 |
9 | #define zpp__assert_construct(exp, exception_method_fn, ...)\
10 | ( (void)( (exp) ? ((void)0) : exception_method_fn(__VA_ARGS__) ))
11 |
12 | #define zpp__assert_not_construct(exp, exception_method_fn, ...)\
13 | ( (void)( (exp) ? exception_method_fn(__VA_ARGS__) : ((void)0) ))
14 |
15 | #define zpp__assert_eq_ptr(x, y) ( x==y )
16 | #define zpp__assert_nullptr(x) (x==NULL)
17 |
18 | // Panic
19 |
20 | #define zpp__panicif_construct(condition, panic_call, ...) zpp__assert_not_construct(condition, panic_call, __VA_ARGS__)
21 |
22 | #define zpp__static_assert_primitive(exp, msg)\
23 | typedef struct {\
24 | char static_assert__##msg[(exp)?1:-1];\
25 | } zpp__CAT(line__, __LINE__)
26 | #define zpp__static_assert(exp) _Static_assert(exp, #exp)
27 | #define zpp__static_assert_msg(exp, msg) _Static_assert(exp, #exp ":" msg)
28 |
29 | #endif
30 |
31 |
--------------------------------------------------------------------------------
/src/lib/prep/nm/cond.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZPREP_NM_CONDITIONAL_H
2 | #define ZAKAROUF__ZPREP_NM_CONDITIONAL_H
3 |
4 | #include "../call.h"
5 | #include "ident.h"
6 | // Condtionals
7 |
8 | #define zpp__do_construct(method1, method2) \
9 | for(int zpp__macIdent(keep) = 1; \
10 | zpp__macIdent(keep) && ({ zpp__call_raw method1; 1;});\
11 | zpp__macIdent(keep) = 0, zpp__call_raw method2)
12 |
13 | #define zpp__doif_construct_onlypre(condition, method, ...)\
14 | if(condition)\
15 | for(int keep = 1 ; keep?({ method(__VA_ARGS__); 1; }):0 ; keep ^= 1)
16 |
17 | #define zpp__doif_construct_onlypost(condition, method, ...)\
18 | if(condition)\
19 | for(int keep = 1; keep; keep ^= 1, method(__VA_ARGS__) )
20 |
21 | #define zpp__doif_construct(condition, methodandArgs1, methodandArgs2)\
22 | if(condition)\
23 | zpp__do_construct(methodandArgs1, methodandArgs2)
24 |
25 | #define zpp__ter__if(exp) (exp)?
26 | #define zpp__ter__elif(exp) :(exp)?
27 | #define zpp__ter__else :
28 |
29 | #define zpp__ter__op_if(exp) zpp__ter__if(exp) (
30 | #define zpp__ter__op_elif(exp) ) zpp__ter__elif(exp) (
31 | #define zpp__ter__op_else ) zpp__ter__else (
32 | #define zpp__ter__op_endif )
33 |
34 |
35 |
36 | #endif
37 |
--------------------------------------------------------------------------------
/src/lib/prep/nm/ident.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZPREP_NM_IDENT_H
2 | #define ZAKAROUF__ZPREP_NM_IDENT_H
3 |
4 | #include "../base.h"
5 |
6 | #define zpp__macIdent(name) zpp__CAT3(_priv_, __LINE__, name)
7 |
8 | #endif
9 |
10 |
--------------------------------------------------------------------------------
/src/lib/prep/nm/pragma.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZPREP_NM_PRAGMA_H
2 | #define ZAKAROUF__ZPREP_NM_PRAGMA_H
3 |
4 | #define zpp__PRIV_PRAGMA(str_literal) _Pragma(str_literal)
5 | #define zpp__pragma(...) zpp__PRIV_PRAGMA(#__VA_ARGS__)
6 |
7 | #endif
8 |
--------------------------------------------------------------------------------
/src/lib/prep/nm/string.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZPREP_NM_STRING_H
2 | #define ZAKAROUF__ZPREP_NM_STRING_H
3 |
4 | #include "../base.h"
5 | #define zpp__to_string(...) zpp__PRIV_TO_STRING(__VA_ARGS__)
6 |
7 | #endif
8 |
--------------------------------------------------------------------------------
/src/lib/print.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__PRINT_H
2 | #define ZAKAROUF_Z_IMP__PRINT_H
3 |
4 | #include
5 | #include "ansifmt.h"
6 | #include "string.h"
7 | #include "prep/nm/string.h"
8 |
9 | /**
10 | * Basic Print Function (Basically fprintf)
11 | * _vb: with __FILE__ __LINE__ __func__ detail
12 | * _cl256: Custom Foreground Color
13 | * _cl256_full: Custom Color
14 | * _clrgb: 24bit Color, Foreground only
15 | * _clrgb_full: 24bit Color
16 | */
17 | int z__fprint(FILE *fp, const char * __restrict fmt, ...)
18 | __attribute__((format (printf, 2, 3)));
19 |
20 | int z__print(const char * __restrict fmt, ...)
21 | __attribute__((format (printf, 1, 2)));
22 |
23 | int z__print_String(z__String const str);
24 |
25 | int z__fprint_str(FILE *fp, char const *str, z__size size);
26 | int z__print_str(char const *str, z__size size);
27 | int z__print_char(z__u32 ch);
28 |
29 | /* impl */
30 | #define z__fprint_cl256(fp, bg, fg, fmt, ...)\
31 | z__fprint(fp, z__ansi_fmt((cl256, %d, %d)) fmt z__ansi_fmt((plain)), bg, fg, ##__VA_ARGS__)
32 |
33 | #define z__fprint_cl256f(fp, fg, fmt, ...)\
34 | z__fprint(fp, z__ansi_fmt((cl256_fg, %d)) fmt z__ansi_fmt((plain)), fg, ##__VA_ARGS__)
35 |
36 | #define z__fprint_clrgb(fp, b_r, b_g, b_b, f_r, f_g, f_b, fmt, ...)\
37 | z__fprint(fp,\
38 | z__ansi_fmt((clrgb, b_r, b_g, b_b, f_r, f_g, f_b))\
39 | fmt\
40 | z__ansi_fmt((plain)), ##__VA_ARGS__ )
41 |
42 | #define z__fprint_clrgbf(fp, f_r, f_g, f_b, fmt, ...)\
43 | z__fprint(fp,\
44 | z__ansi_fmt((clrgb_fg, f_r, f_g, f_b))\
45 | fmt\
46 | z__ansi_fmt((plain)), ##__VA_ARGS__ )
47 |
48 |
49 | #define z__fprint_vb(fp, fmt, ...)\
50 | z__fprint(fp\
51 | , __FILE__ ":" zpp__to_string(__LINE__) " @ %s(): " fmt\
52 | , __func__, ##__VA_ARGS__ )
53 |
54 | #define z__fprint_vb_cl256(fp, bg, fg, fmt, ...)\
55 | z__fprint_cl256(fp, bg, fg\
56 | , __FILE__ ":" zpp__to_string(__LINE__) " @ %s(): " fmt\
57 | , __func__, ##__VA_ARGS__ )
58 |
59 | #define z__fprint_vb_cl256f(fp, fg, fmt, ...)\
60 | z__fprint_cl256f(fp, fg\
61 | , __FILE__ ":" zpp__to_string(__LINE__) " @ %s(): " fmt\
62 | , __func__, ##__VA_ARGS__ )
63 |
64 |
65 | #define z__fprint_vb_clrgb(fp, b_r, b_g, b_b, f_r, f_g, f_b, fmt, ...)\
66 | z__fprint_clrgb(fp, b_r, b_g, b_b, f_r, f_g, f_b\
67 | , __FILE__ ":" zpp__to_string(__LINE__) " @ %s(): " fmt\
68 | , __func__, ##__VA_ARGS__ )
69 |
70 | #define z__fprint_vb_clrgbf(fp, f_r, f_g, f_b, fmt, ...)\
71 | z__fprint_clrgbf(fp, f_r, f_g, f_b\
72 | , __FILE__ ":" zpp__to_string(__LINE__) " @ %s(): " fmt\
73 | , __func__, ##__VA_ARGS__ )
74 |
75 |
76 | #ifdef Z__IMPLEMENTATION
77 | #include
78 | #include
79 | #include
80 |
81 | int z__fprint(FILE *fp, const char *restrict fmt, ...)
82 | {
83 | va_list arg;
84 | va_start(arg, fmt);
85 |
86 | int val = vfprintf(fp, fmt, arg);
87 |
88 | va_end(arg);
89 | return val;
90 | }
91 |
92 | int z__print(const char * restrict fmt, ...)
93 | {
94 | va_list arg;
95 | va_start(arg, fmt);
96 |
97 | int val = vprintf(fmt, arg);
98 |
99 | va_end(arg);
100 | return val;
101 | }
102 |
103 | int z__print_String(z__String const str)
104 | {
105 | return fwrite(str.data, str.lenUsed, 1, stdout);
106 | }
107 |
108 | int z__print_str(char const *str, z__size size)
109 | {
110 | return fwrite(str, 1, size, stdout);
111 | }
112 |
113 | int z__fprint_str(FILE *fp, char const *str, z__size size)
114 | {
115 | return fwrite(str, 1, size, fp);
116 | }
117 |
118 | int z__print_char(z__u32 ch)
119 | {
120 | return fputc(ch, stdout);
121 | }
122 | #endif //Z__IMPLEMENTATION
123 |
124 | #endif // ZAKAROUF_Z_IMP__PRINT_H
125 |
126 |
--------------------------------------------------------------------------------
/src/lib/random.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z__RANDOM_H
2 | #define ZAKAROUF_Z__RANDOM_H
3 |
4 | #include "std/primitives.h"
5 |
6 | z__u64 z__rnd_spm64(z__u64 counter, z__u64 seed);
7 | z__u64 z__rnd_hash64_splitmix(z__u64 x);
8 | z__u64 z__rnd_hash64_wang(z__u64 x);
9 | z__u64 z__rnd_hash64_murmur(z__u64 x);
10 |
11 | #ifdef Z__IMPLEMENTATION
12 |
13 | z__u64 z__rnd_hash64_splitmix(z__u64 x)
14 | {
15 | x ^= x >> 30;
16 | x *= 0xbf58476d1ce4e5b9ULL;
17 | x ^= x >> 27;
18 | x *= 0x94d049bb133111ebULL;
19 | x ^= x >> 31;
20 | return x;
21 | }
22 |
23 | z__u64 z__rnd_hash64_wang(z__u64 x)
24 | {
25 | x = (~x) + (x << 21);
26 | x = x ^ (x >> 24);
27 | x = (x + (x << 3)) + (x << 8);
28 | x = x ^ (x >> 14);
29 | x = (x + (x << 2)) + (x << 4);
30 | x = x ^ (x >> 28);
31 | x = x + (x << 31);
32 | return x;
33 | }
34 |
35 | z__u64 z__rnd_hash64_murmur(z__u64 x)
36 | {
37 | x ^= x >> 33;
38 | x *= 0xff51afd7ed558ccdULL;
39 | x ^= x >> 33;
40 | x *= 0xc4ceb9fe1a85ec53ULL;
41 | x ^= x >> 33;
42 | return x;
43 | }
44 |
45 | z__u64 z__rnd_spm64(z__u64 x, z__u64 seed)
46 | {
47 | x ^= seed;
48 | x ^= x >> 30;
49 | x *= 0xbf58476d1ce4e5b9ULL;
50 | x ^= x >> 27;
51 | x *= 0x94d049bb133111ebULL;
52 | x ^= x >> 31;
53 | return x;
54 | }
55 |
56 | #endif
57 |
58 | #endif
59 |
60 |
--------------------------------------------------------------------------------
/src/lib/result.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__RESULT_H
2 | #define ZAKAROUF__ZTYPES_TYPES__RESULT_H
3 |
4 | #include "enum.h"
5 |
6 | typedef z__Enum__PRIV__mapArgGlobal_to_C_Enum((Ok), (Err)) Result__Tags;
7 | #define z__Result(Name, T, E)\
8 | z__Enum__PRIV__onlytype(\
9 | Result__##Name,\
10 | Result__Tags,\
11 | (Ok, T),\
12 | (Err, E)\
13 | )
14 |
15 |
16 |
17 | #define z__ResultType(Name) Result__##Name
18 | #define z__Ok(Name, ...) z__Enum_grave(Result__##Name, Ok, __VA_ARGS__)
19 | #define z__Err(Name, ...) z__Enum_grave(Result__##Name, Err, __VA_ARGS__)
20 |
21 | #define z__OkChip(Res, ...) z__Enum_chip(Res, Ok, __VA_ARGS__)
22 | #define z__ErrChip(Res, ...) z__Enum_chip(Res, Err, __VA_ARGS__)
23 |
24 | #define z__Result_unwrap_or(res, or)\
25 | ({\
26 | z__typeof(res) _z__unwrap_or__var__R = res;\
27 | (z__Enum_matches(_z__unwrap_or__var__R, Ok)? _z__unwrap_or__var__R.data.Ok._0 : or);\
28 | })
29 |
30 |
31 | #endif
32 |
33 |
--------------------------------------------------------------------------------
/src/lib/return.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__RETURN_H
2 | #define ZAKAROUF_Z_IMP__RETURN_H
3 |
4 | #include
5 | #include "prep/base.h"
6 |
7 | #define z__return_def(name, retval, ...)\
8 | goto zpp__CAT(_L_returnsk_, name); \
9 | zpp__CAT(_L_return_, name): { \
10 | __VA_ARGS__; \
11 | return retval; \
12 | } zpp__CAT2(_L_returnsk_, name):;
13 |
14 | #define z__return_with(name) { goto zpp__CAT(_L_return_, name); }
15 |
16 | #endif
17 |
18 |
--------------------------------------------------------------------------------
/src/lib/soarr.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__Z__SOARR_H
2 | #define ZAKAROUF__Z__SOARR_H
3 |
4 | #include "std/mem.h"
5 | #include "prep/args.h"
6 |
7 | #define z__SoArr(N, ...)\
8 | z__SoArrDef(N, __VA_ARGS__);\
9 | z__SoArrImpl(N, __VA_ARGS__)
10 |
11 | #define z__SoArrDef(N, ...)\
12 | struct N { \
13 | union { \
14 | void *data[zpp__Args_Count(__VA_ARGS__)]; \
15 | struct { \
16 | zpp__Args_map(\
17 | z__PRIV__SoArr_STRUCT_ELEMENT_UNWRAP, __VA_ARGS__)\
18 | } item; \
19 | }; \
20 | z__u32 len, lenUsed; \
21 | }
22 |
23 | #define z__SoArrExt(N, ...)\
24 | void N##_new(struct N* self, z__u32 len); \
25 | void N##_delete(struct N *self); \
26 | void N##_expand(struct N *self, z__u32 by); \
27 | void N##_push(struct N *self, zpp__Args_maplist( \
28 | z__PRIV__SoArr_PUSH_ARG_UNWRAP, __VA_ARGS__)); \
29 |
30 | #define z__SoArrImpl(N, ...)\
31 | void N##_new(struct N* self, z__u32 len) { \
32 | z__PRIV__SoArr_STRUCT_FN_CALC_ELEM(__VA_ARGS__); \
33 | self->data[0] = z__MALLOC(total_size); \
34 | z__PRIV__SoArr_STRUCT_ELEM_UPDATE() \
35 | self->len = len; \
36 | self->lenUsed = 0; \
37 | } \
38 | \
39 | void N##_delete(struct N *self){ \
40 | z__FREE(self->data[0]); \
41 | } \
42 | \
43 | void N##_expand(struct N *self, z__u32 by){ \
44 | z__u32 const len = by + self->len; \
45 | z__PRIV__SoArr_STRUCT_FN_CALC_ELEM(__VA_ARGS__); \
46 | self->data[0] = z__REALLOC(self->data[0], total_size); \
47 | self->len = len; \
48 | } \
49 | void N##_push(struct N *self, zpp__Args_maplist(z__PRIV__SoArr_PUSH_ARG_UNWRAP, __VA_ARGS__)) {\
50 | if(self->lenUsed >= self->len) { N##_expand(self, self->len); }\
51 | zpp__Args_map(z__PRIV__SoArr_PUSH_ASSIGN_UNWRAP, __VA_ARGS__)\
52 | self->lenUsed += 1;\
53 | }
54 |
55 |
56 | /** -------- PRIV --------- **/
57 |
58 | #define z__PRIV__SoArr_STRUCT_ELEMENT(T, N) T* N;
59 | #define z__PRIV__SoArr_STRUCT_ELEMENT_UNWRAP(x)\
60 | z__PRIV__SoArr_STRUCT_ELEMENT x
61 |
62 | #define z__PRIV__SoArr_STRUCT_SIZE(T, N) (sizeof(*(self)->item.N) * len )
63 | #define z__PRIV__SoArr_STRUCT_SIZE_UNWRAP(x)\
64 | z__PRIV__SoArr_STRUCT_SIZE x
65 |
66 | #define z__PRIV__SoArr_PUSH_ARG(T, N) T N
67 | #define z__PRIV__SoArr_PUSH_ARG_UNWRAP(x)\
68 | z__PRIV__SoArr_PUSH_ARG x
69 |
70 | #define z__PRIV__SoArr_PUSH_ASSIGN(T, N) self->item.N[self->lenUsed] = N;
71 | #define z__PRIV__SoArr_PUSH_ASSIGN_UNWRAP(x)\
72 | z__PRIV__SoArr_PUSH_ASSIGN x
73 |
74 |
75 | #define z__PRIV__SoArr_STRUCT_FN_CALC_ELEM(...)\
76 | z__u32 const elements = sizeof(self->data)/sizeof(self->data[0]); \
77 | z__u32 const element_sizes[sizeof(self->data)/sizeof(self->data[0])]\
78 | = { zpp__Args_maplist(z__PRIV__SoArr_STRUCT_SIZE_UNWRAP \
79 | , __VA_ARGS__) \
80 | }; \
81 | z__size total_size = element_sizes[0] * len; \
82 | for (size_t i = 1; i < elements; i++) { \
83 | total_size += element_sizes[i] * len; \
84 | } \
85 |
86 | #define z__PRIV__SoArr_STRUCT_ELEM_UPDATE()\
87 | z__byte* ptr = self->data[0]; \
88 | ptr += (element_sizes[0] * len); \
89 | for (size_t i = 1; i < elements; i++) { \
90 | self->data[i] = ptr; \
91 | ptr += (element_sizes[i] * len); \
92 | } \
93 |
94 |
95 | #endif
96 |
97 |
--------------------------------------------------------------------------------
/src/lib/std/alloc.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/src/lib/std/alloc.h
--------------------------------------------------------------------------------
/src/lib/std/arch.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_STD_ARCH_H
2 | #define ZAKAROUF_Z_STD_ARCH_H
3 |
4 | #if defined(__x86_64__) || defined(_M_X64)
5 | #define Z__ARCH_x86_64
6 | #define Z__ARCHSTR() "x86_64"
7 |
8 | #elif defined(i386) || defined(__i386__) || defined(__i386) || defined(_M_IX86)
9 | #define Z__ARCH_x86_32
10 | #define Z__ARCHSTR() "x86_32"
11 |
12 | #elif defined(__ARM_ARCH_2__)
13 | #define Z__ARCH_ARM2
14 | #define Z__ARCHSTR() "ARM2"
15 |
16 | #elif defined(__ARM_ARCH_3__) || defined(__ARM_ARCH_3M__)
17 | #define Z__ARCH_ARM3
18 | #define Z__ARCHSTR() "ARM3"
19 |
20 | #elif defined(__ARM_ARCH_4T__) || defined(__TARGET_ARM_4T)
21 | #define Z__ARCH_ARM4T
22 | #define Z__ARCHSTR() "ARM4T"
23 |
24 | #elif defined(__ARM_ARCH_5_) || defined(__ARM_ARCH_5E_)
25 | #define Z__ARCH_ARM5
26 | #define Z__ARCHSTR() "ARM5"
27 |
28 | #elif defined(__ARM_ARCH_6T2_) || defined(__ARM_ARCH_6T2_)
29 | #define Z__ARCH_ARM6T2
30 | #define Z__ARCHSTR() "ARM6T2"
31 |
32 | #elif defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__)
33 | #define Z__ARCH_ARM6
34 | #define Z__ARCHSTR() "ARM6"
35 |
36 | #elif defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__)
37 | #define Z__ARCH_ARM7
38 | #define Z__ARCHSTR() "ARM7"
39 |
40 | #elif defined(__ARM_ARCH_7A__) || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__)
41 | #define Z__ARCH_ARM7A
42 | #define Z__ARCHSTR() "ARM7A"
43 |
44 | #elif defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) || defined(__ARM_ARCH_7S__)
45 | #define Z__ARCH_ARM7R
46 | #define Z__ARCHSTR() "ARM7R"
47 |
48 | #elif defined(__ARM_ARCH_7M__)
49 | #define Z__ARCH_ARM7M
50 | #define Z__ARCHSTR() "ARM7M"
51 |
52 | #elif defined(__ARM_ARCH_7S__)
53 | #define Z__ARCH_ARM7S
54 | #define Z__ARCHSTR() "ARM7S"
55 |
56 | #elif defined(__aarch64__) || defined(_M_ARM64)
57 | #define Z__ARCH_ARM64
58 | #define Z__ARCHSTR() "ARM64"
59 |
60 | #elif defined(mips) || defined(__mips__) || defined(__mips)
61 | #define Z__ARCH_MIPS
62 | #define Z__ARCHSTR() "MIPS"
63 |
64 | #elif defined(__sh__)
65 | #define Z__ARCH_SUPERH
66 | #define Z__ARCHSTR() "SUPERH"
67 |
68 | #elif defined(__powerpc) || defined(__powerpc__) || defined(__powerpc64__) || defined(__POWERPC__) || defined(__ppc__) || defined(__PPC__) || defined(_ARCH_PPC)
69 | #define Z__ARCH_POWERPC
70 | #define Z__ARCHSTR() "POWERPC"
71 |
72 | #elif defined(__PPC64__) || defined(__ppc64__) || defined(_ARCH_PPC64)
73 | #define Z__ARCH_POWERPC64
74 | #define Z__ARCHSTR() "POWERPC64"
75 |
76 | #elif defined(__sparc__) || defined(__sparc)
77 | #define Z__ARCH_SPARC
78 | #define Z__ARCHSTR() "SPARC"
79 |
80 | #elif defined(__m68k__)
81 | #define Z__ARCH_M68K
82 | #define Z__ARCHSTR() "M68K"
83 |
84 | #else
85 | #define Z__ARCH_UNKNOWN
86 | #define Z__ARCHSTR() "UNKNOWN"
87 |
88 | #endif
89 |
90 | #endif
91 |
--------------------------------------------------------------------------------
/src/lib/std/io.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z__STD_IO_H
2 | #define ZAKAROUF_Z__STD_IO_H
3 |
4 | #include "../types/primitives.h"
5 | #include "../types/arr.h"
6 | #include "../types/string.h"
7 |
8 | typedef struct z__File z__File;
9 | struct z__File {
10 | z__u8Arr buffer;
11 | z__u64 fd;
12 | z__u16 flush_at;
13 | struct flags {
14 | z__u8 _read: 1
15 | , _append: 1
16 | , _write: 1;
17 | };
18 | };
19 |
20 | int z__File_open(z__File *file, z__CStr _path, const char *__restrict flag);
21 | int z__File_close(z__File *file);
22 | int z__File_flush(z__File *file);
23 |
24 | int z__File_read(z__File *file, void *dest, z__size size);
25 | int z__File_write(z__File *file, void const *data, z__size size);
26 |
27 |
28 | #endif
29 |
--------------------------------------------------------------------------------
/src/lib/std/primitives.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_BASE_H
2 | #define ZAKAROUF__ZTYPES_BASE_H
3 |
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | /* Basic Types */
10 | // Unsigned
11 | typedef uint8_t z__u8;
12 | typedef uint16_t z__u16;
13 | typedef uint32_t z__u32;
14 | typedef uint64_t z__u64;
15 |
16 | typedef uint_fast8_t z__u8_f;
17 | typedef uint_fast16_t z__u16_f;
18 | typedef uint_fast32_t z__u32_f;
19 | typedef uint_fast64_t z__u64_f;
20 |
21 | // Signed
22 | typedef int8_t z__i8;
23 | typedef int16_t z__i16;
24 | typedef int32_t z__i32;
25 | typedef int64_t z__i64;
26 |
27 | typedef int_fast8_t z__i8_f;
28 | typedef int_fast16_t z__i16_f;
29 | typedef int_fast32_t z__i32_f;
30 | typedef int_fast64_t z__i64_f;
31 |
32 | // Flaats
33 | typedef float z__f32;
34 | typedef double z__f64;
35 |
36 | // Misc
37 | typedef int z__int;
38 | typedef float z__float;
39 | typedef _Bool z__bool;
40 | typedef void* z__ptr;
41 | typedef z__u8 z__byte;
42 |
43 | typedef intmax_t z__imax;
44 | typedef uintmax_t z__umax;
45 | typedef intptr_t z__iptr;
46 | typedef uintptr_t z__uptr;
47 |
48 | typedef ptrdiff_t z__ptrdiff;
49 |
50 | #define z__sizeof sizeof
51 | typedef size_t z__size;
52 |
53 |
54 | #define TYPEDEFVEC(T, n) typedef T T##x##n[n]
55 |
56 | /* 8-bit vec */
57 | TYPEDEFVEC(z__i8, 2);
58 | TYPEDEFVEC(z__i8, 4);
59 | TYPEDEFVEC(z__i8, 6);
60 | TYPEDEFVEC(z__i8, 8);
61 | TYPEDEFVEC(z__u8, 12);
62 | TYPEDEFVEC(z__i8, 16);
63 |
64 | TYPEDEFVEC(z__u8, 2);
65 | TYPEDEFVEC(z__u8, 4);
66 | TYPEDEFVEC(z__u8, 6);
67 | TYPEDEFVEC(z__u8, 8);
68 | TYPEDEFVEC(z__u8, 12);
69 | TYPEDEFVEC(z__u8, 16);
70 |
71 |
72 | /* 16-bit vec */
73 | TYPEDEFVEC(z__i16, 2);
74 | TYPEDEFVEC(z__i16, 3);
75 | TYPEDEFVEC(z__i16, 4);
76 | TYPEDEFVEC(z__i16, 8);
77 |
78 | TYPEDEFVEC(z__u16, 2);
79 | TYPEDEFVEC(z__u16, 3);
80 | TYPEDEFVEC(z__u16, 4);
81 | TYPEDEFVEC(z__u16, 8);
82 |
83 |
84 | /* 32-bit vec */
85 | TYPEDEFVEC(z__i32, 2);
86 | TYPEDEFVEC(z__i32, 3);
87 | TYPEDEFVEC(z__i32, 4);
88 |
89 | TYPEDEFVEC(z__u32, 2);
90 | TYPEDEFVEC(z__u32, 3);
91 | TYPEDEFVEC(z__u32, 4);
92 |
93 |
94 | /* 64-bit vec */
95 | TYPEDEFVEC(z__i64, 2);
96 | TYPEDEFVEC(z__i64, 3);
97 | TYPEDEFVEC(z__i64, 4);
98 |
99 | TYPEDEFVEC(z__u64, 2);
100 | TYPEDEFVEC(z__u64, 3);
101 | TYPEDEFVEC(z__u64, 4);
102 |
103 |
104 | /* floats vec */
105 | TYPEDEFVEC(z__f32, 2);
106 | TYPEDEFVEC(z__f32, 3);
107 | TYPEDEFVEC(z__f32, 4);
108 |
109 |
110 | /* double vec */
111 | TYPEDEFVEC(z__f64, 2);
112 | TYPEDEFVEC(z__f64, 3);
113 | TYPEDEFVEC(z__f64, 4);
114 |
115 | #undef TYPEDEFVEC
116 |
117 | #define z__cast(T, val) ((T)(val))
118 | #define z__constptr(T) T const * const
119 |
120 |
121 | #endif // Header Guard
122 |
--------------------------------------------------------------------------------
/src/lib/string8.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zakarouf/z_/7ae96d729f728920d0ec86abd640f2aeb948e290/src/lib/string8.h
--------------------------------------------------------------------------------
/src/lib/strto.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__STRTO_H
2 | #define ZAKAROUF_Z_IMP__STRTO_H
3 |
4 | #include "typegen.h"
5 |
6 | #define z__PRIV__strto_scan_typegen(v)\
7 | z__typegen_def(*(v), "",\
8 | , (z__i16, "%" PRIi16) \
9 | , (z__u16, "%" PRIu16) \
10 | , (z__i32, "%" PRIi32) \
11 | , (z__u32, "%" PRIu32) \
12 | , (z__i64, "%" PRIi64) \
13 | , (z__u64, "%" PRIu64) \
14 | \
15 | , (z__f32, "%f") \
16 | , (z__f64, "%lF") \
17 | \
18 | , (char, "%c") \
19 | , (char *, "%s") \
20 | )
21 |
22 | #define z__strto(str, v)\
23 | sscanf(str, z__PRIV__strto_scan_typegen(v), v)
24 |
25 |
26 | #define z__String_to(str, v)\
27 | {\
28 | if(str.len)\
29 | z__strto(str.data, v);\
30 | }
31 |
32 |
33 | #endif
34 |
35 |
--------------------------------------------------------------------------------
/src/lib/subp.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__SUB_H
2 | #define ZAKAROUF_Z_IMP__SUB_H
3 |
4 | /**
5 | * Get Enviorment Variable
6 | */
7 | char** z__subp_getenv(void);
8 |
9 | /**
10 | * Use fork() to create a child process
11 | */
12 | int z__subp_fork(char const *cmd);
13 |
14 | /**
15 | * Use posix_spawn() to create a child process
16 | */
17 | int z__subp_spawn(char * const cmd);
18 |
19 | /**
20 | * Use posix_spawn() to execute a binary, must provide absolute path of the binary
21 | */
22 | int z__subp_exec_raw(char const *absolute_exec_path, char * const *argv);
23 |
24 | /**
25 | * Same as z__sub_exec_raw(), but does not wait for child process to exit.
26 | */
27 | int z__subp_exec_nowait_raw(int *pid, char const *absolute_exec_path, char * const *argv);
28 |
29 | /**
30 | * Macro wrapper around z__sub_exec_nowait_raw()
31 | */
32 | #define z__subp_exec_nowait(p, exec, ...)\
33 | z__subp_exec_nowait_raw(p, exec, (char *const[]){__VA_ARGS__, NULL})
34 |
35 | /**
36 | * Macro wrapper around z__sub_exec_raw()
37 | */
38 | #define z__subp_exec(exec_path, ...)\
39 | z__subp_exec_raw(exec_path, (char *const []){__VA_ARGS__, NULL})
40 |
41 |
42 | #ifdef Z__IMPLEMENTATION
43 | #include
44 | #include
45 | #include
46 | #include
47 | #include
48 |
49 | extern char **environ;
50 |
51 | char** z__subp_getenv(void) { return environ; }
52 |
53 | int z__subp_fork(const char *command)
54 | {
55 | pid_t child, p;
56 | int status;
57 |
58 | /*
59 | * Prepare pipes et cetera first.
60 | */
61 |
62 | /* Fork to create the subprocess. */
63 | child = fork();
64 | if (child == (pid_t)-1) {
65 | /* Cannot fork(); usually out of resources (user limits).
66 | * see errno for details. With , you can use
67 | * strerror(errno) to obtain the error string itself. */
68 | return 0;
69 |
70 | } else if (!child) {
71 | /* This is the child process itself.
72 | * Do whatever cleanup is necessary, then
73 | * execute the subprocess command. */
74 | execl("/bin/sh", "sh", "-c", command, NULL);
75 |
76 | /* This is only reached if the exec failed;
77 | * again, see errno for reason.
78 | * Always have the child process exit! */
79 | return 0;
80 | }
81 |
82 | /* This is only run by the parent process
83 | * (because the child always exits within the
84 | * else if body above).
85 | *
86 | * The subprocess PID is 'child'.
87 | */
88 |
89 | /* Wait for the child process to exit. */
90 | do {
91 | status = 0;
92 | p = waitpid(child, &status, 0);
93 | if (p == (pid_t)-1 && errno != EINTR)
94 | break; /* Error */
95 | } while (p != child);
96 |
97 | if (p != child) {
98 | /* Child process was lost.
99 | * If (p == (pid_t)-1), errno describes the error.
100 | */
101 |
102 | } else if (WIFEXITED(status)) {
103 | /* Child process exited with WEXITSTATUS(status) status.
104 | * A status of 0 (or EXIT_SUCCESS) means success,
105 | * no errors occurred. Nonzero usually means an error,
106 | * but codes vary from binary to binary.
107 | */
108 |
109 | } else if (WIFSIGNALED(status)) {
110 | /* Child process died from WTERMSIG(status) signal.
111 | * If you include , you can use
112 | * strsignal(WTERMSIG(status))
113 | * to obtain the name (string) of the terminating signal.
114 | */
115 |
116 | } else {
117 | /* Child process died from unknown causes.
118 | */
119 |
120 | }
121 |
122 | /* All done. */
123 | return 1;
124 | }
125 |
126 | int z__subp_exec_nowait_raw(int *pid, char const *exec_path, char * const *argv)
127 | {
128 | int status = posix_spawn(pid, exec_path, NULL, NULL, argv, environ);
129 | return status;
130 | }
131 |
132 | int z__subp_exec_raw(char const *exec_path, char * const *argv)
133 | {
134 | pid_t pid;
135 | int status = posix_spawn(&pid, exec_path, NULL, NULL, argv, environ);
136 | if (status == 0) {
137 | do {
138 | if (waitpid(pid, &status, 0) == -1) {
139 | exit(1);
140 | }
141 | } while (!WIFEXITED(status) && !WIFSIGNALED(status));
142 | }
143 | return 1;
144 | }
145 |
146 | int z__subp_spawn(char * const cmd)
147 | {
148 | return z__subp_exec("/bin/sh", "sh", "-c", cmd);
149 | }
150 |
151 | #endif //Z__IMPLEMENTATION
152 |
153 | #endif
154 |
155 |
--------------------------------------------------------------------------------
/src/lib/sys.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__SYS_H
2 | #define ZAKAROUF_Z_IMP__SYS_H
3 |
4 | #include "string.h"
5 |
6 | /**
7 | * Get Ram Usage by the current program (in bytes)
8 | */
9 | long z__sys_getRamUsage(void);
10 |
11 | /**
12 | * Panic And Exit
13 | */
14 | void z__panic(int code, char const *str_msg_fmt, ...);
15 |
16 | /**
17 | * Get current working directory
18 | */
19 | z__String z__sys_getcwd(void);
20 |
21 | /**
22 | * Get current working Folder
23 | */
24 | z__String z__sys_getcwf(void);
25 |
26 | /**
27 | * Print message & Exit.
28 | */
29 | int z__exit(int code, char const *fmt, ...);
30 |
31 | #ifdef Z__IMPLEMENTATION
32 | #include
33 | #include
34 | #include
35 | #include
36 | #include
37 |
38 | #include "print.h"
39 |
40 | #if defined(_WIN32) || defined (_WIN64) || defined (WINDOWS) || defined (_MSC_VER)
41 | #include
42 | #include
43 | #define getcwd _getcwd
44 | #else
45 | #include
46 | #endif
47 |
48 | long z__sys_getRamUsage(void)
49 | {
50 |
51 | struct rusage usage;
52 | getrusage(RUSAGE_SELF, &usage);
53 |
54 | return usage.ru_maxrss;
55 | }
56 |
57 |
58 | void z__panic(int code, char const *str_msg_fmt, ...)
59 | {
60 | va_list arg;
61 | va_start(arg, str_msg_fmt);
62 | z__fprint_cl256f(stderr, 1, "panic: ");
63 | vfprintf(stderr, str_msg_fmt, arg);
64 | va_end(arg);
65 | exit(code);
66 | }
67 |
68 |
69 | int z__exit(int code, char const *fmt, ...)
70 | {
71 | va_list arg;
72 | va_start(arg, fmt);
73 | vfprintf(stderr, fmt, arg);
74 | va_end(arg);
75 | exit(code);
76 | }
77 | #endif //Z__IMPLEMENTATION
78 |
79 | #endif
80 |
--------------------------------------------------------------------------------
/src/lib/termio.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_IMP_TERMIO_H
2 | #define ZAKAROUF__ZTYPES_IMP_TERMIO_H
3 |
4 | #include "string.h"
5 |
6 | /**
7 | * Get Size of the Current Terminal
8 | */
9 | void z__termio_get_term_size(z__u32 * _Nonnull x, z__u32 * _Nonnull y);
10 |
11 | /**
12 | * Get a key input as char (blocking)
13 | */
14 | z__u8 z__termio_getchar(void);
15 |
16 | /**
17 | * Get a key input as char (non blocking)
18 | */
19 | z__u8 z__termio_getchar_nowait(void);
20 |
21 | /**
22 | * Get Key Hit
23 | */
24 | int z__termio_kbhit(void);
25 |
26 | /**
27 | * Block Terminal io
28 | */
29 | void z__termio_block(void);
30 |
31 | /**
32 | * Un-Block Terminal io
33 | */
34 | void z__termio_nonblock(void);
35 |
36 | /**
37 | * Enable/Disable Key Echo
38 | */
39 | void z__termio_echo(int tog);
40 |
41 | /**
42 | * Put Char on terminal
43 | */
44 | void z__termio_putchar(z__char c);
45 |
46 | /**
47 | * Put a string on terminal
48 | */
49 | void z__termio_putString(const z__String * _Nonnull str);
50 |
51 |
52 | #ifdef Z__IMPLEMENTATION
53 | #include
54 | #include
55 | #include
56 | #include
57 | #include
58 |
59 | #include "std/primitives.h"
60 | #include "string.h"
61 |
62 | void z__termio_nonblock(void)
63 | {
64 | struct termios ttystate;
65 |
66 | //get the terminal state
67 | tcgetattr(STDIN_FILENO, &ttystate);
68 | //turn off canonical mode
69 | ttystate.c_lflag &= ~ICANON;
70 | //minimum of number input read.
71 | ttystate.c_cc[VMIN] = 1;
72 |
73 | //set the terminal attributes.
74 | tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
75 | }
76 |
77 | void z__termio_block(void)
78 | {
79 | struct termios ttystate;
80 |
81 | //get the terminal state
82 | tcgetattr(STDIN_FILENO, &ttystate);
83 | //turn on canonical mode
84 | ttystate.c_lflag |= ICANON;
85 | //set the terminal attributes.
86 | tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
87 | }
88 |
89 | void z__termio_echo(int tog)
90 | {
91 | struct termios ttystate;
92 | tcgetattr(STDIN_FILENO, &ttystate);
93 |
94 | if(tog) ttystate.c_lflag |= ECHO;
95 | else ttystate.c_lflag &= ~ECHO;
96 |
97 | tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
98 | }
99 |
100 | int z__termio_kbhit(void)
101 | {
102 | struct timeval tv;
103 | fd_set fds;
104 | tv.tv_sec = 0;
105 | tv.tv_usec = 0;
106 | FD_ZERO(&fds);
107 | FD_SET(STDIN_FILENO, &fds); //STDIN_FILENO is 0
108 | select(STDIN_FILENO+1, &fds, NULL, NULL, &tv);
109 | return FD_ISSET(STDIN_FILENO, &fds);
110 | }
111 |
112 | z__u8 z__termio_getchar(void)
113 | {
114 | char buf = 0;
115 | struct termios old = {0};
116 |
117 | fflush(stdout);
118 | tcgetattr(0, &old);
119 |
120 | old.c_lflag &= ~ICANON;
121 | old.c_lflag &= ~ECHO;
122 | old.c_cc[VMIN] = 1;
123 | old.c_cc[VTIME] = 0;
124 |
125 | tcsetattr(0, TCSANOW, &old);
126 | read(0, &buf, 1);
127 |
128 | old.c_lflag |= ICANON;
129 | old.c_lflag |= ECHO;
130 | tcsetattr(0, TCSADRAIN, &old);
131 |
132 | return buf;
133 | }
134 |
135 | #define k(n) z__termio_key_##n
136 | enum z__termio_key {
137 | k(null) = 0
138 | , k(soh)
139 |
140 | , k(space) = 32
141 | , k(tilde) = 126
142 | , k(delete) = 127
143 |
144 | , k(arrow_up) = 128
145 | };
146 |
147 | z__u32 z__termio_getkey(void)
148 | {
149 | char buf = 0;
150 | struct termios old = {0};
151 |
152 | fflush(stdout);
153 | tcgetattr(0, &old);
154 |
155 | old.c_lflag &= ~ICANON;
156 | old.c_lflag &= ~ECHO;
157 | old.c_cc[VMIN] = 1;
158 | old.c_cc[VTIME] = 0;
159 |
160 | tcsetattr(0, TCSANOW, &old);
161 | read(0, &buf, 1);
162 |
163 | old.c_lflag |= ICANON;
164 | old.c_lflag |= ECHO;
165 | tcsetattr(0, TCSADRAIN, &old);
166 |
167 | return buf;
168 | }
169 |
170 | z__u8 z__termio_getchar_nowait(void)
171 | {
172 | struct termios ttystate;
173 |
174 | //get the terminal state
175 | tcgetattr(STDIN_FILENO, &ttystate);
176 | //turn off canonical mode
177 | ttystate.c_lflag &= ~ICANON;
178 | //minimum of number input read.
179 | ttystate.c_cc[VMIN] = 1;
180 |
181 | //set the terminal attributes.
182 | tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
183 |
184 | z__u8 c = 0;
185 | if(z__termio_kbhit()) {
186 | c = fgetc(stdin);
187 | }
188 |
189 | //get the terminal state
190 | tcgetattr(STDIN_FILENO, &ttystate);
191 | //turn on canonical mode
192 | ttystate.c_lflag |= ICANON;
193 | //set the terminal attributes.
194 | tcsetattr(STDIN_FILENO, TCSANOW, &ttystate);
195 |
196 | return c;
197 | }
198 |
199 | void z__termio_get_term_size(z__u32 *x, z__u32 *y)
200 | {
201 | struct winsize ws;
202 | ioctl(1, TIOCGWINSZ, &ws);
203 | *x = ws.ws_col;
204 | *y = ws.ws_row;
205 | }
206 |
207 | void z__termio_getln_b(z__String *str)
208 | {
209 | while(true) {
210 | int key = z__termio_getkey();
211 | if (key == '\n') { return; }
212 | z__String_pushChar(str, key);
213 | }
214 | }
215 |
216 | z__String z__termio_getln(void)
217 | {
218 | z__String str = z__String_new(64);
219 | z__termio_getln_b(&str);
220 | return str;
221 | }
222 |
223 | void z__termio_putString(z__String const * const str)
224 | {
225 | fwrite(str->data, sizeof *str->data, str->len, stdout);
226 | }
227 |
228 | #endif //Z__IMPLEMENTATION
229 |
230 | #endif
231 |
232 |
--------------------------------------------------------------------------------
/src/lib/test.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__TEST_H
2 | #define ZAKAROUF_Z_IMP__TEST_H
3 |
4 | #include "prep/base.h"
5 | #include "prep/args.h"
6 | #include "prep/nm/assert.h"
7 | #include "prep/nm/string.h"
8 |
9 | #include "print.h"
10 | #include "ansifmt.h"
11 | #include "typeof.h"
12 |
13 | #ifndef z__PRIV__test_ON_ASSERT_FALIURE
14 | #define z__PRIV__test_ON_ASSERT_FALIURE() goto _L_test_return;
15 | #endif
16 |
17 | #define z__PRIV__test_exception(fmt, ...)\
18 | ({ z__fprint_cl256f(stdout, 1,\
19 | " Assertion Failed!\n" \
20 | " =>> " __FILE__ ":" zpp__to_string(__LINE__) ": for test %i:\"%s\" \n" \
21 | z__ansi_fmt((cl256_fg, 4), (bold)) \
22 | "Description: " z__ansi_fmt((plain)) "%s\n" \
23 | fmt "\n" , _id_, _name_, _description_, ##__VA_ARGS__); _name_ = 0; z__PRIV__test_ON_ASSERT_FALIURE(); })
24 |
25 | #define z__test_assert(exp, fmt, ...)\
26 | zpp__assert_construct(exp, z__PRIV__test_exception,\
27 | z__ansi_fmt((bold)) "`" #exp "`\n" z__ansi_fmt((plain))\
28 | fmt ,##__VA_ARGS__)
29 |
30 | #define z__test_assert_int(exp, expected) { \
31 | z__typeof(exp) e1 = exp \
32 | , e2 = expected; \
33 | zpp__assert_construct( \
34 | e1 == e2 \
35 | , z__PRIV__test_exception \
36 | , "Expected: %i\nGot: %i", e2, e1); \
37 | }
38 |
39 | #define z__test_done(...) {\
40 | _L_test_return: \
41 | __VA_ARGS__; \
42 | return (size_t)_name_; \
43 | }
44 |
45 | #define z__test_def(suite, name)\
46 | static int zpp__CAT3(suite, , name)(char const * _name_, char const * _description_, const unsigned int _id_)
47 |
48 | #define z__test_run(suite, ...)\
49 | ({ \
50 | const char *fn_names[] = {zpp__Args_maplist(zpp__to_string, __VA_ARGS__)}; \
51 | int (*fn[])(const char *, const char *, const unsigned int) = { \
52 | zpp__Args_maplist_Pattern(suite,, __VA_ARGS__) \
53 | }; \
54 | size_t passed = 0; \
55 | z__fprint_cl256f(stdout, 5, \
56 | z__ansi_fmt((bold)) \
57 | "Running Suite \"" #suite "\"\n"); \
58 | for (size_t i = 0; i < zpp__Args_Count(__VA_ARGS__); i++) { \
59 | z__fprint_cl256f(stdout, 4, \
60 | " * Running Test [%zu/" zpp__to_string(zpp__Args_Count(__VA_ARGS__))"] \"%s\"\n"\
61 | , i + 1, fn_names[i]);\
62 | if((fn[i])(fn_names[i], "", i)) { \
63 | z__fprint_cl256f(stdout, 6 \
64 | , " -> Test [%zu/"\
65 | zpp__to_string(zpp__Args_Count(__VA_ARGS__))\
66 | "] Passed O : %s\n" \
67 | , i+1, fn_names[i]); \
68 | passed += 1; \
69 | } else { \
70 | z__fprint_cl256f(stdout, 1 \
71 | , " -> Test [%zu/"\
72 | zpp__to_string(zpp__Args_Count(__VA_ARGS__))\
73 | "] Failed X : %s\n", i+1, fn_names[i]); \
74 | } \
75 | } \
76 | z__fprint_cl256f(stdout, 3 \
77 | , "For Suite \"" #suite "\" %zu out of " \
78 | zpp__to_string(zpp__Args_Count(__VA_ARGS__)) \
79 | " test passed & %zu failed\n\n", passed, zpp__Args_Count(__VA_ARGS__) - passed);\
80 | passed; \
81 | })
82 |
83 | #define z__PRIV__test_defsu(suite)\
84 | int zpp__CAT(_test__, suite)(int *_total_)
85 |
86 | #define z__test_impl(suite, ...)\
87 | z__PRIV__test_defsu(suite) {\
88 | *_total_ = zpp__Args_Count(__VA_ARGS__);\
89 | return z__test_run(suite, __VA_ARGS__);\
90 | }
91 |
92 | #define z__test_implext(suite)\
93 | extern z__PRIV__test_defsu(suite)
94 |
95 | #define z__test_callsu(suite, total) zpp__CAT(_test__, suite)(total);
96 |
97 | #endif
98 |
99 |
--------------------------------------------------------------------------------
/src/lib/time.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__TIME_H
2 | #define ZAKAROUF_Z_IMP__TIME_H
3 |
4 | #include "std/primitives.h"
5 | #include "string.h"
6 |
7 | /**
8 | * Sleep for `n` milliseconds
9 | */
10 | void z__time_msleep(int milliseconds);
11 |
12 | /**
13 | * Get Local Time
14 | */
15 | int z__time_getLocalTime(int *h, int *m, int *s);
16 | void z__time_getLocalTime_str(z__String *str);
17 |
18 | /**
19 | * Get time
20 | */
21 | z__u64 z__time(void);
22 | z__u64 z__time_process(void);
23 | z__u64 z__time_tps(void);
24 | z__f64 z__time_delta(z__u64 process_0, z__u64 process_1);
25 | z__u64 z__time_mfps(z__u32 fps);
26 |
27 |
28 | #ifdef Z__IMPLEMENTATION
29 | #include
30 | #ifdef WIN32
31 | #include
32 | #elif _POSIX_C_SOURCE >= 199309L
33 | #include // for nanosleep
34 | #else
35 | #include // for usleep
36 | #endif
37 |
38 | #include "string.h"
39 |
40 | void z__time_msleep(int milliseconds)
41 | { // cross-platform sleep function
42 | #ifdef WIN32
43 | Sleep(milliseconds);
44 | #elif _POSIX_C_SOURCE >= 199309L
45 | struct timespec ts = {0};
46 | ts.tv_sec = milliseconds / 1000;
47 | ts.tv_nsec = (milliseconds % 1000) * 1000000;
48 | nanosleep(&ts, NULL);
49 | #else
50 | if (milliseconds >= 1000) {
51 | sleep(milliseconds / 1000);
52 | }
53 | usleep((milliseconds % 1000) * 1000);
54 | #endif
55 | }
56 |
57 | int z__time_getLocalTime(int *h, int *m, int *s)
58 | {
59 | time_t tCurrentTime;
60 | struct tm timeinfo;
61 | time(&tCurrentTime);
62 | localtime_r(&tCurrentTime, &timeinfo);
63 | *h = timeinfo.tm_hour;
64 | *m = timeinfo.tm_min;
65 | *s = timeinfo.tm_sec;
66 |
67 | return 0;
68 | }
69 |
70 | z__u64 z__time(void)
71 | {
72 | return time(NULL);
73 | }
74 |
75 | z__u64 z__time_process(void)
76 | {
77 | return clock();
78 | }
79 |
80 | z__u64 z__time_mfps(z__u32 fps)
81 | {
82 | z__float x = 1.0f/fps;
83 | return (z__u64)(x * 1000);
84 | }
85 |
86 | z__u64 z__time_tps(void)
87 | {
88 | return CLOCKS_PER_SEC;
89 | }
90 |
91 | z__f64 z__time_delta(z__u64 process_0, z__u64 process_1)
92 | {
93 | return (z__f64)(process_1 - process_0) / CLOCKS_PER_SEC;
94 | }
95 |
96 | void z__time_getLocalTime_str(z__String *str)
97 | {
98 | if(str->len == 0) z__String_new(28);
99 | if(str->len < 27) z__String_expand(str, 27);
100 | time_t tCurrentTime;
101 | time(&tCurrentTime);
102 | ctime_r(&tCurrentTime, str->data);
103 | str->data[24] = ' ';
104 | }
105 | #endif //Z__IMPLEMENTATION
106 | #endif
107 |
--------------------------------------------------------------------------------
/src/lib/tree.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__TREE_H
2 | #define ZAKAROUF__ZTYPES_TYPES__TREE_H
3 |
4 | #include "prep/base.h"
5 | #include "prep/map.h"
6 | #include "prep/args.h"
7 | #include "prep/nm/assert.h"
8 | #include "arr.h"
9 | #include "record.h"
10 | #include "vector.h"
11 |
12 | #define z__PRIV__Tree_apply_pointer(x) *x
13 |
14 | #define z__Tree(Name, node_names, T) \
15 | typedef struct Name Name; \
16 | struct Name { \
17 | T data; \
18 | union { \
19 | union { \
20 | Name *raw[zpp__Args_Count(zpp__UNPAREN(node_names))]; \
21 | struct { \
22 | Name zpp__Args_maplist( \
23 | z__PRIV__Tree_apply_pointer, zpp__UNPAREN(node_names)); \
24 | }; \
25 | } nodes; \
26 | z__Vector(z__u64, zpp__UNPAREN(node_names)) index_nodes; \
27 | }; \
28 | Name *parent_node; \
29 | z__u8 nodesUsed; \
30 | }
31 |
32 | #define z__Tree_data(t) (t)->data
33 | #define z__Tree_node(t, of) (t)->nodes.raw[of]
34 | #define z__Tree_nodes(t) (t)->nodes
35 | #define z__Tree_is_leaf(t) !(t)->nodesUsed
36 | #define z__Tree_nodesUsed(t) (t)->nodesUsed
37 | #define z__Tree_get_nodeCount(t) (sizeof((t)->nodes.raw)/sizeof((t)->nodes.raw[0]))
38 |
39 | #define z__Tree_setas_leaf(t, parent_node, ...) { \
40 | (t)->data = __VA_ARGS__; \
41 | (t)->nodesUsed = 0; \
42 | }
43 |
44 | #define z__Tree_setas_node(t, parent_node, nd, ...) { \
45 | z__Record_assign((&(t)->nodes), zpp__UNPAREN(nd)); \
46 | (t)->data = __VA_ARGS__; \
47 | (t)->nodesUsed = zpp__Args_Count(zpp__UNPAREN(nd)); \
48 | _Static_assert(zpp__Args_Count(zpp__UNPAREN(nd)) == z__Tree_get_nodeCount(t), \
49 | "Dangling/Unused nodes detected "\
50 | "Of :: " #t);\
51 | }
52 |
53 | #define z__Tree_setas_index_node(t, parent_node, nd, ...) { \
54 | z__Record_assign((&(t)->index_nodes), zpp__UNPAREN(nd)); \
55 | (t)->data = __VA_ARGS__; \
56 | (t)->nodesUsed = zpp__Args_Count(zpp__UNPAREN(nd)); \
57 | _Static_assert(zpp__Args_Count(zpp__UNPAREN(nd)) == z__Tree_get_nodeCount(t), \
58 | "Dangling/Unused nodes detected " \
59 | "Of :: " #t);\
60 | }
61 |
62 | #define z__Tree_changeto_node(t, ...) { \
63 | z__Record_assign(&(t)->nodes, __VA_ARGS__); \
64 | (t)->nodesUsed = zpp__Args_Count(__VA_ARGS__); \
65 | _Static_assert(zpp__Args_Count(zpp__UNPAREN(nd)) == z__Tree_get_nodeCount(t), \
66 | "Dangling/Unused nodes detected " \
67 | "Of :: " #t); \
68 | }
69 |
70 | #define z__Tree_changeto_leaf(t) { \
71 | (t)->nodesUsed = 0; \
72 | for(z__size i = 0; i < z__Tree_get_nodeCount(t); i += 1) {\
73 | (t)->nodes.raw[i] = NULL; \
74 | } \
75 | }
76 |
77 | #endif
78 |
79 |
--------------------------------------------------------------------------------
/src/lib/typegen.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__TYPEGEN_H
2 | #define ZAKAROUF__ZTYPES_TYPES__TYPEGEN_H
3 |
4 | #include "prep/map.h"
5 | #include "prep/args.h"
6 |
7 | #define z__PRIV__typegen_def_map(V) zpp__PRIV__Args_get_1 V: zpp__Args_skip_1 V,
8 | #define z__typegen_def(A, defaultVal, P, ...)\
9 | _Generic(A, \
10 | zpp__Args_map(z__PRIV__typegen_def_map, __VA_ARGS__) \
11 | default: defaultVal) P
12 |
13 | #endif
14 |
15 |
--------------------------------------------------------------------------------
/src/lib/typeid.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP__TYPE_H
2 | #define ZAKAROUF_Z_IMP__TYPE_H
3 |
4 | #include "type.h"
5 | #include "_typeid.h"
6 | #include "typegen.h"
7 | #include "typeof.h"
8 |
9 | enum {
10 | _Z__PRIV__TYPE_M_ATOM_INT = 0x01,
11 | _Z__PRIV__TYPE_M_ATOM_FLOAT,
12 | _Z__PRIV__TYPE_M_VECTOR,
13 | _Z__PRIV__TYPE_M_MATRIX,
14 | _Z__PRIV__TYPE_M_PRIM_ARR,
15 | _Z__PRIV__TYPE_M_PRIM_ARR_FLOAT,
16 | _Z__PRIV__TYPE_M_ARR,
17 | _Z__PRIV__TYPE_M_ARR_FLOAT,
18 | _Z__PRIV__TYPE_M_OBJ,
19 | };
20 |
21 | typedef union {
22 | struct {
23 | z__u8 minor, major;
24 | };
25 | z__u16 raw;
26 | } z__typeid;
27 |
28 | #define z__typeid_getmajor_raw(raw) (raw >> 4 * 2)
29 | #define z__typeid_getminor_raw(raw) (raw & (0x00ff))
30 |
31 | #define z__typeid_make_id(major, minor) ((major << 4 * 2) | (minor & 0x00ff))
32 |
33 | #define z__type_id_lit(n) zpp__CAT(_Z__PRIV__TYPE_ID__, n)
34 | #define t(n) z__type_id_lit(n)
35 | enum {
36 | t(u8) = z__typeid_make_id(_Z__PRIV__TYPE_M_ATOM_INT, 1),
37 | t(byte),
38 | t(i16),
39 | t(u16),
40 | t(i32),
41 | t(u32),
42 | t(i64),
43 | t(u64),
44 |
45 | t(char) ,//= z__typeid_make_id(_Z__PRIV__TYPE_M_ATOM_CHAR, 1),
46 | t(ptr) ,//= z__typeid_make_id(_Z__PRIV__TYPE_M_ATOM_PTR, 1),
47 |
48 |
49 | t(f32) = z__typeid_make_id(_Z__PRIV__TYPE_M_ATOM_FLOAT, 1),
50 | t(f64),
51 |
52 | /**/
53 | t(Vint2) = z__typeid_make_id(_Z__PRIV__TYPE_M_VECTOR, 1),
54 | t(Vint3),
55 | t(Vint4),
56 |
57 | t(Vector2),
58 | t(Vector3),
59 | t(Vector4),
60 |
61 | t(Matrix2) = z__typeid_make_id(_Z__PRIV__TYPE_M_MATRIX, 1),
62 | t(Matrix3),
63 | t(Matrix4),
64 |
65 | /**/
66 | t(prim_arr_u8) = z__typeid_make_id(_Z__PRIV__TYPE_M_PRIM_ARR, 1),
67 | t(prim_arr_byte),
68 | t(prim_arr_i16),
69 | t(prim_arr_u16),
70 | t(prim_arr_i32),
71 | t(prim_arr_u32),
72 | t(prim_arr_i64),
73 | t(prim_arr_u64),
74 |
75 | t(prim_arr_char),
76 | t(prim_arr_ptr),
77 |
78 | t(prim_arr_f32) = z__typeid_make_id(_Z__PRIV__TYPE_M_PRIM_ARR_FLOAT, 1),
79 | t(prim_arr_f64),
80 |
81 | /**/
82 | t(arr_u8) = z__typeid_make_id(_Z__PRIV__TYPE_M_ARR, 1),
83 | t(arr_byte),
84 | t(arr_i16),
85 | t(arr_u16),
86 | t(arr_i32),
87 | t(arr_u32),
88 | t(arr_i64),
89 | t(arr_u64),
90 |
91 | t(arr_char),
92 | t(arr_ptr),
93 |
94 | t(arr_f32) = z__typeid_make_id(_Z__PRIV__TYPE_M_ARR_FLOAT, 1),
95 | t(arr_f64),
96 |
97 | /**/
98 | t(dynt) = z__typeid_make_id(_Z__PRIV__TYPE_M_OBJ, 1),
99 | t(cstr),
100 | t(str),
101 | t(string),
102 | t(stringlist),
103 | };
104 | #undef t
105 |
106 | #define z__PRIV__type_eval(T)\
107 | z__typeset_def(z__typeof(T), 0 \
108 | , (z__u8 , z__type_id_lit(u8))\
109 | , (z__byte ,z__type_id_lit(byte))\
110 | , (z__i16, z__type_id_lit(i16))\
111 | , (z__u16, z__type_id_lit(u16))\
112 | , (z__i32, z__type_id_lit(i32))\
113 | , (z__u32, z__type_id_lit(u32))\
114 | , (z__i64, z__type_id_lit(i64))\
115 | , (z__u64, z__type_id_lit(u64))\
116 | \
117 | , (char , z__type_id_lit(char))\
118 | , (void *, z__type_id_lit(ptr))\
119 | \
120 | , (z__f32, z__type_id_lit(f32))\
121 | , (z__f64, z__type_id_lit(f64))\
122 | \
123 | , (char const *, z__type_id_lit(cstr))\
124 | , (char const [], z__type_id_lit(cstr))\
125 | \
126 | , (z__Vint2, z__type_id_lit(Vint2))\
127 | , (z__Vint3, z__type_id_lit(Vint3))\
128 | , (z__Vint4, z__type_id_lit(Vint4))\
129 | \
130 | , (z__Vector2, z__type_id_lit(Vector2))\
131 | , (z__Vector3, z__type_id_lit(Vector3))\
132 | , (z__Vector4, z__type_id_lit(Vector4))\
133 | \
134 | \
135 | , (z__Matrix2, z__type_id_lit(Matrix2))\
136 | , (z__Matrix3, z__type_id_lit(Matrix3))\
137 | , (z__Matrix4, z__type_id_lit(Matrix4))\
138 | \
139 | \
140 | , (z__u8[] , z__type_id_lit(prim_arr_u8))\
141 | , (z__byte[] , z__type_id_lit(prim_arr_byte))\
142 | , (z__i16[], z__type_id_lit(prim_arr_i16))\
143 | , (z__u16[], z__type_id_lit(prim_arr_u16))\
144 | , (z__i32[], z__type_id_lit(prim_arr_i32))\
145 | , (z__u32[], z__type_id_lit(prim_arr_u32))\
146 | , (z__i64[], z__type_id_lit(prim_arr_i64))\
147 | , (z__u64[], z__type_id_lit(prim_arr_u64))\
148 | \
149 | , (char[] , z__type_id_lit(prim_arr_char))\
150 | , (void*[], z__type_id_lit(prim_arr_ptr))\
151 | \
152 | , (z__f32[], z__type_id_lit(prim_arr_f32))\
153 | , (z__f64[], z__type_id_lit(prim_arr_f64))\
154 | \
155 | \
156 | , (z__u8Arr , z__type_id_lit(arr_u8))\
157 | , (z__byteArr , z__type_id_lit(arr_byte))\
158 | , (z__i16Arr, z__type_id_lit(arr_i16))\
159 | , (z__u16Arr, z__type_id_lit(arr_u16))\
160 | , (z__i32Arr, z__type_id_lit(arr_i32))\
161 | , (z__u32Arr, z__type_id_lit(arr_u32))\
162 | , (z__i64Arr, z__type_id_lit(arr_i64))\
163 | , (z__u64Arr, z__type_id_lit(arr_u64))\
164 | \
165 | , (z__i8Arr , z__type_id_lit(arr_char))\
166 | , (z__ptrArr, z__type_id_lit(arr_ptr))\
167 | \
168 | , (z__f32Arr, z__type_id_lit(arr_f32))\
169 | , (z__f64Arr, z__type_id_lit(arr_f64))\
170 | \
171 | \
172 | , (z__Dynt, z__type_id_lit(dynt))\
173 | , (z__Str, z__type_id_lit(str))\
174 | , (z__String, z__type_id_lit(string))\
175 | , (z__StringList, z__type_id_lit(stringlist))\
176 | )
177 |
178 | #define z__typeid_raw(T) z__PRIV__type_eval(T)
179 | #define z__typeid(T) ((z__typeid){.raw = z__typeid_raw(T)})
180 |
181 | #endif
182 |
183 |
--------------------------------------------------------------------------------
/src/lib/typeof.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__TYPEOF_H
2 | #define ZAKAROUF__ZTYPES_TYPES__TYPEOF_H
3 |
4 | #define z__typeof __typeof__
5 | #define z__cont_typeof(T, mem) z__typeof(((T *)0)->mem)
6 |
7 | #endif
8 |
--------------------------------------------------------------------------------
/src/lib/u8arr.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF_Z_IMP_U8ARR_H
2 | #define ZAKAROUF_Z_IMP_U8ARR_H
3 |
4 | #include "arr.h"
5 |
6 | z__u8* z__u8Arr_pushStream(z__u8Arr *arr, void const *data, z__u32 data_size);
7 | int z__u8Arr_loadStream(z__u8Arr *arr, z__size from, z__size size, void *data);
8 | z__u8* z__u8Arr_pushBlank(z__u8Arr *arr, z__u32 size);
9 |
10 | #ifdef Z__IMPLEMENTATION
11 | #include "string.h"
12 | #include
13 |
14 | z__u8* z__u8Arr_pushBlank(z__u8Arr *arr, z__u32 size)
15 | {
16 | if((size + arr->lenUsed) > arr->len) {
17 | z__Arr_expand(arr, size);
18 | }
19 |
20 | z__u8 *data = arr->data + arr->lenUsed;
21 | memset(data, 0, size);
22 |
23 | arr->lenUsed += size;
24 | return data;
25 | }
26 |
27 | z__u8* z__u8Arr_pushStream(z__u8Arr *arr, void const *data, z__u32 data_size)
28 | {
29 | if(data_size + arr->lenUsed > arr->len) {
30 | z__Arr_expand(arr, data_size);
31 | }
32 | memcpy(arr->data + arr->lenUsed, data, data_size);
33 | z__u8 *new_data = arr->data + arr->lenUsed;
34 | arr->lenUsed += data_size;
35 | return new_data;
36 | }
37 |
38 | int z__u8Arr_loadStream(z__u8Arr *arr, z__size from, z__size size, void *data)
39 | {
40 | if(from > arr->lenUsed) return 0;
41 | if((from + size) > arr->lenUsed) return 0;
42 |
43 | memcpy(data, arr->data + from, size);
44 | return 1;
45 | }
46 |
47 | #endif //Z__IMPLEMENTATION
48 |
49 | #endif // !ZAKAROUF_Z_IMP_U8ARR_H
50 |
--------------------------------------------------------------------------------
/src/lib/utf8.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__Z__UTF_H
2 | #define ZAKAROUF__Z__UTF_H
3 |
4 | #include "std/primitives.h"
5 |
6 | z__bool z__utf8_is_valid(z__u8 const *str
7 | , z__size size);
8 | z__bool z__utf8_is_ascii(z__u8 const *str
9 | , z__size size);
10 | z__size z__utf8_get_len(z__u8 const *str
11 | , z__size size);
12 |
13 |
14 | #ifdef Z__IMPLEMENTATION
15 | inline static z__u8 _utf8_eval_codepoint(z__byte const c)
16 | {
17 | z__u8 num_bytes = 1;
18 | if ((c & 0xE0) == 0xC0) num_bytes = 2; // 2-byte UTF-8
19 | else if ((c & 0xF0) == 0xE0) num_bytes = 3; // 3-byte UTF-8
20 | else if ((c & 0xF8) == 0xF0) num_bytes = 4; // 4-byte UTF-8
21 | return num_bytes;
22 | }
23 |
24 | inline static z__u8 _utf8_char_is_valid(z__byte const c)
25 | {
26 | if ((c & 0x80) == 0) return 1; // 1-byte ASCII
27 | else if ((c & 0xE0) == 0xC0) return 2; // 2-byte UTF-8
28 | else if ((c & 0xF0) == 0xE0) return 3; // 3-byte UTF-8
29 | else if ((c & 0xF8) == 0xF0) return 4; // 4-byte UTF-8
30 | return 0; // Invalid
31 | }
32 |
33 | z__bool z__utf8_is_valid(z__u8 const *str
34 | , z__size size)
35 | {
36 | z__u8 const *str_end = str + size;
37 | while(_utf8_char_is_valid(*str)) { str+=1; }
38 | return str == str_end;
39 | }
40 |
41 | z__bool z__utf8_is_ascii(z__u8 const *str
42 | , z__size size)
43 | {
44 | z__u8 const *str_end = str + size;
45 | while (_utf8_char_is_valid(*str) == 1) { str+=1; }
46 | return str == str_end;
47 | }
48 |
49 | z__size z__utf8_get_len(z__u8 const *str
50 | , z__size size)
51 | {
52 | z__u8 const *str_end = str + size;
53 | z__size len = 0;
54 | while(str < str_end) { str += _utf8_eval_codepoint(*str);
55 | len += 1; }
56 | return len;
57 | }
58 |
59 | #endif //Z__IMPLEMENTATION
60 |
61 | #endif
62 |
--------------------------------------------------------------------------------
/src/lib/utils.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__BASE_UTIL_H
2 | #define ZAKAROUF__ZTYPES_TYPES__BASE_UTIL_H
3 |
4 | #define z__xy2Dto1D(x, y, X) ((x) + ((y) * (X)))
5 | #define z__xyz3Dto1D(x, y, z, X, Y) (z__xy2Dto1D(x, y, X) + ((z) * (X) * (Y)))
6 |
7 | #define z__min_unsafe(x, y) (x > y? y : x)
8 | #define z__min(x, y)\
9 | ({ \
10 | z__typeof(x) tmpx = x; \
11 | z__typeof(y) tmpy = y; \
12 | z__min_unsafe(tmpx, tmpy); \
13 | })
14 |
15 | #define z__max_unsafe(x, y) (x > y? x : y)
16 | #define z__max(x, y)\
17 | ({ \
18 | z__typeof(x) tmpx = x; \
19 | z__typeof(y) tmpy = y; \
20 | z__max_unsafe(tmpx, tmpy); \
21 | })
22 |
23 | #define z__swap(x, y)\
24 | { \
25 | z__typeof(x) _temp_x = x; \
26 | x = y; \
27 | y = _temp_x; \
28 | }
29 |
30 | #define z__is_in_range_unsafe(number, min, max) ((number >= min) & (number <= max))
31 | #define z__is_in_range(number, min, max)\
32 | ({ \
33 | z__typeof(number) _temp_number = number; \
34 | z__is_in_range_unsafe(_temp_number, min, max); \
35 | )}
36 |
37 | #define z__abs_unsafe(number) (number>0? number : -number)
38 | #define z__abs(number) \
39 | ({ \
40 | z__typeof(number) _temp_number = number;\
41 | z__abs_unsafe(_temp_number); \
42 | })
43 |
44 | #define z__hasOppositeSign(x, y) ((x ^ y) < 0)
45 | #define z__isPowOf2(x) !(x & (x - 1))
46 |
47 | /* Normalize u32, between 0.0 to 1.0 in float-32 */
48 | #define z__u32norm(u32) z__cast(z__f32, ((u32 >> 8) * (1.0f / (1U << 24))))
49 |
50 | /* Normalize u64, between 0.0 to 1.0 in float-64 */
51 | #define z__u64norm(u64) z__cast(z__f64, ((u64 >> 11) * (1.0 / (1ULL << 53))))
52 |
53 |
54 |
55 | #define z__cmp_ptr_data(pX, pX_len, pY, pY_len, cmpfn)\
56 | ((sizeof(*pX) * pX_len) == (sizeof(*pY) * pY_len))?\
57 | (cmpfn(pX, pY, (sizeof(*pX) * pX_len))):\
58 | ((int)(sizeof(*pX) * pX_len) - (int)(sizeof(*pY) * pY_len))
59 |
60 |
61 | /**
62 | * Functions
63 | */
64 | #include "std/primitives.h"
65 |
66 | z__u32 z__u64_count_digits(z__u64 number);
67 |
68 |
69 | /** IMPLEMENTATION **/
70 | #ifdef Z__IMPLEMENTATION
71 | z__u32 z__u64_count_digits(z__u64 number)
72 | {
73 | z__u32 count = 0;
74 | while(number) {
75 | number >>= 1;
76 | count += 1;
77 | }
78 | return count;
79 | }
80 | #endif // Z__IMPLEMENTATION
81 |
82 | #endif
83 |
84 |
--------------------------------------------------------------------------------
/src/lib/vector.h:
--------------------------------------------------------------------------------
1 | #ifndef ZAKAROUF__ZTYPES_TYPES__GMATH_VECTOR_H
2 | #define ZAKAROUF__ZTYPES_TYPES__GMATH_VECTOR_H
3 |
4 | #include "_config_types.h"
5 | #include "typeof.h"
6 | #include "std/primitives.h"
7 | #include "prep/args.h"
8 |
9 | // Start
10 |
11 | #define z__vec_x(vec) vec[0]
12 | #define z__vec_y(vec) vec[1]
13 | #define z__vec_z(vec) vec[2]
14 | #define z__vec_w(vec) vec[3]
15 |
16 | /* New type declarator, Vector and Matrix */
17 | #define z__VectorSt(T, ...)\
18 | struct { \
19 | z__typeof(T) __VA_ARGS__; \
20 | }
21 |
22 | #define z__Vector(T, ...)\
23 | union { \
24 | z__typeof(T) raw[zpp__Args_Count(__VA_ARGS__)];\
25 | z__VectorSt(T, __VA_ARGS__); \
26 | }
27 |
28 | #define z__vec(T, sz) z__typeof(T[sz])
29 |
30 | /* |x, y, z, w| -> w is the last */
31 | #ifndef Z__CONFIG_VECTOR_CGLM_ALIAS_TYPE_VECTOR
32 | typedef z__i32 z__ivec2[2];
33 | typedef z__i32 z__ivec3[3];
34 | typedef z__i32 z__ivec4[4];
35 |
36 | typedef z__f32 z__vec2[2];
37 | typedef z__f32 z__vec3[3];
38 | typedef z__f32 z__vec4[4];
39 |
40 | typedef z__vec4 z__versor;
41 |
42 | #ifdef Z__CONFIG_VECTOR_USE_f64_FOR_VECTOR_STRUCT
43 | #define __vecDefType z__f64
44 | #else
45 | #define __vecDefType z__f32
46 | #endif
47 |
48 | typedef z__Vector(__vecDefType, x, y) z__Vector2;
49 | typedef z__Vector(__vecDefType, x, y, z) z__Vector3;
50 | typedef z__Vector(__vecDefType, x, y, z, w) z__Vector4;
51 | typedef union {
52 | z__vec4 raw;
53 | z__VectorSt(z__f32, x, y, z, w);
54 | struct {
55 | z__vec3 real;
56 | z__f32 imag;
57 | };
58 | } z__Versor;
59 |
60 | #undef __vecDefType
61 |
62 |
63 | #else
64 | #include "cglm.h"
65 |
66 | typedef int z__ivec2[2];
67 | typedef ivec3 z__ivec3;
68 | typedef int z__ivec4[4];
69 |
70 | typedef vec2 z__vec2;
71 | typedef vec3 z__vec3;
72 | typedef vec4 z__vec4;
73 |
74 | typedef versor z__versor;
75 |
76 | typedef vec2s z__Vector2;
77 | typedef vec3s z__Vector3;
78 | typedef vec4s z__Vector4;
79 |
80 | typedef ivec3s z__Vint3;
81 |
82 | typedef versors z__Versor;
83 |
84 |
85 | #endif
86 |
87 |
88 | // Integer Vector Struct and Integer Matrix Struct are not
89 | // present in cglm (except Vint3).
90 | #ifdef Z__CONFIG_VECTOR_USE_i64_FOR_INT_VECTOR_STRUCT
91 | #define __vecDefType z__i64
92 | #else
93 | #define __vecDefType z__i32
94 | #endif
95 |
96 | typedef z__Vector(__vecDefType, x, y) z__Vint2;
97 | typedef z__Vector(__vecDefType, x, y, z, w) z__Vint4;
98 |
99 | #ifndef Z__CONFIG_VECTOR_CGLM_ALIAS_TYPE_VECTOR
100 | typedef z__Vector(__vecDefType, x, y, z) z__Vint3;
101 | #endif
102 |
103 | #undef __vecDefType
104 |
105 | /**
106 | * Additional size specific vectors
107 | */
108 | #define _make_vec(type)\
109 | typedef z__Vector(type, x, y) type##Vec2;\
110 | typedef z__Vector(type, x, y, z) type##Vec3;\
111 | typedef z__Vector(type, x, y, z, w) type##Vec4;
112 | _make_vec(z__i8)
113 | _make_vec(z__i16)
114 | _make_vec(z__i32)
115 | _make_vec(z__i64)
116 |
117 | _make_vec(z__u8)
118 | _make_vec(z__u16)
119 | _make_vec(z__u32)
120 | _make_vec(z__u64)
121 | #undef _make_vec
122 |
123 | /* Basic Arithmatic Operations For Vectors And Matrix
124 | **/
125 | #define z__Vector2_A(a, b, operator, dest) \
126 | { \
127 | (dest)->raw[0] = (a).raw[0] operator (b).raw[0]; \
128 | (dest)->raw[1] = (a).raw[1] operator (b).raw[1]; \
129 | }
130 | #define z__Vector3_A(a, b, operator, dest) \
131 | { \
132 | (dest)->raw[0] = (a).raw[0] operator (b).raw[0]; \
133 | (dest)->raw[1] = (a).raw[1] operator (b).raw[1]; \
134 | (dest)->raw[2] = (a).raw[2] operator (b).raw[2]; \
135 | }
136 | #define z__Vector4_A(a, b, operator, dest) \
137 | { \
138 | (dest)->raw[0] = (a).raw[0] operator (b).raw[0]; \
139 | (dest)->raw[1] = (a).raw[1] operator (b).raw[1]; \
140 | (dest)->raw[2] = (a).raw[2] operator (b).raw[2]; \
141 | (dest)->raw[3] = (a).raw[3] operator (b).raw[3]; \
142 | }
143 | #define z__Vector3_dot(a,b) (((a).x * (b).x)+((a).y * (b).y)+((a).z * (b).z))
144 |
145 |
146 |
147 | #endif
148 |
149 |
--------------------------------------------------------------------------------
/src/testlib/arena.c:
--------------------------------------------------------------------------------
1 | #include "../lib/test.h"
2 | #include "../lib/arena.h"
3 |
4 | #include
5 |
6 | #define TEST_SUITE arena
7 | #define TEST(name) z__test_def(TEST_SUITE, name)
8 |
9 | #define z__size_MB(x) (z__size_KB(x) * 1024)
10 | #define z__size_KB(x) (x * 1024)
11 | #define z__size_B(x) (x)
12 |
13 | TEST(arena) {
14 | int sizes[10] = { 1023, 525, 10000, 23, 25
15 | , 1100, 431, 23410, 89, 2521525 };
16 | z__size total_size = sizeof(z__Arena);
17 |
18 | z__Arena *arena = z__Arena_new(z__size_MB(13));
19 |
20 | for (size_t i = 0; i < 10; i++) {
21 | z__Arena_alloc(arena, sizes[i]);
22 | total_size += sizes[i];
23 | }
24 |
25 | z__size arena_used = z__Arena_total_used(arena);
26 | z__test_assert(arena_used == total_size, "%"PRIu64 " != %"PRIu64
27 | , arena_used, total_size);
28 |
29 | z__Arena_delete(arena);
30 |
31 | z__test_assert(1, "");
32 | z__test_done();
33 | }
34 |
35 | z__test_impl(TEST_SUITE, arena);
36 |
--------------------------------------------------------------------------------
/src/testlib/imp/argparse.c:
--------------------------------------------------------------------------------
1 | #include "../../lib/argparse.h"
2 | #include "../../lib/test.h"
3 | #include "../../lib/std/primitives.h"
4 |
5 | #include
6 |
7 | #define TEST_SUITE imp_argparse
8 | #define TEST(name) z__test_def(TEST_SUITE, name)
9 |
10 | static
11 | const char *argv[] = {
12 | "--run",
13 | "filename",
14 | };
15 |
16 | static
17 | const int argc = sizeof(argv)/sizeof(*argv);
18 |
19 | TEST(argparse) {
20 |
21 | int a;
22 | z__argp_start(argv, 0, argc) {
23 | z__argp_ifarg_custom(("-a", 1), ("--append", 1)) {
24 |
25 | }
26 | z__argp_ifarg(&a, ("a", 1)) {
27 |
28 |
29 | }
30 | }
31 |
32 | z__test_assert(1, "");
33 | z__test_done();
34 | }
35 |
36 | z__test_impl(TEST_SUITE, argparse);
37 |
38 |
--------------------------------------------------------------------------------
/src/testlib/imp/print.c:
--------------------------------------------------------------------------------
1 | #include "../../lib/print.h"
2 | #include "../../lib/test.h"
3 |
4 | #define TEST(name) z__test_def(imp_print, name)
5 |
6 | TEST(print) {
7 |
8 | z__fprint_cl256f(stdout, 4, "cl256f\n");
9 | z__fprint_vb(stdout, "vb Number => %d\n", 24);
10 | z__fprint_vb_cl256f(stdout, 32, "Hello\n");
11 |
12 | z__fprint_clrgb(stdout, 10, 52, 9, 100, 3, 52, "cl\n");
13 | z__fprint_clrgbf(stdout, 100, 3, 52, "clf\n");
14 |
15 | z__fprint_vb_clrgb(stdout, 10, 52, 9, 100, 3, 52, "vb\n");
16 | z__fprint_vb_clrgbf(stdout, 100, 3, 52, "\n");
17 |
18 | z__test_assert(1 == 1, "");
19 |
20 | z__test_done();
21 | }
22 |
23 | z__test_impl(imp_print, print);
24 |
25 |
--------------------------------------------------------------------------------
/src/testlib/imp/time.c:
--------------------------------------------------------------------------------
1 | #include "../../lib/time.h"
2 | #include "../../lib/test.h"
3 | #include
4 | #include
5 |
6 | #define TEST_SUITE time
7 | #define TEST(name) z__test_def(TEST_SUITE, name)
8 |
9 | TEST(main)
10 | {
11 |
12 | printf("time => %" PRIu64 "\n", z__time());
13 | printf("tps => %"PRIu64"\n", z__time_tps());
14 | printf("process time => %"PRIu64"\n", z__time_process());
15 | printf("Execution Time => %f\n", z__time_delta(0, z__time_process()));
16 |
17 | z__test_assert(1, ""); // Supress Warning
18 | z__test_done();
19 | }
20 |
21 | z__test_impl(TEST_SUITE, main);
22 |
23 |
--------------------------------------------------------------------------------
/src/testlib/main.c:
--------------------------------------------------------------------------------
1 | #include "../lib/test.h"
2 | #include "../lib/prep/args.h"
3 | #include
4 | #include
5 |
6 | #define import_sin(x) z__test_implext(x);
7 | #define import(...) zpp__Args_map(import_sin, __VA_ARGS__)
8 |
9 | #define testrun_sin(suite)\
10 | { \
11 | int total = 0; \
12 | int passed = z__test_callsu(suite, &total); \
13 | if(total != passed) { \
14 | z__fprint(stdout, "Test in suite \"" #suite "\" Failed!\n"); \
15 | abort(); \
16 | } \
17 | }
18 |
19 | #define testrun(...)\
20 | zpp__Args_map(testrun_sin, __VA_ARGS__);\
21 |
22 | #define test_list_types \
23 | types_primitives\
24 | , arr\
25 | , string\
26 | , hashset\
27 | , types_obj
28 |
29 | #define test_list_imp \
30 | imp_print\
31 | , time\
32 | , arena
33 |
34 | import(test_list_types);
35 | import(test_list_imp);
36 | import(playground);
37 |
38 | void die(const char *msg)
39 | {
40 | z__print("%s", msg);
41 | abort();
42 | }
43 |
44 | int main (void)
45 | {
46 | /* Types */
47 | testrun(test_list_types);
48 |
49 | /* Imp */
50 | testrun(test_list_imp);
51 |
52 | /**/
53 | testrun(playground);
54 |
55 | z__print(
56 | z__ansi_fmt((bold))
57 | "All Test Passed"
58 | z__ansi_fmt((plain))
59 | "\n"
60 | );
61 |
62 | return 0;
63 | }
64 |
--------------------------------------------------------------------------------
/src/testlib/playground.c:
--------------------------------------------------------------------------------
1 | #define Z__IMPLEMENTATION
2 |
3 | #include "../lib/std/primitives.h"
4 |
5 | #include "../lib/test.h"
6 | #include "../lib/forrange.h"
7 |
8 | #include "../lib/prep/args.h"
9 | #include "../lib/prep/loop.h"
10 | #include "../lib/prep/num.h"
11 |
12 | #include "../lib/string.h"
13 | #include "../lib/time.h"
14 |
15 | #include "../lib/record.h"
16 | #include "../lib/vector.h"
17 | #include "../lib/arr.h"
18 | #include "../lib/llist.h"
19 | #include "../lib/string.h"
20 | #include "../lib/hashset.h"
21 |
22 | #include "../lib/bitf.h"
23 |
24 | #include "../lib/obj.h"
25 | #include "../lib/assert.h"
26 |
27 | #include
28 | #include
29 | #include
30 | #include
31 |
32 | #include "../lib/arena.h"
33 |
34 | #define def(name) z__test_def(playground, name)
35 |
36 | #define log(fmt, ...)\
37 | printf("%s:%u @ %s(): " fmt "\n", __FILE__, __LINE__, __func__ zpp__Args_Expandif((,), (), __VA_ARGS__));
38 |
39 | #define z__PRIV__String_fmt(...)
40 |
41 | def(repeat) {
42 | int i = zpp__Repeat(2, +, 1);
43 | z__test_assert_int(i, 2);
44 | z__test_done();
45 | }
46 |
47 | /*
48 | #include "../lib/bnf.h"
49 | def(bnf)
50 | {
51 | int i = zpp__Repeat(2, +, 1);
52 | z__test_assert_int(i, 2);
53 |
54 | z__Bnf grammer;
55 | z__Bnf_new(&grammer);
56 |
57 | #define n(as, ...) zpp__CAT(z__BnfNode_, as)(grammer.node_pool, __VA_ARGS__)
58 | #define val(x) ((z__Str){.data = x, .len = sizeof(x)-1})
59 |
60 | z__BnfNode *digit =
61 | n(as_or, n(as_value, val("0"))
62 | , n(as_or, n(as_value, val("1"))
63 | , n(as_or, n(as_value, val("2"))
64 | , n(as_or, n(as_value, val("3"))
65 | , n(as_or, n(as_value, val("4"))
66 | , n(as_or, n(as_value, val("5"))
67 | , n(as_or, n(as_value, val("6"))
68 | , n(as_or, n(as_value, val("7"))
69 | , n(as_or, n(as_value, val("8"))
70 | , n(as_value, val("9"))
71 | )))))))));
72 |
73 | z__Bnf_add_rule(&grammer, "digit", 5, digit);
74 |
75 | z__BnfNode *numbers =
76 | n(as_or, n(as_and
77 | , n(as_rule, &grammer, val("digit"))
78 | , n(as_rule, &grammer, val("digit")))
79 | , n(as_rule, &grammer, val("digit"))
80 | );
81 |
82 | z__Bnf_add_rule(&grammer, "numbers", 7, numbers);
83 |
84 |
85 | z__print_bnf(&grammer);
86 |
87 | z__test_assert(1, "");
88 | z__test_done();
89 | }
90 | #include "../lib/prep/base.h"
91 | */
92 |
93 | typedef struct z__HashSetMeta z__HashSetMeta;
94 | struct z__HashSetMeta {
95 | z__u32 *keys;
96 | z__u32 *values;
97 |
98 | z__u32Arr unused_buff;
99 |
100 | z__u32 used;
101 | z__u32 capacity;
102 | };
103 |
104 |
105 | // refer to "../lib/tree.h" for ensuring all Assignment6
106 | #include "../lib/soarr.h"
107 | typedef struct Entities Entities;
108 | z__SoArr(Entities, (z__u32, pos), (z__u32, id))
109 |
110 | #include
111 |
112 | void print_utf8_chars(const char *utf8_str) {
113 | while (*utf8_str) {
114 | unsigned char c = *utf8_str;
115 |
116 | // Detect first byte of a UTF-8 sequence
117 | int num_bytes;
118 | if ((c & 0x80) == 0) num_bytes = 1; // 1-byte ASCII
119 | else if ((c & 0xE0) == 0xC0) num_bytes = 2; // 2-byte UTF-8
120 | else if ((c & 0xF0) == 0xE0) num_bytes = 3; // 3-byte UTF-8
121 | else if ((c & 0xF8) == 0xF0) num_bytes = 4; // 4-byte UTF-8
122 | else { utf8_str++; continue; } // Skip invalid byte
123 |
124 | printf("Char: ");
125 | for (int i = 0; i < num_bytes; i++) {
126 | printf("%02X ", (unsigned char)utf8_str[i]);
127 | }
128 | printf("\n");
129 |
130 | utf8_str += num_bytes;
131 | }
132 | }
133 |
134 | void z__print_bytes_as_bits(z__byte const *data, z__size size)
135 | {
136 | for (size_t i = 0; i < size; i++) {
137 | z__byte byte = data[i];
138 | z__byte bits[8];
139 | for (int j = 7; j >= 0; j--) {
140 | bits[j] = (byte&1) + '0';
141 | byte = byte >> 1;
142 | }
143 | z__print("0b%.*s(0x%02hhx) ", 8, bits, data[i]);
144 | }
145 | }
146 |
147 |
148 | def(utf8) {
149 |
150 | }
151 |
152 | def(main) {
153 | Entities entt;
154 | Entities_new(&entt, 8);
155 |
156 | z__print_bytes_as_bits((z__byte[8]){ 0x80
157 | , 0xE0
158 | , 0xC0
159 |
160 | , 0xF0
161 | , 0xE0
162 |
163 | , 0xF8
164 | , 0xF0}, 7);
165 | z__test_assert_int(1, 1);
166 | z__test_done();
167 | }
168 |
169 | z__test_impl(playground, main);
170 |
171 |
--------------------------------------------------------------------------------
/src/testlib/types/arr.c:
--------------------------------------------------------------------------------
1 | #include "../../lib/arr.h"
2 | #include "../../lib/std/mem.h"
3 | #include "../../lib/test.h"
4 |
5 | #include
6 | #include
7 | #include
8 |
9 | #define TEST(name) z__test_def(arr, name)
10 |
11 | TEST(init_and_delete) {
12 |
13 | z__Arr(int) f = z__Arr_init(f, 1, 2, 3, 5, 6);
14 |
15 | z__test_assert(
16 | z__Arr_cmpdata(f, ((int[]){1, 2, 3, 5, 6}), 5 * sizeof(int)) == 0,"");
17 |
18 | z__Arr_delete(&f);
19 | z__test_done();
20 | }
21 |
22 | TEST(copy) {
23 | z__u64Arr arr, copy_arr;
24 | z__Arr_new(&arr, 32);
25 |
26 | z__Arr_foreach(i, arr, normal, 0, arr.len) {
27 | *i = random();
28 | }
29 |
30 | z__Arr_newCopy(©_arr, arr);
31 |
32 | z__test_assert(z__Arr_cmp(arr, copy_arr) == 0, "");
33 | z__Arr_delete(&arr);
34 | z__Arr_delete(©_arr);
35 | z__test_done();
36 | }
37 |
38 | z__test_impl(arr, init_and_delete, copy);
39 |
40 |
--------------------------------------------------------------------------------
/src/testlib/types/base.c:
--------------------------------------------------------------------------------
1 | #include "../../lib/std/primitives.h"
2 | #include "../../lib/test.h"
3 |
4 | #define TEST_SUITE types_primitives
5 | #define TEST(name) z__test_def(TEST_SUITE, name)
6 |
7 |
8 | TEST(size_evaluation)
9 | {
10 | z__test_assert(sizeof(z__i8) == 1, "");
11 | z__test_assert(sizeof(z__i16) == 2, "");
12 | z__test_assert(sizeof(z__i32) == 4, "");
13 | z__test_assert(sizeof(z__i64) == 8, "");
14 |
15 | z__test_assert(sizeof(z__u8) == 1, "");
16 | z__test_assert(sizeof(z__u16) == 2, "");
17 | z__test_assert(sizeof(z__u32) == 4, "");
18 | z__test_assert(sizeof(z__u64) == 8, "");
19 |
20 | z__test_assert(sizeof(z__f32) == 4, "");
21 | z__test_assert(sizeof(z__f64) == 8, "");
22 |
23 | z__test_assert(sizeof(z__size) == 8, "");
24 | z__test_assert(sizeof(z__byte) == 1, "");
25 | z__test_assert(sizeof(z__byte) == sizeof(z__u8), "");
26 | z__test_assert(sizeof(z__ptr) == sizeof(z__size), "");
27 |
28 | z__test_done();
29 | }
30 |
31 | z__test_impl(TEST_SUITE, size_evaluation);
32 |
33 |
--------------------------------------------------------------------------------
/src/testlib/types/bit.c:
--------------------------------------------------------------------------------
1 | #include "../../lib/bitf.h"
2 | #include "../../lib/test.h"
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | #define TEST_SUITE bit
9 | #define TEST(name) z__test_def(TEST_SUITE, name)
10 |
11 | static void print_bytebit(z__u8 b, char endchar)
12 | {
13 | #define PRINT_BIT(offset)\
14 | fputc("01"[z__bit_has(b, offset)], stdout);
15 |
16 | PRINT_BIT(0);
17 | PRINT_BIT(1);
18 | PRINT_BIT(2);
19 | PRINT_BIT(3);
20 | PRINT_BIT(4);
21 | PRINT_BIT(5);
22 | PRINT_BIT(6);
23 | PRINT_BIT(7);
24 | fputc(endchar, stdout);
25 | }
26 |
27 | static void print_bytesbit(z__u8 b[static 1], z__size size)
28 | {
29 | for (size_t i = 0; i < size; i++) {
30 | print_bytebit(b[i], ' ');
31 | }
32 | printf("\n");
33 | }
34 |
35 |
36 | TEST(bit)
37 | {
38 | #define tassbit(x, n) z__bitf_has(x, n)
39 |
40 | z__u8 x[10] = {0};
41 | print_bytesbit(x, 10);
42 |
43 | z__bitf_set(x, 12);
44 | z__bitf_set(x, 56);
45 | z__bitf_set(x, 22);
46 | z__bitf_set(x, 3);
47 |
48 | int y = 0;
49 | z__bitf_set(&y, 0);
50 | z__bitf_set(&y, 19);
51 | z__bitf_set(&y, 31);
52 | z__bitf_set(&y, 2);
53 |
54 | #define bitass(x)\
55 | zpp__static_assert((((x >> 3) + 1) << 3) > x)
56 |
57 | #define printsize(x)\
58 | printf("(%u %u) ", x, ((x >> 3) + 1) << 3);
59 |
60 | bitass(1);
61 | bitass(8);
62 | bitass(729480);
63 | bitass(0);
64 |
65 | z__test_assert(1, "");
66 | z__test_done();
67 | }
68 |
69 | z__test_impl(TEST_SUITE, bit);
70 |
--------------------------------------------------------------------------------
/src/testlib/types/hashset.c:
--------------------------------------------------------------------------------
1 | #include "../../lib/hashset.h"
2 | #include "../../lib/test.h"
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | #define TEST_SUITE hashset
9 | #define TEST(name) z__test_def(TEST_SUITE, name)
10 |
11 | static int memctr = 0; // Counting memory allocation since we are dealing w/ macros
12 | #define heapcall(f, ...) f(__VA_ARGS__); memctr+=1;
13 | #define heapfree(f, ...) f(__VA_ARGS__); memctr-=1;
14 |
15 | #define malloc(x) heapcall(malloc, x)
16 | #define calloc(x, y) heapcall(calloc, x, y)
17 | #define strdup(x) heapcall(strdup, x)
18 | #define z__Str_newCopy(x) heapcall(z__Str_newCopy, x)
19 |
20 | #define free(x) heapfree(free, x)
21 |
22 | TEST(reset) {
23 | _description_ = "Create a HashStr, use reset and reuse the Hash Func.";
24 | #define cstr(cstr) z__Str(cstr, sizeof(cstr)-1)
25 | #define set(k, v) z__HashStr_set(&ht, cstr(k), v)
26 |
27 | z__HashStr(z__u32) ht;
28 | z__HashStr_new(&ht);
29 |
30 | set("Los", 0);
31 | set("Troal", 1);
32 | set("Noct", 2);
33 | set("Belic", 3);
34 | set("Aragaratata", 4);
35 | set("Poe", 5);
36 | set("Xr", 6);
37 | set("L'kor", 7);
38 | set("Mitac 2031", 8);
39 | set("67 + 23", 90);
40 |
41 | #define tassval(k, v) \
42 | z__HashStr_getreff(&ht, cstr(k), &val);\
43 | z__test_assert(val != NULL, "%s Not Found", k);\
44 | z__test_assert(*val == v, "Value found incorrect, expected: `%d` but found `%d`", v, *val);
45 |
46 | z__u32 *val = NULL;
47 | tassval("Los", 0);
48 | tassval("Troal", 1);
49 | tassval("Noct", 2);
50 | tassval("Belic", 3);
51 | tassval("Aragaratata", 4);
52 | tassval("Poe", 5);
53 | tassval("Xr", 6);
54 | tassval("L'kor", 7);
55 | tassval("Mitac 2031", 8);
56 | tassval("67 + 23", 90);
57 |
58 | z__HashStr_reset_all_val_decon(&ht, zpp__IGNORE);
59 | z__test_assert(ht.len, "Hash Set len is zero after reset");
60 | z__test_assert(ht.lenUsed == 0, "Hash Set len Used is not zero after reset, `%u`", ht.lenUsed);
61 |
62 | z__test_done();
63 | }
64 |
65 | TEST(hashstr) {
66 |
67 | #define cstr(cstr) z__Str(cstr, sizeof(cstr)-1)
68 | #define set(k, v) z__HashStr_set(&ht, cstr(k), v)
69 |
70 | #define tassheap(expected) \
71 | z__test_assert(memctr == expected, "Heap Alloc Counter at %i, expected %i", memctr, expected);
72 |
73 | #undef tassval
74 | #define tassval(k, v) \
75 | z__HashStr_getreff(&ht, cstr(k), &val);\
76 | z__test_assert(val != NULL, "%s Not Found", k);\
77 | z__test_assert(*val == v, "Value found incorrect, expected: `%d` but found `%d`", v, *val);
78 |
79 | #define memctr_print() z__print("Memctr at %i\n", memctr);
80 |
81 | z__HashStr(int) ht;
82 | z__HashStr_new(&ht);
83 | tassheap(3);
84 |
85 | set("Mango", 1); tassheap(4);
86 | set("Orange", 2); tassheap(5);
87 | set("Banana", 3); tassheap(6);
88 | set("Apple", 4); tassheap(7);
89 | set("Velvet", 90); tassheap(8);
90 | set("Vio", 12); tassheap(9);
91 | set("R", 65); tassheap(10);
92 | set("Mex", 22); tassheap(11);
93 | set("TRESSSS", 124); tassheap(12);
94 |
95 | z__HashSet_foreach(i, &ht) {
96 | printf("K(\"%s\") => %d\n", i.key->data, *i.value);
97 | }
98 |
99 | int *val = NULL;
100 |
101 | tassval("Mango", 1);
102 | tassval("Orange", 2);
103 | tassval("Banana", 3);
104 | tassval("Apple", 4);
105 |
106 | z__HashStr_delete(&ht);
107 |
108 | tassheap(0);
109 | memctr_print();
110 |
111 |
112 | z__test_done();
113 | }
114 |
115 | struct {
116 | z__i32 key; z__u64 value;
117 | } const pairs[] = {
118 | #define s(k, v) { .key = k, .value = v},
119 | s(0, 1000)
120 | s(1, 1001) s(2, 1002) s(3, 1003)
121 | s(4, 1004) s(5, 1005) s(6, 1006)
122 | s(7, 1007) s(8, 1008) s(9, 1009)
123 | s(10, 1010)s(11, 1011)s(12, 1012)
124 | s(13, 1013)s(14, 1011)s(15, 1012)
125 | s(16, 1016)s(17, 1017)s(18, 1018)
126 | s(19, 1019)s(20, 1020)s(21, 1021)
127 | #undef s
128 | };
129 |
130 | TEST(hashint) {
131 |
132 | const z__u32 length = sizeof(pairs)/sizeof(*pairs);
133 |
134 | z__HashInt(z__u64) ht;
135 | z__HashInt_new(&ht);
136 |
137 | for (size_t i = 0; i < length; i++) {
138 | z__HashInt_set(&ht, pairs[i].key, pairs[i].value);
139 | }
140 |
141 | for (size_t i = 0; i < length; i++) {
142 | z__u64 *value = NULL;
143 | z__HashInt_getreff(&ht, pairs[i].key, &value);
144 | z__test_assert(value, "value is null, for key `%i`", pairs[i].key);
145 | z__test_assert(*value == pairs[i].value, "{k(%i), v(%"PRIu64")} -> %" PRIu64, pairs[i].key, pairs[i].value, *value);
146 | }
147 |
148 | z__test_assert(1, "");
149 | z__test_done();
150 | }
151 |
152 | z__test_impl(TEST_SUITE, hashstr, hashint, reset);
153 |
154 |
--------------------------------------------------------------------------------
/src/testlib/types/obj.c:
--------------------------------------------------------------------------------
1 | #include "../../lib/obj.h"
2 | #include "../../lib/test.h"
3 |
4 | #define TEST_SUITE types_obj
5 | #define TEST(name) z__test_def(TEST_SUITE, name)
6 |
7 | typedef
8 | struct {
9 | int x, y;
10 | /* Data Related to player */
11 | } Player;
12 |
13 | typedef
14 | struct {
15 | int x, y;
16 | /* Data Related to enemy */
17 | } Enemy;
18 |
19 | static
20 | z__objfn_defcl(Enemy, Actor, Move, void, int x, int y)
21 | {
22 | z__objfn_clself(Enemy);
23 | self->x += x;
24 | self->y += y;
25 | }
26 |
27 | static
28 | z__objfn_defcl(Player, Actor, Move, void, int x, int y)
29 | {
30 | z__objfn_clself(Player);
31 | self->x += x;
32 | self->y += y;
33 | }
34 |
35 | z__objcl(Actor, (), (Move, void, int, int));
36 | z__objcl_impl(Actor, Player, Move);
37 | z__objcl_impl(Actor, Enemy, Move);
38 |
39 | TEST(obj_check)
40 | {
41 | Player p = { .x = 10, .y = 50 };
42 | Enemy e = { .x = 10, .y = 50 };
43 |
44 | Actor actor[2];
45 | z__objcl_set(&actor[0], Actor, &p, Player);
46 | z__objcl_set(&actor[1], Actor, &e, Enemy);
47 |
48 | z__objcl_call(&actor[0], Move, 10, 23);
49 | z__objcl_call(&actor[1], Move, -3, 20);
50 |
51 | z__test_assert((p.x == 20 && p.y == 73), "");
52 | z__test_assert((e.x == 7 && e.y == 70), "");
53 |
54 | z__test_done();
55 | }
56 |
57 | z__test_impl(TEST_SUITE, obj_check);
58 |
59 |
--------------------------------------------------------------------------------
/src/testlib/types/string.c:
--------------------------------------------------------------------------------
1 | #include "../../lib/string.h"
2 | #include "../../lib/test.h"
3 |
4 | #include
5 |
6 | #define TEST(name) z__test_def(string, name)
7 |
8 | TEST(new_and_replace_and_append) {
9 | /* Basic C (Null Terminated String) */
10 | const char *name = "Jason";
11 |
12 | /* Creating a Basic z__String using primitive types and strings */
13 | z__String s = z__String("Hello this is", name, "from Street No.", 31);
14 | z__test_assert(strncmp(s.data, "Hello this is Jason from Street No. 31", s.lenUsed) == 0, "Got: %.*s", s.lenUsed, s.data);
15 |
16 | /* Checking Replace */
17 | z__String_replace(&s, "%s left, at %i in the evening", name, 6);
18 | z__test_assert(strncmp(s.data, "Jason left, at 6 in the evening", s.lenUsed) == 0, "Got %s", s.data);
19 |
20 | /* Deleting String Object */
21 | z__String_delete(&s);
22 |
23 |
24 | /* Testing Append */
25 | s = z__String_new(31);
26 | z__String_append_nopad(&s, "Eggs");
27 | z__String_append_nopad(&s, ", Onions,");
28 | z__String_append(&s, 1, ' ', "Potatoes");
29 | z__String_append(&s, 1, ',', " Chilli Powder");
30 | z__String_append(&s, 3, '.', "");
31 |
32 | z__test_assert(strncmp(s.data, "Eggs, Onions, Potatoes, Chilli Powder...", s.lenUsed) == 0, "Got :%s", s.data);
33 | z__String_delete(&s);
34 |
35 | z__test_done();
36 | }
37 |
38 | TEST(split) {
39 | z__String s = z__String(
40 | "1. Oranges\n"
41 | "2. Bananas\n"
42 | "3. Apples\n"
43 | "4. Berries\n"
44 | "5. Melons\n"
45 | "6. Tomatoes\n"
46 | "7. Mytoes\n"
47 | );
48 |
49 | z__StringList list = z__String_splitTok(s, z__Str("\n", 1));
50 | z__test_assert_int(list.lenUsed, 7);
51 | z__test_assert(strcmp(list.data[2], "3. Apples") == 0, "Got: %s", list.data[2]);
52 |
53 | z__String_delete(&s);
54 | z__StringList_delete(&list);
55 |
56 | z__test_done();
57 | }
58 |
59 | TEST(tokens) {
60 | z__String s = z__String(
61 | "Step 1: Run,"
62 | "Step 2: Hide,"
63 | "Step 3: Cower,"
64 | "Step 4: Accept Fate"
65 | );
66 |
67 | z__u32 prev = 0;
68 | z__u32 i = z__String_tok(s, prev, z__Str(",", 1)); // i now points to [Run, <---- Here]
69 | z__test_assert(strncmp(s.data + prev, "Step 1: Run,", i-prev) == 0, "");
70 |
71 | prev = i;
72 | i = z__String_tok(s, prev, z__Str(",", 1));
73 | z__test_assert(strncmp(s.data + prev, "Step 2: Hide,", i-prev) == 0, "");
74 |
75 | prev = i;
76 | i = z__String_tok(s, prev, z__Str("4", 1)); // Something Different...
77 | z__test_assert(strncmp(s.data + prev, "Step 3: Cower,Step 4", i-prev) == 0, "");
78 |
79 | prev = i;
80 | i = z__String_tok(s, prev, z__Str(",", 1));
81 | z__test_assert(strncmp(s.data + prev, ": Accept Fate", i-prev) == 0, "");
82 |
83 | prev = i;
84 | i = z__String_tok(s, prev, z__Str(",", 1));
85 | z__test_assert(strncmp(s.data + prev, "", i-prev) == 0, "");
86 |
87 | z__test_done(
88 | z__String_delete(&s);
89 | );
90 |
91 | }
92 |
93 | TEST(join) {
94 | z__String s = z__String_newFrom("Hello");
95 | z__String s2 = z__String_newFrom(", World!");
96 | z__String s3 = z__String_newFrom("42");
97 |
98 | z__test_assert(s.lenUsed == 5, "Got: %i", s.lenUsed);
99 | z__test_assert(s2.lenUsed == 8, "Got: %i", s2.lenUsed);
100 | z__test_assert(s3.lenUsed == 2, "Got: %i", s3.lenUsed);
101 |
102 | z__String_join(&s, &s2);
103 | z__test_assert(strncmp(s.data, "Hello, World!", s.lenUsed) == 0, "Got: %s|%u", s.data, s.lenUsed);
104 |
105 | z__String_join(&s, &s3);
106 | z__test_assert(strncmp(s.data, "Hello, World!42", s.lenUsed) == 0, "Got: %s|%u", s.data, s.lenUsed);
107 |
108 | z__test_done(
109 | z__String_delete(&s);
110 | z__String_delete(&s2);
111 | z__String_delete(&s3);
112 | );
113 |
114 | }
115 |
116 | z__test_impl(string, new_and_replace_and_append, split, tokens, join);
117 |
118 |
--------------------------------------------------------------------------------