Skip to content

Commit

Permalink
Merge pull request #12 from t-mat/issue/8
Browse files Browse the repository at this point in the history
Fix issue #8 (partly)
  • Loading branch information
t-mat committed Jun 10, 2013
2 parents 7f753a1 + f9eb3ac commit 54029c2
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 18 deletions.
8 changes: 6 additions & 2 deletions src/lz4mt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,9 @@ lz4mtDecompress(Lz4MtContext* ctx, Lz4MtStreamDescriptor* sd)
);
}
writeBin(ctx, srcPtr, srcSize);
futureStreamHash.wait();
if(futureStreamHash.valid()) {
futureStreamHash.wait();
}
} else {
BufferPtr dst(dstBufferPool.alloc());

Expand All @@ -614,7 +616,9 @@ lz4mtDecompress(Lz4MtContext* ctx, Lz4MtStreamDescriptor* sd)
);
}
writeBin(ctx, dstPtr, decSize);
futureStreamHash.wait();
if(futureStreamHash.valid()) {
futureStreamHash.wait();
}
}

if(futureBlockHash.valid()) {
Expand Down
2 changes: 1 addition & 1 deletion src/lz4mt_compat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# endif
#endif

#if (GCC_VERSION == 406)
#if (GCC_VERSION <= 406)
# define LAUNCH_SINGLE_THREAD std::launch::sync
#else
# define LAUNCH_SINGLE_THREAD std::launch::deferred
Expand Down
18 changes: 15 additions & 3 deletions src/lz4mt_io_cstdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
#include "lz4mt.h"

namespace {
bool isNullFp(const Lz4MtContext* ctx, FILE* fp) {
return reinterpret_cast<const FILE*>(ctx) == fp;
}

FILE* fopen_(const char* filename, const char* mode) {
#if defined(_MSC_VER)
FILE* fp = nullptr;
Expand Down Expand Up @@ -71,9 +75,11 @@ bool openIstream(Lz4MtContext* ctx, const std::string& filename) {
return nullptr != fp;
}

bool openOstream(Lz4MtContext* ctx, const std::string& filename) {
bool openOstream(Lz4MtContext* ctx, const std::string& filename, bool nullWrite) {
FILE* fp = nullptr;
if("stdout" == filename) {
if(nullWrite) {
fp = reinterpret_cast<FILE*>(ctx);
} else if("stdout" == filename) {
fp = getStdout();
} else {
fp = fopen_(filename.c_str(), "wb");
Expand All @@ -88,7 +94,10 @@ void closeIstream(Lz4MtContext* ctx) {
}

void closeOstream(Lz4MtContext* ctx) {
fclose_(writeCtx(ctx));
auto* fp = writeCtx(ctx);
if(!isNullFp(ctx, fp)) {
fclose_(fp);
}
ctx->writeCtx = nullptr;
}

Expand Down Expand Up @@ -129,6 +138,9 @@ int readEof(const Lz4MtContext* ctx) {

int write(const Lz4MtContext* ctx, const void* source, int sourceSize) {
if(auto* fp = writeCtx(ctx)) {
if(isNullFp(ctx, fp)) {
return sourceSize;
}
return static_cast<int>(::fwrite(source, 1, sourceSize, fp));
} else {
return 0;
Expand Down
2 changes: 1 addition & 1 deletion src/lz4mt_io_cstdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Lz4Mt { namespace Cstdio {

bool fileExist(const std::string& filename);
bool openIstream(Lz4MtContext* ctx, const std::string& filename);
bool openOstream(Lz4MtContext* ctx, const std::string& filename);
bool openOstream(Lz4MtContext* ctx, const std::string& filename, bool nullWrite);
void closeIstream(Lz4MtContext* ctx);
void closeOstream(Lz4MtContext* ctx);
int read(Lz4MtContext* ctx, void* dst, int dstSize);
Expand Down
45 changes: 34 additions & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,22 @@ const char usage[] =
" -c1/-hc : Compress (lz4hc)\n"
" -d : Decompress\n"
" -y : Overwrite without prompting\n"
" -s : Single thread mode\n"
" -m : Multi thread mode (default)\n"
" -H : Help (this text + advanced options)\n"
" input : can be 'stdin' (pipe) or a filename\n"
" output : can be 'stdout'(pipe) or a filename\n"// "or 'null'\n"
;

const char usage_advanced[] =
"\nAdvanced options :\n"
// " -t : test compressed file \n"
" -t : decode test mode (do not output anything)\n"
" -B# : Block size [4-7](default : 7)\n"
" -x : enable block checksum (default:disabled)\n"
" -nx : disable stream checksum (default:enabled)\n"
" -BX : enable block checksum (default:disabled)\n"
" -Sx : disable stream checksum (default:enabled)\n"
" -b# : benchmark files, using # [0-1] compression level\n"
" -i# : iteration loops [1-9](default : 3), benchmark mode"
" only\n"
" -s : Single thread mode\n"
" -m : Multi thread mode (default)\n"
" input : can be 'stdin' (pipe) or a filename\n"
" output : can be 'stdout'(pipe) or a filename or 'null'\n"
;

struct Option {
Expand All @@ -56,9 +56,12 @@ struct Option {
, mode(LZ4MT_MODE_DEFAULT)
, inpFilename()
, outFilename()
, nullWrite(false)
, overwrite(false)
, benchmark()
{
static const std::string nullFilename = "null";

std::map<std::string, std::function<void ()>> opts;
opts["-c0"] =
opts["-c" ] = [&] { compMode = CompMode::COMPRESS_C0; };
Expand All @@ -81,8 +84,13 @@ struct Option {
sd.bd.blockMaximumSize = static_cast<char>(i);
};
}
opts["-x" ] = [&] { sd.flg.blockChecksum = 1; };
opts["-nx"] = [&] { sd.flg.streamChecksum = 0; };
opts["-BX"] = [&] { sd.flg.blockChecksum = 1; };
opts["-Sx"] = [&] { sd.flg.streamChecksum = 0; };
opts["-t" ] = [&] {
compMode = CompMode::DECOMPRESS;
outFilename = nullFilename;
};

for(int i = 0; i <= 1; ++i) {
opts["-b" + std::to_string(i)] = [&, i] {
if(i == 0) {
Expand Down Expand Up @@ -119,6 +127,10 @@ struct Option {
error = true;
}
}

if(cmpFilename(nullFilename, outFilename)) {
nullWrite = true;
}
}

bool isCompress() const {
Expand All @@ -130,6 +142,16 @@ struct Option {
return CompMode::DECOMPRESS == compMode;
}

static bool cmpFilename(const std::string& lhs, const std::string& rhs) {
const auto pLhs = lhs.c_str();
const auto pRhs = rhs.c_str();
#if defined(_WIN32)
return 0 == _stricmp(pLhs, pRhs);
#else
return 0 == strcmp(pLhs, pRhs);
#endif
}

enum class CompMode {
DECOMPRESS
, COMPRESS_C0
Expand All @@ -143,6 +165,7 @@ struct Option {
int mode;
std::string inpFilename;
std::string outFilename;
bool nullWrite;
bool overwrite;
Lz4Mt::Benchmark benchmark;
};
Expand Down Expand Up @@ -206,7 +229,7 @@ int main(int argc, char* argv[]) {
exit(EXIT_FAILURE);
}

if(!opt.overwrite && fileExist(opt.outFilename)) {
if(!opt.nullWrite && !opt.overwrite && fileExist(opt.outFilename)) {
const int ch = [&]() -> int {
if("stdin" != opt.inpFilename) {
std::cerr << "Overwrite [y/N]? ";
Expand All @@ -221,7 +244,7 @@ int main(int argc, char* argv[]) {
}
}

if(!openOstream(&ctx, opt.outFilename)) {
if(!openOstream(&ctx, opt.outFilename, opt.nullWrite)) {
std::cerr << "ERROR: Can't open output file ["
<< opt.outFilename << "]\n";
exit(EXIT_FAILURE);
Expand Down

0 comments on commit 54029c2

Please sign in to comment.