Skip to content

Commit

Permalink
Merge pull request #14 from tmthrgd/master
Browse files Browse the repository at this point in the history
Add fz_compress_writer and fz_decompress_writer to C interface
  • Loading branch information
gtoubassi committed May 6, 2016
2 parents c6c1068 + f0b1b7b commit 7a9ea27
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
22 changes: 22 additions & 0 deletions cpp/libfz/src/femtozip.cpp
Expand Up @@ -108,6 +108,17 @@ int fz_compress(void *model, const char *source, int source_len, char *dest, int
return outstr.length();
}

int fz_compress_writer(void *model, const char *source, size_t source_len, int (*dest_writer)(const char *buf, size_t len, void *arg), void *arg) {
CompressionModel *m = reinterpret_cast<CompressionModel*>(model);
ostringstream out;
m->compress(source, source_len, out);
string outstr = out.str();
if (outstr.length() == 0) {
return 1;
}
return dest_writer(outstr.c_str(), outstr.length(), arg);
}

int fz_decompress(void *model, const char *source, int source_len, char *dest, int dest_capacity) {
CompressionModel *m = reinterpret_cast<CompressionModel*>(model);
ostringstream out;
Expand All @@ -120,6 +131,17 @@ int fz_decompress(void *model, const char *source, int source_len, char *dest, i
return outstr.length();
}

int fz_decompress_writer(void *model, const char *source, size_t source_len, int (*dest_writer)(const char *buf, size_t len, void *arg), void *arg) {
CompressionModel *m = reinterpret_cast<CompressionModel*>(model);
ostringstream out;
m->decompress(source, source_len, out);
string outstr = out.str();
if (outstr.length() == 0) {
return 1;
}
return dest_writer(outstr.c_str(), outstr.length(), arg);
}


#ifdef __cplusplus
}
Expand Down
21 changes: 21 additions & 0 deletions cpp/libfz/src/femtozip.h
Expand Up @@ -100,6 +100,17 @@ void fz_release_model(void *model);
*/
int fz_compress(void *model, const char *source, int source_len, char *dest, int dest_capacity);

/*
* Attempts to compress 'source_len' bytes starting at 'source'. 'dest_writer'
* is called with a pointer to the destination buffer and it's length. The
* pointer is valid only for the lifetime of 'dest_writer'. Returns 0 on
* success, non zero if an error occurred. Calling fz_decompress with the
* resulting buffer will return the original bytes.
*
* @see fz_decompress_writer.
*/
int fz_compress_writer(void *model, const char *source, size_t source_len, int (*dest_writer)(const char *buf, size_t len, void *arg), void *arg);

/*
* Attempts to dcompress 'source_len' bytes starting at 'source' into 'dest'
* using the specified 'model'. 'dest_capacity' represents the maximum size of
Expand All @@ -112,6 +123,16 @@ int fz_compress(void *model, const char *source, int source_len, char *dest, int
*/
int fz_decompress(void *model, const char *source, int source_len, char *dest, int dest_capacity);

/*
* Attempts to dcompress 'source_len' bytes starting at 'source'. 'dest_writer'
* is called with a pointer to the destination buffer and it's length. The
* pointer is valid only for the lifetime of 'dest_writer'. Returns 0 on
* success, non zero if an error occurred.
*
* @see fz_compress_writer.
*/
int fz_decompress_writer(void *model, const char *source, size_t source_len, int (*dest_writer)(const char *buf, size_t len, void *arg), void *arg);


#ifdef __cplusplus
}
Expand Down

0 comments on commit 7a9ea27

Please sign in to comment.