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