From 776f21257adea5750e2b9cd07b36f8d4d7f96e37 Mon Sep 17 00:00:00 2001 From: retoor Date: Tue, 14 Jan 2025 18:53:15 +0100 Subject: [PATCH] New repository. --- .clang-format | 192 + .../workflows/make-rlib-single-platform.yml | 29 + .gitignore | 17 + Makefile | 213 + README.md | 94 + _rlib.h | 45 + arena.c | 35 + arena.h | 47 + build.gif | Bin 0 -> 602585 bytes clean | 42 + license.h | 22 + main.h | 51 + nsock.c | 11 + nsock.h | 419 + r.c | 20 + rargs.h | 58 + rautocomplete.c | 32 + rautocomplete.h | 63 + rbench.c | 388 + rbench.cpp | 251 + rbench.h | 370 + rbuffer.c | 39 + rbuffer.h | 174 + rcase.c | 3 + rcase.h | 164 + rcat.c | 3 + rcat.h | 29 + rcov.c | 3 + rcov.h | 77 + reditor.c | 162 + remo.c | 7 + remo.h | 247 + resources/large.json | 1 + rfalloc.c | 113 + rhashtable.c | 12 + rhashtable.h | 67 + rhttp.c | 72 + rhttp.h | 653 ++ rhttpc.c | 11 + ricli.c | 336 + rinterp.c | 40 + rio.c | 9 + rio.h | 117 + rjson.c | 19 + rjson.h | 130 + rkeytable.c | 12 + rkeytable.h | 68 + rlexer.c | 189 + rlexer.h | 370 + rlib.c | 3 + rlib.h | 8450 +++++++++++++++++ rlibrlibso.c | 8450 +++++++++++++++++ rlibso.c | 18 + rliza.c | 156 + rliza.h | 775 ++ rliza.py | 41 + rmalloc.c | 44 + rmalloc.h | 145 + rmath.h | 35 + rmerge.c | 3 + rmerge.h | 178 + rnet.c | 42 + rnet.h | 480 + rprint.c | 47 + rprint.h | 287 + rrex3.c | 10 + rrex3.h | 1228 +++ rrex4.c | 158 + rrex4.h | 758 ++ rstring.c | 179 + rstring.h | 439 + rstring_list.c | 27 + rstring_list.h | 47 + rtemp.c | 38 + rtemp.h | 53 + rterm.c | 34 + rterm.h | 303 + rterminal.c | 4 + rterminal.h | 189 + rtest.h | 122 + rtime.c | 19 + rtime.h | 147 + rtree.c | 67 + rtree.h | 95 + rtypes.c | 14 + rtypes.h | 32 + uuid.h | 46 + 87 files changed, 28689 insertions(+) create mode 100644 .clang-format create mode 100644 .github/workflows/make-rlib-single-platform.yml create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 _rlib.h create mode 100644 arena.c create mode 100644 arena.h create mode 100644 build.gif create mode 100755 clean create mode 100644 license.h create mode 100644 main.h create mode 100644 nsock.c create mode 100644 nsock.h create mode 100644 r.c create mode 100644 rargs.h create mode 100644 rautocomplete.c create mode 100644 rautocomplete.h create mode 100644 rbench.c create mode 100644 rbench.cpp create mode 100644 rbench.h create mode 100644 rbuffer.c create mode 100644 rbuffer.h create mode 100644 rcase.c create mode 100644 rcase.h create mode 100644 rcat.c create mode 100644 rcat.h create mode 100644 rcov.c create mode 100644 rcov.h create mode 100644 reditor.c create mode 100644 remo.c create mode 100644 remo.h create mode 100644 resources/large.json create mode 100644 rfalloc.c create mode 100644 rhashtable.c create mode 100644 rhashtable.h create mode 100644 rhttp.c create mode 100644 rhttp.h create mode 100644 rhttpc.c create mode 100644 ricli.c create mode 100644 rinterp.c create mode 100644 rio.c create mode 100644 rio.h create mode 100644 rjson.c create mode 100644 rjson.h create mode 100644 rkeytable.c create mode 100644 rkeytable.h create mode 100644 rlexer.c create mode 100644 rlexer.h create mode 100644 rlib.c create mode 100644 rlib.h create mode 100644 rlibrlibso.c create mode 100644 rlibso.c create mode 100644 rliza.c create mode 100644 rliza.h create mode 100644 rliza.py create mode 100644 rmalloc.c create mode 100644 rmalloc.h create mode 100644 rmath.h create mode 100644 rmerge.c create mode 100644 rmerge.h create mode 100644 rnet.c create mode 100644 rnet.h create mode 100644 rprint.c create mode 100644 rprint.h create mode 100644 rrex3.c create mode 100644 rrex3.h create mode 100644 rrex4.c create mode 100644 rrex4.h create mode 100644 rstring.c create mode 100644 rstring.h create mode 100644 rstring_list.c create mode 100644 rstring_list.h create mode 100644 rtemp.c create mode 100644 rtemp.h create mode 100644 rterm.c create mode 100644 rterm.h create mode 100644 rterminal.c create mode 100644 rterminal.h create mode 100644 rtest.h create mode 100644 rtime.c create mode 100644 rtime.h create mode 100644 rtree.c create mode 100644 rtree.h create mode 100644 rtypes.c create mode 100644 rtypes.h create mode 100644 uuid.h diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..de7da58 --- /dev/null +++ b/.clang-format @@ -0,0 +1,192 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignArrayOfStructures: None +AlignConsecutiveMacros: None +AlignConsecutiveAssignments: None +AlignConsecutiveBitFields: None +AlignConsecutiveDeclarations: None +AlignEscapedNewlines: Right +AlignOperands: Align +AlignTrailingComments: true +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortEnumsOnASingleLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: All +AllowShortLambdasOnASingleLine: All +AllowShortIfStatementsOnASingleLine: Never +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: true + SplitEmptyRecord: true + SplitEmptyNamespace: true +BreakBeforeBinaryOperators: None +BreakBeforeConceptDeclarations: true +BreakBeforeBraces: Attach +BreakBeforeInheritanceComma: false +BreakInheritanceList: BeforeColon +BreakBeforeTernaryOperators: true +BreakConstructorInitializersBeforeComma: false +BreakConstructorInitializers: BeforeColon +BreakAfterJavaFieldAnnotations: false +BreakStringLiterals: true +ColumnLimit: 140 +CommentPragmas: '^ IWYU pragma:' +QualifierAlignment: Leave +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DeriveLineEnding: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +PackConstructorInitializers: BinPack +BasedOnStyle: '' +ConstructorInitializerAllOnOneLineOrOnePerLine: false +AllowAllConstructorInitializersOnNextLine: true +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: '^"(llvm|llvm-c|clang|clang-c)/' + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: '^(<|"(gtest|gmock|isl|json)/)' + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: '.*' + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: '(Test)?$' +IncludeIsMainSourceRegex: '' +IndentAccessModifiers: false +IndentCaseLabels: false +IndentCaseBlocks: false +IndentGotoLabels: true +IndentPPDirectives: None +IndentExternBlock: AfterExternBlock +IndentRequires: false +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertTrailingCommas: None +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +LambdaBodyIndentation: Signature +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 2 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +PenaltyIndentedWhitespace: 0 +PointerAlignment: Right +PPIndentWidth: -1 +ReferenceAlignment: Pointer +ReflowComments: true +RemoveBracesLLVM: false +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDefinitionName: false + AfterFunctionDeclarationName: false + AfterIfMacros: true + AfterOverloadedOperator: false + BeforeNonEmptyParentheses: false +SpaceAroundPointerQualifiers: Default +SpaceBeforeRangeBasedForLoopColon: true +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +SpaceBeforeSquareBrackets: false +BitFieldColonSpacing: Both +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 8 +UseCRLF: false +UseTab: Never +WhitespaceSensitiveMacros: + - STRINGIZE + - PP_STRINGIZE + - BOOST_PP_STRINGIZE + - NS_SWIFT_NAME + - CF_SWIFT_NAME +... + diff --git a/.github/workflows/make-rlib-single-platform.yml b/.github/workflows/make-rlib-single-platform.yml new file mode 100644 index 0000000..884fa1e --- /dev/null +++ b/.github/workflows/make-rlib-single-platform.yml @@ -0,0 +1,29 @@ +name: Build and run rrex2 + + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +env: + BUILD_TYPE: Release + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Install dependencies + run: sudo apt update && sudo apt install clang-format -y + - name: Build + working-directory: ${{github.workspace}} + run: make + + - name: Install + working-directory: ${{github.workspace}} + run: make install + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..64d7627 --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +a.out +.vscore +.bzr +.bzrignore +.vscode +C-Data-Structs +rasmd +*yur* +yur* +hashmap.h +primes.h +errors.h +*.tty +*.o +build +.history + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..773eb40 --- /dev/null +++ b/Makefile @@ -0,0 +1,213 @@ +# Compiler +CC = gcc + +# Compiler flags +CFLAGS = -Wall -Wextra -Werror -Ofast -std=c2x -lm -lrt -pthread -fPIC +LDFLAGS = -lm + +# -D_POSIX_C_SOURCE=199309L +all: clear test_rbuffer test_rliza test_rnet test_rmalloc test_rtemp test_rjson test_rrex4 test_rstring_list test_rhttp test_rtime test_arena test_rtree test_rstring test_rlexer test_rrex3 test_rio test_rhashtable test_rkeytable test_rterminal test_rmerge run_rmerge format_all build format_all test_rlib + + + + +format_all: format_rlib_h + clang-format *.c *.h *.cpp -i --sort-includes=false + +format_rlib_h: + clang-format rlib.h build/rlib.h -i --sort-includes --verbose + +clear: + -@rm -rf build + @mkdir build + @echo "Build directory emptied" + +rebuild: clear all + +test_rbuffer: build_rbuffer run_rbuffer +build_rbuffer: + $(CC) $(CFLAGS) rbuffer.c -o ./build/rbuffer +run_rbuffer: + ./build/rbuffer test + +test_rliza: build_rliza run_rliza +build_rliza: + $(CC) $(CFLAGS) rliza.c -o ./build/rliza +run_rliza: + ./build/rliza test + +test_rnet: build_rnet run_rnet +build_rnet: + $(CC) $(CFLAGS) rnet.c -o ./build/rnet +run_rnet: + ./build/rnet test + +test_rtemp: build_rtemp run_rtemp +build_rtemp: + $(CC) $(CFLAGS) rtemp.c -o ./build/rtemp +run_rtemp: + ./build/rtemp + +test_rjson: build_rjson run_rjson +build_rjson: + $(CC) $(CFLAGS) rjson.c -o ./build/rjson +run_rjson: + ./build/rjson + + +test_rstring_list: build_rstring_list run_rstring_list +build_rstring_list: + $(CC) $(CFLAGS) rstring_list.c -o ./build/rstring_list +run_rstring_list: + ./build/rstring_list + +test_rhttp: build_rhttp run_rhttp +build_rhttp: + $(CC) $(CFLAGS) rhttp.c -o ./build/rhttp +run_rhttp: + ./build/rhttp + +test_rautocomplete: build_rautocomplete run_rautocomplete +build_rautocomplete: + $(CC) $(CFLAGS) rautocomplete.c -o ./build/rautocomplete +run_rautocomplete: + ./build/rautocomplete + +test_rlexer: build_rlexer run_rlexer +build_rlexer: + $(CC) $(CFLAGS) rlexer.c -o ./build/rlexer +run_rlexer: + ./build/rlexer + + +test_rrex4: build_rrex4 run_rrex4 +build_rrex4: + $(CC) $(CFLAGS) rrex4.c -o ./build/rrex4 +run_rrex4: + ./build/rrex4 + +test_rrex3: build_rrex3 run_rrex3 +build_rrex3: + $(CC) $(CFLAGS) rrex3.c -o ./build/rrex3 +run_rrex3: + ./build/rrex3 + +test_rio: build_rio run_rio +build_rio: + $(CC) $(CFLAGS) rio.c -o ./build/rio +run_rio: + ./build/rio + + + +test_rterm: build_rterm run_rterm +build_rterm: + $(CC) $(CFLAGS) rterm.c -o ./build/rterm +run_rterm: + ./build/rterm + +test_rterminal: build_rterminal run_rterminal +build_rterminal: + $(CC) $(CFLAGS) rterminal.c -o ./build/rterminal +run_rterminal: + ./build/rterminal + +test_rmerge: build_rmerge run_rmerge +build_rmerge: + $(CC) $(CFLAGS) rmerge.c -o ./build/rmerge +run_rmerge: + ./build/rmerge _rlib.h > ./build/rlib.h + cp ./build/rlib.h ./rlib.h + +test_rprint: build_rprint run_rprint +build_rprint: + $(CC) $(CFLAGS) rprint.c -o ./build/rprint +run_rprint: + ./build/rprint + +test_rstring: build_rstring run_rstring +build_rstring: + $(CC) $(CFLAGS) rstring.c -o ./build/rstring +run_rstring: + ./build/rstring + +test_rbench: build_rbench run_rbench +build_rbench: + $(CC) $(CFLAGS) rbench.c -o ./build/rbench +run_rbench: + ./build/rbench + +test_rbench_cpp: build_rbench_cpp run_rbench_cpp +build_rbench_cpp: + g++ $(CFLAGS) ./C-Data-Structs/src/primes.c rbench.cpp -o ./build/rbench.cpp -I./C-Data-Structs/include +run_rbench_cpp: + ./build/rbench.cpp + +test_yurii_cpp: format_all build_yurii_cpp run_yurii_cpp +build_yurii_cpp: + rmerge rbench.cpp > yurii_hashmap.cpp + g++ C-Data-Structs/src/primes.c yurii_hashmap.cpp -o ./build/yurii_hashmap.cpp -IC-Data-Structs/include +run_yurii_cpp: + ./build/yurii_hashmap.cpp + +test_rmalloc: build_rmalloc run_rmalloc +build_rmalloc: + $(CC) $(CFLAGS) rmalloc.c -o ./build/rmalloc +run_rmalloc: + ./build/rmalloc + +test_rtime: build_rtime run_rtime +build_rtime: + $(CC) $(CFLAGS) rtime.c -o ./build/rtime +run_rtime: + ./build/rtime + +test_arena: build_arena run_arena +build_arena: + $(CC) $(CFLAGS) arena.c -o ./build/arena +run_arena: + ./build/arena + +test_rlib: build_rlib run_rlib +build_rlib: + $(CC) $(CFLAGS) rlib.c -o ./build/rlib +run_rlib: + ./build/rlib test + +test_rtree: build_rtree run_rtree +build_rtree: + $(CC) $(CFLAGS) rtree.c -o ./build/rtree +run_rtree: + ./build/rtree + +test_rhashtable: build_rhashtable run_rhashtable +build_rhashtable: + $(CC) $(CFLAGS) rhashtable.c -o ./build/rhashtable +run_rhashtable: + ./build/rhashtable + +test_rkeytable: build_rkeytable run_rkeytable +build_rkeytable: + $(CC) $(CFLAGS) rkeytable.c -o ./build/rkeytable +run_rkeytable: + ./build/rkeytable + +build: format_rlib_h + cp ./clean build/clean + cp rlib.h rlibrlibso.c + @gcc rlibrlibso.c $(CFLAGS) -shared -o ./build/librlib.so + @echo "Built a new rlib.so" + @gcc rlibso.c -L./build -Wl,-rpath=. -lrlib -o ./build/rlibso ${CFLAGS} + @cd ./build && ./rlibso + @echo "Build succesful" + +install: + sudo cp ./build/rlib.h /usr/include/rlib.h + sudo cp ./build/rmerge /usr/local/bin/rmerge + sudo cp ./build/clean /usr/local/bin/clean + sudo cp ./build/rlib /usr/local/bin/rlib + +publish: + brz add + brz commit + brz push lp:rlib diff --git a/README.md b/README.md new file mode 100644 index 0000000..31be88e --- /dev/null +++ b/README.md @@ -0,0 +1,94 @@ +# RLIB + +Collection of frequently used C functions by me. + +It contains + - An advanced benchmark system. + - An alternative to printf supporting printing of time between messages for e.g. benchmarking. + and colors. + - Super fast tree map (much faster than hash table). + - Stdout redirection. + - Terminal progress bar. + - Multi purpose lexer. + - Custom malloc for counting allocations and free's. + - Simple test library that works like assert that also checks memory if rmalloc is used and + creates summary and provides exit code. + - Several time functions supporting nano seconds. + - Several math functions for if not available by std (on termux for example). + - Arena blazing fast memory. + +## Extensive tested and benchmarked +All files get merged together to one file called `rlib.c` that can be used as a header file. +The result is a single header framework. +![Gif of build process](build.gif) + +## ENVIRONMENT VARIABLES + +### Disabling color +Set env RDISABLE_COLORS = 1 to disable color on stdout. stderr will still have colors for gui. + +## Compiler and Flags +- **`CC = gcc`**: Specifies the C compiler (GCC). +- **`CFLAGS = -Wall -Wextra -Ofast`**: Compiler flags: + - `-Wall`: Enable all warnings. + - `-Wextra`: Enable extra warnings. + - `-Ofast`: Optimize for speed. +- **`LDFLAGS = -lm`**: Linker flags to link with the math library. + +## Main Targets + +### `all:` +Runs the following steps in sequence: +- Testing (`test_*` targets). +- Formatting code (`format_all`). +- Building the project (`build`). +- Installing executables (`install`). + +### `format_all:` +Runs `clang-format` to format all `.c`, `.h`, and `.cpp` files in the directory. + +### `build:` +Builds the `rlib` shared library (`librlib.so`) and an executable `rlibso` that uses this library: +- Compiles `rlib.c` into `librlib.so`. +- Compiles `rlibso.c` linking it with `librlib.so`. +- Runs the `rlibso` executable. + +### `install:` +Installs the `rmerge` and `clean` executables to `/usr/bin` using `sudo`. + +## Testing Targets (`test_*`) + +Each `test_*` target follows these steps: + +### `build_*:` +Compiles a specific `.c` file into an executable in the `./build` directory. + +### `run_*:` +Runs the corresponding executable. + +Example for `rlexer`: +- **`build_rlexer:`** Compiles `rlexer.c` to `./build/rlexer`. +- **`run_rlexer:`** Runs `./build/rlexer`. + +## Backup + +### `backup:` +Compresses the source files, Makefile, and additional project files into a `rlib.rzip` archive. + +## Special Cases + +### `test_rbench_cpp:` +Builds and runs a C++ benchmark program: +- Compiles `rbench.cpp` along with other necessary files. +- Runs the resulting executable. + +## Other Targets + +### `format_rlib_c:` +Specifically formats the `rlib.c` file. + +### `build_*:` +Targets that compile source files into executables. + +### `run_*:` +Targets that run the compiled executables. \ No newline at end of file diff --git a/_rlib.h b/_rlib.h new file mode 100644 index 0000000..7dec69c --- /dev/null +++ b/_rlib.h @@ -0,0 +1,45 @@ +#include "license.h" +#ifndef RLIB_H +#define RLIB_H +// BEGIN OF RLIB + +/* + * Line below will be filtered by rmerge +