1 /*
2  * Copyright (c) 2016-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under both the BSD-style license (found in the
6  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7  * in the COPYING file in the root directory of this source tree).
8  */
9 #pragma once
10 
11 #define ZSTD_STATIC_LINKING_ONLY
12 #include "zstd.h"
13 #undef ZSTD_STATIC_LINKING_ONLY
14 
15 #include <cstdint>
16 #include <string>
17 #include <vector>
18 
19 namespace pzstd {
20 
21 struct Options {
22   enum class WriteMode { Regular, Auto, Sparse };
23 
24   unsigned numThreads;
25   unsigned maxWindowLog;
26   unsigned compressionLevel;
27   bool decompress;
28   std::vector<std::string> inputFiles;
29   std::string outputFile;
30   bool overwrite;
31   bool keepSource;
32   WriteMode writeMode;
33   bool checksum;
34   int verbosity;
35 
36   enum class Status {
37     Success, // Successfully parsed options
38     Failure, // Failure to parse options
39     Message  // Options specified to print a message (e.g. "-h")
40   };
41 
42   Options();
OptionsOptions43   Options(unsigned numThreads, unsigned maxWindowLog, unsigned compressionLevel,
44           bool decompress, std::vector<std::string> inputFiles,
45           std::string outputFile, bool overwrite, bool keepSource,
46           WriteMode writeMode, bool checksum, int verbosity)
47       : numThreads(numThreads), maxWindowLog(maxWindowLog),
48         compressionLevel(compressionLevel), decompress(decompress),
49         inputFiles(std::move(inputFiles)), outputFile(std::move(outputFile)),
50         overwrite(overwrite), keepSource(keepSource), writeMode(writeMode),
51         checksum(checksum), verbosity(verbosity) {}
52 
53   Status parse(int argc, const char **argv);
54 
determineParametersOptions55   ZSTD_parameters determineParameters() const {
56     ZSTD_parameters params = ZSTD_getParams(compressionLevel, 0, 0);
57     params.fParams.contentSizeFlag = 0;
58     params.fParams.checksumFlag = checksum;
59     if (maxWindowLog != 0 && params.cParams.windowLog > maxWindowLog) {
60       params.cParams.windowLog = maxWindowLog;
61       params.cParams = ZSTD_adjustCParams(params.cParams, 0, 0);
62     }
63     return params;
64   }
65 
66   std::string getOutputFile(const std::string &inputFile) const;
67 };
68 }
69