Don't use bool in command-line tool (C99 only)

This commit is contained in:
Emmanuel Marty 2020-10-13 16:08:35 +02:00 committed by GitHub
parent a35fc03a27
commit 6d96a8275d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,7 +31,6 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#ifdef _WIN32 #ifdef _WIN32
@ -866,11 +865,11 @@ int main(int argc, char **argv) {
const char *pszInFilename = NULL; const char *pszInFilename = NULL;
const char *pszOutFilename = NULL; const char *pszOutFilename = NULL;
const char *pszDictionaryFilename = NULL; const char *pszDictionaryFilename = NULL;
bool bArgsError = false; int nArgsError = 0;
bool bCommandDefined = false; int nCommandDefined = 0;
bool bVerifyCompression = false; int nVerifyCompression = 0;
bool bMinMatchDefined = false; int nMinMatchDefined = 0;
bool bFormatVersionDefined = false; int nFormatVersionDefined = 0;
char cCommand = 'z'; char cCommand = 'z';
int nMinMatchSize = 0; int nMinMatchSize = 0;
unsigned int nOptions = OPT_FAVOR_RATIO; unsigned int nOptions = OPT_FAVOR_RATIO;
@ -878,51 +877,51 @@ int main(int argc, char **argv) {
for (i = 1; i < argc; i++) { for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-d")) { if (!strcmp(argv[i], "-d")) {
if (!bCommandDefined) { if (!nCommandDefined) {
bCommandDefined = true; nCommandDefined = 1;
cCommand = 'd'; cCommand = 'd';
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strcmp(argv[i], "-z")) { else if (!strcmp(argv[i], "-z")) {
if (!bCommandDefined) { if (!nCommandDefined) {
bCommandDefined = true; nCommandDefined = 1;
cCommand = 'z'; cCommand = 'z';
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strcmp(argv[i], "-c")) { else if (!strcmp(argv[i], "-c")) {
if (!bVerifyCompression) { if (!nVerifyCompression) {
bVerifyCompression = true; nVerifyCompression = 1;
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strcmp(argv[i], "-cbench")) { else if (!strcmp(argv[i], "-cbench")) {
if (!bCommandDefined) { if (!nCommandDefined) {
bCommandDefined = true; nCommandDefined = 1;
cCommand = 'B'; cCommand = 'B';
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strcmp(argv[i], "-dbench")) { else if (!strcmp(argv[i], "-dbench")) {
if (!bCommandDefined) { if (!nCommandDefined) {
bCommandDefined = true; nCommandDefined = 1;
cCommand = 'b'; cCommand = 'b';
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strcmp(argv[i], "-test")) { else if (!strcmp(argv[i], "-test")) {
if (!bCommandDefined) { if (!nCommandDefined) {
bCommandDefined = true; nCommandDefined = 1;
cCommand = 't'; cCommand = 't';
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strcmp(argv[i], "-D")) { else if (!strcmp(argv[i], "-D")) {
if (!pszDictionaryFilename && (i + 1) < argc) { if (!pszDictionaryFilename && (i + 1) < argc) {
@ -930,119 +929,119 @@ int main(int argc, char **argv) {
i++; i++;
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strncmp(argv[i], "-D", 2)) { else if (!strncmp(argv[i], "-D", 2)) {
if (!pszDictionaryFilename) { if (!pszDictionaryFilename) {
pszDictionaryFilename = argv[i] + 2; pszDictionaryFilename = argv[i] + 2;
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strcmp(argv[i], "-m")) { else if (!strcmp(argv[i], "-m")) {
if (!bMinMatchDefined && (i + 1) < argc) { if (!nMinMatchDefined && (i + 1) < argc) {
char *pEnd = NULL; char *pEnd = NULL;
nMinMatchSize = (int)strtol(argv[i + 1], &pEnd, 10); nMinMatchSize = (int)strtol(argv[i + 1], &pEnd, 10);
if (pEnd && pEnd != argv[i + 1] && (nMinMatchSize >= 2 && nMinMatchSize <= 5)) { if (pEnd && pEnd != argv[i + 1] && (nMinMatchSize >= 2 && nMinMatchSize <= 5)) {
i++; i++;
bMinMatchDefined = true; nMinMatchDefined = 1;
nOptions &= (~OPT_FAVOR_RATIO); nOptions &= (~OPT_FAVOR_RATIO);
} }
else { else {
bArgsError = true; nArgsError = 1;
} }
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strncmp(argv[i], "-m", 2)) { else if (!strncmp(argv[i], "-m", 2)) {
if (!bMinMatchDefined) { if (!nMinMatchDefined) {
char *pEnd = NULL; char *pEnd = NULL;
nMinMatchSize = (int)strtol(argv[i] + 2, &pEnd, 10); nMinMatchSize = (int)strtol(argv[i] + 2, &pEnd, 10);
if (pEnd && pEnd != (argv[i]+2) && (nMinMatchSize >= 2 && nMinMatchSize <= 5)) { if (pEnd && pEnd != (argv[i]+2) && (nMinMatchSize >= 2 && nMinMatchSize <= 5)) {
bMinMatchDefined = true; nMinMatchDefined = 1;
nOptions &= (~OPT_FAVOR_RATIO); nOptions &= (~OPT_FAVOR_RATIO);
} }
else { else {
bArgsError = true; nArgsError = 1;
} }
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strcmp(argv[i], "--prefer-ratio")) { else if (!strcmp(argv[i], "--prefer-ratio")) {
if (!bMinMatchDefined) { if (!nMinMatchDefined) {
nMinMatchSize = 0; nMinMatchSize = 0;
bMinMatchDefined = true; nMinMatchDefined = 1;
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strcmp(argv[i], "--prefer-speed")) { else if (!strcmp(argv[i], "--prefer-speed")) {
if (!bMinMatchDefined) { if (!nMinMatchDefined) {
nMinMatchSize = 3; nMinMatchSize = 3;
nOptions &= (~OPT_FAVOR_RATIO); nOptions &= (~OPT_FAVOR_RATIO);
bMinMatchDefined = true; nMinMatchDefined = 1;
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strcmp(argv[i], "-f")) { else if (!strcmp(argv[i], "-f")) {
if (!bFormatVersionDefined && (i + 1) < argc) { if (!nFormatVersionDefined && (i + 1) < argc) {
char *pEnd = NULL; char *pEnd = NULL;
nFormatVersion = (int)strtol(argv[i + 1], &pEnd, 10); nFormatVersion = (int)strtol(argv[i + 1], &pEnd, 10);
if (pEnd && pEnd != argv[i + 1] && (nFormatVersion >= 1 && nFormatVersion <= 2)) { if (pEnd && pEnd != argv[i + 1] && (nFormatVersion >= 1 && nFormatVersion <= 2)) {
i++; i++;
bFormatVersionDefined = true; nFormatVersionDefined = 1;
} }
else { else {
bArgsError = true; nArgsError = 1;
} }
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strncmp(argv[i], "-f", 2)) { else if (!strncmp(argv[i], "-f", 2)) {
if (!bFormatVersionDefined) { if (!nFormatVersionDefined) {
char *pEnd = NULL; char *pEnd = NULL;
nFormatVersion = (int)strtol(argv[i] + 2, &pEnd, 10); nFormatVersion = (int)strtol(argv[i] + 2, &pEnd, 10);
if (pEnd && pEnd != (argv[i] + 2) && (nFormatVersion >= 1 && nFormatVersion <= 2)) { if (pEnd && pEnd != (argv[i] + 2) && (nFormatVersion >= 1 && nFormatVersion <= 2)) {
bFormatVersionDefined = true; nFormatVersionDefined = 1;
} }
else { else {
bArgsError = true; nArgsError = 1;
} }
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strcmp(argv[i], "-v")) { else if (!strcmp(argv[i], "-v")) {
if ((nOptions & OPT_VERBOSE) == 0) { if ((nOptions & OPT_VERBOSE) == 0) {
nOptions |= OPT_VERBOSE; nOptions |= OPT_VERBOSE;
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strcmp(argv[i], "-r")) { else if (!strcmp(argv[i], "-r")) {
if ((nOptions & OPT_RAW) == 0) { if ((nOptions & OPT_RAW) == 0) {
nOptions |= OPT_RAW; nOptions |= OPT_RAW;
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strcmp(argv[i], "-b")) { else if (!strcmp(argv[i], "-b")) {
if ((nOptions & OPT_RAW_BACKWARD) == 0) { if ((nOptions & OPT_RAW_BACKWARD) == 0) {
nOptions |= OPT_RAW_BACKWARD; nOptions |= OPT_RAW_BACKWARD;
} }
else else
bArgsError = true; nArgsError = 1;
} }
else if (!strcmp(argv[i], "-stats")) { else if (!strcmp(argv[i], "-stats")) {
if ((nOptions & OPT_STATS) == 0) { if ((nOptions & OPT_STATS) == 0) {
nOptions |= OPT_STATS; nOptions |= OPT_STATS;
} }
else else
bArgsError = true; nArgsError = 1;
} }
else { else {
if (!pszInFilename) if (!pszInFilename)
@ -1051,21 +1050,21 @@ int main(int argc, char **argv) {
if (!pszOutFilename) if (!pszOutFilename)
pszOutFilename = argv[i]; pszOutFilename = argv[i];
else else
bArgsError = true; nArgsError = 1;
} }
} }
} }
if (!bArgsError && (nOptions & OPT_RAW_BACKWARD) && !(nOptions & OPT_RAW)) { if (!nArgsError && (nOptions & OPT_RAW_BACKWARD) && !(nOptions & OPT_RAW)) {
fprintf(stderr, "error: -b (compress backwards) requires -r (raw block format)\n"); fprintf(stderr, "error: -b (compress backwards) requires -r (raw block format)\n");
return 100; return 100;
} }
if (!bArgsError && cCommand == 't') { if (!nArgsError && cCommand == 't') {
return do_self_test(nOptions, nMinMatchSize, nFormatVersion); return do_self_test(nOptions, nMinMatchSize, nFormatVersion);
} }
if (bArgsError || !pszInFilename || !pszOutFilename) { if (nArgsError || !pszInFilename || !pszOutFilename) {
fprintf(stderr, "lzsa command-line tool v" TOOL_VERSION " by Emmanuel Marty and spke\n"); fprintf(stderr, "lzsa command-line tool v" TOOL_VERSION " by Emmanuel Marty and spke\n");
fprintf(stderr, "usage: %s [-c] [-d] [-v] [-r] <infile> <outfile>\n", argv[0]); fprintf(stderr, "usage: %s [-c] [-d] [-v] [-r] <infile> <outfile>\n", argv[0]);
fprintf(stderr, " -c: check resulting stream after compressing\n"); fprintf(stderr, " -c: check resulting stream after compressing\n");
@ -1089,7 +1088,7 @@ int main(int argc, char **argv) {
if (cCommand == 'z') { if (cCommand == 'z') {
int nResult = do_compress(pszInFilename, pszOutFilename, pszDictionaryFilename, nOptions, nMinMatchSize, nFormatVersion); int nResult = do_compress(pszInFilename, pszOutFilename, pszDictionaryFilename, nOptions, nMinMatchSize, nFormatVersion);
if (nResult == 0 && bVerifyCompression) { if (nResult == 0 && nVerifyCompression) {
return do_compare(pszOutFilename, pszInFilename, pszDictionaryFilename, nOptions, nFormatVersion); return do_compare(pszOutFilename, pszInFilename, pszDictionaryFilename, nOptions, nFormatVersion);
} else { } else {
return nResult; return nResult;