Skip to content

Commit

Permalink
Added compressFileAdv and advanced options
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdragn committed Sep 3, 2014
1 parent 745a050 commit a5ab2ec
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
84 changes: 74 additions & 10 deletions src/python-lz4.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@

#define MAX(a, b) ((a) > (b) ? (a) : (b))

static PyObject *inputError;

typedef int (*compressor)(const char *source, char *dest, int isize);

static inline void store_le32(char *c, uint32_t x) {
Expand All @@ -55,6 +57,16 @@ static inline uint32_t load_le32(const char *c) {
return d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);
}

static inline char* add_extension(char* input) {
char* output;

output = (char*)malloc(strlen(input)+4);
strcpy(output, input);
strcat(output, ".lz4");

return output;
}

static const int hdr_size = sizeof(uint32_t);

static PyObject *compress_with(compressor compress, PyObject *self, PyObject *args) {
Expand Down Expand Up @@ -129,25 +141,75 @@ static PyObject *py_lz4_uncompress(PyObject *self, PyObject *args) {
return result;
}


static PyObject *py_lz4_compressFileDefault(PyObject *self, PyObject *args) {
char* input;
char* output;
int compLevel;
int compLevel = 0;

(void)self;
if (!PyArg_ParseTuple(args, "si", &input, &compLevel)) {
if (!PyArg_ParseTuple(args, "s|i", &input, &compLevel)) {
return NULL;
}

output = (char*)malloc(strlen(input)+4);
strcpy(output, input);
strcat(output, ".lz4");
output = add_extension(input);

LZ4IO_compressFilename(input, output, compLevel);
return Py_None;
}

static PyObject *py_lz4_compressFileAdv(PyObject *self, PyObject *args, PyObject *keywds) {
char* input;
char* output = NULL;
int compLevel = 0;
int overwrite = NULL;
int blockSizeID = NULL;
int blockMode = 1;
int blockCheck = NULL;
int streamCheck = NULL;
int verbosity = NULL;

static char *kwlist[] = {"input", "compLevel", "output", "overwrite",
"blockSizeID", "blockMode", "blockCheck",
"streamCheck", "verbosity", NULL};
(void)self;
if (!PyArg_ParseTupleAndKeywords(args, keywds, "si|siiiii", kwlist,
&input, &compLevel, &output, &overwrite,
&blockSizeID, &blockMode, &blockCheck,
&streamCheck, &verbosity)) {
return NULL;
}

if (!output) {
output = add_extension(input);
}
if (overwrite) {
LZ4IO_setOverwrite(overwrite);
}
if (blockSizeID) {
if ((3 < blockSizeID) && (blockSizeID < 8)) {
LZ4IO_setBlockSizeID(blockSizeID);
}
else {
PyErr_SetString(inputError, "Invalid input for blockSizeID");
}
}
if ( (blockMode == 0) || (blockMode == 1)) {
if (blockMode == 0 ) {
printf("%s", "Getting this far, 2");
LZ4IO_setBlockMode(independentBlocks);
}
printf("%s", "Getting this far, 3");
}
else {
PyErr_SetString(inputError, "Invalid input for blockMode");
return NULL;
}
if (blockCheck) {
}

LZ4IO_compressFilename(input, output, compLevel);
return Py_None;
}

static PyObject *py_lz4_decompressFileDefault(PyObject *self, PyObject *args) {
char* input;
Expand All @@ -163,8 +225,6 @@ static PyObject *py_lz4_decompressFileDefault(PyObject *self, PyObject *args) {
output = (char*)calloc(outLen, sizeof(char));
strncpy(output, input, outLen);

printf("%s \n", output);

LZ4IO_decompressFilename(input, output);
return Py_None;
}
Expand All @@ -178,7 +238,8 @@ static PyMethodDef Lz4Methods[] = {
{"uncompress", py_lz4_uncompress, METH_VARARGS, UNCOMPRESS_DOCSTRING},
{"decompress", py_lz4_uncompress, METH_VARARGS, UNCOMPRESS_DOCSTRING},
{"dumps", py_lz4_compress, METH_VARARGS, COMPRESS_DOCSTRING},
{"loads", py_lz4_uncompress, METH_VARARGS, UNCOMPRESS_DOCSTRING},
{"loads", py_lz4_uncompress, METH_VARARGS, UNCOMPRESS_DOCSTRING},
{"compressFileAdv", (PyCFunction)py_lz4_compressFileAdv, METH_VARARGS | METH_KEYWORDS, COMPRESS_FILE_DOCSTRING},
{"compressFileDefault", py_lz4_compressFileDefault, METH_VARARGS, COMPRESS_FILE_DOCSTRING},
{"decompressFileDefault", py_lz4_decompressFileDefault, METH_VARARGS, DECOMPRESS_FILE_DOCSTRING},
{NULL, NULL, 0, NULL}
Expand All @@ -190,6 +251,7 @@ struct module_state {
PyObject *error;
};


#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
#else
Expand Down Expand Up @@ -248,7 +310,9 @@ void initlz4(void)
Py_DECREF(module);
INITERROR;
}

inputError = PyErr_NewException("input.error", NULL, NULL);
Py_INCREF(inputError);
PyModule_AddObject(module, "error", inputError);
#if PY_MAJOR_VERSION >= 3
return module;
#endif
Expand Down
1 change: 1 addition & 0 deletions src/python-lz4.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

static PyObject *py_lz4_compress(PyObject *self, PyObject *args);
static PyObject *py_lz4_uncompress(PyObject *self, PyObject *args);
static PyObject *py_lz4_compressFileAdv(PyObject *self, PyObject *args, PyObject *keywds);
static PyObject *py_lz4_compressFileDefault(PyObject *self, PyObject *args);
static PyObject *py_lz4_decompressFileDefault(PyObject *self, PyObject *args);

Expand Down

0 comments on commit a5ab2ec

Please sign in to comment.