Annotation of parser3/src/main/pa_dictionary.C, revision 1.17.2.6

1.1       parser      1: /**    @file
                      2:        Parser: dictionary class  impl.
                      3: 
1.17.2.4  paf         4:        Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.12      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.15      paf         6: */
1.1       parser      7: 
1.17.2.6! paf         8: static const char* IDENT_DICTIONARY_C="$Date: 2003/02/04 10:32:12 $";
1.1       parser      9: 
                     10: #include "pa_dictionary.h"
1.4       parser     11: #include "pa_exception.h"
1.1       parser     12: 
1.17.2.6! paf        13: // could not make this static: gcc complains "prev decl was extern"
        !            14: void pa_dictionary_add_first(Table::element_type row, Dictionary* self) {
1.4       parser     15:        // get a=>b values
1.17.2.3  paf        16:        StringPtr a=row->get(0);
                     17:        StringPtr b=row->get(1);
1.4       parser     18:        // empty 'a' check
1.17.2.1  paf        19:        if(a->is_empty()) {
1.14      paf        20:                throw Exception("parser.runtime",
1.17.2.1  paf        21:                        a, 
1.4       parser     22:                        "dictionary table 'from' column elements must not be empty");
                     23:        }
1.17.2.1  paf        24:        unsigned char c=(unsigned char)a->first_char();
                     25:        if(!self->starting_line_of[c])
                     26:                self->starting_line_of[c]=self->constructor_line;
                     27: 
                     28:        double ratio=((double)b->size())/a->size();
                     29:        if(ratio>self->fmax_ratio)
                     30:                self->fmax_ratio=ratio;
1.4       parser     31: 
1.17.2.1  paf        32:        self->constructor_line++;
1.1       parser     33: }
1.17.2.1  paf        34: Dictionary::Dictionary(TablePtr  atable, double amin_ratio):
1.4       parser     35:        table(atable), 
                     36:        fmax_ratio(amin_ratio)  {
                     37: 
                     38:        // clear starting_lines
                     39:        memset(starting_line_of, 0, sizeof(starting_line_of));
1.1       parser     40:        // grab first letters of first column of a table
1.17.2.6! paf        41:        constructor_line=1;  table->for_each(pa_dictionary_add_first, this);
1.1       parser     42: }
                     43: 
1.4       parser     44: #ifndef DOXYGEN
                     45: struct First_that_starts_info {
                     46:        int line;
1.17.2.4  paf        47:        const char* src;  size_t src_size;
1.4       parser     48: };
                     49: #endif
1.17.2.1  paf        50: static bool starts(Table::element_type row, First_that_starts_info* info) {
1.4       parser     51:        // skip irrelevant lines
1.17.2.1  paf        52:        if(info->line>1) {
                     53:                --info->line;
1.4       parser     54:                return 0;
                     55:        }
1.1       parser     56: 
                     57:        int partial;
1.17.2.1  paf        58:        row->get(0)->cmp(partial, info->src, info->src_size);
1.3       parser     59:        return (
1.1       parser     60:                partial==0 || // full match
1.17.2.1  paf        61:                partial==1); // typo left column starts 'src'
1.1       parser     62: }
                     63: 
1.17.2.4  paf        64: Table::element_type Dictionary::first_that_starts(const char* src, size_t src_size) const {
1.4       parser     65:        First_that_starts_info info;
                     66:        if(info.line=starting_line_of[(unsigned char)*src]) {
                     67:                info.src=src;
1.7       parser     68:                info.src_size=src_size;
1.17.2.1  paf        69:                return table->first_that(starts, &info);
1.4       parser     70:        } else
1.17.2.2  paf        71:                return Table::element_type(0);
1.1       parser     72: }

E-mail: