1 /*
2     datagencli.c
3     compressible data command line generator
4     Copyright (C) Yann Collet 2012-2016
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 *  Constants
38 **************************************/
39 #define KB *(1 <<10)
40 #define MB *(1 <<20)
41 #define GB *(1U<<30)
42 
43 #define SIZE_DEFAULT (64 KB)
44 #define SEED_DEFAULT 0
45 #define COMPRESSIBILITY_DEFAULT 50
46 
47 
48 /**************************************
49 *  Macros
50 **************************************/
51 #define DISPLAY(...)         fprintf(stderr, __VA_ARGS__)
52 #define DISPLAYLEVEL(l, ...) if (displayLevel>=l) { DISPLAY(__VA_ARGS__); }
53 static unsigned displayLevel = 2;
54 
55 
56 /*********************************************************
57 *  Command line
58 *********************************************************/
usage(char * programName)59 static int usage(char* programName)
60 {
61     DISPLAY( "Compressible data generator\n");
62     DISPLAY( "Usage :\n");
63     DISPLAY( "      %s [size] [args]\n", programName);
64     DISPLAY( "\n");
65     DISPLAY( "Arguments :\n");
66     DISPLAY( " -g#    : generate # data (default:%i)\n", SIZE_DEFAULT);
67     DISPLAY( " -s#    : Select seed (default:%i)\n", SEED_DEFAULT);
68     DISPLAY( " -P#    : Select compressibility in %% (default:%i%%)\n", COMPRESSIBILITY_DEFAULT);
69     DISPLAY( " -h     : display help and exit\n");
70     DISPLAY( "Special values :\n");
71     DISPLAY( " -P0    : generate incompressible noise\n");
72     DISPLAY( " -P100  : generate sparse files\n");
73     return 0;
74 }
75 
76 
main(int argc,char ** argv)77 int main(int argc, char** argv)
78 {
79     int argNb;
80     double proba = (double)COMPRESSIBILITY_DEFAULT / 100;
81     double litProba = 0.0;
82     U64 size = SIZE_DEFAULT;
83     U32 seed = SEED_DEFAULT;
84     char* programName;
85 
86     /* Check command line */
87     programName = argv[0];
88     for(argNb=1; argNb<argc; argNb++)
89     {
90         char* argument = argv[argNb];
91 
92         if(!argument) continue;   /* Protection if argument empty */
93 
94         /* Handle commands. Aggregated commands are allowed */
95         if (*argument=='-')
96         {
97             argument++;
98             while (*argument!=0)
99             {
100                 switch(*argument)
101                 {
102                 case 'h':
103                     return usage(programName);
104                 case 'g':
105                     argument++;
106                     size=0;
107                     while ((*argument>='0') && (*argument<='9'))
108                     {
109                         size *= 10;
110                         size += *argument - '0';
111                         argument++;
112                     }
113                     if (*argument=='K') { size <<= 10; argument++; }
114                     if (*argument=='M') { size <<= 20; argument++; }
115                     if (*argument=='G') { size <<= 30; argument++; }
116                     if (*argument=='B') { argument++; }
117                     break;
118                 case 's':
119                     argument++;
120                     seed=0;
121                     while ((*argument>='0') && (*argument<='9'))
122                     {
123                         seed *= 10;
124                         seed += *argument - '0';
125                         argument++;
126                     }
127                     break;
128                 case 'P':
129                     argument++;
130                     proba=0.0;
131                     while ((*argument>='0') && (*argument<='9'))
132                     {
133                         proba *= 10;
134                         proba += *argument - '0';
135                         argument++;
136                     }
137                     if (proba>100.) proba=100.;
138                     proba /= 100.;
139                     break;
140                 case 'L':   /* hidden argument : Literal distribution probability */
141                     argument++;
142                     litProba=0.;
143                     while ((*argument>='0') && (*argument<='9'))
144                     {
145                         litProba *= 10;
146                         litProba += *argument - '0';
147                         argument++;
148                     }
149                     if (litProba>100.) litProba=100.;
150                     litProba /= 100.;
151                     break;
152                 case 'v':
153                     displayLevel = 4;
154                     argument++;
155                     break;
156                 default:
157                     return usage(programName);
158                 }
159             }
160 
161         }
162     }
163 
164     DISPLAYLEVEL(4, "Data Generator %s \n", LZ4_VERSION_STRING);
165     DISPLAYLEVEL(3, "Seed = %u \n", seed);
166     if (proba!=COMPRESSIBILITY_DEFAULT) DISPLAYLEVEL(3, "Compressibility : %i%%\n", (U32)(proba*100));
167 
168     RDG_genOut(size, proba, litProba, seed);
169     DISPLAYLEVEL(1, "\n");
170 
171     return 0;
172 }
173