lz4
1/*
2datagencli.c
3compressible data command line generator
4Copyright (C) Yann Collet 2012-2020
5
6GPL v2 License
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License along
19with this program; if not, write to the Free Software Foundation, Inc.,
2051 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
22You can contact the author at :
23- LZ4 source repository : https://github.com/lz4/lz4
24- Public forum : https://groups.google.com/forum/#!forum/lz4c
25*/
26
27
28/**************************************
29* Includes
30**************************************/
31#include "util.h" /* U32 */32#include <stdio.h> /* fprintf, stderr */33#include "datagen.h" /* RDG_generate */34#include "loremOut.h" /* LOREM_genOut */35#include "lz4.h" /* LZ4_VERSION_STRING */36
37
38/**************************************
39* Compiler specific
40**************************************/
41#ifdef _MSC_VER /* Visual Studio */42# pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */43# define strtoull _strtoui64 /* https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/strtoui64-wcstoui64-strtoui64-l-wcstoui64-l */44#endif45
46
47/**************************************
48* Constants
49**************************************/
50#define KB *(1 <<10)51#define MB *(1 <<20)52#define GB *(1U<<30)53
54#define SIZE_DEFAULT (64 KB)55#define SEED_DEFAULT 056#define COMPRESSIBILITY_NOT_SET 999957
58
59/**************************************
60* Macros
61**************************************/
62#define DISPLAY(...) fprintf(stderr, __VA_ARGS__)63#define DISPLAYLEVEL(l, ...) do { if (displayLevel>=(l)) DISPLAY(__VA_ARGS__); } while (0)64static unsigned displayLevel = 2;65
66
67/*********************************************************
68* Command line
69*********************************************************/
70static int usage(char* programName)71{
72DISPLAY( "Compressible data generator\n");73DISPLAY( "Usage :\n");74DISPLAY( " %s [size] [args]\n", programName);75DISPLAY( "\n");76DISPLAY( "Arguments :\n");77DISPLAY( " -g# : generate # data (default:%i)\n", SIZE_DEFAULT);78DISPLAY( " -s# : Select seed (default:%i)\n", SEED_DEFAULT);79DISPLAY( " -P# : Select compressibility in %% (range [0-100])\n");80DISPLAY( " -h : display help and exit\n");81DISPLAY( "Special values :\n");82DISPLAY( " -P0 : generate incompressible noise\n");83DISPLAY( " -P100 : generate sparse files\n");84return 0;85}
86
87
88int main(int argc, char** argv)89{
90int argNb;91unsigned long long proba = COMPRESSIBILITY_NOT_SET;92double litProba = 0.0;93U64 size = SIZE_DEFAULT;94unsigned seed = SEED_DEFAULT;95char* programName;96
97/* Check command line */98programName = argv[0];99for(argNb=1; argNb<argc; argNb++) {100char* argument = argv[argNb];101
102if(!argument) continue; /* Protection if argument empty */103
104/* Handle commands. Aggregated commands are allowed */105if (*argument=='-') {106argument++;107while (*argument!=0) {108switch(*argument) {109case 'h':110return usage(programName);111case 'g':112argument++;113size = strtoull(argument, &argument, 10);114if (*argument=='K') { size <<= 10; argument++; }115if (*argument=='M') { size <<= 20; argument++; }116if (*argument=='G') { size <<= 30; argument++; }117if (*argument=='B') { argument++; }118break;119case 's':120argument++;121seed = (unsigned)strtoul(argument, &argument, 10);122break;123case 'P':124argument++;125proba = strtoull(argument, &argument, 10);126break;127case 'L': /* hidden argument : Literal distribution probability */128argument++;129litProba = (double) strtoull(argument, &argument, 10);130if (litProba>100.) litProba=100.;131litProba /= 100.;132break;133case 'v':134displayLevel = 4;135argument++;136break;137default:138return usage(programName);139}140}141} /* if (*argument=='-') */142} /* for(argNb=1; argNb<argc; argNb++) */143
144DISPLAYLEVEL(4, "Data Generator %s \n", LZ4_VERSION_STRING);145DISPLAYLEVEL(3, "Seed = %u \n", seed);146if (proba != COMPRESSIBILITY_NOT_SET) {147DISPLAYLEVEL(3, "Compressibility : %i%%\n", (int)proba);148RDG_genOut(size, (double)proba / 100., litProba, seed);149} else {150LOREM_genOut(size, seed);151}152DISPLAYLEVEL(1, "\n");153
154return 0;155}
156