summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-05-27 10:14:04 -0600
committermo khan <mo.khan@gmail.com>2020-05-27 10:24:03 -0600
commit199c539ba36e684b71e571f47f74152224020d15 (patch)
tree2ca18c4e3d784f4cc2c5eec4b1bfc579ef30d395
Create program with a lot of issuesmain
-rw-r--r--.gitignore2
-rw-r--r--CMakeLists.txt10
-rwxr-xr-xbuild.sh13
-rw-r--r--conanfile.txt5
-rw-r--r--example.c63
-rw-r--r--logo.pngbin0 -> 9152 bytes
6 files changed, 93 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..45d47c9
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/build
+*.zst
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..8bfcdd7
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,10 @@
+cmake_minimum_required(VERSION 2.8.11)
+project(example C)
+
+set(CMAKE_VERBOSE_MAKEFILE FALSE)
+
+include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
+conan_basic_setup()
+
+add_executable(${PROJECT_NAME} example.c)
+target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
diff --git a/build.sh b/build.sh
new file mode 100755
index 0000000..4ad3ef6
--- /dev/null
+++ b/build.sh
@@ -0,0 +1,13 @@
+#!/bin/bash
+
+set -ex
+
+rm -rf build
+mkdir build
+pushd build
+
+conan install --build zstd ..
+
+cmake .. -DCMAKE_BUILD_TYPE=Release
+cmake --build .
+bin/example "../logo.png"
diff --git a/conanfile.txt b/conanfile.txt
new file mode 100644
index 0000000..7edf745
--- /dev/null
+++ b/conanfile.txt
@@ -0,0 +1,5 @@
+[requires]
+zstd/1.4.4
+
+[generators]
+cmake
diff --git a/example.c b/example.c
new file mode 100644
index 0000000..a05f2ec
--- /dev/null
+++ b/example.c
@@ -0,0 +1,63 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <zstd.h>
+
+static void compress(char* fname, char* outName, int compress_level)
+{
+ FILE* source_file = fopen(fname, "rb");
+ FILE* destination_file = fopen(outName, "wb");
+ size_t input_buffer_size = ZSTD_CStreamInSize();
+ void* input_buffer = malloc(input_buffer_size);
+ size_t output_buffer_size = ZSTD_CStreamOutSize();
+ void* output_buffer = malloc(output_buffer_size);
+ ZSTD_CStream* stream = ZSTD_createCStream();
+ ZSTD_initCStream(stream, compress_level);
+ size_t read, to_read = input_buffer_size;
+
+ while (read = fread(input_buffer, 1, to_read, source_file)) {
+ ZSTD_inBuffer input = { input_buffer, read, 0 };
+
+ while (input.pos < input.size) {
+ ZSTD_outBuffer output = { output_buffer, output_buffer_size, 0 };
+ to_read = ZSTD_compressStream(stream, &output, &input);
+
+ if (to_read > input_buffer_size)
+ to_read = input_buffer_size;
+
+ fwrite(output_buffer, 1, output.pos, destination_file);
+ }
+ }
+
+ ZSTD_outBuffer output = { output_buffer, output_buffer_size, 0 };
+ ZSTD_endStream(stream, &output);
+
+ fwrite(output_buffer, 1, output.pos, destination_file);
+
+ ZSTD_freeCStream(stream);
+ fclose(destination_file);
+ fclose(source_file);
+ free(input_buffer);
+ free(output_buffer);
+}
+
+static char* destination_filename_from(char* filename)
+{
+ size_t length = strlen(filename) + 5;
+ void* buffer = malloc(length);
+ memset(buffer, 0, length);
+ strcat(buffer, filename);
+ strcat(buffer, ".zst");
+ return (char*)buffer;
+}
+
+int main(int argc, char** argv)
+{
+ char* source = argv[1];
+ char* destination = destination_filename_from(source);
+ int compression_level = 1;
+
+ printf("Compressing %s to %s with compression level %d…\n", source, destination, compression_level);
+ compress(source, destination, compression_level);
+ return 0;
+}
diff --git a/logo.png b/logo.png
new file mode 100644
index 0000000..535a436
--- /dev/null
+++ b/logo.png
Binary files differ