git subrepo pull (merge) --force deps/libchdr
[pcsx_rearmed.git] / deps / libchdr / deps / zstd-1.5.6 / tests / datagencli.c
CommitLineData
f535537f 1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
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 * You may select, at your option, one of the above-listed licenses.
9 */
10
11/*-************************************
12 * Dependencies
13 **************************************/
14#include <stdio.h> /* fprintf, stderr */
15#include "datagen.h" /* RDG_generate */
16#include "loremOut.h" /* LOREM_genOut */
17#include "util.h" /* Compiler options */
18
19/*-************************************
20 * Constants
21 **************************************/
22#define KB *(1 << 10)
23#define MB *(1 << 20)
24#define GB *(1U << 30)
25
26#define SIZE_DEFAULT ((64 KB) + 1)
27#define SEED_DEFAULT 0
28#define COMPRESSIBILITY_DEFAULT 9999
29
30/*-************************************
31 * Macros
32 **************************************/
33#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)
34#define DISPLAYLEVEL(l, ...) \
35 if (displayLevel >= l) { \
36 DISPLAY(__VA_ARGS__); \
37 }
38static unsigned displayLevel = 2;
39
40/*-*******************************************************
41 * Command line
42 *********************************************************/
43static int usage(const char* programName)
44{
45 DISPLAY("Compressible data generator\n");
46 DISPLAY("Usage :\n");
47 DISPLAY(" %s [args]\n", programName);
48 DISPLAY("\n");
49 DISPLAY("Arguments :\n");
50 DISPLAY(" -g# : generate # data (default:%i)\n", SIZE_DEFAULT);
51 DISPLAY(" -s# : Select seed (default:%i)\n", SEED_DEFAULT);
52 DISPLAY(" -P# : Select compressibility in %% (range [0-100])\n");
53 DISPLAY(" -h : display help and exit\n");
54 return 0;
55}
56
57int main(int argc, const char** argv)
58{
59 unsigned probaU32 = COMPRESSIBILITY_DEFAULT;
60 double litProba = 0.0;
61 U64 size = SIZE_DEFAULT;
62 U32 seed = SEED_DEFAULT;
63 const char* const programName = argv[0];
64
65 int argNb;
66 for (argNb = 1; argNb < argc; argNb++) {
67 const char* argument = argv[argNb];
68
69 if (!argument)
70 continue; /* Protection if argument empty */
71
72 /* Handle commands. Aggregated commands are allowed */
73 if (*argument == '-') {
74 argument++;
75 while (*argument != 0) {
76 switch (*argument) {
77 case 'h':
78 return usage(programName);
79 case 'g':
80 argument++;
81 size = 0;
82 while ((*argument >= '0') && (*argument <= '9'))
83 size *= 10, size += (U64)(*argument++ - '0');
84 if (*argument == 'K') {
85 size <<= 10;
86 argument++;
87 }
88 if (*argument == 'M') {
89 size <<= 20;
90 argument++;
91 }
92 if (*argument == 'G') {
93 size <<= 30;
94 argument++;
95 }
96 if (*argument == 'B') {
97 argument++;
98 }
99 break;
100 case 's':
101 argument++;
102 seed = 0;
103 while ((*argument >= '0') && (*argument <= '9'))
104 seed *= 10, seed += (U32)(*argument++ - '0');
105 break;
106 case 'P':
107 argument++;
108 probaU32 = 0;
109 while ((*argument >= '0') && (*argument <= '9'))
110 probaU32 *= 10,
111 probaU32 += (U32)(*argument++ - '0');
112 if (probaU32 > 100)
113 probaU32 = 100;
114 break;
115 case 'L': /* hidden argument : Literal distribution
116 probability */
117 argument++;
118 litProba = 0.;
119 while ((*argument >= '0') && (*argument <= '9'))
120 litProba *= 10, litProba += *argument++ - '0';
121 if (litProba > 100.)
122 litProba = 100.;
123 litProba /= 100.;
124 break;
125 case 'v':
126 displayLevel = 4;
127 argument++;
128 break;
129 default:
130 return usage(programName);
131 }
132 }
133 }
134 } /* for(argNb=1; argNb<argc; argNb++) */
135
136 DISPLAYLEVEL(4, "Compressible data Generator \n");
137 DISPLAYLEVEL(3, "Seed = %u \n", (unsigned)seed);
138
139 if (probaU32 != COMPRESSIBILITY_DEFAULT) {
140 DISPLAYLEVEL(3, "Compressibility : %i%%\n", probaU32);
141 RDG_genStdout(size, (double)probaU32 / 100, litProba, seed);
142 } else {
143 LOREM_genOut(size, seed);
144 }
145
146 DISPLAYLEVEL(3, "\n");
147
148 return 0;
149}