#include <cstring>
#include <cstdlib>
#include <cstdio>

#include "myspell.hxx"

#ifndef WINDOWS
using namespace std;
#endif

int main(int argc, char **argv)
{
    FILE *wtclst;
    int i;
    int dp;
    char buf[101];
    MySpell *pMS;
    char **wlst;

    /* first parse the command line options */

    for (i = 1; i < 3; i++)
        if (!argv[i]) {
            fprintf(stderr, "correct syntax is:\nexample affix_file");
            fprintf(stderr, "dictionary_file file_of_words_to_check\n");
            exit(1);
        }

    /* open the words to check list */

    wtclst = fopen(argv[3], "r");
    if (!wtclst) {
        fprintf(stderr, "Error - could not open file to check\n");
        exit(1);
    }

    pMS = new MySpell(argv[1], argv[2]);
    while (fgets(buf, 100, wtclst)) {
        *(buf + strlen(buf) - 1) = '\0';
        dp = pMS->spell(buf);
        if (dp) {
            fprintf(stdout, "\"%s\" is okay\n\n", buf);
            int ns = pMS->suggest_stems(&wlst, buf);
            if (ns > 0) {
                fprintf(stdout, "   stems:\n");
                for (int i = 0; i < ns; i++) {
                    fprintf(stdout, "    ...\"%s\"\n", wlst[i]);
                    free(wlst[i]);
                }
                fprintf(stdout, "\n");
            }
            if (wlst) free(wlst);
        } else {
            fprintf(stdout, "\"%s\" is incorrect!\n", buf);
            fprintf(stdout, "   suggestions:\n");
            int ns = pMS->suggest(&wlst, buf);
            for (int i = 0; i < ns; i++) {
                fprintf(stdout, "    ...\"%s\"\n", wlst[i]);
                free(wlst[i]);
            }
            fprintf(stdout, "\n");
            if (wlst) free(wlst);
            
            ns = pMS->suggest_pos_stems(&wlst, buf);
            if (ns > 0) {
                fprintf(stdout, "   possible stems:\n");
                for (int i = 0; i < ns; i++) {
                    fprintf(stdout, "    ...\"%s\"\n", wlst[i]);
                    free(wlst[i]);
                }
                fprintf(stdout, "\n");
            }
            if (wlst) free(wlst);
        }
    }

    delete pMS;
    return 0;
}
