zstd/dictBuilder/dibcli.c

248 lines
9.1 KiB
C
Raw Normal View History

2016-01-27 23:29:58 +00:00
/*
dibcli - Command Line Interface (cli) for Dictionary Builder
Copyright (C) Yann Collet 2016
GPL v2 License
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
You can contact the author at :
- zstd source repository : https://github.com/Cyan4973/zstd
*/
2016-01-29 01:45:26 +00:00
/*-************************************
2016-01-27 23:29:58 +00:00
* Includes
**************************************/
#include <stdlib.h> /* exit, calloc, free */
#include <string.h> /* strcmp, strlen */
2016-01-29 01:45:26 +00:00
#include <stdio.h> /* fprintf, getchar */
2016-01-27 23:29:58 +00:00
#include "dictBuilder.h"
2016-01-29 01:45:26 +00:00
/*-************************************
2016-01-27 23:29:58 +00:00
* Constants
**************************************/
#define PROGRAM_DESCRIPTION "Dictionary builder"
#ifndef PROGRAM_VERSION
# define QUOTE(str) #str
# define EXP_Q(str) QUOTE(str)
# define PROGRAM_VERSION "v" EXP_Q(DiB_VERSION_MAJOR) "." EXP_Q(DiB_VERSION_MINOR) "." EXP_Q(DiB_VERSION_RELEASE)
#endif
#define AUTHOR "Yann Collet"
#define WELCOME_MESSAGE "*** %s %s %i-bits, by %s ***\n", PROGRAM_DESCRIPTION, PROGRAM_VERSION, (int)(sizeof(void*)*8), AUTHOR
#define KB *(1 <<10)
#define MB *(1 <<20)
#define GB *(1U<<30)
2016-01-29 01:45:26 +00:00
static const unsigned compressionLevelDefault = 5;
2016-01-27 23:29:58 +00:00
static const unsigned selectionLevelDefault = 9; /* determined experimentally */
static const unsigned maxDictSizeDefault = 110 KB;
static const char* dictFileNameDefault = "dictionary";
2016-01-29 01:45:26 +00:00
/*-************************************
2016-01-27 23:29:58 +00:00
* Display Macros
**************************************/
2016-01-29 01:45:26 +00:00
#define DISPLAY(...) fprintf(g_displayOut, __VA_ARGS__)
#define DISPLAYLEVEL(l, ...) if (g_displayLevel>=l) { DISPLAY(__VA_ARGS__); }
static FILE* g_displayOut;
static unsigned g_displayLevel = 2; // 0 : no display // 1: errors // 2 : + result + interaction + warnings ; // 3 : + progression; // 4 : + information
2016-01-27 23:29:58 +00:00
2016-01-29 01:45:26 +00:00
/*-************************************
2016-01-27 23:29:58 +00:00
* Exceptions
**************************************/
#define DEBUG 0
#define DEBUGOUTPUT(...) if (DEBUG) DISPLAY(__VA_ARGS__);
#define EXM_THROW(error, ...) \
{ \
DEBUGOUTPUT("Error defined at %s, line %i : \n", __FILE__, __LINE__); \
DISPLAYLEVEL(1, "Error %i : ", error); \
DISPLAYLEVEL(1, __VA_ARGS__); \
DISPLAYLEVEL(1, "\n"); \
exit(error); \
}
2016-01-29 01:45:26 +00:00
/*-************************************
2016-01-27 23:29:58 +00:00
* Command Line
**************************************/
static int usage(const char* programName)
{
DISPLAY( "Usage :\n");
DISPLAY( " %s [arg] [filenames]\n", programName);
DISPLAY( "\n");
DISPLAY( "Arguments :\n");
DISPLAY( " -o : name of dictionary file (default: %s) \n", dictFileNameDefault);
2016-01-29 01:45:26 +00:00
DISPLAY( "--maxdict : limit dictionary to specified size (default : %u) \n", maxDictSizeDefault);
2016-01-27 23:29:58 +00:00
DISPLAY( " -h/-H : display help/long help and exit\n");
return 0;
}
static int usage_advanced(const char* programName)
{
DISPLAY(WELCOME_MESSAGE);
usage(programName);
DISPLAY( "\n");
DISPLAY( "Advanced arguments :\n");
DISPLAY( " -V : display Version number and exit\n");
2016-01-29 01:45:26 +00:00
DISPLAY( "--fast : fast sampling mode\n");
DISPLAY( " -L# : target compression level (default: %u)\n", compressionLevelDefault);
2016-01-29 15:48:10 +00:00
DISPLAY( " -S# : dictionary selectivity level (default: %u)\n", selectionLevelDefault);
2016-01-27 23:29:58 +00:00
DISPLAY( " -v : verbose mode\n");
2016-01-29 15:48:10 +00:00
DISPLAY( " -q : suppress notifications; specify twice to suppress errors too\n");
2016-01-27 23:29:58 +00:00
return 0;
}
static int badusage(const char* programName)
{
DISPLAYLEVEL(1, "Incorrect parameters\n");
2016-01-29 01:45:26 +00:00
if (g_displayLevel >= 1) usage(programName);
2016-01-27 23:29:58 +00:00
return 1;
}
static void waitEnter(void)
{
int unused;
DISPLAY("Press enter to continue...\n");
unused = getchar();
(void)unused;
}
int main(int argCount, const char** argv)
{
int i,
main_pause=0,
operationResult=0,
nextArgumentIsMaxDict=0,
nextArgumentIsDictFileName=0;
2016-01-29 01:45:26 +00:00
unsigned cLevel = compressionLevelDefault;
2016-01-28 14:39:52 +00:00
unsigned maxDictSize = maxDictSizeDefault;
2016-01-27 23:29:58 +00:00
unsigned selectionLevel = selectionLevelDefault;
const char** filenameTable = (const char**)malloc(argCount * sizeof(const char*)); /* argCount >= 1 */
unsigned filenameIdx = 0;
const char* programName = argv[0];
const char* dictFileName = dictFileNameDefault;
/* init */
2016-01-29 01:45:26 +00:00
g_displayOut = stderr; /* unfortunately, cannot be set at declaration */
2016-01-27 23:29:58 +00:00
if (filenameTable==NULL) EXM_THROW(1, "not enough memory\n");
/* Pick out program name from path. Don't rely on stdlib because of conflicting behavior */
for (i = (int)strlen(programName); i > 0; i--) { if ((programName[i] == '/') || (programName[i] == '\\')) { i++; break; } }
programName += i;
/* command switches */
for(i=1; i<argCount; i++) {
const char* argument = argv[i];
if(!argument) continue; /* Protection if argument empty */
if (nextArgumentIsDictFileName) {
nextArgumentIsDictFileName=0;
dictFileName = argument;
continue;
}
if (nextArgumentIsMaxDict) {
nextArgumentIsMaxDict = 0;
maxDictSize = 0;
while ((*argument>='0') && (*argument<='9'))
maxDictSize = maxDictSize * 10 + (*argument - '0'), argument++;
if (*argument=='k' || *argument=='K')
maxDictSize <<= 10;
continue;
}
/* long commands (--long-word) */
2016-01-29 01:45:26 +00:00
if (!strcmp(argument, "--version")) { g_displayOut=stdout; DISPLAY(WELCOME_MESSAGE); return 0; }
if (!strcmp(argument, "--help")) { g_displayOut=stdout; return usage_advanced(programName); }
if (!strcmp(argument, "--verbose")) { g_displayLevel++; if (g_displayLevel<3) g_displayLevel=3; continue; }
if (!strcmp(argument, "--quiet")) { g_displayLevel--; continue; }
2016-01-27 23:29:58 +00:00
if (!strcmp(argument, "--maxdict")) { nextArgumentIsMaxDict=1; continue; }
2016-01-29 01:45:26 +00:00
if (!strcmp(argument, "--fast")) { selectionLevel=0; cLevel=1; continue; }
2016-01-27 23:29:58 +00:00
/* Decode commands (note : aggregated commands are allowed) */
if (argument[0]=='-') {
argument++;
while (argument[0]!=0) {
switch(argument[0])
{
/* Display help */
2016-01-29 01:45:26 +00:00
case 'V': g_displayOut=stdout; DISPLAY(WELCOME_MESSAGE); return 0; /* Version Only */
2016-01-27 23:29:58 +00:00
case 'H':
2016-01-29 01:45:26 +00:00
case 'h': g_displayOut=stdout; return usage_advanced(programName);
/* Selection level */
case 'S': argument++;
selectionLevel = 0;
while ((*argument >= '0') && (*argument <= '9'))
selectionLevel *= 10, selectionLevel += *argument++ - '0';
break;
/* Selection level */
case 'L': argument++;
cLevel = 0;
while ((*argument >= '0') && (*argument <= '9'))
cLevel *= 10, cLevel += *argument++ - '0';
break;
2016-01-27 23:29:58 +00:00
/* Verbose mode */
2016-01-29 01:45:26 +00:00
case 'v': g_displayLevel++; if (g_displayLevel<3) g_displayLevel=3; argument++; break;
2016-01-27 23:29:58 +00:00
/* Quiet mode */
2016-01-29 01:45:26 +00:00
case 'q': g_displayLevel--; argument++; break;
2016-01-27 23:29:58 +00:00
/* dictionary name */
case 'o': nextArgumentIsDictFileName=1; argument++; break;
/* Pause at the end (hidden option) */
case 'p': main_pause=1; argument++; break;
/* unknown command */
default : return badusage(programName);
} }
continue;
}
/* add filename to list */
filenameTable[filenameIdx++] = argument;
}
/* Welcome message (if verbose) */
DISPLAYLEVEL(3, WELCOME_MESSAGE);
/* check nb files */
2016-01-27 23:29:58 +00:00
if (filenameIdx==0) return badusage(programName);
if (filenameIdx < 100)
{
DISPLAYLEVEL(2, "Warning : set contains only %u files ... \n", filenameIdx);
DISPLAYLEVEL(3, "!! For better results, consider providing > 1.000 samples !!\n");
DISPLAYLEVEL(3, "!! Each sample should preferably be stored as a separate file !!\n");
}
2016-01-27 23:29:58 +00:00
/* building ... */
2016-01-29 01:45:26 +00:00
DiB_setNotificationLevel(g_displayLevel);
operationResult = DiB_trainDictionary(dictFileName, maxDictSize, selectionLevel, cLevel, filenameTable, filenameIdx);
2016-01-27 23:29:58 +00:00
if (main_pause) waitEnter();
free((void*)filenameTable);
return operationResult;
}