From c17b523d4710134218bca4f2fdbe8fd0cee39ecd Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Sun, 12 Jul 2026 15:19:57 +0200 Subject: Erstes Beispiel für die Benutzung von zlib MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .clang-format | 45 ++++++++++++++++++++++++++++++++ .clang-tidy | 19 ++++++++++++++ .gitignore | 2 ++ compile_flags.txt | 4 +++ config.mk | 2 ++ makefile | 24 +++++++++++++++++ src/simple.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 173 insertions(+) create mode 100644 .clang-format create mode 100644 .clang-tidy create mode 100644 .gitignore create mode 100644 compile_flags.txt create mode 100644 config.mk create mode 100644 makefile create mode 100644 src/simple.c diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..21efcac --- /dev/null +++ b/.clang-format @@ -0,0 +1,45 @@ +--- +AccessModifierOffset: -4 +AlignConsecutiveAssignments: 'true' +AlignConsecutiveDeclarations: 'true' +AlignEscapedNewlines: Left +AlignTrailingComments: 'true' +AlwaysBreakAfterReturnType: TopLevelDefinitions +BreakBeforeBraces: Stroustrup +BreakConstructorInitializers: BeforeComma +BreakInheritanceList: BeforeComma +ColumnLimit: '0' +CompactNamespaces: 'false' +Cpp11BracedListStyle: 'false' +FixNamespaceComments: 'true' +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^.*(precomp|pch|stdafx)' + Priority: -1 + - Regex: '^<.*>' + Priority: 1 + - Regex: '^".*"' + Priority: 2 + - Regex: '.*' + Priority: 3 +IndentCaseLabels: 'false' +IndentPPDirectives: AfterHash +IndentWidth: '4' +IndentWrappedFunctionNames: 'false' +KeepEmptyLinesAtTheStartOfBlocks: 'false' +PointerAlignment: Right +SortIncludes: 'true' +SpaceAfterCStyleCast: 'true' +SpaceAfterTemplateKeyword: 'false' +SpaceBeforeAssignmentOperators: 'true' +SpaceBeforeParens: ControlStatements +SpaceBeforeRangeBasedForLoopColon: 'false' +SpaceInEmptyParentheses: 'false' +SpacesInAngles: 'false' +SpacesInCStyleCastParentheses: 'false' +SpacesInConditionalStatement: 'true' +SpacesInParentheses: 'false' +Standard: Auto +TabWidth: '4' +UseTab: ForIndentation +... diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..0bceb5b --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,19 @@ +--- +Checks: "*, + -abseil-*, + -altera-*, + -android-*, + -fuchsia-*, + -google-*, + -llvm*, + -modernize-use-trailing-return-type, + -zircon-*, + -readability-else-after-return, + -readability-static-accessed-through-instance, + -readability-avoid-const-params-in-decls, + -cppcoreguidelines-non-private-member-variables-in-classes, + -misc-non-private-member-variables-in-classes, +" +WarningsAsErrors: '' +HeaderFilterRegex: '' +FormatStyle: none diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cd42ee3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/ +obj/ diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..d87afcf --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1,4 @@ +-Wall +-Werror +-pedantic +-std=c99 diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..bf800da --- /dev/null +++ b/config.mk @@ -0,0 +1,2 @@ +CFLAGS=-Wall -Werror -pedantic -std=c99 +LDFLAGS=-lz diff --git a/makefile b/makefile new file mode 100644 index 0000000..4ce05bc --- /dev/null +++ b/makefile @@ -0,0 +1,24 @@ +include config.mk + +SIMPLE=bin/simple +SIMPLE_OBJ_FILES=obj/simple.o + +release: $(SIMPLE) +release: CFLAGS+=-DNDEBUG -O2 + +debug: $(SIMPLE) +debug: CFLAGS+=-g + +$(SIMPLE): $(SIMPLE_OBJ_FILES) | bin + cc $(LDFLAGS) $^ -o $@ + +obj/%.o: src/%.c | obj + cc $(CFLAGS) -c $< -o $@ + +bin obj: + mkdir -p $@ + +clean: + rm -rf bin obj + +.PHONY: release debug clean diff --git a/src/simple.c b/src/simple.c new file mode 100644 index 0000000..5209841 --- /dev/null +++ b/src/simple.c @@ -0,0 +1,77 @@ +// Standard C +#include +#include + +// System +#include + +// zlib +#include + +static void +usage(void) +{ + (void) fprintf(stderr, "compress [-d] infile outfile\n"); + exit(EXIT_FAILURE); +} + +int +main(int argc, char *argv[]) +{ + // int compress = 1; + int level = 5; + + int opt = 0; + while ( (opt = getopt(argc, argv, "dl")) != -1 ) { + switch ( opt ) { + case 'd': + // compress = 0; // decompress + break; + + case 'l': + level = 9; + break; + + case '?': + default: + usage(); + break; + } + } + argc -= optind; + argv += optind; + + if ( argc != 2 ) { + usage(); + } + + FILE *in_file = fopen(argv[0], "rb"); + FILE *out_file = fopen(argv[1], "wb"); + + (void) fseek(in_file, 0, SEEK_END); + unsigned long insize = ftell(in_file); // max 2GB... + (void) fseek(in_file, 0, SEEK_SET); + + unsigned char *indata = malloc(insize); + (void) fread(indata, 1, insize, in_file); + + unsigned long outsize = compressBound(insize); + unsigned char *outdata = malloc(outsize); + + int res = compress2(outdata, &outsize, indata, insize, level); + + if ( res == Z_OK ) { + (void) fwrite(outdata, 1, outsize, out_file); + } + else { + printf("Error: %d\n", res); + } + + free(outdata); + free(indata); + + (void) fclose(out_file); + (void) fclose(in_file); + + return EXIT_SUCCESS; +} -- cgit v1.3