diff options
-rw-r--r-- | .gitignore | 8 | ||||
-rw-r--r-- | example/UTF-8-demo.html | 213 | ||||
-rw-r--r-- | example/arena.c | 11 | ||||
-rwxr-xr-x | example/build.sh | 45 | ||||
-rw-r--r-- | example/macros.c | 110 | ||||
-rw-r--r-- | example/string.c | 41 | ||||
-rw-r--r-- | example/sys.c | 55 | ||||
-rw-r--r-- | prb.h | 38 | ||||
-rw-r--r-- | prb_arena.h | 48 | ||||
-rw-r--r-- | prb_linux.h | 90 | ||||
-rw-r--r-- | prb_macros.h | 54 | ||||
-rw-r--r-- | prb_math.h | 419 | ||||
-rw-r--r-- | prb_string.h | 217 | ||||
-rw-r--r-- | prb_sys.h | 8 | ||||
-rw-r--r-- | prb_types.h | 49 | ||||
-rw-r--r-- | prb_windows.h | 86 |
16 files changed, 117 insertions, 1375 deletions
@@ -1,2 +1,6 @@ -example/example -example/example.exe +example/arena +example/macros +example/string +example/sys +libprb.a +*.o diff --git a/example/UTF-8-demo.html b/example/UTF-8-demo.html deleted file mode 100644 index b8157db..0000000 --- a/example/UTF-8-demo.html +++ /dev/null @@ -1,213 +0,0 @@ -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" - "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> -<html xmlns="http://www.w3.org/1999/xhtml"> - <head><title>UTF-8 test file</title></head> - <body> - <p>Original by Markus Kuhn, adapted for HTML by Martin Dürst.</p> -<pre> -UTF-8 encoded sample plain-text file -‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ - -Markus Kuhn [ˈmaʳkʊs kuːn] <mkuhn@acm.org> — 1999-08-20 - - -The ASCII compatible UTF-8 encoding of ISO 10646 and Unicode -plain-text files is defined in RFC 2279 and in ISO 10646-1 Annex R. - - -Using Unicode/UTF-8, you can write in emails and source code things such as - -Mathematics and Sciences: - - ∮ E⋅da = Q, n → ∞, ∑ f(i) = ∏ g(i), ∀x∈ℝ: ⌈x⌉ = −⌊−x⌋, α ∧ ¬β = ¬(¬α ∨ β), - - ℕ ⊆ ℕ₀ ⊂ ℤ ⊂ ℚ ⊂ ℝ ⊂ ℂ, ⊥ < a ≠ b ≡ c ≤ d ≪ ⊤ ⇒ (A ⇔ B), - - 2H₂ + O₂ ⇌ 2H₂O, R = 4.7 kΩ, ⌀ 200 mm - -Linguistics and dictionaries: - - ði ıntəˈnæʃənəl fəˈnɛtık əsoʊsiˈeıʃn - Y [ˈʏpsilɔn], Yen [jɛn], Yoga [ˈjoːgɑ] - -APL: - - ((V⍳V)=⍳⍴V)/V←,V ⌷←⍳→⍴∆∇⊃‾⍎⍕⌈ - -Nicer typography in plain text files: - - ╔══════════════════════════════════════════╗ - ║ ║ - ║ • ‘single’ and “double” quotes ║ - ║ ║ - ║ • Curly apostrophes: “We’ve been here” ║ - ║ ║ - ║ • Latin-1 apostrophe and accents: '´` ║ - ║ ║ - ║ • ‚deutsche‘ „Anführungszeichen“ ║ - ║ ║ - ║ • †, ‡, ‰, •, 3–4, —, −5/+5, ™, … ║ - ║ ║ - ║ • ASCII safety test: 1lI|, 0OD, 8B ║ - ║ ╭─────────╮ ║ - ║ • the euro symbol: │ 14.95 € │ ║ - ║ ╰─────────╯ ║ - ╚══════════════════════════════════════════╝ - -Greek (in Polytonic): - - The Greek anthem: - - Σὲ γνωρίζω ἀπὸ τὴν κόψη - τοῦ σπαθιοῦ τὴν τρομερή, - σὲ γνωρίζω ἀπὸ τὴν ὄψη - ποὺ μὲ βία μετράει τὴ γῆ. - - ᾿Απ᾿ τὰ κόκκαλα βγαλμένη - τῶν ῾Ελλήνων τὰ ἱερά - καὶ σὰν πρῶτα ἀνδρειωμένη - χαῖρε, ὦ χαῖρε, ᾿Ελευθεριά! - - From a speech of Demosthenes in the 4th century BC: - - Οὐχὶ ταὐτὰ παρίσταταί μοι γιγνώσκειν, ὦ ἄνδρες ᾿Αθηναῖοι, - ὅταν τ᾿ εἰς τὰ πράγματα ἀποβλέψω καὶ ὅταν πρὸς τοὺς - λόγους οὓς ἀκούω· τοὺς μὲν γὰρ λόγους περὶ τοῦ - τιμωρήσασθαι Φίλιππον ὁρῶ γιγνομένους, τὰ δὲ πράγματ᾿ - εἰς τοῦτο προήκοντα, ὥσθ᾿ ὅπως μὴ πεισόμεθ᾿ αὐτοὶ - πρότερον κακῶς σκέψασθαι δέον. οὐδέν οὖν ἄλλο μοι δοκοῦσιν - οἱ τὰ τοιαῦτα λέγοντες ἢ τὴν ὑπόθεσιν, περὶ ἧς βουλεύεσθαι, - οὐχὶ τὴν οὖσαν παριστάντες ὑμῖν ἁμαρτάνειν. ἐγὼ δέ, ὅτι μέν - ποτ᾿ ἐξῆν τῇ πόλει καὶ τὰ αὑτῆς ἔχειν ἀσφαλῶς καὶ Φίλιππον - τιμωρήσασθαι, καὶ μάλ᾿ ἀκριβῶς οἶδα· ἐπ᾿ ἐμοῦ γάρ, οὐ πάλαι - γέγονεν ταῦτ᾿ ἀμφότερα· νῦν μέντοι πέπεισμαι τοῦθ᾿ ἱκανὸν - προλαβεῖν ἡμῖν εἶναι τὴν πρώτην, ὅπως τοὺς συμμάχους - σώσομεν. ἐὰν γὰρ τοῦτο βεβαίως ὑπάρξῃ, τότε καὶ περὶ τοῦ - τίνα τιμωρήσεταί τις καὶ ὃν τρόπον ἐξέσται σκοπεῖν· πρὶν δὲ - τὴν ἀρχὴν ὀρθῶς ὑποθέσθαι, μάταιον ἡγοῦμαι περὶ τῆς - τελευτῆς ὁντινοῦν ποιεῖσθαι λόγον. - - Δημοσθένους, Γ´ ᾿Ολυνθιακὸς - -Georgian: - - From a Unicode conference invitation: - - გთხოვთ ახლავე გაიაროთ რეგისტრაცია Unicode-ის მეათე საერთაშორისო - კონფერენციაზე დასასწრებად, რომელიც გაიმართება 10-12 მარტს, - ქ. მაინცში, გერმანიაში. კონფერენცია შეჰკრებს ერთად მსოფლიოს - ექსპერტებს ისეთ დარგებში როგორიცაა ინტერნეტი და Unicode-ი, - ინტერნაციონალიზაცია და ლოკალიზაცია, Unicode-ის გამოყენება - ოპერაციულ სისტემებსა, და გამოყენებით პროგრამებში, შრიფტებში, - ტექსტების დამუშავებასა და მრავალენოვან კომპიუტერულ სისტემებში. - -Russian: - - From a Unicode conference invitation: - - Зарегистрируйтесь сейчас на Десятую Международную Конференцию по - Unicode, которая состоится 10-12 марта 1997 года в Майнце в Германии. - Конференция соберет широкий круг экспертов по вопросам глобального - Интернета и Unicode, локализации и интернационализации, воплощению и - применению Unicode в различных операционных системах и программных - приложениях, шрифтах, верстке и многоязычных компьютерных системах. - -Thai (UCS Level 2): - - Excerpt from a poetry on The Romance of The Three Kingdoms (a Chinese - classic 'San Gua'): - - [----------------------------|------------------------] - ๏ แผ่นดินฮั่นเสื่อมโทรมแสนสังเวช พระปกเกศกองบู๊กู้ขึ้นใหม่ - สิบสองกษัตริย์ก่อนหน้าแลถัดไป สององค์ไซร้โง่เขลาเบาปัญญา - ทรงนับถือขันทีเป็นที่พึ่ง บ้านเมืองจึงวิปริตเป็นนักหนา - โฮจิ๋นเรียกทัพทั่วหัวเมืองมา หมายจะฆ่ามดชั่วตัวสำคัญ - เหมือนขับไสไล่เสือจากเคหา รับหมาป่าเข้ามาเลยอาสัญ - ฝ่ายอ้องอุ้นยุแยกให้แตกกัน ใช้สาวนั้นเป็นชนวนชื่นชวนใจ - พลันลิฉุยกุยกีกลับก่อเหตุ ช่างอาเพศจริงหนาฟ้าร้องไห้ - ต้องรบราฆ่าฟันจนบรรลัย ฤๅหาใครค้ำชูกู้บรรลังก์ ฯ - - (The above is a two-column text. If combining characters are handled - correctly, the lines of the second column should be aligned with the - | character above.) - -Ethiopian: - - Proverbs in the Amharic language: - - ሰማይ አይታረስ ንጉሥ አይከሰስ። - ብላ ካለኝ እንደአባቴ በቆመጠኝ። - ጌጥ ያለቤቱ ቁምጥና ነው። - ደሀ በሕልሙ ቅቤ ባይጠጣ ንጣት በገደለው። - የአፍ ወለምታ በቅቤ አይታሽም። - አይጥ በበላ ዳዋ ተመታ። - ሲተረጉሙ ይደረግሙ። - ቀስ በቀስ፥ ዕንቁላል በእግሩ ይሄዳል። - ድር ቢያብር አንበሳ ያስር። - ሰው እንደቤቱ እንጅ እንደ ጉረቤቱ አይተዳደርም። - እግዜር የከፈተውን ጉሮሮ ሳይዘጋው አይድርም። - የጎረቤት ሌባ፥ ቢያዩት ይስቅ ባያዩት ያጠልቅ። - ሥራ ከመፍታት ልጄን ላፋታት። - ዓባይ ማደሪያ የለው፥ ግንድ ይዞ ይዞራል። - የእስላም አገሩ መካ የአሞራ አገሩ ዋርካ። - ተንጋሎ ቢተፉ ተመልሶ ባፉ። - ወዳጅህ ማር ቢሆን ጨርስህ አትላሰው። - እግርህን በፍራሽህ ልክ ዘርጋ። - -Runes: - - ᚻᛖ ᚳᚹᚫᚦ ᚦᚫᛏ ᚻᛖ ᛒᚢᛞᛖ ᚩᚾ ᚦᚫᛗ ᛚᚪᚾᛞᛖ ᚾᚩᚱᚦᚹᛖᚪᚱᛞᚢᛗ ᚹᛁᚦ ᚦᚪ ᚹᛖᛥᚫ - - (Old English, which transcribed into Latin reads 'He cwaeth that he - bude thaem lande northweardum with tha Westsae.' and means 'He said - that he lived in the northern land near the Western Sea.') - -Braille: - - ⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌ - - ⡍⠜⠇⠑⠹ ⠺⠁⠎ ⠙⠑⠁⠙⠒ ⠞⠕ ⠃⠑⠛⠔ ⠺⠊⠹⠲ ⡹⠻⠑ ⠊⠎ ⠝⠕ ⠙⠳⠃⠞ - ⠱⠁⠞⠑⠧⠻ ⠁⠃⠳⠞ ⠹⠁⠞⠲ ⡹⠑ ⠗⠑⠛⠊⠌⠻ ⠕⠋ ⠙⠊⠎ ⠃⠥⠗⠊⠁⠇ ⠺⠁⠎ - ⠎⠊⠛⠝⠫ ⠃⠹ ⠹⠑ ⠊⠇⠻⠛⠹⠍⠁⠝⠂ ⠹⠑ ⠊⠇⠻⠅⠂ ⠹⠑ ⠥⠝⠙⠻⠞⠁⠅⠻⠂ - ⠁⠝⠙ ⠹⠑ ⠡⠊⠑⠋ ⠍⠳⠗⠝⠻⠲ ⡎⠊⠗⠕⠕⠛⠑ ⠎⠊⠛⠝⠫ ⠊⠞⠲ ⡁⠝⠙ - ⡎⠊⠗⠕⠕⠛⠑⠰⠎ ⠝⠁⠍⠑ ⠺⠁⠎ ⠛⠕⠕⠙ ⠥⠏⠕⠝ ⠰⡡⠁⠝⠛⠑⠂ ⠋⠕⠗ ⠁⠝⠹⠹⠔⠛ ⠙⠑ - ⠡⠕⠎⠑ ⠞⠕ ⠏⠥⠞ ⠙⠊⠎ ⠙⠁⠝⠙ ⠞⠕⠲ - - ⡕⠇⠙ ⡍⠜⠇⠑⠹ ⠺⠁⠎ ⠁⠎ ⠙⠑⠁⠙ ⠁⠎ ⠁ ⠙⠕⠕⠗⠤⠝⠁⠊⠇⠲ - - ⡍⠔⠙⠖ ⡊ ⠙⠕⠝⠰⠞ ⠍⠑⠁⠝ ⠞⠕ ⠎⠁⠹ ⠹⠁⠞ ⡊ ⠅⠝⠪⠂ ⠕⠋ ⠍⠹ - ⠪⠝ ⠅⠝⠪⠇⠫⠛⠑⠂ ⠱⠁⠞ ⠹⠻⠑ ⠊⠎ ⠏⠜⠞⠊⠊⠥⠇⠜⠇⠹ ⠙⠑⠁⠙ ⠁⠃⠳⠞ - ⠁ ⠙⠕⠕⠗⠤⠝⠁⠊⠇⠲ ⡊ ⠍⠊⠣⠞ ⠙⠁⠧⠑ ⠃⠑⠲ ⠔⠊⠇⠔⠫⠂ ⠍⠹⠎⠑⠇⠋⠂ ⠞⠕ - ⠗⠑⠛⠜⠙ ⠁ ⠊⠕⠋⠋⠔⠤⠝⠁⠊⠇ ⠁⠎ ⠹⠑ ⠙⠑⠁⠙⠑⠌ ⠏⠊⠑⠊⠑ ⠕⠋ ⠊⠗⠕⠝⠍⠕⠝⠛⠻⠹ - ⠔ ⠹⠑ ⠞⠗⠁⠙⠑⠲ ⡃⠥⠞ ⠹⠑ ⠺⠊⠎⠙⠕⠍ ⠕⠋ ⠳⠗ ⠁⠝⠊⠑⠌⠕⠗⠎ - ⠊⠎ ⠔ ⠹⠑ ⠎⠊⠍⠊⠇⠑⠆ ⠁⠝⠙ ⠍⠹ ⠥⠝⠙⠁⠇⠇⠪⠫ ⠙⠁⠝⠙⠎ - ⠩⠁⠇⠇ ⠝⠕⠞ ⠙⠊⠌⠥⠗⠃ ⠊⠞⠂ ⠕⠗ ⠹⠑ ⡊⠳⠝⠞⠗⠹⠰⠎ ⠙⠕⠝⠑ ⠋⠕⠗⠲ ⡹⠳ - ⠺⠊⠇⠇ ⠹⠻⠑⠋⠕⠗⠑ ⠏⠻⠍⠊⠞ ⠍⠑ ⠞⠕ ⠗⠑⠏⠑⠁⠞⠂ ⠑⠍⠏⠙⠁⠞⠊⠊⠁⠇⠇⠹⠂ ⠹⠁⠞ - ⡍⠜⠇⠑⠹ ⠺⠁⠎ ⠁⠎ ⠙⠑⠁⠙ ⠁⠎ ⠁ ⠙⠕⠕⠗⠤⠝⠁⠊⠇⠲ - - (The first couple of paragraphs of "A Christmas Carol" by Dickens) - -Compact font selection example text: - - ABCDEFGHIJKLMNOPQRSTUVWXYZ /0123456789 - abcdefghijklmnopqrstuvwxyz £©µÀÆÖÞßéöÿ - –—‘“”„†•…‰™œŠŸž€ ΑΒΓΔΩαβγδω АБВГДабвгд - ∀∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi�⑀₂ἠḂӥẄɐː⍎אԱა - -Greetings in various languages: - - Hello world, Καλημέρα κόσμε, コンニチハ - -Box drawing alignment tests: █ - ▉ - ╔══╦══╗ ┌──┬──┐ ╭──┬──╮ ╭──┬──╮ ┏━━┳━━┓ ┎┒┏┑ ╷ ╻ ┏┯┓ ┌┰┐ ▊ ╱╲╱╲╳╳╳ - ║┌─╨─┐║ │╔═╧═╗│ │╒═╪═╕│ │╓─╁─╖│ ┃┌─╂─┐┃ ┗╃╄┙ ╶┼╴╺╋╸┠┼┨ ┝╋┥ ▋ ╲╱╲╱╳╳╳ - ║│╲ ╱│║ │║ ║│ ││ │ ││ │║ ┃ ║│ ┃│ ╿ │┃ ┍╅╆┓ ╵ ╹ ┗┷┛ └┸┘ ▌ ╱╲╱╲╳╳╳ - ╠╡ ╳ ╞╣ ├╢ ╟┤ ├┼─┼─┼┤ ├╫─╂─╫┤ ┣┿╾┼╼┿┫ ┕┛┖┚ ┌┄┄┐ ╎ ┏┅┅┓ ┋ ▍ ╲╱╲╱╳╳╳ - ║│╱ ╲│║ │║ ║│ ││ │ ││ │║ ┃ ║│ ┃│ ╽ │┃ ░░▒▒▓▓██ ┊ ┆ ╎ ╏ ┇ ┋ ▎ - ║└─╥─┘║ │╚═╤═╝│ │╘═╪═╛│ │╙─╀─╜│ ┃└─╂─┘┃ ░░▒▒▓▓██ ┊ ┆ ╎ ╏ ┇ ┋ ▏ - ╚══╩══╝ └──┴──┘ ╰──┴──╯ ╰──┴──╯ ┗━━┻━━┛ └╌╌┘ ╎ ┗╍╍┛ ┋ ▁▂▃▄▅▆▇█ - -</pre> -</body> -</html> diff --git a/example/arena.c b/example/arena.c index addf4ee..6579849 100644 --- a/example/arena.c +++ b/example/arena.c @@ -1,14 +1,15 @@ #include "prb.h" +#include <string.h> +#include <stdio.h> i32 main(void) { - printf("Arenas:\n"); - - arena_t arena = alloc_arena(10); + info("arenas"); + struct arena arena = alloc_arena(10); u8 *first = push_arena(&arena, 5); - prb_memset(first, 5, 5); + memset(first, 5, 5); u8 *second = push_arena(&arena, 5); - prb_memset(second, 10, 5); + memset(second, 10, 5); printf("|"); for (i32 i = 0; i < 10; i++) printf("%x%s", first[i], ((i+1) % 5 == 0) ? "|" : " "); diff --git a/example/build.sh b/example/build.sh index e6cee7c..285b072 100755 --- a/example/build.sh +++ b/example/build.sh @@ -1,22 +1,31 @@ #!/bin/sh -compiler='gcc' -path=`dirname $0` -cd $path -if [ $# -eq 1 ] ; then - if [ $1 = 'windows' ] ; then - compiler='x86_64-w64-mingw32-gcc' - fi - if [ $1 = 'clean' ] ; then - set -x - rm -f macros macros.exe arena arena.exe sys sys.exe string string.exe - exit - fi +start=`pwd` +dir=`dirname "$0"` +if [ "$1" = 'clean' ] ; then + set -x + cd "$dir" + rm -f macros macros.exe arena arena.exe sys sys.exe string string.exe + cd "$start" + exit fi +cc='tcc' +if [ "$1" = 'windows' ] ; then + cc='x86_64-w64-mingw32-gcc' +fi +libsdir='../..' +if [ ! -d "$libsdir/prb" ] ; then + echo "error: failed to get prb library path" + exit +fi +"$libsdir/prb/build.sh" cflags='-g -Wall' -include='-I..' -libs='-lm' +include="-I$libsdir/prb" +lflags="-L$libsdir/prb" +libs='-lprb' set -x -$compiler $cflags $include -o macros macros.c $libs -$compiler $cflags $include -o arena arena.c $libs -$compiler $cflags $include -o sys sys.c $libs -$compiler $cflags $include -o string string.c $libs +cd "$dir" +$cc $cflags $include $lflags -o macros macros.c $libs +$cc $cflags $include $lflags -o arena arena.c $libs +$cc $cflags $include $lflags -o sys sys.c $libs +$cc $cflags $include $lflags -o string string.c $libs +cd "$start" diff --git a/example/macros.c b/example/macros.c index 169a2ca..0e836b0 100644 --- a/example/macros.c +++ b/example/macros.c @@ -1,96 +1,44 @@ #include "prb.h" +#include <stdio.h> -typedef struct { - i32 i32_element; - i16 i16_element; - u8 u8_element; -} type_t; +struct type { + i32 i32_element; + i16 i16_element; + u8 u8_element; +}; -#define MAX_NODES 10 +#define nodes_count 10 -typedef struct stack_node_t { - i32 value; - struct stack_node_t *next; -} stack_node_t; +struct stack_node { + i32 value; + struct stack_node *next; +}; -typedef struct { - stack_node_t *first; - stack_node_t *last; -} stack_t; +struct stack { + struct stack_node *first; + struct stack_node *last; +}; -typedef struct node_t { - i32 value; - struct node_t *next; - struct node_t *prev; -} node_t; - -typedef struct { - node_t *first; - node_t *last; -} list_t; +static void print_stack(struct stack s) +{ + for (struct stack_node *node = s.first; node; node = node->next) + printf("%d%s", node->value, ((node->next) ? " -> " : "\n")); +} i32 main(void) { - u64 offset = offsetof(type_t, u8_element); + u64 offset = offsetof(struct type, u8_element); assert(offset == 6); - printf("offsetof(type_t, u8_element) = %lu\n", offset); - i32 clamped = clamp(0, -1, 1); - assert(clamped == 0); - printf("clamp(0, -1, 1) = %d\n", clamped); - clamped = clamp(0, 2, 1); - assert(clamped == 1); - printf("clamp(0, 2, 1) = %d\n", clamped); - - i32 nums[] = {0, 1, 2, 3}; - printf("mem0: ["); - for (i32 i = 0; i < array_count(nums); ++i) - printf("%x%s", nums[i], (i == array_count(nums)-1) ? "" : ", "); - memzero(nums, sizeof(nums)); - printf("] -> ["); - for (i32 i = 0; i < array_count(nums); ++i) { - assert(!nums[i]); - printf("%x%s", nums[i], (i == array_count(nums)-1) ? "" : ", "); - } - printf("]\n\n"); - - printf("Singly linked list (stack for example):\n"); - - stack_node_t stack_nodes[MAX_NODES]; - stack_t stack = {0}; - for (i32 i = 0; i < MAX_NODES; ++i) { - stack_node_t *node = stack_nodes+i; + info("offsetof(type_t, u8_element) = %lu", offset); + info("stack:"); + struct stack_node nodes[nodes_count]; + struct stack stack = {0}; + for (i32 i = 0; i < array_count(nodes); ++i) { + struct stack_node *node = nodes + i; node->value = i; sllpush(stack.first, stack.last, node); } - - for (stack_node_t *node = stack.first; node; node = node->next) - printf("%d%s", node->value, ((node->next) ? " -> " : "\n")); - + print_stack(stack); sllpop(stack.first, stack.last); - - printf("After pop:\n"); - for (stack_node_t *node = stack.first; node; node = node->next) - printf("%d%s", node->value, ((node->next) ? " -> " : "\n")); - - node_t nodes[MAX_NODES]; - list_t list = {0}; - for (i32 i = 0; i < MAX_NODES; ++i) { - node_t *node = nodes+i; - node->value = i; - dllpushback(list.first, list.last, node); - } - - printf("Doubly linked list:\n"); - for (node_t *node = list.first; node; node = node->next) - printf("%d%s", node->value, ((node->next) ? " -> " : "\n")); - - printf("Remove odd numbers:\n"); - for (node_t *node = list.first; node; node = node->next) - if (node->value % 2 == 1) - dllremove(list.first, list.last, node); - - for (node_t *node = list.first; node; node = node->next) - printf("%d%s", node->value, ((node->next) ? " -> " : "\n")); - - printf("\n"); + print_stack(stack); } diff --git a/example/string.c b/example/string.c index fbd0943..57c460d 100644 --- a/example/string.c +++ b/example/string.c @@ -2,33 +2,18 @@ i32 main(void) { - printf("Strings:\n"); - - arena_t str_arena = alloc_arena(0); - str8_list_t strlist = {0}; - str8_list_push(&str_arena, &strlist, 0, str8lit("first ")); - str8_list_push(&str_arena, &strlist, 0, str8lit("second ")); - str8_list_push(&str_arena, &strlist, 0, str8lit("third\n")); - str8_print(str8lit("strlist: ")); - str8_list_print(&strlist); - - str8_t fstr = str8_pushf(&str_arena, "Formatted string: %d\n", 69); - str8_print(fstr); - - memzero_struct(&strlist); - str8_list_pushf(&str_arena, &strlist, 0, "first %d -> ", 34); - str8_list_pushf(&str_arena, &strlist, 0, "second: %d -> ", 35); - str8_list_pushf(&str_arena, &strlist, 0, "sum: %d\n", 34+35); - str8_list_pushf(&str_arena, &strlist, 1, "%s", "To the front -> "); - - str8_print(str8lit("Formatted string list:\n")); - str8_list_print(&strlist); - - str8_t catstr = str8_list_join(&str_arena, &strlist); - str8_print(str8lit("Concatenated string list:\n")); - str8_print(catstr); - - release_arena(&str_arena); - + struct arena arena = alloc_arena(0); + struct string fstr = push_fstring(&arena, "formatted string: %d\n", 69); + print_string(fstr); + info("string list:"); + struct string_list list = {0}; + push_fstring_to_list(&arena, &list, 0, "first %d -> ", 34); + push_fstring_to_list(&arena, &list, 0, "second: %d -> ", 35); + push_fstring_to_list(&arena, &list, 0, "sum: %d\n", 34+35); + push_fstring_to_list(&arena, &list, 1, "%s", "To the front -> "); + print_string_list(&list); + struct string catstr = join_string_list(&arena, &list); + info("Concatenated string list:"); + print_string(catstr); return(0); } diff --git a/example/sys.c b/example/sys.c index f9f5c33..881d2be 100644 --- a/example/sys.c +++ b/example/sys.c @@ -1,43 +1,30 @@ #include "prb.h" +#include <libgen.h> +#include <stdio.h> -i32 main(void) +i32 main(int c, char **v) { - void *mem; - arena_t arena; - char *dir, *content, *path; - const char *filename = "build.sh"; - - printf("os: "); - switch (OS) { - case OS_LINUX: - printf("linux\n"); + info("os:"); + switch (os) { + case os_linux: + info("linux"); break; - case OS_WINDOWS: - printf("windows\n"); + case os_windows: + info("windows"); break; default: - printf("unsupported\n"); + info("unsupported"); return 1; } - - mem = sys_alloc(megabytes(32)); - assert(mem); - sys_free(mem, megabytes(32)); - - arena = alloc_arena(megabytes(32)); - - dir = sys_getbindir(&arena); - printf("bin directory \"%s\"\n", dir); - - path = push_arena(&arena, MAX_PATH); - memset(path, 0, MAX_PATH); - snprintf(path, MAX_PATH, "%s/%s", dir, filename); - - if (sys_read_file(&arena, &content, path)) - printf("%s content:\n%s", path, content); - else - printf("failed to read %s\n", path); - - if (!sys_read_file(&arena, &content, filename)) - die("failed to read %s\n", filename); + char *dir = dirname(*v); + info("bin directory \"%s\"", dir); + const char *filename = "build.sh"; + char path[1024] = {0}; + snprintf(path, 1024, "%s/%s", dir, filename); + struct arena arena = alloc_arena(megabytes(32)); + char *content = 0; + if (!(content = read_entire_file(&arena, 0, path))) + die("failed to read %s", path); + info("%s content:\n%s", path, content); + return 0; } @@ -1,30 +1,22 @@ -#ifndef PRB_H -#define PRB_H +#ifndef prb_h +#define prb_h -#include <stdint.h> -#include <math.h> -#include <stdarg.h> -#include <stddef.h> -#include <stdio.h> +#include "types.h" +#include "macros.h" +#include "sys.h" +#include "arena.h" +#include "prbm.h" +#include "prbs.h" -#include "prb_types.h" -#include "prb_macros.h" -#include "prb_math.h" -#include "prb_arena.h" -#include "prb_sys.h" -#include "prb_string.h" - -#define OS_NONE 0 -#define OS_WINDOWS 1 -#define OS_LINUX 2 +#define os_none 0 +#define os_windows 1 +#define os_linux 2 #if defined(__linux) -#define OS OS_LINUX -#include "prb_linux.h" +# define os os_linux #elif defined(__WIN64) -#define OS OS_WINDOWS -#include <windows.h> -#include "prb_windows.h" +# define os os_windows +# include <windows.h> #endif -#endif /* PRB_H */ +#endif diff --git a/prb_arena.h b/prb_arena.h deleted file mode 100644 index 0a6de9b..0000000 --- a/prb_arena.h +++ /dev/null @@ -1,48 +0,0 @@ -#define DEFAULT_ARENA_SIZE kilobytes(4) - -arena_t alloc_arena(u64 capacity) -{ - void *sys_alloc(u64 length); - - /* TODO(pryazha): Find reasonable maximum capacity through testing */ - assert(capacity <= (u64)gigabytes(16)); - - if (!capacity) - capacity = DEFAULT_ARENA_SIZE; - - /* TODO(pryazha): Use OS specific memory allocator - * (like VirtualAlloc on Windows or mmap on Linux) - */ - void *memory = sys_alloc(capacity); - assert(memory); - - arena_t arena = {memory, capacity, 0}; - - return arena; -} - -void release_arena(arena_t *arena) -{ - void sys_free(void *memory, u64 length); - - assert(arena); - sys_free(arena->memory, arena->capacity); - arena->memory = 0; - arena->capacity = 0; - arena->used = 0; -} - -void *push_arena(arena_t *arena, u64 size) -{ - assert(arena); - assert(arena->used + size <= arena->capacity); - void *memory = arena->memory + arena->used; - arena->used += size; - return memory; -} - -void pop_arena(arena_t *arena, u64 size) -{ - assert(arena); - arena->used -= min(size, arena->used); -} diff --git a/prb_linux.h b/prb_linux.h deleted file mode 100644 index 64485d6..0000000 --- a/prb_linux.h +++ /dev/null @@ -1,90 +0,0 @@ -#include <sys/mman.h> -#include <stdio.h> -#include <unistd.h> -#include <string.h> - -void *sys_alloc(u64 length) -{ - assert(length); - void *result = mmap(0, length, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); - return result; -} - -void sys_free(void *memory, u64 length) -{ - assert(memory); - assert(length); - munmap(memory, length); -} - -void die(const char *error_string, ...) -{ - va_list args; - va_start(args, error_string); - fprintf(stderr, "error: "); - vfprintf(stderr, error_string, args); - fprintf(stderr, "\n"); - va_end(args); - _exit(1); -} - -void info(const char *format, ...) -{ - va_list args; - va_start(args, format); - printf("info: "); - vprintf(format, args); - printf("\n"); - va_end(args); -} - -u64 sys_read_file(arena_t *arena, char **buffer, const char *filename) -{ - FILE *file = fopen(filename, "rb"); - if (!file) - return 0; - - if (fseek(file, 0, SEEK_END) == -1) - goto error; - long len = ftell(file); - if (!len || (len == -1)) - goto error; - if (fseek(file, 0, SEEK_SET) == -1) - goto error; - - *buffer = push_arena(arena, len + 1); - if (!fread(*buffer, 1, len, file)) { - pop_arena(arena, len + 1); - *buffer = 0; - goto error; - } - (*buffer)[len] = 0; - fclose(file); - - return len; -error: - fclose(file); - *buffer = 0; - return 0; -} - -char *sys_getbindir(arena_t *arena) -{ - char path[MAX_PATH]; - i64 len = readlink("/proc/self/exe", path, MAX_PATH - 1); - if (len <= 0) - return 0; - - path[len] = 0; - - char *dir = strrchr(path, '/'); - assert(dir); - assert(dir > path); - - len = dir - path; - dir = push_arena(arena, len + 1); - prb_memmove(dir, path, len); - dir[len] = 0; - - return dir; -} diff --git a/prb_macros.h b/prb_macros.h deleted file mode 100644 index 8324919..0000000 --- a/prb_macros.h +++ /dev/null @@ -1,54 +0,0 @@ -/* -#ifndef assert -#define assert(expr) if (!(expr)) { *(int *)0 = 0; } -#endif -*/ -#include <assert.h> - -#define array_count(array) (sizeof(array)/sizeof(*(array))) - -#ifndef offsetof -#define offsetof(type, element) ((size_t)(&(((type *)0)->element))) -#endif - -#define min(a, b) (((a) < (b)) ? (a) : (b)) -#define max(a, b) (((a) > (b)) ? (a) : (b)) - -#define clamp(a, x, b) \ - (((x) < (a)) ? (a) : \ - (((x) > (b)) ? (b) : (x))) - -#define swap(type, a, b) { type tmp = (a); (a) = (b); (b) = tmp; } - -/* NOTE(pryazha): Memory */ -#define memzero(ptr, size) prb_memset((ptr), 0, (size)) -#define memzero_struct(ptr) memzero((ptr), sizeof(*(ptr))) - -#define kilobytes(n) n*1024 -#define megabytes(n) kilobytes(n)*1024 -#define gigabytes(n) megabytes(n)*1024 - -/* NOTE(pryazha): Singly linked list */ -#define sllpush(first, last, node) \ - ((first) == 0 ? \ - ((first) = (last) = (node), (node)->next = 0) : \ - ((last)->next = (node), (last) = (node), (node)->next = 0)) -#define sllpop(first, last) \ - ((first) == (last) ? \ - ((first) = (last) = 0) : \ - ((first) = (first)->next)) - -/* NOTE(pryazha): Doubly linked list */ -#define dllpushback_np(first, last, node, next, prev) \ - ((first) == 0 ? \ - ((first) = (last) = (node), (node)->next = (node)->prev = 0) : \ - ((node)->prev = (last), (last)->next = (node), (last) = (node), (node)->next = 0)) -#define dllpushback(first, last, node) dllpushback_np(first, last, node, next, prev) -#define dllpushfront(first, last, node) dllpushback_np(last, first, node, prev, next) - -#define dllremove(first, last, node) \ - ((first) == (node) ? \ - ((first) == (last) ? (first) = (last) = 0 : \ - ((first) = (first)->next, (first)->prev = 0)) : \ - ((last) == (node) ? ((last) = (last)->prev, (last)->next = 0) : \ - ((node)->next->prev = (node)->prev, (node)->prev->next = (node)->next))) diff --git a/prb_math.h b/prb_math.h deleted file mode 100644 index 4e4a378..0000000 --- a/prb_math.h +++ /dev/null @@ -1,419 +0,0 @@ -#define V2_ZERO (v2){ 0.0f, 0.0f} -#define V2_ONE (v2){ 1.0f, 1.0f} -#define V2_RIGHT (v2){ 1.0f, 0.0f} -#define V2_UP (v2){ 0.0f, 1.0f} -#define V2_LEFT (v2){-1.0f, 0.0f} -#define V2_DOWN (v2){ 0.0f, -1.0f } - -#define V3_ZERO (v3){ 0.0f, 0.0f, 0.0f} -#define V3_ONE (v3){ 1.0f, 1.0f, 1.0f} -#define V3_RIGHT (v3){ 1.0f, 0.0f, 0.0f} -#define V3_UP (v3){ 0.0f, 1.0f, 0.0f} -#define V3_LEFT (v3){-1.0f, 0.0f, 0.0f} -#define V3_DOWN (v3){ 0.0f, -1.0f, 0.0f} -#define V3_FORWARD (v3){ 0.0f, 0.0f, 1.0f} -#define V3_BACKWARD (v3){ 0.0f, 0.0f, -1.0f} - -#define V4_ZERO (v4){0.0f, 0.0f, 0.0f, 0.0f} -#define V4_ONE (v4){1.0f, 1.0f, 1.0f, 1.0f} - -#define MAT4_IDENTITY (mat4){ \ - {1.0f, 0.0f, 0.0f, 0.0f}, \ - {0.0f, 1.0f, 0.0f, 0.0f}, \ - {0.0f, 0.0f, 1.0f, 0.0f}, \ - {0.0f, 0.0f, 0.0f, 1.0f}} - -#define F_PI 3.14159265359f - -#define deg2rad(angle) (F_PI/180.0f*(angle)) - -#define QUAT_IDENTITY (v4){0.0f, 0.0f, 0.0f, 1.0f} - -// vectors -v2 v2_fill(f32 x) -{ - v2 v = {x, x}; - return v; -} - -v2 v2_inv(v2 a) -{ - v2 v = {-a.x, -a.y}; - return v; -} - -v2 v2_add(v2 a, v2 b) -{ - v2 v = {a.x+b.x, a.y+b.y}; - return v; -} - -v2 v2_sub(v2 a, v2 b) -{ - v2 v = {a.x-b.x, a.y-b.y}; - return v; -} - -v2 v2_scalef(v2 a, f32 s) -{ - v2 v = {a.x*s, a.y*s}; - return v; -} - -v2 v2_scale(v2 a, v2 s) -{ - v2 v = {a.x*s.x, a.y*s.y}; - return v; -} - -f32 v2_dot(v2 a, v2 b) -{ - f32 v = a.x*b.x+a.y*b.y; - return v; -} - -f32 v2_len2(v2 a) -{ - f32 v = v2_dot(a, a); - return v; -} - -f32 v2_len(v2 a) -{ - f32 v = sqrtf(v2_len2(a)); - return v; -} - -v2 v2_norm(v2 a) -{ - v2 v = {0}; - f32 len = v2_len(a); - if (len) - v = (v2){a.x/len, a.y/len}; - return v; -} - -v3 v3_fill(f32 x) -{ - v3 v = {x, x, x}; - return v; -} - -v3 v3_from_v2(v2 a) -{ - v3 v = {a.x, a.y, 0.0f}; - return v; -} - -v3 v3_from_v4(v4 a) -{ - v3 v = {a.x, a.y, a.z}; - return v; -} - -v3 v3_inv(v3 a) -{ - v3 v = {-a.x, -a.y, -a.z}; - return v; -} - -v3 v3_add(v3 a, v3 b) -{ - v3 v = {a.x+b.x, a.y+b.y, a.z+b.z}; - return v; -} - -v3 v3_sub(v3 a, v3 b) -{ - v3 v = {a.x-b.x, a.y-b.y, a.z-b.z}; - return v; -} - -v3 v3_scalef(v3 a, f32 s) -{ - v3 v = {a.x*s, a.y*s, a.z*s}; - return v; -} - -v3 v3_scale(v3 a, v3 s) -{ - v3 v = {a.x*s.x, a.y*s.y, a.z*s.z}; - return v; -} - -f32 v3_dot(v3 a, v3 b) -{ - f32 v = a.x*b.x+a.y*b.y+a.z*b.z; - return v; -} - -v3 v3_cross(v3 l, v3 r) -{ - v3 v = {(l.y*r.z - r.y*l.z), (r.x*l.z - l.x*r.z), (l.x*r.y - r.x*l.y)}; - return v; -} - -f32 v3_len2(v3 a) -{ - f32 v = v3_dot(a, a); - return v; -} - -f32 v3_len(v3 a) -{ - f32 v = sqrtf(v3_len2(a)); - return v; -} - -v3 v3_norm(v3 a) -{ - v3 v = V3_ZERO; - f32 len = v3_len(a); - if (len) - v = (v3){a.x/len, a.y/len, a.z/len}; - return v; -} - -v4 v4_fill(f32 x) -{ - v4 v = {x, x, x, x}; - return v; -} - -v4 v4_from_v3(v3 a) -{ - v4 v = {a.x, a.y, a.z, 0.0f}; - return v; -} - -v4 v4_inv(v4 a) -{ - v4 v = {-a.x, -a.y, -a.z, -a.w}; - return v; -} - -v4 v4_add(v4 a, v4 b) -{ - v4 v = {a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w}; - return v; -} - -v4 v4_sub(v4 a, v4 b) -{ - v4 v = {a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w}; - return v; -} - -v4 v4_scalef(v4 a, f32 s) -{ - v4 v = {a.x*s, a.y*s, a.z*s, a.w*s}; - return v; -} - -v4 v4_scale(v4 a, v4 s) -{ - v4 v = {a.x*s.x, a.y*s.y, a.z*s.z, a.w*s.w}; - return v; -} - -f32 v4_dot(v4 a, v4 b) -{ - f32 v = a.x*b.x+a.y*b.y+a.z*b.z+a.w*b.w; - return v; -} - -f32 v4_len2(v4 a) -{ - f32 v = v4_dot(a, a); - return v; -} - -f32 v4_len(v4 a) -{ - f32 v = sqrtf(v4_len2(a)); - return v; -} - -v4 v4_norm(v4 a) -{ - v4 v = V4_ZERO; - f32 len = v4_len(a); - if (len) - v = (v4){a.x/len, a.y/len, a.z/len, a.w/len}; - return v; -} - -// matrices -f32 mat4_det(mat4 m) -{ - f32 m00 = m.c0.x, m10 = m.c0.y, m20 = m.c0.z, m30 = m.c0.w; - f32 m01 = m.c1.x, m11 = m.c1.y, m21 = m.c1.z, m31 = m.c1.w; - f32 m02 = m.c2.x, m12 = m.c2.y, m22 = m.c2.z, m32 = m.c2.w; - f32 m03 = m.c3.x, m13 = m.c3.y, m23 = m.c3.z, m33 = m.c3.w; - - f32 m00minor = ((m11*m22*m33)+(m12*m23*m31)+(m21*m32*m13)- - (m31*m22*m13)-(m21*m12*m33)-(m11*m32*m23)); - - f32 m01minor = ((m10*m22*m33)+(m12*m23*m30)+(m20*m32*m13)- - (m13*m22*m30)-(m23*m32*m10)-(m12*m20*m33)); - - f32 m02minor = ((m10*m21*m33)+(m20*m31*m13)+(m11*m23*m31)- - (m13*m21*m30)-(m23*m31*m10)-(m11*m20*m33)); - - f32 m03minor = ((m10*m21*m32)+(m20*m31*m12)+(m11*m22*m30)- - (m12*m21*m30)-(m11*m20*m32)-(m22*m31*m10)); - - f32 result = m00*m00minor+m01*m01minor-m02*m02minor+m03*m03minor; - - return result; -} - -mat4 mat4_transp(mat4 m) -{ - swap(f32, m.c0.y, m.c1.x); - swap(f32, m.c0.z, m.c2.x); - swap(f32, m.c0.w, m.c3.x); - - swap(f32, m.c1.z, m.c2.y); - swap(f32, m.c1.w, m.c3.y); - - swap(f32, m.c2.w, m.c3.z); - - return m; -} - -mat4 mat4_mul(mat4 left, mat4 right) -{ - f32 l00 = left.c0.x, l01 = left.c0.y, l02 = left.c0.z, l03 = left.c0.w; - f32 l10 = left.c1.x, l11 = left.c1.y, l12 = left.c1.z, l13 = left.c1.w; - f32 l20 = left.c2.x, l21 = left.c2.y, l22 = left.c2.z, l23 = left.c2.w; - f32 l30 = left.c3.x, l31 = left.c3.y, l32 = left.c3.z, l33 = left.c3.w; - - f32 r00 = right.c0.x, r01 = right.c0.y, r02 = right.c0.z, r03 = right.c0.w; - f32 r10 = right.c1.x, r11 = right.c1.y, r12 = right.c1.z, r13 = right.c1.w; - f32 r20 = right.c2.x, r21 = right.c2.y, r22 = right.c2.z, r23 = right.c2.w; - f32 r30 = right.c3.x, r31 = right.c3.y, r32 = right.c3.z, r33 = right.c3.w; - - mat4 result = { - { - l00*r00+l10*r01+l20*r02+l30*r03, - l01*r00+l11*r01+l21*r02+l31*r03, - l02*r00+l12*r01+l22*r02+l32*r03, - l03*r00+l13*r01+l23*r02+l33*r03 - }, - { - l00*r10+l10*r11+l20*r12+l30*r13, - l01*r10+l11*r11+l21*r12+l31*r13, - l02*r10+l12*r11+l22*r12+l32*r13, - l03*r10+l13*r11+l23*r12+l33*r13 - }, - { - l00*r20+l10*r21+l20*r22+l30*r23, - l01*r20+l11*r21+l21*r22+l31*r23, - l02*r20+l12*r21+l22*r22+l32*r23, - l03*r20+l13*r21+l23*r22+l33*r23 - }, - { - l00*r30+l10*r31+l20*r32+l30*r33, - l01*r30+l11*r31+l21*r32+l31*r33, - l02*r30+l12*r31+l22*r32+l32*r33, - l03*r30+l13*r31+l23*r32+l33*r33 - } - }; - - return result; -} - -mat4 mat4_make_transl(v3 v) -{ - mat4 translate = { - {1.0f, 0.0f, 0.0f, 0.0f}, - {0.0f, 1.0f, 0.0f, 0.0f}, - {0.0f, 0.0f, 1.0f, 0.0f}, - {v.x, v.y, v.z, 1.0f} - }; - return translate; -} - -mat4 mat4_make_scale(v3 v) -{ - mat4 scale = { - {v.x, 0.0f, 0.0f, 0.0f}, - {0.0f, v.y, 0.0f, 0.0f}, - {0.0f, 0.0f, v.z, 0.0f}, - {0.0f, 0.0f, 0.0f, 1.0f} - }; - return scale; -} - -mat4 mat4_make_rotate(v3 x, v3 y, v3 z) -{ - mat4 rotate = { - {x.x, x.y, x.z, 0.0f}, - {y.x, y.y, y.z, 0.0f}, - {z.x, z.y, z.z, 0.0f}, - {0.0f, 0.0f, 0.0f, 1.0f} - }; - return rotate; -} - -mat4 mat4_transl(mat4 m, v3 v) -{ - mat4 translate = mat4_make_transl(v); - mat4 result = mat4_mul(translate, m); - return result; -} - -mat4 mat4_scale(mat4 m, v3 v) -{ - mat4 scale = mat4_make_scale(v); - mat4 result = mat4_mul(scale, m); - return result; -} - -/* - * NOTE(pryazha): Angles in degrees - * | 1 0 0 | | cy 0 sy | | cz -sz 0 | | cy*cz -cy*sz sy | - * | 0 cx -sx |*| 0 1 0 |*| sz cz 0 |=| sx*sy*cz+cx*sz -sx*sy*sz+cx*cz -sx*cy | - * | 0 sx cx | | -sy 0 cy | | 0 0 1 | | -cx*sy*cz+sx*sz cx*sy*sz+sx*cz cx*cy | - */ -mat4 mat4_rotate(mat4 mat, v3 angles) -{ - f32 angle = deg2rad(angles.x); - f32 cx = cosf(angle); - f32 sx = sinf(angle); - angle = deg2rad(angles.y); - f32 cy = cosf(angle); - f32 sy = sinf(angle); - angle = deg2rad(angles.z); - f32 cz = cosf(angle); - f32 sz = sinf(angle); - - v3 x = {cy*cz, sx*sy*cz+cx*sz, -cx*sy*cz+sx*sz}; - v3 y = {-cy*sz, -sx*sy*sz+cx*cz, cx*sy*sz+sx*cz}; - v3 z = {sy, -sx*cy, cx*cy}; - mat4 rotate = mat4_make_rotate(x, y, z); - - mat4 result = mat4_mul(rotate, mat); - - return result; -} - -v4 mat4_v4_mul(mat4 m, v4 v) -{ - v4 result = { - m.c0.x*v.x+m.c1.x*v.y+m.c2.x*v.z+m.c3.x*v.w, - m.c0.y*v.x+m.c1.y*v.y+m.c2.y*v.z+m.c3.y*v.w, - m.c0.z*v.x+m.c1.z*v.y+m.c2.z*v.z+m.c3.z*v.w, - m.c0.w*v.x+m.c1.w*v.y+m.c2.w*v.z+m.c3.w*v.w - }; - return result; -} - -i32 in_rect(v2 pos, rect_t rect) -{ - i32 result = (pos.x > rect.start.x) && (pos.x < rect.end.x) && - (pos.y > rect.start.y) && (pos.y < rect.end.y); - return result; -} - diff --git a/prb_string.h b/prb_string.h deleted file mode 100644 index cd7297f..0000000 --- a/prb_string.h +++ /dev/null @@ -1,217 +0,0 @@ -/* -void prb_memset(void *dest, i32 fill, u64 n); -void prb_memmove(void *dest, const void *src, u64 size); -i32 prb_memeq(const void *p1, const void *p2, u64 size); -i32 streq(const char *str1, const char *str2); - -char *str8_to_cstr(arena_t *arena, str8_t str); -i32 str8eq(str8_t str1, str8_t str2); -u8 *str8ch(str8_t str, i32 c); -u8 *str8rch(str8_t str, i32 c); -str8_t str8_range(u8 *start, u8 *end); -str8_t str8_getdir(str8_t str); -str8_t str8_pushfv(arena_t *arena, const char *format, va_list args); -str8_t str8_pushf(arena_t *arena, const char *format, ...); -void str8_print(str8_t str); -void str8_printf(const char *format, ...); -void str8_list_push(arena_t *arena, str8_list_t *list, i32 front, str8_t str); -void str8_list_pushf(arena_t *arena, str8_list_t *list, i32 front, const char *format, ...); -str8_t str8_list_join(arena_t *arena, str8_list_t *list); -void str8_list_print(str8_list_t *list); -*/ - -void prb_memset(void *dest, i32 fill, u64 n) -{ - u8 *byte = dest; - while (n--) - *byte++ = fill; -} - -void prb_memmove(void *dest, const void *src, u64 size) -{ - u8 *byte_dest = dest; - const u8 *byte_src = src; - while (size--) - *byte_dest++ = *byte_src++; -} - -i32 prb_memeq(const void *p1, const void *p2, u64 size) -{ - const u8 *byte_p1 = p1; - const u8 *byte_p2 = p2; - while (size--) - if (*byte_p1++ != *byte_p2++) - return 0; - return 1; -} - -i32 streq(const char *str1, const char *str2) -{ - if (!str1 || !str2) - return 0; - while (*str1++ == *str2++) - if (!*str1) - return 1; - return 0; -} - -// fixed length strings -#define str8lit(str) (str8_t){(u8 *)(str), sizeof(str) - 1} -#define str8exp(str) (i32)((str).len), ((str).ptr) - -#define MAX_FLS_BUFFER 1024 - -typedef struct { - u8 *ptr; - u64 len; -} str8_t; - -typedef struct str8_node_t { - str8_t str; - struct str8_node_t *next; - struct str8_node_t *prev; -} str8_node_t; - -typedef struct { - struct str8_node_t *first; - struct str8_node_t *last; - u64 len; - i32 cnt; -} str8_list_t; - -char *str8_to_cstr(arena_t *arena, str8_t str) -{ - u64 len = str.len + 1; - char *cstr = push_arena(arena, len); - prb_memmove(cstr, str.ptr, len); - cstr[str.len] = 0; - return cstr; -} - -i32 str8eq(str8_t str1, str8_t str2) -{ - if (str1.len != str2.len) - return 0; - for (u64 i = 0; i < str1.len; ++i) - if (str1.ptr[i] != str2.ptr[i]) - return 0; - return 1; -} - -u8 *str8ch(str8_t str, i32 c) -{ - for (u64 i = 0; i < str.len; ++i) - if (str.ptr[i] == c) - return str.ptr + i; - return 0; -} - -u8 *str8rch(str8_t str, i32 c) -{ - for (u64 i = str.len - 1; i > 0; --i) - if (str.ptr[i] == c) - return str.ptr + i; - return 0; -} - -str8_t str8_range(u8 *start, u8 *end) -{ - assert(start); - assert(end); - assert(start < end); - str8_t result = {start, end - start}; - return result; -} - -str8_t str8_getdir(str8_t str) -{ - assert(str.len); - u8 *slash = str8rch(str, '/'); - if (!slash) - return str; - str8_t result = str8_range(str.ptr, slash); - return result; -} - -str8_t str8_pushfv(arena_t *arena, const char *format, va_list args) -{ - char tmp[MAX_FLS_BUFFER]; - i32 n = vsnprintf(tmp, MAX_FLS_BUFFER, format, args); - str8_t str = {0}; - if (n > 0) { - u8 *ptr = push_arena(arena, n); - prb_memmove(ptr, tmp, n); - str = (str8_t){ptr, n}; - str.ptr = ptr; - str.len = n; - } - return str; -} - -str8_t str8_pushf(arena_t *arena, const char *format, ...) -{ - va_list args; - va_start(args, format); - str8_t str = str8_pushfv(arena, format, args); - va_end(args); - return str; -} - -void str8_print(str8_t str) -{ - assert(str.ptr); - assert(str.len); - printf("%.*s", str8exp(str)); -} - -void str8_printf(const char *format, ...) -{ - va_list args; - va_start(args, format); - arena_t temp = alloc_arena(MAX_FLS_BUFFER); - str8_t str = str8_pushfv(&temp, format, args); - va_end(args); - str8_print(str); - release_arena(&temp); -} - -void str8_list_push(arena_t *arena, str8_list_t *list, i32 front, str8_t str) -{ - str8_node_t *node = push_arena(arena, sizeof(str8_node_t)); - node->str = str; - if (front) - dllpushfront(list->first, list->last, node); - else - dllpushback(list->first, list->last, node); - list->len += str.len; - list->cnt++; -} - -void str8_list_pushf(arena_t *arena, str8_list_t *list, i32 front, const char *format, ...) -{ - va_list args; - va_start(args, format); - str8_t str = str8_pushfv(arena, format, args); - va_end(args); - str8_list_push(arena, list, front, str); -} - -/* TODO(pryazha): Check if that works :) */ -str8_t str8_list_join(arena_t *arena, str8_list_t *list) -{ - assert(arena); - assert(list); - str8_t str = {arena->memory + arena->used, 0}; - for (str8_node_t *node = list->first; node; node = node->next) { - u8 *ptr = push_arena(arena, node->str.len); - prb_memmove(ptr, node->str.ptr, node->str.len); - str.len += node->str.len; - } - return str; -} - -void str8_list_print(str8_list_t *list) -{ - for (str8_node_t *node = list->first; node; node = node->next) - str8_print(node->str); -} diff --git a/prb_sys.h b/prb_sys.h deleted file mode 100644 index 1ec79d1..0000000 --- a/prb_sys.h +++ /dev/null @@ -1,8 +0,0 @@ -#define MAX_PATH 1024 - -extern void *sys_alloc(u64 length); -extern void sys_free(void *memory, u64 length); -extern void die(const char *format, ...); -extern void info(const char *format, ...); -extern u64 sys_read_file(arena_t *arena, char **buffer, const char *filename); -extern char *sys_getbindir(arena_t *arena); diff --git a/prb_types.h b/prb_types.h deleted file mode 100644 index bf6cd43..0000000 --- a/prb_types.h +++ /dev/null @@ -1,49 +0,0 @@ -typedef int8_t i8; -typedef int16_t i16; -typedef int32_t i32; -typedef int64_t i64; - -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; - -typedef float f32; -typedef double f64; - -typedef struct { - f32 x; - f32 y; -} v2; - -typedef struct { - f32 x; - f32 y; - f32 z; -} v3; - -typedef struct { - f32 x; - f32 y; - f32 z; - f32 w; -} v4; - -// column-major -typedef struct { - v4 c0; - v4 c1; - v4 c2; - v4 c3; -} mat4; - -typedef struct { - v2 start; - v2 end; -} rect_t; - -typedef struct { - void *memory; - u64 capacity; - u64 used; -} arena_t; diff --git a/prb_windows.h b/prb_windows.h deleted file mode 100644 index 9514c75..0000000 --- a/prb_windows.h +++ /dev/null @@ -1,86 +0,0 @@ -void *sys_alloc(u64 length) -{ - assert(length); - void *result = VirtualAlloc(0, length, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE); - return result; -} - -void sys_free(void *memory, u64 length) -{ - assert(memory); - assert(length); - assert(VirtualFree(memory, 0, MEM_RELEASE)); -} - -void die(const char *error_string, ...) -{ - va_list args; - va_start(args, error_string); - printf("error: "); - vprintf(error_string, args); - printf("\n"); - va_end(args); - exit(1); -} - -u64 sys_read_file(arena_t *arena, char **buffer, const char *filename) -{ - *buffer = 0; - - HANDLE file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, - 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); - if (file == INVALID_HANDLE_VALUE) { - printf("error: failed to open file \"%s\"\n", filename); - return 0; - } - - LARGE_INTEGER lisize; - if (!GetFileSizeEx(file, &lisize)) { - printf("error: failed to get size of the file \"%s\"", filename); - CloseHandle(file); - return 0; - } - - u64 size = lisize.QuadPart; - if (!size) { - printf("error: file \"%s\" is empty", filename); - CloseHandle(file); - return 0; - } - - *buffer = push_arena(arena, size + 1); - - DWORD bytes_read; - if (!ReadFile(file, *buffer, size, &bytes_read, 0)) { - CloseHandle(file); - pop_arena(arena, size); - printf("error: failed to read file \"%s\", bytes read: %lu\n", filename, bytes_read); - return 0; - } - CloseHandle(file); - - *(*buffer + size) = 0; - - return size + 1; -} - -char *sys_getbindir(arena_t *arena) -{ - char path[MAX_PATH]; - u32 len = GetModuleFileName(0, path, MAX_PATH); - if (!len) { - printf("error: failed to get file path\n"); - return 0; - } - - char *dir = strrchr(path, '\\'); - assert(dir); - assert(dir > path); - - len = dir - path; - dir = push_arena(arena, len + 1); - prb_memmove(dir, path, len); - dir[len] = 0; - - return dir; -} |