APG v2.1.0

This commit is contained in:
Adel I. Mirzazhanov
2002-09-13 15:10:49 +07:00
committed by skinc
parent 8087f2a5e4
commit 900ff5ea18
44 changed files with 2999 additions and 880 deletions

170
apgbfm.c
View File

@@ -1,5 +1,5 @@
/*
** Copyright (c) 2001
** Copyright (c) 2001, 2002
** Adel I. Mirzazhanov. All rights reserved
**
** Redistribution and use in source and binary forms, with or without
@@ -32,12 +32,18 @@
#include <unistd.h>
#include "bloom.h"
#include "errs.h"
#include "getopt.h"
#ifdef __CYGWIN__
#include <getopt.h>
#endif /* __CYGWIN__ */
#define VERSION "1.0.0a0"
#define VERSION "2.1.0"
#define FOUND "FOUND"
#define NOT_FOUND "NOT FOUND"
/*
#define FOUND "YES"
#define NOT_FOUND "NO"
*/
int main (int argc, char *argv[]);
void print_help(void);
@@ -55,6 +61,7 @@ main (int argc, char *argv[])
FILE *f_filter; /* filter file descriptor */
char *word; /* word to add or check */
char *tmp; /* just tmp char pointer */
h_val wc = 0L; /* amount of words to build dictionaty */
h_val filter_size =0L; /* filter size in bits */
int dummy_test = 0; /* variable to make dummy test for */
@@ -69,6 +76,7 @@ main (int argc, char *argv[])
flag new_flag = FALSE; /* -n */
flag new_from_dict_flag = FALSE; /* -d */
flag filter_flag = FALSE; /* -f */
flag silent_flag = FALSE; /* -s */
/* end of flags section */
/* Analize options */
@@ -78,43 +86,43 @@ main (int argc, char *argv[])
exit(-1);
}
while ((option = getopt (argc, argv, "a:A:c:C:n:d:f:hv")) != -1)
while ((option = apg_getopt (argc, argv, "a:A:c:C:n:d:f:hvq")) != -1)
{
switch(option)
{
case 'a':
word = optarg;
word = apg_optarg;
add_word_flag = TRUE;
dummy_test = dummy_test + 2;
break;
case 'A':
dictfile = optarg;
dictfile = apg_optarg;
add_file_flag = TRUE;
dummy_test = dummy_test + 2;
break;
case 'c':
word = optarg;
word = apg_optarg;
check_word_flag = TRUE;
dummy_test = dummy_test + 2;
break;
case 'C':
dictfile = optarg;
dictfile = apg_optarg;
check_file_flag = TRUE;
dummy_test = dummy_test + 2;
break;
case 'n':
checkopt(optarg);
wc = atoi(optarg);
checkopt(apg_optarg);
wc = atoi(apg_optarg);
new_flag = TRUE;
dummy_test = dummy_test + 2;
break;
case 'd':
dictfile = optarg;
dictfile = apg_optarg;
new_from_dict_flag = TRUE;
dummy_test = dummy_test + 2;
break;
case 'f':
filter = optarg;
filter = apg_optarg;
filter_flag = TRUE;
dummy_test = dummy_test + 1;
break;
@@ -124,8 +132,11 @@ main (int argc, char *argv[])
case 'v':
printf ("APG Bloom filter management programm");
printf ("\nversion %s", VERSION);
printf ("\nCopyright (c) 2001 Adel I. Mirzazhanov\n");
printf ("\nCopyright (c) 2001, 2002 Adel I. Mirzazhanov\n");
return (0);
case 'q':
silent_flag = TRUE;
break;
default:
print_help();
exit(-1);
@@ -135,98 +146,123 @@ main (int argc, char *argv[])
if (dummy_test != 3) err_app_fatal ("apg", "too many options");
/* Main part */
/* At this point we can be sure that all options a correct */
if (add_word_flag == TRUE)
if (add_word_flag == TRUE) /* -a word */
{
if ( (f_filter = open_filter(filter)) == NULL)
if ( (f_filter = open_filter(filter, "r+")) == NULL)
err_sys_fatal("open_filter");
filter_size = get_filtersize(f_filter);
if (filter_size == 0) err_sys_fatal("get_filtersize");
if ( insert_word (word, f_filter, filter_size) == -1)
err_sys_fatal("insert_word");
printf ("Word %s added\n",word);
if (silent_flag != TRUE)
printf ("Word %s added\n",word);
return (0);
}
if (add_file_flag == TRUE)
if (add_file_flag == TRUE) /* -A dictfile */
{
word = (char *) calloc(1,MAX_DICT_STRLEN);
if ( (f_dictfile = fopen(dictfile,"r")) == NULL)
err_sys_fatal("fopen");
if( (f_filter = open_filter(filter)) == NULL)
if( (f_filter = open_filter(filter,"r+")) == NULL)
err_sys_fatal("open_filter");
filter_size = get_filtersize(f_filter);
if (filter_size == 0) err_sys_fatal("get_filtersize");
while ((fgets(word, MAX_DICT_STRLEN, f_dictfile) != NULL))
{
word = (char *)strtok (word," \t\n\0");
tmp = (char *)strtok (word," \t\n\0");
if( tmp != NULL)
{
word = tmp;
}
else
{
continue;
}
if ( insert_word (word, f_filter, filter_size) == -1)
err_sys_fatal("insert_word");
i++;
if ( i % 100 == 0)
if (silent_flag != TRUE)
{
fprintf (stdout,".");
fflush (stdout);
if ( i % 100 == 0)
{
fprintf (stdout,".");
fflush (stdout);
}
}
bzero((void *)word, MAX_DICT_STRLEN);
(void)memset((void *)word, 0, MAX_DICT_STRLEN);
}
printf ("\n");
if (silent_flag != TRUE) printf ("\n");
free ( (void *)word);
fclose (f_dictfile);
fclose (f_filter);
close_filter (f_filter);
return (0);
}
if (check_word_flag == TRUE)
if (check_word_flag == TRUE) /* -c word */
{
if ( (f_filter = open_filter(filter)) == NULL)
if ( (f_filter = open_filter(filter, "r")) == NULL)
err_sys_fatal("open_filter");
filter_size = get_filtersize(f_filter);
if (filter_size == 0) err_sys_fatal("get_filtersize");
switch(check_word (word, f_filter, filter_size))
{
case -1:
err_sys_fatal("check_word");
break;
case 1:
printf ("%s: FOUND \n",word);
printf ("%s: %s \n",word, FOUND);
break;
case 0:
printf ("%s: NOT FOUND\n",word);
printf ("%s: %s\n",word, NOT_FOUND);
break;
}
return (0);
}
if (check_file_flag == TRUE)
if (check_file_flag == TRUE) /* -C dictfile */
{
word = (char *) calloc(1,MAX_DICT_STRLEN);
if ( (f_dictfile = fopen(dictfile,"r")) == NULL)
err_sys_fatal("fopen");
wc = count_words (f_dictfile);
if( (f_filter = open_filter(filter)) == NULL)
if (wc == 0) err_sys_fatal("count_words");
if( (f_filter = open_filter(filter, "r")) == NULL)
err_sys_fatal("open_filter");
filter_size = get_filtersize(f_filter);
if (filter_size == 0) err_sys_fatal("get_filtersize");
while ((fgets(word, MAX_DICT_STRLEN, f_dictfile) != NULL))
{
word = (char *)strtok (word," \t\n\0");
tmp = (char *)strtok (word," \t\n\0");
if( tmp != NULL)
{
word = tmp;
}
else
{
continue;
}
switch(check_word (word, f_filter, filter_size))
{
case -1:
err_sys_fatal("check_word");
break;
case 1:
printf ("%s: FOUND \n",word);
printf ("%s: %s\n",word, FOUND);
break;
case 0:
printf ("%s: NOT FOUND\n",word);
printf ("%s: %s\n",word, NOT_FOUND);
break;
}
bzero((void *)word, MAX_DICT_STRLEN);
(void)memset((void *)word, 0, MAX_DICT_STRLEN);
}
free ( (void *)word);
fclose (f_dictfile);
fclose (f_filter);
close_filter (f_filter);
return (0);
}
if (new_flag == TRUE) /* -n nwords */
{
if ((f_filter = create_filter(filter, wc)) == NULL)
err_sys_fatal("create_filter");
fclose(f_filter);
close_filter(f_filter);
return (0);
}
if (new_from_dict_flag == TRUE) /* -d dictfile */
@@ -234,37 +270,57 @@ main (int argc, char *argv[])
word = (char *) calloc(1,MAX_DICT_STRLEN);
if ( (f_dictfile = fopen(dictfile,"r")) == NULL)
err_sys_fatal("fopen");
fprintf (stdout,"Counting words in dictionary. Please wait...\n");
fflush (stdout);
if (silent_flag != TRUE)
{
fprintf (stdout,"Counting words in dictionary. Please wait...\n");
fflush (stdout);
}
wc = count_words (f_dictfile);
if (wc == 0) err_sys_fatal("count_words");
if( (f_filter = create_filter(filter, wc)) == NULL)
err_sys_fatal("create_filter");
filter_size = get_filtersize(f_filter);
if (filter_size == 0) err_sys_fatal("get_filtersize");
while ((fgets(word, MAX_DICT_STRLEN, f_dictfile) != NULL))
{
word = (char *)strtok (word," \t\n\0");
tmp = (char *)strtok (word," \t\n\0");
if( tmp != NULL)
{
word = tmp;
}
else
{
continue;
}
if ( insert_word (word, f_filter, filter_size) == -1)
err_sys_fatal("insert_word");
i++;
if ( i % 100 == 0)
if (silent_flag != TRUE)
{
fprintf (stdout, ".");
fflush (stdout);
if ( i % 100 == 0)
{
fprintf (stdout, ".");
fflush (stdout);
}
}
bzero((void *)word, MAX_DICT_STRLEN);
(void)memset((void *)word, 0, MAX_DICT_STRLEN);
}
printf ("\n");
if (silent_flag != TRUE) printf ("\n");
free ( (void *)word);
fclose (f_dictfile);
fclose (f_filter);
close_filter (f_filter);
return (0);
}
return (0);
}
/*
** print_help - prints short help info
** INPUT: NOTHING
** RETURN: NOTHING
** print_help() - prints short help info
** INPUT:
** none.
** OUTPUT:
** prints help info to the stdout.
** NOTES:
** none.
*/
void
print_help(void)
@@ -272,7 +328,7 @@ print_help(void)
printf ("\napgbfm APG Bloom filter management\n");
printf (" Copyright (c) 2001 Adel I. Mirzazhanov\n");
printf ("\napgbfm -f filter < [-a word] | [-A dictfile] | [-n numofwords] |\n");
printf (" [-c word] | [-C dictfile] | [-d dictfile] >\n");
printf (" [-c word] | [-C dictfile] | [-d dictfile] > [-s]\n");
printf ("apgbfm [-v] [-h]\n\n");
printf ("-a word add word to filter\n");
printf ("-A dictfile add words from dictfile to filter\n");
@@ -281,10 +337,20 @@ print_help(void)
printf ("-n numofwords create new empty filter\n");
printf ("-d dictfile create new filter and add all words from dictfile\n");
printf ("-f filtername use filtername as the name for filter\n");
printf ("-q quiet mode (do not print dots for -A and -d)\n");
printf ("-v print version information\n");
printf ("-h print help (this screen)\n");
}
/*
** checkopt() - check options
** INPUT:
** char * - option string.
** OUTPUT:
** none.
** NOTES:
** checks only is the option string numeral.
*/
void
checkopt(char *opt)
{