Exemple #1
0
void fill_grid(grid * g,int len) {
  g->len = len;
  g->xd = 0;
  g->yd = 0;
  g->lv = 0;
  g->rooks = 0;
  g->gridx = make_bitfield(len);
  g->gridy = make_bitfield(len);
  g->xproj = make_bitfield(len);
  g->yproj = make_bitfield(len);
  g->xproj_split = make_bitfield(len);
  g->yproj_split = make_bitfield(len);
  g->buffer = malloc(sizeof(dims)*len*len);
  g->filled_floor = 0;
  g->xs = 0;
  g->ys = 0;
}
Exemple #2
0
int
main(int argc, char **argv)
{
    char *dictionary = NULL;
    char *contains = NULL;
    struct word phrase, contain;
    struct bitfield *temp_bf;
    UChar *internal, *alphabet;
    struct wordlist *words = NULL, *stack = NULL, *w;
    int maxlen, minlen = 0;
    int maxwords = INT_MAX, maxtotal = INT_MAX;
    int opt, longindex;
    bool just_test = false, include_contains = false, just_words = false, just_anagram_words = false;
    const struct option long_opts[] = {
        {"contain", required_argument, 0, 'c'},
        {"dict", required_argument, 0, 'd'},
        {"length", required_argument, 0, 'l'},
        {"minimum", required_argument, 0, 'm'},
        {"number", required_argument, 0, 'n'},
        {"words", no_argument, 0, 'w'},
        {"help", no_argument, 0, 'h'},
        {"used", required_argument, 0, 'u'},
        {"test", required_argument, 0, 't'},
        {"version", no_argument, 0, 'v'},
        {0, 0, 0, 0}
    };

    memset(&phrase, 0, sizeof(phrase));
    memset(&contain, 0, sizeof(contain));

    /* parse arguments */

    while ((opt = getopt_long (argc, argv, "c:d:hl:m:n:t:u:vwW",
                               long_opts, &longindex)) != -1) {
        switch (opt) {
        case 'c':
            contains = safe_strdup(optarg);
            just_test = false;
            include_contains = true;
            break;
        case 'd':
            dictionary = safe_strdup(optarg);
            break;
        case 'h':
        case '?':
            print_help(argv[0]);
            exit(0);
        case 'l':
            maxwords = atoi(optarg);
            break;
        case 'm':
            minlen = atoi(optarg);
            break;
        case 'n':
            maxtotal = atoi(optarg);
            break;
        case 't':
            contains = safe_strdup(optarg);
            just_test = true;
            break;
        case 'u':
            contains = safe_strdup(optarg);
            include_contains = false;
            break;
        case 'v':
            printf("%s\n", VERSION);
            exit(0);
        case 'w':
            just_words = true;
            break;
        case 'W':
            just_anagram_words = true;
            break;
        default:
            fprintf(stderr, "Unexpected option %c\n", optopt);
            exit(99);
        }
    }

    if (argc - optind != 1) {
        fprintf(stderr, "%s: incorrect number of arguments\n", argv[0]);
        print_help(argv[0]);
        exit(1);
    }

    phrase.utf8_form = safe_strdup(argv[optind]);

    internal = utf8tointernal(phrase.utf8_form);

    maxlen = phrase.length = u_strlen(internal);

    alphabet = make_alphabet(internal);
    if (u_strlen(alphabet) > BITFIELD_MAX_LENGTH) {
        fprintf(stderr, "The phrase contains too many unique letters.\n");
        exit(1);
    }

    phrase.bits = make_bitfield(internal, alphabet);
    free(internal);

    if (contains) {
        contain.utf8_form = contains;
        internal = utf8tointernal(contains);
        contain.length = u_strlen(internal);
        contain.bits = make_bitfield(internal, alphabet);
        if ((contain.bits == NULL) || !bf_contains(phrase.bits, contain.bits)) {
            printf("Phrase '%s' does not contain '%s'\n",
                   phrase.utf8_form, contain.utf8_form);
            exit(1);
        }
        if (include_contains)
            stack = push_wordstack(stack, &contain);
        temp_bf = bf_subtract(phrase.bits, contain.bits);
        free_bitfield(phrase.bits);
        phrase.bits = temp_bf;
        maxlen -= contain.length;
        if (just_test) {
            if (maxlen == 0) {
                printf("%s can be made from %s\n",
                       contain.utf8_form,
                       phrase.utf8_form);
                exit(0);
            } else {
                printf("%s can not be made from %s\n",
                       contain.utf8_form,
                       phrase.utf8_form);
                exit(1);
            }
        }
    }

    if (dictionary == NULL)
        dictionary = safe_strdup(DEFAULT_DICT);

    load_words(&words, alphabet, phrase.bits, maxlen, minlen, dictionary);

    free(alphabet);

    if (just_words) {
        for (w = words; w != NULL; w = w->next) {
            /* printf(" %3d %s\n", w->word->length, w->word->utf8_form); */
            puts(w->word->utf8_form);
        }
    } else if (just_anagram_words) {
        find_anagram_words(words, stack, phrase.bits, maxwords, maxlen, &maxtotal);
    } else {
        find_anagrams(words, stack, phrase.bits, maxwords, maxlen, &maxtotal);
    }

    return 0;
}