Ejemplo n.º 1
0
/**
 *
 Creates a hashtable, and inserts words into it.
 The table is then printed before we free the memory allocated to it.

 Note: The tablesize of the hashtable is determined by the first command line
 argument if there is one, with a default table tablesize of 113.
 The number of statistical snapshots to print is determined by the second
 command line argument if there is one, with a default number of 10.

 tablesize = the maximum number of positions in the hash table.
 word      = the string to be inserted into the hash table.
 ht        = the hash table using eith double hashing or linear probing.
 snapshots = the number of statistical snapshots to be used.

 @param argc the number of command-line arguments.
 @param argv an array of strings containing the command-line arguments.

 @return EXIT_SUCCESS if the program is successful.

*/
int main(int argc, char **argv) {
  time_t start,end;
  bool_t entire_table = FALSE, double_hashing = FALSE, print_stats = FALSE,
    do_spell_check = FALSE;
  int tablesize = 113, snapshots = 10;
  char word[256];
  char *filename;
  htable ht;

  set_flags(argc, argv, &do_spell_check, &filename, &double_hashing, &entire_table,
	    &print_stats, &snapshots, &tablesize);

  ht = htable_new(tablesize, (double_hashing) ? DOUBLE_H : LINEAR_P);
  start = clock();
  while (getword(word, sizeof word, stdin) != EOF) {
    htable_insert(ht, word);
  }
  end = clock();
  if(do_spell_check) {
    spell_check(ht,filename,(end-start)/(double)CLOCKS_PER_SEC);
  }
  if (entire_table) {
    htable_print_entire_table(ht, stderr);
  }
  if (print_stats) {
    htable_print_stats(ht, stdout, snapshots);
  } else if (!do_spell_check){ /* print words and frequencies */
    htable_print(ht, stdout);
  }
  htable_delete(ht);

  return EXIT_SUCCESS;
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
    char ch;
    int ret = EXIT_FAILURE;
    struct sc_config config;

    config.in_place = 0;
    config.file[FILE_DIC] = DEFAULT_DICTIONARY;
    while ((ch = getopt(argc, argv, "d:hi:o:pv")) != -1) {
        switch (ch) {
        case 'd':
            config.file[FILE_DIC] = optarg;
            break;
        case 'h':
            print_content(SC_FILE_HELP);
            return EXIT_SUCCESS;
        case 'i':
            config.file[FILE_DOC] = optarg;
            break;
        case 'o':
            if (config.in_place) {
                log_fatal("output file cannot be specified "
                          "if in-place edit is asked for");
                return EXIT_FAILURE;
            }
            config.file[FILE_OUT] = optarg;
            break;
        case 'p':
            if (config.file[FILE_OUT]) {
                log_fatal("document cannot be edited in place "
                          "if output file is specified");
                return EXIT_FAILURE;
            }
            config.in_place = 1;
            break;
        case 'v':
            print_content(SC_FILE_VERSION);
            return EXIT_SUCCESS;
        default:
            print_content(SC_FILE_HELP);
            return EXIT_FAILURE;
        }
    }

    if (check_config(&config, argc, argv)) {
        print_content(SC_FILE_HELP);
        goto ERROR;
    }

    if (spell_check(&config))
        goto ERROR;
    print_content(SC_FILE_BANNER);
    ret = EXIT_SUCCESS;

ERROR:
    if (config.file[FILE_OUT])
        free(config.file[FILE_OUT]);
    return ret;
}
Ejemplo n.º 3
0
void test4(const char *dictionary, const char *word)
{
    int file_result;

    printf("\nTest4--------------------------------------------------------\n");
    file_result = spell_check(dictionary, word);
    if (file_result == FILE_ERR_OPEN)
    {
        printf("Can't open %s\n", dictionary);
        return;
    }

    if (file_result == WORD_OK)
        printf("The word %s is spelled correctly.\n", word);
    else
        printf("The word %s is misspelled.\n", word);
}
Ejemplo n.º 4
0
void test5(void)
{
    int i;
    int file_result;
    const char *words[] = {"Four", "score", "and", "seven", "years", "ago", "our",
                           "fathers", "brought", "forth", "on", "this", "continent",
                           "a", "new", "nation"
                          };

    int num_misspelled = 0;
    int num_words = sizeof(words) / sizeof(*words);

    /* Name of the dictionary file */
    const char *dictionary = "allwords.txt";

    printf("\nTest5--------------------------------------------------------\n");
    printf("Misspelled words: ");
    for (i = 0; i < num_words; i++)
    {
        file_result = spell_check(dictionary, words[i]);
        if (file_result == FILE_ERR_OPEN)
        {
            printf("Can't open %s\n", dictionary);
            return;
        }

        if (file_result == WORD_BAD)
        {
            printf("%s ", words[i]);
            num_misspelled++;
        }
    }
    if (!num_misspelled)
        printf("*** None ***");
    printf("\n");
}
Ejemplo n.º 5
0
static int search_spell(const char *filename, unsigned parnum, unsigned pos)
{
struct	rfc2045	*rfcp, *textp;
struct	buf newtext, current_line;
off_t start_pos, end_pos, start_body;
int	made_replacements, has_misspelling;
char *new_line;
unsigned paragraph;
const char	*ignoreword="";
const char	*replacefrom="";
const char	*replaceto="";
int	checked=0;
off_t	dummy;
FILE	*fp=0;
int	x;

	x=maildir_safeopen(filename, O_RDONLY, 0);
	if (x >= 0)
		if ((fp=fdopen(x, "r")) == 0)
			close(x);

	if (!fp)	return (0);
	rfcp=rfc2045_fromfp(fp);
	if (!rfcp)	enomem();

	textp=findtext(rfcp);

	if (!textp)
	{
		rfc2045_free(rfcp);
		fclose(fp);
		return (0);
	}

	buf_init(&newtext);
	buf_init(&current_line);

        rfc2045_mimepos(textp, &start_pos, &end_pos, &start_body,
		&dummy, &dummy);
        if (fseek(fp, start_body, SEEK_SET) == -1)
                enomem();

	made_replacements=0;
	has_misspelling=0;
	paragraph=0;
        for ( ; start_body < end_pos; start_body++)
	{
	int	c=getc(fp);

		if (c < 0)	enomem();
		if (c != '\n')
		{
			buf_append(&current_line, c);
			continue;
		}
		buf_append(&current_line, '\0');
		if (parnum)
		{
			--parnum;
			buf_cat(&newtext, current_line.ptr);
			buf_cat(&newtext, "\n");
			current_line.cnt=0;
			++paragraph;
			continue;
		}

		if (!checked)
		{
		int	l;

			checked=1;
			if ((l=strlen(cgi("word"))) > 0)
			{

/* Ok, what should we do? */

			const char *newword=cgi("REPLACE");

				if (!*newword || strcmp(newword, "#other") == 0)
					newword=cgi("OTHER");
				/*
				** Perhaps they entered the word without
				** checking this checkmark.
				*/
				else if (*newword == '#')
					newword="";

				if (*newword && pos + l <= strlen(current_line.ptr))
				{
				struct buf tempbuf;

					buf_init(&tempbuf);
					buf_cpyn(&tempbuf, current_line.ptr,
						pos);
					buf_cat(&tempbuf, newword);
					buf_cat(&tempbuf,
						current_line.ptr+pos+l);
					pos += strlen(newword);
					if (*cgi("REPLACEALL"))
					{
						replacefrom=cgi("word");
						replaceto=newword;
					}
					buf_append(&tempbuf, '\0');
					buf_cpy(&current_line, tempbuf.ptr);
					buf_append(&current_line, '\0');
					buf_free(&tempbuf);
					made_replacements=1;
				}
				else
				{
					pos += l;
					if (strcmp(cgi("REPLACE"),
						"#ignoreall") == 0)
						ignoreword=cgi("word");
				}

				if (strcmp(cgi("REPLACE"),
						"#insert") == 0)
				{
					spelladd(cgi("word"));
				}
			}
		}


		if (*current_line.ptr == '>')
		{
			buf_cat(&newtext, current_line.ptr);
			buf_cat(&newtext, "\n");
			pos=0;
			current_line.cnt=0;
			++paragraph;
			continue;
		}
		if (!has_misspelling)
		{
			new_line=spell_check(current_line.ptr, paragraph, pos,
				ignoreword, replacefrom, replaceto,
				&has_misspelling);
			if (new_line)
			{
				buf_cat(&newtext, new_line);
				free(new_line);
				made_replacements=1;
			}
			else	buf_cat(&newtext, current_line.ptr);
		}
		else	buf_cat(&newtext, current_line.ptr);
		buf_cat(&newtext, "\n");
		pos=0;
		current_line.cnt=0;
		++paragraph;
	}
	if (current_line.cnt)
		buf_cat(&newtext, "\n");
	rfc2045_free(rfcp);
	fclose(fp);
	if (made_replacements)
	{
	char	*p=newmsg_createdraft_do(filename, newtext.ptr,
					 NEWMSG_SQISPELL);

		if (p)	free(p);

		if (*cgi("error"))
		{
			has_misspelling=0;	/* Abort spell checking */
		}
	}

	buf_free(&newtext);
	buf_free(&current_line);

	if (*ignoreword)
	{
	static char *p=0;

		if (p)	free(p);
		p=malloc(strlen(cgi("globignore")) + 2 + strlen(ignoreword));

		if (!p)	enomem();

		strcpy(p, cgi("globignore"));
		if (*p)	strcat(p, ":");
		strcat(p, ignoreword);
		cgi_put("globignore", p);
	}

	if (*replacefrom)
	{
	static char *p=0;

		if (p)	free(p);
		p=malloc(strlen(cgi("globreplace"))+3
			+strlen(replacefrom)+strlen(replaceto));

		if (!p)	enomem();
		strcpy(p, cgi("globreplace"));
		if (*p)	strcat(p, ":");
		strcat(strcat(strcat(p, replacefrom), ":"), replaceto);
		cgi_put("globreplace", p);
		free(p);
	}
	if (has_misspelling)	return (1);
	return (0);
}
Ejemplo n.º 6
0
static int make_ofile(int kflag, char **Cflags, char *o_file, char *c_file)
{
#if	!defined(USE_WIN32)
    int		pid;
#endif

    struct stat	stat_c, stat_o;

    static char	*cnp	= (char *)NULL;
    char	*av[64];		/* hope that this is enough! */
    int		status;
    int		ac, i;

/*  FIRSTLY, ENSURE THAT THE SOURCEFILE EXISTS AND NEEDS RECOMPILING */

    if(stat(c_file, &stat_c) == -1) {
       fprintf(stderr,"%s: cannot find sourcefile %s\n",argv0,c_file);
	++nerrors;
	return(-1);
    }
    if(stat(o_file, &stat_o) == 0 && stat_c.st_mtime <= stat_o.st_mtime)
	return(0);

#if	CHECK_RECEIVE_SPELLING
    if(spell_check(c_file) == FALSE)
	return(-1);
#endif

    if(cnp == (char *)NULL) {
	cnp	= find_cnetfile("cnet.h", TRUE, TRUE);
	if(vflag)
	    fprintf(stderr,"using include directory \"%s\"\n", cnp);
    }

#if defined(USE_WIN32)
    ac	= 0;

    av[ac++] = "CL";
    av[ac++] = OS_DEFINE;

    av[ac++] =	"/DHAVE_LONG_LONG=1";
    sprintf(chararray, "/DSIZEOF_INT=%d",	sizeof(int));
    av[ac++] =	strdup(chararray);
    sprintf(chararray, "/DSIZEOF_LONG=%d",	sizeof(long));
    av[ac++] =	strdup(chararray);

    sprintf(chararray, "/I%s", cnp);
    av[ac++] = strdup(chararray);

    while(*Cflags)		/* add C compiler switches */
	av[ac++] =	*Cflags++;

    av[ac++] = "/c";
    sprintf(chararray, "/Fo%s", o_file);
    av[ac++] = strdup(chararray);

    if(!vflag)
	av[ac++] = "/NOLOGO";

    av[ac++] = c_file;
    av[ac  ] =	NULL;

    if(dflag) {
	fputs(av[0], stderr);
	for(i=1 ; i<ac ; i++)
	    fprintf(stderr," %s",av[i]);
	fputc('\n',stderr);
    }
    else
	fprintf(stderr,"compiling %s\n", c_file);

    status  = _spawnvp(_P_WAIT, av[0], &av[1]);
    if(status != 0) {
	if(status == -1)
	    fprintf(stderr,"%s: spawn of %s unsuccessful: %s\n",
				argv0,av[0],_sys_errlist[(int)errno]);
	exit(1);
    }

#else
    switch (pid = fork()) {
    case -1 :
	fprintf(stderr,"%s: cannot fork\n",argv0);
	exit(1);
	break;
    case 0 :
	ac	 = 0;

#if	USE_GCC_COMPILER
	av[ac++] = findenv("CNETGCC", CNETGCC);
	av[ac++] = "gcc";
	if(!kflag)			/* not using "old" K&R C */
	    av[ac++] = "-ansi";
#if	GCC_WERROR_WANTED
	av[ac++] = "-Werror";
#endif
#if	GCC_WALL_WANTED
	av[ac++] = "-Wall";
#endif

#else
	av[ac++] = findenv("CNETCC", CNETCC);
	av[ac++] = "cc";
#endif

	ac	 =	add_compile_args(ac, av, kflag);
	av[ac++] =	OS_DEFINE;

#if	HAVE_LONG_LONG
	av[ac++] =	"-DHAVE_LONG_LONG=1";
#endif
	sprintf(chararray, "-DSIZEOF_INT=%d",	sizeof(int));
	av[ac++] =	strdup(chararray);
	sprintf(chararray, "-DSIZEOF_LONG=%d",	sizeof(long));
	av[ac++] =	strdup(chararray);

	while(*Cflags)		/* add C compiler switches */
	    av[ac++] =	*Cflags++;

	sprintf(chararray, "-I%s", cnp);
	av[ac++] =	strdup(chararray);

	av[ac++] =	"-c";
	av[ac++] =	"-o";
	av[ac++] =	o_file;
	av[ac++] =	c_file;
	av[ac  ] =	NULL;

	if(dflag) {
	    fputs(av[0], stderr);
	    for(i=2 ; i<ac ; i++)
		fprintf(stderr," %s",av[i]);
	    fputc('\n',stderr);
	}
	else
	    fprintf(stderr,"compiling %s\n", c_file);

	execvp(av[0], &av[1]);
	fprintf(stderr,"%s: cannot exec %s\n",argv0,av[0]);
	exit(1);
        break;

    default :
	while(wait(&status) != pid)
	    ;
	if(status != 0)
	    exit(1);
	break;
    }
#endif
    return(0);
}