/* 
 *  call-seq:
 *     QueryParser.new(options = {}) -> QueryParser
 *
 *  Create a new QueryParser. The QueryParser is used to convert string
 *  queries into Query objects. The options are;
 *
 *  === Options
 *
 *  :default_field::        Default: "*" (all fields). The default field to
 *                          search when no field is specified in the search
 *                          string. It can also be an array of fields.
 *  :analyzer::             Default: StandardAnalyzer. Analyzer used by the
 *                          query parser to parse query terms
 *  :wild_card_downcase::   Default: true. Specifies whether wild-card queries
 *                          should be downcased or not since they are not
 *                          passed through the parser
 *  :fields::               Default: []. Lets the query parser know what
 *                          fields are available for searching, particularly
 *                          when the "*" is specified as the search field
 *  :tokenized_fields::     Default: :fields. Lets the query parser know which
 *                          fields are tokenized so it knows which fields to
 *                          run the analyzer over.
 *  :validate_fields::      Default: false. Set to true if you want an
 *                          exception to be raised if there is an attempt to
 *                          search a non-existent field
 *  :or_default::           Default: true. Use "OR" as the default boolean
 *                          operator
 *  :default_slop::         Default: 0. Default slop to use in PhraseQuery
 *  :handle_parser_errors:: Default: true. QueryParser will quietly handle all
 *                          parsing errors internally. If you'd like to handle
 *                          them yourself, set this parameter to false.
 *  :clean_string::         Default: true. QueryParser will do a quick
 *                          once-over the query string make sure that quotes
 *                          and brackets match up and special characters are
 *                          escaped
 *  :max_clauses::          Default: 512. the maximum number of clauses
 *                          allowed in boolean queries and the maximum number
 *                          of terms allowed in multi, prefix, wild-card or
 *                          fuzzy queries when those queries are generated by
 *                          rewriting other queries
 */                   
static VALUE
frt_qp_init(int argc, VALUE *argv, VALUE self)
{
    VALUE roptions;
    VALUE rval;
    Analyzer *analyzer = NULL;
    bool has_options = false;

    HashSet *all_fields = NULL;
    HashSet *tkz_fields = NULL;
    HashSet *def_fields = NULL;
    QParser *qp;

    if (rb_scan_args(argc, argv, "01", &roptions) > 0) {
        if (TYPE(roptions) == T_HASH) {
            has_options = true;
            if (Qnil != (rval = rb_hash_aref(roptions, sym_default_field))) {
                def_fields = frt_get_fields(rval);
            }
            if (Qnil != (rval = rb_hash_aref(roptions, sym_analyzer))) {
                analyzer = frt_get_cwrapped_analyzer(rval);
            }
            if (Qnil != (rval = rb_hash_aref(roptions, sym_all_fields))) {
                all_fields = frt_get_fields(rval);
            }
            if (Qnil != (rval = rb_hash_aref(roptions, sym_fields))) {
                all_fields = frt_get_fields(rval);
            }
            if (Qnil != (rval = rb_hash_aref(roptions, sym_tkz_fields))) {
                tkz_fields = frt_get_fields(rval);
            }
        } else {
            def_fields = frt_get_fields(roptions);
        }
    }
    if (all_fields == NULL) {
        all_fields = hs_new_str(&free);
    }

    if (!analyzer) {
        analyzer = mb_standard_analyzer_new(true);
    }

    qp = qp_new(all_fields, def_fields, tkz_fields, analyzer);
    qp->allow_any_fields = true;
    qp->clean_str = true;
    /* handle options */
    if (argc > 0) {
        if (Qnil != (rval = rb_hash_aref(roptions, sym_handle_parse_errors))) {
            qp->handle_parse_errors = RTEST(rval);
        }
        if (Qnil != (rval = rb_hash_aref(roptions, sym_validate_fields))) {
            qp->allow_any_fields = !RTEST(rval);
        }
        if (Qnil != (rval = rb_hash_aref(roptions, sym_wild_card_downcase))) {
            qp->wild_lower = RTEST(rval);
        }
        if (Qnil != (rval = rb_hash_aref(roptions, sym_or_default))) {
            qp->or_default = RTEST(rval);
        }
        if (Qnil != (rval = rb_hash_aref(roptions, sym_default_slop))) {
            qp->def_slop = FIX2INT(rval);
        }
        if (Qnil != (rval = rb_hash_aref(roptions, sym_clean_string))) {
            qp->clean_str = RTEST(rval);
        }
        if (Qnil != (rval = rb_hash_aref(roptions, sym_max_clauses))) {
            qp->max_clauses = FIX2INT(rval);
        }
    }
    Frt_Wrap_Struct(self, frt_qp_mark, frt_qp_free, qp);
    object_add(qp, self);
    return self;
}