Skip to content

Commit

Permalink
reformat c example
Browse files Browse the repository at this point in the history
  • Loading branch information
fangq committed Aug 20, 2022
1 parent eab19fb commit ccf0546
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
2 changes: 1 addition & 1 deletion example/c/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
all:
$(CC) -g -Wall -pedantic testzmat.c -o testzmat -DNO_LZMA -DNO_LZ4 -I../../src -L../../lib -lzmat -lz
$(CC) -g -Wall -pedantic testzmat.c -o testzmat -DNO_LZMA -DNO_LZ4 -I../../include -L../../lib -lzmat -lz
clean:
-rm -f testzmat
77 changes: 46 additions & 31 deletions example/c/testzmat.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/***************************************************************************//**
** \mainpage ZMat - A portable C-library and MATLAB/Octave toolbox for inline data compression
** \mainpage ZMat - A portable C-library and MATLAB/Octave toolbox for inline data compression
**
** \author Qianqian Fang <q.fang at neu.edu>
** \copyright Qianqian Fang, 2019-2020
Expand All @@ -15,73 +15,88 @@
#include <string.h>

/**
* if only zlib/gzip/base64 is used, one only need to add -I/path/to/zmatlib.h
* if only zlib/gzip/base64 is used, one only need to add -I/path/to/zmatlib.h
* if lzma/lzip is used, one must add -I/path/to/src/easylzma/
* if lz4/lz4hc is used, one must add -I/path/to/src/lz4
*/

#include "zmatlib.h"

int main(void){
char *test[]={"__o000o__(o)(o)__o000o__ =^_^= __o000o__(o)(o)__o000o__"};
int ret=0, status=0;
int main(void) {
char* test[] = {"__o000o__(o)(o)__o000o__ =^_^= __o000o__(o)(o)__o000o__"};

int ret = 0, status = 0;
size_t compressedlen, encodedlen, decodelen, decompressedlen;

/*output buffers will be allocated inside zmat functions, host is responsible to free after use*/
unsigned char *compressed=NULL, *encoded=NULL, *decoded=NULL, *decompressed=NULL;
unsigned char* compressed = NULL, *encoded = NULL, *decoded = NULL, *decompressed = NULL;

/*=====================================*/
/* compressing and encoding the string */
/*=====================================*/

/*first, perform zlib compression use the highest compression (-9); one can use zmat_encode as well*/
ret=zmat_run(strlen(test[0]),(unsigned char*)test[0], &compressedlen, &compressed, zmZlib, &status, -9);
if(ret==0){
ret = zmat_run(strlen(test[0]), (unsigned char*)test[0], &compressedlen, &compressed, zmZlib, &status, -9);

if (ret == 0) {
/*next, encode the compressed data using base64*/
ret=zmat_encode(compressedlen,compressed, &encodedlen, &encoded, zmBase64, &status);
if(ret==0){
printf("{\n\t\"original\":\"%s\",\n",test[0]);
printf("\t\"encoded\":\"%s\",\n",encoded);
}
ret = zmat_encode(compressedlen, compressed, &encodedlen, &encoded, zmBase64, &status);

if (ret == 0) {
printf("{\n\t\"original\":\"%s\",\n", test[0]);
printf("\t\"encoded\":\"%s\",\n", encoded);
}
}

/* error handling */
if(ret){
printf("encoding failed, error code: %d: encoder error code %d\n",ret,status);
return ret;
if (ret) {
printf("encoding failed, error code: %d: encoder error code %d\n", ret, status);
return ret;
}

/*==========================================================*/
/* decode and then decompress to restore the orginal string */
/*==========================================================*/

/*first, perform base64 decoding*/
ret=zmat_decode(encodedlen,encoded, &decodelen, &decoded, zmBase64, &status);
if(ret==0){
ret = zmat_decode(encodedlen, encoded, &decodelen, &decoded, zmBase64, &status);

if (ret == 0) {
/*next, decompress using zlib (deflate) */
ret=zmat_decode(decodelen,decoded, &decompressedlen, &decompressed, zmZlib, &status);
if(ret==0)
printf("\t\"decompressed\":\"%s\",\n",decompressed);
ret = zmat_decode(decodelen, decoded, &decompressedlen, &decompressed, zmZlib, &status);

if (ret == 0) {
printf("\t\"decompressed\":\"%s\",\n", decompressed);
}
}

printf("}\n");

/* error handling */
if(ret){
printf("decoding failed, error code: %d: decoder error code %d\n",ret,status);
return ret;
if (ret) {
printf("decoding failed, error code: %d: decoder error code %d\n", ret, status);
return ret;
}

/*==================================*/
/* host must free buffers after use */
/*==================================*/

if(compressed)
if (compressed) {
free(compressed);
if(encoded)
}

if (encoded) {
free(encoded);
if(decoded)
}

if (decoded) {
free(decoded);
if(decompressed)
}

if (decompressed) {
free(decompressed);
}

return 0;
}
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ pretty:
--suffix=none \
--formatted \
--break-blocks \
"*.c" "../include/*.h" "*.cpp"
"*.c" "../include/*.h" "*.cpp" "../example/c/*.c"

.PHONY: all mex oct lib dll

Expand Down

0 comments on commit ccf0546

Please sign in to comment.