/* -*- Mode:Text -*- */ /* * tree.c - a tree style dictionary for user's personal words * * Pace Willisson, 1983 */ #include #include #include "ispell.h" char *getenv(); char *upcase(); static struct node *root = NULL; struct node *tinsert(); static char personaldict[100]; static FILE *dictf; static newwords = 0; treeinit () { char *p; char buf[BUFSIZ]; p = getenv ("HOME"); if (p == NULL) return; strcpy (personaldict, p); strcat (personaldict, "/ispell.words"); if ((dictf = fopen (personaldict, "r")) == NULL) return; while (fgets (buf, sizeof buf, dictf) != NULL) { int len = strlen (buf) - 1; if (buf [ len ] == '\n') buf [ len ] = 0; treeinsert (buf, 1); } fclose (dictf); newwords = 0; if (!lflag && !aflag && access (personaldict, 2) < 0) printf ("Warning: Cannot update personal dictionary (%s)\r\n", personaldict); } treeprint () { printf ("("); tprint (root); printf (")"); } static tprint (root) struct node *root; { if (root == NULL) return; printf ("%s ", root->word); tprint (root->left); tprint (root->right); } treeinsert (word, keep) char *word; { char nword[BUFSIZ]; strcpy (nword, word); root = tinsert (upcase (nword), root, keep); newwords = newwords || keep; } static struct node * tinsert (word, root, keep) char *word; struct node *root; { int cmp; if (root == NULL) { root = (struct node *) calloc (1, sizeof (struct node)); root->word = (char *) malloc (strlen (word) + 1); strcpy (root->word, word); root->keep = keep; return (root); } cmp = strcmp (word, root->word); if (cmp == 0) return (root); if (cmp < 0) root->left = tinsert (word, root->left, keep); else root->right = tinsert (word, root->right, keep); return (root); } treelookup (word) char *word; { char nword[BUFSIZ]; strcpy (nword, word); if (tlookup (upcase (nword), root)) { return (1); } return (0); } static tlookup (word, root) char *word; struct node *root; { int cmp; if (root == NULL) return (0); cmp = strcmp (word, root->word); if (cmp == 0) return (1); if (cmp < 0) return (tlookup (word, root->left)); else return (tlookup (word, root->right)); } treeoutput () { if (newwords == 0) return; if ((dictf = fopen (personaldict, "w")) == NULL) { fprintf (stderr, "Can't create %s\r\n", personaldict); return; } toutput1 (root); newwords = 0; fclose (dictf); } static toutput1 (root) struct node *root; { if (root == NULL) return; if (root->keep) fprintf (dictf, "%s\n", root->word); toutput1 (root->left); toutput1 (root->right); } char * upcase (s) register char *s; { register char *os = s; while (*s) { if (islower (*s)) *s = toupper (*s); s++; } return (os); }