APG v2.0.0a0

This commit is contained in:
Adel I. Mirzazhanov
2001-03-21 10:46:44 +06:00
committed by skinc
parent 8b93e6095b
commit 8087f2a5e4
17 changed files with 1352 additions and 29 deletions

298
apgbfm.c Normal file
View File

@@ -0,0 +1,298 @@
/*
** Copyright (c) 2001
** Adel I. Mirzazhanov. All rights reserved
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1.Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimer.
** 2.Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3.The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
** OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
** GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <unistd.h>
#include "bloom.h"
#include "errs.h"
#ifdef __CYGWIN__
#include <getopt.h>
#endif /* __CYGWIN__ */
#define VERSION "1.0.0a0"
int main (int argc, char *argv[]);
void print_help(void);
void checkopt(char *opt);
int
main (int argc, char *argv[])
{
int option = 0;
char *dictfile; /* dictionary filename */
FILE *f_dictfile; /* dictionary file descriptor */
char *filter; /* filter file name */
FILE *f_filter; /* filter file descriptor */
char *word; /* word to add or check */
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 */
/* options correctness */
h_val i = 0L; /* cointer */
/* flags */
flag add_word_flag = FALSE; /* -a */
flag add_file_flag = FALSE; /* -A */
flag check_word_flag = FALSE; /* -c */
flag check_file_flag = FALSE; /* -C */
flag new_flag = FALSE; /* -n */
flag new_from_dict_flag = FALSE; /* -d */
flag filter_flag = FALSE; /* -f */
/* end of flags section */
/* Analize options */
if (argc < 2)
{
print_help();
exit(-1);
}
while ((option = getopt (argc, argv, "a:A:c:C:n:d:f:hv")) != -1)
{
switch(option)
{
case 'a':
word = optarg;
add_word_flag = TRUE;
dummy_test = dummy_test + 2;
break;
case 'A':
dictfile = optarg;
add_file_flag = TRUE;
dummy_test = dummy_test + 2;
break;
case 'c':
word = optarg;
check_word_flag = TRUE;
dummy_test = dummy_test + 2;
break;
case 'C':
dictfile = optarg;
check_file_flag = TRUE;
dummy_test = dummy_test + 2;
break;
case 'n':
checkopt(optarg);
wc = atoi(optarg);
new_flag = TRUE;
dummy_test = dummy_test + 2;
break;
case 'd':
dictfile = optarg;
new_from_dict_flag = TRUE;
dummy_test = dummy_test + 2;
break;
case 'f':
filter = optarg;
filter_flag = TRUE;
dummy_test = dummy_test + 1;
break;
case 'h':
print_help();
return (0);
case 'v':
printf ("APG Bloom filter management programm");
printf ("\nversion %s", VERSION);
printf ("\nCopyright (c) 2001 Adel I. Mirzazhanov\n");
return (0);
default:
print_help();
exit(-1);
}
}
if (filter_flag != TRUE) err_app_fatal ("apg", "-f option is required");
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 ( (f_filter = open_filter(filter)) == NULL)
err_sys_fatal("open_filter");
filter_size = get_filtersize(f_filter);
if ( insert_word (word, f_filter, filter_size) == -1)
err_sys_fatal("insert_word");
printf ("Word %s added\n",word);
return (0);
}
if (add_file_flag == TRUE)
{
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)
err_sys_fatal("open_filter");
filter_size = get_filtersize(f_filter);
while ((fgets(word, MAX_DICT_STRLEN, f_dictfile) != NULL))
{
word = (char *)strtok (word," \t\n\0");
if ( insert_word (word, f_filter, filter_size) == -1)
err_sys_fatal("insert_word");
i++;
if ( i % 100 == 0)
{
fprintf (stdout,".");
fflush (stdout);
}
bzero((void *)word, MAX_DICT_STRLEN);
}
printf ("\n");
free ( (void *)word);
fclose (f_dictfile);
fclose (f_filter);
return (0);
}
if (check_word_flag == TRUE)
{
if ( (f_filter = open_filter(filter)) == NULL)
err_sys_fatal("open_filter");
filter_size = get_filtersize(f_filter);
switch(check_word (word, f_filter, filter_size))
{
case -1:
err_sys_fatal("check_word");
break;
case 1:
printf ("%s: FOUND \n",word);
break;
case 0:
printf ("%s: NOT FOUND\n",word);
break;
}
return (0);
}
if (check_file_flag == TRUE)
{
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)
err_sys_fatal("open_filter");
filter_size = get_filtersize(f_filter);
while ((fgets(word, MAX_DICT_STRLEN, f_dictfile) != NULL))
{
word = (char *)strtok (word," \t\n\0");
switch(check_word (word, f_filter, filter_size))
{
case -1:
err_sys_fatal("check_word");
break;
case 1:
printf ("%s: FOUND \n",word);
break;
case 0:
printf ("%s: NOT FOUND\n",word);
break;
}
bzero((void *)word, MAX_DICT_STRLEN);
}
free ( (void *)word);
fclose (f_dictfile);
fclose (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);
return (0);
}
if (new_from_dict_flag == TRUE) /* -d dictfile */
{
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);
wc = count_words (f_dictfile);
if( (f_filter = create_filter(filter, wc)) == NULL)
err_sys_fatal("create_filter");
filter_size = get_filtersize(f_filter);
while ((fgets(word, MAX_DICT_STRLEN, f_dictfile) != NULL))
{
word = (char *)strtok (word," \t\n\0");
if ( insert_word (word, f_filter, filter_size) == -1)
err_sys_fatal("insert_word");
i++;
if ( i % 100 == 0)
{
fprintf (stdout, ".");
fflush (stdout);
}
bzero((void *)word, MAX_DICT_STRLEN);
}
printf ("\n");
free ( (void *)word);
fclose (f_dictfile);
fclose (f_filter);
return (0);
}
return (0);
}
/*
** print_help - prints short help info
** INPUT: NOTHING
** RETURN: NOTHING
*/
void
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 ("apgbfm [-v] [-h]\n\n");
printf ("-a word add word to filter\n");
printf ("-A dictfile add words from dictfile to filter\n");
printf ("-c word check word against filter\n");
printf ("-C dictfile check dictfile against filter\n");
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 ("-v print version information\n");
printf ("-h print help (this screen)\n");
}
void
checkopt(char *opt)
{
int i;
for(i=0; i < strlen(opt);i++)
if(opt[i] != '0' && opt[i] != '1' && opt[i] != '2' && opt[i] != '3' &&
opt[i] != '4' && opt[i] != '5' && opt[i] != '6' && opt[i] != '7' &&
opt[i] != '8' && opt[i] != '9')
err_app_fatal ("checkopt", "wrong option format");
}