📄 lzmaalone.cpp
字号:
// LzmaAlone.cpp
#include "StdAfx.h"
#include "../../../Common/MyWindows.h"
#include "../../../Common/MyInitGuid.h"
#include <stdio.h>
#if defined(_WIN32) || defined(OS2) || defined(MSDOS)
#include <fcntl.h>
#include <io.h>
#define MY_SET_BINARY_MODE(file) _setmode(_fileno(file), O_BINARY)
#else
#define MY_SET_BINARY_MODE(file)
#endif
#include "../../../Common/CommandLineParser.h"
#include "../../../Common/StringConvert.h"
#include "../../../Common/StringToInt.h"
#include "../../Common/FileStreams.h"
#include "../../Common/StreamUtils.h"
#include "../LZMA/LZMADecoder.h"
#include "../LZMA/LZMAEncoder.h"
#include "LzmaBenchCon.h"
#include "LzmaRam.h"
#ifdef COMPRESS_MF_MT
#include "../../../Windows/System.h"
#endif
#include "../../MyVersion.h"
extern "C"
{
#include "LzmaRamDecode.h"
}
using namespace NCommandLineParser;
#ifdef _WIN32
bool g_IsNT = false;
static inline bool IsItWindowsNT()
{
OSVERSIONINFO versionInfo;
versionInfo.dwOSVersionInfoSize = sizeof(versionInfo);
if (!::GetVersionEx(&versionInfo))
return false;
return (versionInfo.dwPlatformId == VER_PLATFORM_WIN32_NT);
}
#endif
static const char *kCantAllocate = "Can not allocate memory";
static const char *kReadError = "Read error";
static const char *kWriteError = "Write error";
namespace NKey {
enum Enum
{
kHelp1 = 0,
kHelp2,
kMode,
kDictionary,
kFastBytes,
kMatchFinderCycles,
kLitContext,
kLitPos,
kPosBits,
kMatchFinder,
kMultiThread,
kEOS,
kStdIn,
kStdOut,
kFilter86
};
}
static const CSwitchForm kSwitchForms[] =
{
{ L"?", NSwitchType::kSimple, false },
{ L"H", NSwitchType::kSimple, false },
{ L"A", NSwitchType::kUnLimitedPostString, false, 1 },
{ L"D", NSwitchType::kUnLimitedPostString, false, 1 },
{ L"FB", NSwitchType::kUnLimitedPostString, false, 1 },
{ L"MC", NSwitchType::kUnLimitedPostString, false, 1 },
{ L"LC", NSwitchType::kUnLimitedPostString, false, 1 },
{ L"LP", NSwitchType::kUnLimitedPostString, false, 1 },
{ L"PB", NSwitchType::kUnLimitedPostString, false, 1 },
{ L"MF", NSwitchType::kUnLimitedPostString, false, 1 },
{ L"MT", NSwitchType::kUnLimitedPostString, false, 0 },
{ L"EOS", NSwitchType::kSimple, false },
{ L"SI", NSwitchType::kSimple, false },
{ L"SO", NSwitchType::kSimple, false },
{ L"F86", NSwitchType::kPostChar, false, 0, 0, L"+" }
};
static const int kNumSwitches = sizeof(kSwitchForms) / sizeof(kSwitchForms[0]);
static void PrintHelp()
{
fprintf(stderr, "\nUsage: LZMA <e|d> inputFile outputFile [<switches>...]\n"
" e: encode file\n"
" d: decode file\n"
" b: Benchmark\n"
"<Switches>\n"
" -a{N}: set compression mode - [0, 1], default: 1 (max)\n"
" -d{N}: set dictionary - [0,30], default: 23 (8MB)\n"
" -fb{N}: set number of fast bytes - [5, 273], default: 128\n"
" -mc{N}: set number of cycles for match finder\n"
" -lc{N}: set number of literal context bits - [0, 8], default: 3\n"
" -lp{N}: set number of literal pos bits - [0, 4], default: 0\n"
" -pb{N}: set number of pos bits - [0, 4], default: 2\n"
" -mf{MF_ID}: set Match Finder: [bt2, bt3, bt4, hc4], default: bt4\n"
" -mt{N}: set number of CPU threads\n"
" -eos: write End Of Stream marker\n"
" -si: read data from stdin\n"
" -so: write data to stdout\n"
);
}
static void PrintHelpAndExit(const char *s)
{
fprintf(stderr, "\nError: %s\n\n", s);
PrintHelp();
throw -1;
}
static void IncorrectCommand()
{
PrintHelpAndExit("Incorrect command");
}
static void WriteArgumentsToStringList(int numArguments, const char *arguments[],
UStringVector &strings)
{
for(int i = 1; i < numArguments; i++)
strings.Add(MultiByteToUnicodeString(arguments[i]));
}
static bool GetNumber(const wchar_t *s, UInt32 &value)
{
value = 0;
if (MyStringLen(s) == 0)
return false;
const wchar_t *end;
UInt64 res = ConvertStringToUInt64(s, &end);
if (*end != L'\0')
return false;
if (res > 0xFFFFFFFF)
return false;
value = UInt32(res);
return true;
}
int main2(int n, const char *args[])
{
#ifdef _WIN32
g_IsNT = IsItWindowsNT();
#endif
fprintf(stderr, "\nLZMA " MY_VERSION_COPYRIGHT_DATE "\n");
if (n == 1)
{
PrintHelp();
return 0;
}
bool unsupportedTypes = (sizeof(Byte) != 1 || sizeof(UInt32) < 4 || sizeof(UInt64) < 4);
if (unsupportedTypes)
{
fprintf(stderr, "Unsupported base types. Edit Common/Types.h and recompile");
return 1;
}
UStringVector commandStrings;
WriteArgumentsToStringList(n, args, commandStrings);
CParser parser(kNumSwitches);
try
{
parser.ParseStrings(kSwitchForms, commandStrings);
}
catch(...)
{
IncorrectCommand();
}
if(parser[NKey::kHelp1].ThereIs || parser[NKey::kHelp2].ThereIs)
{
PrintHelp();
return 0;
}
const UStringVector &nonSwitchStrings = parser.NonSwitchStrings;
int paramIndex = 0;
if (paramIndex >= nonSwitchStrings.Size())
IncorrectCommand();
const UString &command = nonSwitchStrings[paramIndex++];
bool dictionaryIsDefined = false;
UInt32 dictionary = (UInt32)-1;
if(parser[NKey::kDictionary].ThereIs)
{
UInt32 dicLog;
if (!GetNumber(parser[NKey::kDictionary].PostStrings[0], dicLog))
IncorrectCommand();
dictionary = 1 << dicLog;
dictionaryIsDefined = true;
}
UString mf = L"BT4";
if (parser[NKey::kMatchFinder].ThereIs)
mf = parser[NKey::kMatchFinder].PostStrings[0];
UInt32 numThreads = (UInt32)-1;
#ifdef COMPRESS_MF_MT
if (parser[NKey::kMultiThread].ThereIs)
{
UInt32 numCPUs = NWindows::NSystem::GetNumberOfProcessors();
const UString &s = parser[NKey::kMultiThread].PostStrings[0];
if (s.IsEmpty())
numThreads = numCPUs;
else
if (!GetNumber(s, numThreads))
IncorrectCommand();
}
#endif
if (command.CompareNoCase(L"b") == 0)
{
const UInt32 kNumDefaultItereations = 1;
UInt32 numIterations = kNumDefaultItereations;
{
if (paramIndex < nonSwitchStrings.Size())
if (!GetNumber(nonSwitchStrings[paramIndex++], numIterations))
numIterations = kNumDefaultItereations;
}
return LzmaBenchCon(stderr, numIterations, numThreads, dictionary);
}
if (numThreads == (UInt32)-1)
numThreads = 1;
bool encodeMode = false;
if (command.CompareNoCase(L"e") == 0)
encodeMode = true;
else if (command.CompareNoCase(L"d") == 0)
encodeMode = false;
else
IncorrectCommand();
bool stdInMode = parser[NKey::kStdIn].ThereIs;
bool stdOutMode = parser[NKey::kStdOut].ThereIs;
CMyComPtr<ISequentialInStream> inStream;
CInFileStream *inStreamSpec = 0;
if (stdInMode)
{
inStream = new CStdInFileStream;
MY_SET_BINARY_MODE(stdin);
}
else
{
if (paramIndex >= nonSwitchStrings.Size())
IncorrectCommand();
const UString &inputName = nonSwitchStrings[paramIndex++];
inStreamSpec = new CInFileStream;
inStream = inStreamSpec;
if (!inStreamSpec->Open(GetSystemString(inputName)))
{
fprintf(stderr, "\nError: can not open input file %s\n",
(const char *)GetOemString(inputName));
return 1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -