summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2026-07-12 15:19:57 +0200
committerThomas Schmucker <ts@its1.de>2026-07-12 15:19:57 +0200
commitc17b523d4710134218bca4f2fdbe8fd0cee39ecd (patch)
tree2d404a33440469804ef9f01162e52edb1bd70df4
downloaduse-zlib-c17b523d4710134218bca4f2fdbe8fd0cee39ecd.tar.gz
use-zlib-c17b523d4710134218bca4f2fdbe8fd0cee39ecd.tar.bz2
use-zlib-c17b523d4710134218bca4f2fdbe8fd0cee39ecd.zip
Erstes Beispiel für die Benutzung von zlib
-rw-r--r--.clang-format45
-rw-r--r--.clang-tidy19
-rw-r--r--.gitignore2
-rw-r--r--compile_flags.txt4
-rw-r--r--config.mk2
-rw-r--r--makefile24
-rw-r--r--src/simple.c77
7 files changed, 173 insertions, 0 deletions
diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..21efcac
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,45 @@
1---
2AccessModifierOffset: -4
3AlignConsecutiveAssignments: 'true'
4AlignConsecutiveDeclarations: 'true'
5AlignEscapedNewlines: Left
6AlignTrailingComments: 'true'
7AlwaysBreakAfterReturnType: TopLevelDefinitions
8BreakBeforeBraces: Stroustrup
9BreakConstructorInitializers: BeforeComma
10BreakInheritanceList: BeforeComma
11ColumnLimit: '0'
12CompactNamespaces: 'false'
13Cpp11BracedListStyle: 'false'
14FixNamespaceComments: 'true'
15IncludeBlocks: Regroup
16IncludeCategories:
17 - Regex: '^.*(precomp|pch|stdafx)'
18 Priority: -1
19 - Regex: '^<.*>'
20 Priority: 1
21 - Regex: '^".*"'
22 Priority: 2
23 - Regex: '.*'
24 Priority: 3
25IndentCaseLabels: 'false'
26IndentPPDirectives: AfterHash
27IndentWidth: '4'
28IndentWrappedFunctionNames: 'false'
29KeepEmptyLinesAtTheStartOfBlocks: 'false'
30PointerAlignment: Right
31SortIncludes: 'true'
32SpaceAfterCStyleCast: 'true'
33SpaceAfterTemplateKeyword: 'false'
34SpaceBeforeAssignmentOperators: 'true'
35SpaceBeforeParens: ControlStatements
36SpaceBeforeRangeBasedForLoopColon: 'false'
37SpaceInEmptyParentheses: 'false'
38SpacesInAngles: 'false'
39SpacesInCStyleCastParentheses: 'false'
40SpacesInConditionalStatement: 'true'
41SpacesInParentheses: 'false'
42Standard: Auto
43TabWidth: '4'
44UseTab: ForIndentation
45...
diff --git a/.clang-tidy b/.clang-tidy
new file mode 100644
index 0000000..0bceb5b
--- /dev/null
+++ b/.clang-tidy
@@ -0,0 +1,19 @@
1---
2Checks: "*,
3 -abseil-*,
4 -altera-*,
5 -android-*,
6 -fuchsia-*,
7 -google-*,
8 -llvm*,
9 -modernize-use-trailing-return-type,
10 -zircon-*,
11 -readability-else-after-return,
12 -readability-static-accessed-through-instance,
13 -readability-avoid-const-params-in-decls,
14 -cppcoreguidelines-non-private-member-variables-in-classes,
15 -misc-non-private-member-variables-in-classes,
16"
17WarningsAsErrors: ''
18HeaderFilterRegex: ''
19FormatStyle: none
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..cd42ee3
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
1bin/
2obj/
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 @@
1-Wall
2-Werror
3-pedantic
4-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 @@
1CFLAGS=-Wall -Werror -pedantic -std=c99
2LDFLAGS=-lz
diff --git a/makefile b/makefile
new file mode 100644
index 0000000..4ce05bc
--- /dev/null
+++ b/makefile
@@ -0,0 +1,24 @@
1include config.mk
2
3SIMPLE=bin/simple
4SIMPLE_OBJ_FILES=obj/simple.o
5
6release: $(SIMPLE)
7release: CFLAGS+=-DNDEBUG -O2
8
9debug: $(SIMPLE)
10debug: CFLAGS+=-g
11
12$(SIMPLE): $(SIMPLE_OBJ_FILES) | bin
13 cc $(LDFLAGS) $^ -o $@
14
15obj/%.o: src/%.c | obj
16 cc $(CFLAGS) -c $< -o $@
17
18bin obj:
19 mkdir -p $@
20
21clean:
22 rm -rf bin obj
23
24.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 @@
1// Standard C
2#include <stdio.h>
3#include <stdlib.h>
4
5// System
6#include <unistd.h>
7
8// zlib
9#include <zlib.h>
10
11static void
12usage(void)
13{
14 (void) fprintf(stderr, "compress [-d] infile outfile\n");
15 exit(EXIT_FAILURE);
16}
17
18int
19main(int argc, char *argv[])
20{
21 // int compress = 1;
22 int level = 5;
23
24 int opt = 0;
25 while ( (opt = getopt(argc, argv, "dl")) != -1 ) {
26 switch ( opt ) {
27 case 'd':
28 // compress = 0; // decompress
29 break;
30
31 case 'l':
32 level = 9;
33 break;
34
35 case '?':
36 default:
37 usage();
38 break;
39 }
40 }
41 argc -= optind;
42 argv += optind;
43
44 if ( argc != 2 ) {
45 usage();
46 }
47
48 FILE *in_file = fopen(argv[0], "rb");
49 FILE *out_file = fopen(argv[1], "wb");
50
51 (void) fseek(in_file, 0, SEEK_END);
52 unsigned long insize = ftell(in_file); // max 2GB...
53 (void) fseek(in_file, 0, SEEK_SET);
54
55 unsigned char *indata = malloc(insize);
56 (void) fread(indata, 1, insize, in_file);
57
58 unsigned long outsize = compressBound(insize);
59 unsigned char *outdata = malloc(outsize);
60
61 int res = compress2(outdata, &outsize, indata, insize, level);
62
63 if ( res == Z_OK ) {
64 (void) fwrite(outdata, 1, outsize, out_file);
65 }
66 else {
67 printf("Error: %d\n", res);
68 }
69
70 free(outdata);
71 free(indata);
72
73 (void) fclose(out_file);
74 (void) fclose(in_file);
75
76 return EXIT_SUCCESS;
77}