Changeset 2108

Show
Ignore:
Timestamp:
03/31/08 23:47:51 (2 months ago)
Author:
karpet
Message:

add header read/write to xapian example and fix some mem leaks

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libswish3/trunk/src/libswish3/config.c

    r2106 r2108  
    3737extern int SWISH_DEBUG; 
    3838 
    39 void swish_free_config( 
    40     swish_Config *config 
    41 ); 
    42 swish_Config *swish_init_config( 
    43 ); 
    44 void swish_config_set_default( 
    45     swish_Config *config 
    46 ); 
    47 swish_Config *swish_add_config( 
    48     xmlChar *conf, 
    49     swish_Config *config 
    50 ); 
    51 swish_Config *swish_parse_config( 
    52     xmlChar *conf, 
    53     swish_Config *config 
    54 ); 
    55 void swish_debug_config( 
    56     swish_Config *config 
    57 ); 
    58 void swish_config_merge( 
    59     swish_Config *config1, 
    60     swish_Config *config2 
    61 ); 
    6239static void free_string( 
    6340    xmlChar *payload, 
  • libswish3/trunk/src/libswish3/hash.c

    r2103 r2108  
    7171) 
    7272{ 
    73     if (SWISH_DEBUG > 10) 
    74         printf("freeing %s from hash key %s\n", (xmlChar *)val, key); 
    75  
    7673    swish_xfree(val); 
    7774} 
     
    9087 
    9188    return ret; 
     89} 
     90 
     91void 
     92swish_hash_free( 
     93    xmlHashTablePtr hash 
     94) 
     95{ 
     96    xmlHashFree(hash, (xmlHashDeallocator)free_hashval); 
    9297} 
    9398 
  • libswish3/trunk/src/libswish3/header.c

    r2106 r2108  
    108108    xmlChar *name 
    109109); 
    110 static void 
    111 test_meta_unique_ids( 
     110static void test_meta_unique_ids( 
    112111    swish_MetaName *meta, 
    113112    swish_Config *c, 
    114113    xmlChar *name 
    115114); 
    116 static void 
    117 test_prop_unique_ids( 
     115static void test_prop_unique_ids( 
    118116    swish_Property *prop, 
    119117    swish_Config *c, 
     
    269267 
    270268    meta = 
    271         swish_init_metaname(swish_str_tolower 
    272                             ((xmlChar *)xmlTextReaderConstName(reader))); 
     269        swish_init_metaname(swish_str_tolower((xmlChar *)xmlTextReaderConstName(reader))); 
    273270    meta->ref_cnt++; 
    274271    value = NULL; 
     
    369366 
    370367    if (xmlStrEqual(attr, (xmlChar *)"ignore_case")) { 
    371         prop->ignore_case = 
    372             (boolean) strtol((char *)attr_val, (char **)NULL, 10); 
     368        prop->ignore_case = (boolean) strtol((char *)attr_val, (char **)NULL, 10); 
    373369    } 
    374370    else if (xmlStrEqual(attr, (xmlChar *)"max")) { 
     
    414410 
    415411    prop = 
    416         swish_init_property(swish_str_tolower 
    417                             ((xmlChar *)xmlTextReaderConstName(reader))); 
     412        swish_init_property(swish_str_tolower((xmlChar *)xmlTextReaderConstName(reader))); 
    418413    prop->ref_cnt++; 
    419414    value = NULL; 
     
    521516        } 
    522517 
     518        //SWISH_DEBUG_MSG("END ELEMENT name %s  type %d  value %s", name, type, value); 
     519 
    523520        return; 
    524521 
     
    549546            return; 
    550547        } 
     548 
     549        //SWISH_DEBUG_MSG("NOT END ELEMENT name %s  type %d  value %s", name, type, value); 
    551550    } 
    552551 
     
    574573        } 
    575574        else if (h->isalias) { 
    576             read_key_values_pair(reader, h->config->tag_aliases, 
    577                                  (xmlChar *)name); 
     575            read_key_values_pair(reader, h->config->tag_aliases, (xmlChar *)name); 
    578576            return; 
    579577        } 
     
    582580            return; 
    583581        } 
     582 
     583        /* 
     584           SWISH_DEBUG_MSG("STILL NOT END ELEMENT name %s  type %d  value %s", name, type, value);  
     585         */ 
    584586 
    585587    } 
     
    608610 
    609611            for (i = 0; i < strlist->n; i++) { 
    610 /* SWISH_DEBUG_MSG("key_values pair: %s -> %s", strlist->word[i], name); */ 
     612/* SWISH_DEBUG_MSG("key_values pair: %s -> %s", strlist->word[i], name); */ 
    611613                if (swish_hash_exists(hash, strlist->word[i])) { 
    612                     swish_hash_replace(hash, strlist->word[i], 
    613                                        swish_xstrdup(name)); 
     614                    swish_hash_replace(hash, strlist->word[i], swish_xstrdup(name)); 
    614615                } 
    615616                else { 
     
    645646        if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_TEXT) { 
    646647            value = xmlTextReaderConstValue(reader); 
    647 /* SWISH_DEBUG_MSG("read key %s for value %s", name, value); */ 
     648/* SWISH_DEBUG_MSG("read key %s for value %s", name, value); */ 
    648649            if (swish_hash_exists(hash, name)) { 
    649650                swish_hash_replace(hash, name, swish_xstrdup(value)); 
     
    678679    if (stat((char *)filename, &fileinfo)) { 
    679680        reader = 
    680             xmlReaderForMemory((const char *)filename, 
    681                                xmlStrlen((xmlChar *)filename), "[ swish.xml ]", 
    682                                NULL, 0); 
     681            xmlReaderForMemory((const char *)filename, xmlStrlen((xmlChar *)filename), 
     682                               "[ swish.xml ]", NULL, 0); 
    683683 
    684684        if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 
     
    722722) 
    723723{ 
    724     if (meta->alias_for != NULL 
    725         && !swish_hash_exists(c->metanames, meta->alias_for) 
     724    if (meta->alias_for != NULL && !swish_hash_exists(c->metanames, meta->alias_for) 
    726725        ) { 
    727726        SWISH_CROAK 
    728             ("MetaName %s has alias_for value of %s but no such MetaName defined", 
     727            ("MetaName '%s' has alias_for value of '%s' but no such MetaName defined", 
    729728             name, meta->alias_for); 
    730729    } 
     
    738737) 
    739738{ 
    740     if (prop->alias_for != NULL 
    741         && !swish_hash_exists(c->properties, prop->alias_for) 
     739    if (prop->alias_for != NULL && !swish_hash_exists(c->properties, prop->alias_for) 
    742740        ) { 
    743741        SWISH_CROAK 
    744             ("Property %s has alias_for value of %s but no such Property defined", 
     742            ("Property '%s' has alias_for value of '%s' but no such Property defined", 
    745743             name, prop->alias_for); 
    746744    } 
     
    752750) 
    753751{ 
    754     xmlHashScan(c->metanames, (xmlHashScanner) test_meta_alias_for, c); 
    755     xmlHashScan(c->properties, (xmlHashScanner) test_prop_alias_for, c); 
     752    xmlHashScan(c->metanames, (xmlHashScanner)test_meta_alias_for, c); 
     753    xmlHashScan(c->properties, (xmlHashScanner)test_prop_alias_for, c); 
    756754} 
    757755 
     
    763761) 
    764762{ 
    765     c->flags->meta_ids[ meta->id ]++; 
     763    c->flags->meta_ids[meta->id]++; 
    766764} 
    767765 
     
    773771) 
    774772{ 
    775     c->flags->prop_ids[ prop->id ]++; 
     773    c->flags->prop_ids[prop->id]++; 
    776774} 
    777775 
     
    782780{ 
    783781    int i; 
    784     xmlHashScan(c->metanames, (xmlHashScanner) test_meta_unique_ids, c); 
    785     xmlHashScan(c->properties, (xmlHashScanner) test_prop_unique_ids, c); 
    786     for(i=0; i<SWISH_MAX_IDS; i++) { 
     782    xmlHashScan(c->metanames, (xmlHashScanner)test_meta_unique_ids, c); 
     783    xmlHashScan(c->properties, (xmlHashScanner)test_prop_unique_ids, c); 
     784    for (i = 0; i < SWISH_MAX_IDS; i++) { 
    787785        if (c->flags->meta_ids[i] > 1) { 
    788786            SWISH_WARN("meta id %d == %d", i, c->flags->meta_ids[i]); 
     
    791789            SWISH_WARN("prop id %d == %d", i, c->flags->prop_ids[i]); 
    792790        } 
    793          
    794         /* set back to 0 in case we are called again */ 
     791 
     792        /* 
     793           set back to 0 in case we are called again  
     794         */ 
    795795        c->flags->prop_ids[i] = 0; 
    796796        c->flags->meta_ids[i] = 0; 
    797     }  
     797    } 
    798798} 
    799799 
     
    809809    h->isprops = 0; 
    810810    h->ismetas = 0; 
     811    h->isindex = 0; 
     812    h->isalias = 0; 
     813    h->isparser = 0; 
     814    h->ismime = 0; 
    811815    h->parent_name = NULL; 
    812816    h->prop_id = SWISH_PROP_THIS_MUST_COME_LAST_ID; 
     
    823827    h = init_headmaker(); 
    824828    read_header(filename, h); 
    825      
     829 
    826830/*  test that all the alias_for links resolve ok */ 
    827831    swish_config_test_alias_fors(h->config); 
    828      
     832 
    829833/*  make sure ids are all unique */ 
    830834    swish_config_test_unique_ids(h->config); 
     
    857861/*  test that all the alias_for links resolve ok */ 
    858862    swish_config_test_alias_fors(c); 
    859      
     863 
    860864/*  make sure ids are all unique */ 
    861865    swish_config_test_unique_ids(c); 
     
    888892 
    889893    if (rc < 0) { 
    890         SWISH_CROAK("Error writing elemtn %s", tag); 
     894        SWISH_CROAK("Error writing element %s", tag); 
    891895    } 
    892896} 
     
    928932    boolean is_alias; 
    929933    write_open_tag(writer, name); 
    930     if (meta->alias_for == NULL) { 
    931         rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "alias_for", 
    932                                          BAD_CAST ""); 
    933         is_alias = 0; 
    934     } 
    935     else { 
    936         rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "alias_for", 
    937                                          meta->alias_for); 
     934    is_alias = 0; 
     935    rc = 0; 
     936    if (meta->alias_for != NULL) { 
     937        rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "alias_for", meta->alias_for); 
    938938        is_alias = 1; 
    939939    } 
     
    943943 
    944944    if (!is_alias) { 
    945         rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "bias", "%d", 
    946                                                meta->bias); 
     945        rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "bias", "%d", meta->bias); 
    947946        if (rc < 0) { 
    948947            SWISH_CROAK("Error writing metaname bias attribute for %s", name); 
     
    951950    } 
    952951 
    953     rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "id", "%d", 
    954                                            meta->id); 
     952    rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "id", "%d", meta->id); 
    955953    if (rc < 0) { 
    956954        SWISH_CROAK("Error writing metaname id attribute for %s", name); 
     
    965963) 
    966964{ 
    967     xmlHashScan(metanames, (xmlHashScanner) write_metaname, writer); 
     965    xmlHashScan(metanames, (xmlHashScanner)write_metaname, writer); 
    968966} 
    969967 
     
    988986    boolean is_alias; 
    989987    write_open_tag(writer, name); 
    990     if (prop->alias_for == NULL) { 
    991         rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "alias_for", 
    992                                          BAD_CAST ""); 
    993         is_alias = 0; 
    994     } 
    995     else { 
    996         rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "alias_for", 
    997                                          prop->alias_for); 
     988    rc = 0; 
     989    is_alias = 0; 
     990    if (prop->alias_for != NULL) { 
     991        rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "alias_for", prop->alias_for); 
    998992        is_alias = 1; 
    999993    } 
     
    1001995        SWISH_CROAK("Error writing property alias_for attribute for %s", name); 
    1002996    } 
    1003     rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "id", "%d", 
    1004                                            prop->id); 
     997    rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "id", "%d", prop->id); 
    1005998    if (rc < 0) { 
    1006999        SWISH_CROAK("Error writing property id attribute for %s", name); 
     
    10091002/* all other attrs are irrelevant if this is an alias */ 
    10101003    if (!is_alias) { 
    1011         rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "ignore_case", 
    1012                                                "%d", prop->ignore_case); 
     1004        rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "ignore_case", "%d", 
     1005                                               prop->ignore_case); 
    10131006        if (rc < 0) { 
    1014             SWISH_CROAK("Error writing property ignore_case attribute for %s", 
    1015                         name); 
    1016         } 
    1017         rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "verbatim", 
    1018                                                "%d", prop->verbatim); 
     1007            SWISH_CROAK("Error writing property ignore_case attribute for %s", name); 
     1008        } 
     1009        rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "verbatim", "%d", 
     1010                                               prop->verbatim); 
    10191011        if (rc < 0) { 
    1020             SWISH_CROAK("Error writing property verbatim attribute for %s", 
    1021                         name); 
    1022         } 
    1023         rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "type", "%d", 
    1024                                                prop->type); 
     1012            SWISH_CROAK("Error writing property verbatim attribute for %s", name); 
     1013        } 
     1014        rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "type", "%d", prop->type); 
    10251015        if (rc < 0) { 
    10261016            SWISH_CROAK("Error writing property type attribute for %s", name); 
    10271017        } 
    1028         rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "max", "%d", 
    1029                                                prop->max); 
     1018        rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "max", "%d", prop->max); 
    10301019        if (rc < 0) { 
    10311020            SWISH_CROAK("Error writing property max attribute for %s", name); 
    10321021        } 
    1033         rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "sort", "%d", 
    1034                                                prop->sort); 
     1022        rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "sort", "%d", prop->sort); 
    10351023        if (rc < 0) { 
    10361024            SWISH_CROAK("Error writing property sort attribute for %s", name); 
     
    10461034) 
    10471035{ 
    1048     xmlHashScan(properties, (xmlHashScanner) write_property, writer); 
     1036    xmlHashScan(properties, (xmlHashScanner)write_property, writer); 
    10491037} 
    10501038 
     
    10651053) 
    10661054{ 
    1067     xmlHashScan(parsers, (xmlHashScanner) write_parser, writer); 
     1055    xmlHashScan(parsers, (xmlHashScanner)write_parser, writer); 
    10681056} 
    10691057 
     
    10761064{ 
    10771065    if (!swish_hash_exists((xmlHashTablePtr) things->thing1, ext) 
    1078         || !xmlStrEqual(swish_hash_fetch((xmlHashTablePtr) things->thing1, ext), 
    1079                         type) 
     1066        || !xmlStrEqual(swish_hash_fetch((xmlHashTablePtr) things->thing1, ext), type) 
    10801067        ) { 
    10811068 
     
    10831070            SWISH_DEBUG_MSG("writing unique MIME %s => %s", ext, type); 
    10841071        } 
    1085         write_element_with_content((xmlTextWriterPtr) things->thing3, ext, 
    1086                                    type); 
     1072        write_element_with_content((xmlTextWriterPtr) things->thing3, ext, type); 
    10871073    } 
    10881074} 
     
    10971083    things *things; 
    10981084    things = swish_xmalloc(sizeof(things)); 
    1099  
    11001085    things->thing1 = swish_mime_hash(); 
    11011086    things->thing2 = mimes; 
    11021087    things->thing3 = writer; 
    1103     xmlHashScan(mimes, (xmlHashScanner) write_mime, things); 
     1088    xmlHashScan(mimes, (xmlHashScanner)write_mime, things); 
     1089    swish_hash_free( things->thing1 ); 
     1090    swish_xfree(things); 
    11041091} 
    11051092 
     
    11101097) 
    11111098{ 
    1112     xmlHashScan(index, (xmlHashScanner) write_hash_entry, writer); 
     1099    xmlHashScan(index, (xmlHashScanner)write_hash_entry, writer); 
    11131100} 
    11141101 
     
    11191106) 
    11201107{ 
    1121     xmlHashScan(tag_aliases, (xmlHashScanner) write_hash_entry, writer); 
     1108    xmlHashScan(tag_aliases, (xmlHashScanner)write_hash_entry, writer); 
    11221109} 
    11231110 
     
    11281115) 
    11291116{ 
    1130     xmlHashScan(hash, (xmlHashScanner) write_hash_entry, writer); 
     1117    xmlHashScan(hash, (xmlHashScanner)write_hash_entry, writer); 
    11311118} 
    11321119 
     
    11421129    int rc; 
    11431130    xmlTextWriterPtr writer; 
     1131 
     1132    if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 
     1133        swish_debug_config(config); 
     1134    } 
    11441135 
    11451136/* Create a new XmlWriter for uri, with no compression. */ 
     
    11691160 
    11701161/* Write a comment indicating a computer wrote this file */ 
    1171     rc = xmlTextWriterWriteComment(writer, 
    1172                                    BAD_CAST 
    1173                                    "written by libswish3 - DO NOT EDIT"); 
     1162    rc = xmlTextWriterWriteComment(writer, BAD_CAST "written by libswish3 - DO NOT EDIT"); 
    11741163    if (rc < 0) { 
    11751164        SWISH_CROAK("Error at xmlTextWriterWriteComment\n"); 
    11761165    } 
    11771166 
    1178     write_element_with_content(writer, BAD_CAST "swish_verson", 
    1179                                BAD_CAST SWISH_VERSION); 
    1180     write_element_with_content(writer, BAD_CAST "swish_lib_version", 
    1181                                BAD_CAST SWISH_LIB_VERSION); 
     1167    // TODO check for these in reader and croak if mismatch 
     1168    if (!swish_hash_exists(config->misc, BAD_CAST "swish_version")) { 
     1169        write_element_with_content(writer, BAD_CAST "swish_verson", 
     1170                                   BAD_CAST SWISH_VERSION); 
     1171    } 
     1172    if (!swish_hash_exists(config->misc, BAD_CAST "swish_lib_version")) { 
     1173        write_element_with_content(writer, BAD_CAST "swish_lib_version", 
     1174                                   BAD_CAST SWISH_LIB_VERSION); 
     1175    } 
    11821176 
    11831177/* write MetaNames */ 
  • libswish3/trunk/src/libswish3/io.c

    r2103 r2108  
    157157 
    158158} 
     159 
     160boolean 
     161swish_file_exists( 
     162    xmlChar *filename 
     163) 
     164{ 
     165    struct stat info; 
     166    if (stat((char *)filename, &info)) { 
     167        return 0; 
     168    } 
     169    return 1; 
     170} 
  • libswish3/trunk/src/libswish3/libswish3.h

    r2106 r2108  
    8585#define SWISH_HYPERE_FORMAT       "hypere" 
    8686#define SWISH_INDEX_FILEFORMAT    "swish" 
     87#define SWISH_HEADER_FILE         "swish.xml" 
    8788 
    8889/* properties */ 
     
    187188typedef struct swish_Word               swish_Word; 
    188189typedef struct swish_WordList           swish_WordList; 
    189 typedef struct swish_ParserData          swish_ParserData; 
     190typedef struct swish_ParserData         swish_ParserData; 
    190191typedef struct swish_Tag                swish_Tag; 
    191192typedef struct swish_TagStack           swish_TagStack; 
     
    398399xmlChar *   swish_slurp_file_len( xmlChar *filename, long flen ); 
    399400xmlChar *   swish_slurp_file( xmlChar *filename ); 
     401boolean     swish_file_exists( xmlChar *filename ); 
    400402/* 
    401403=cut 
     
    413415void *      swish_hash_fetch( xmlHashTablePtr hash, xmlChar *key ); 
    414416xmlHashTablePtr swish_init_hash(int size); 
     417void        swish_hash_free( xmlHashTablePtr hash ); 
    415418/* 
    416419=cut 
     
    492495void            swish_config_test_unique_ids( swish_Config *c ); 
    493496void            swish_config_test_alias_fors( swish_Config *c ); 
     497swish_ConfigFlags * swish_init_config_flags(); 
     498 
    494499/* 
    495500=cut 
     
    643648swish_Config *  swish_read_header(char *filename); 
    644649void            swish_write_header(char* filename, swish_Config* config); 
     650void            swish_config_test_alias_fors(swish_Config *config); 
     651void            swish_config_test_unique_ids(swish_Config *config); 
    645652/* 
    646653=cut 
  • libswish3/trunk/src/libswish3/parser.c

    r2104 r2108  
    1  
    21/*  
    32 * This file is part of libswish3 
     
    485484     */ 
    486485 
    487     if (SWISH_DEBUG > 2
     486    if (SWISH_DEBUG & SWISH_DEBUG_PARSER
    488487        SWISH_DEBUG_MSG("startDocument()"); 
    489488 
     
    499498{ 
    500499 
    501     if (SWISH_DEBUG > 2
     500    if (SWISH_DEBUG & SWISH_DEBUG_PARSER
    502501        SWISH_DEBUG_MSG("endDocument()"); 
    503502 
     
    643642    parser_data->tag = build_tag(parser_data, (xmlChar *)tag, NULL); 
    644643 
    645     if (SWISH_DEBUG > 2
     644    if (SWISH_DEBUG & SWISH_DEBUG_PARSER
    646645        SWISH_DEBUG_MSG(" endElement(%s) (%s)", (xmlChar *)tag, 
    647646                        parser_data->tag); 
     
    744743) 
    745744{ 
    746     if (SWISH_DEBUG > 2
     745    if (SWISH_DEBUG & SWISH_DEBUG_PARSER
    747746        SWISH_DEBUG_MSG(" >> mycharacters()"); 
    748747 
     
    943942{ 
    944943 
    945     if (SWISH_DEBUG > 9
     944    if (SWISH_DEBUG & SWISH_DEBUG_PARSER
    946945        SWISH_DEBUG_MSG("init parser_data"); 
    947946 
     
    10241023    ptr->ctxt = NULL; 
    10251024 
    1026     if (SWISH_DEBUG > 9
     1025    if (SWISH_DEBUG & SWISH_DEBUG_PARSER
    10271026        SWISH_DEBUG_MSG("init done for parser_data"); 
    10281027 
     
    10371036{ 
    10381037 
    1039     if (SWISH_DEBUG > 9
     1038    if (SWISH_DEBUG & SWISH_DEBUG_PARSER
    10401039        SWISH_DEBUG_MSG("freeing swish_ParserData"); 
    10411040 
     
    12261225    info->ref_cnt++; 
    12271226 
    1228     if (SWISH_DEBUG > 5
     1227    if (SWISH_DEBUG & SWISH_DEBUG_PARSER
    12291228        SWISH_DEBUG_MSG("preparing to parse %d header lines", h->nlines); 
    12301229 
     
    12351234        val = swish_str_skip_ws(++val); 
    12361235 
    1237         if (SWISH_DEBUG > 2) { 
     1236        if (SWISH_DEBUG & SWISH_DEBUG_PARSER) { 
    12381237            SWISH_DEBUG_MSG("%d parsing header line: %s", i, line); 
    12391238 
     
    16401639    head = buf_to_head(buf); 
    16411640 
    1642     if (SWISH_DEBUG > 9
     1641    if (SWISH_DEBUG & SWISH_DEBUG_PARSER
    16431642        SWISH_DEBUG_MSG("number of headlines: %d", head->nlines); 
    16441643 
     
    16611660    (*s3->parser->handler) (parser_data); 
    16621661 
    1663     if (SWISH_DEBUG > 1) { 
     1662    if (SWISH_DEBUG & SWISH_DEBUG_PARSER) { 
    16641663        swish_debug_docinfo(parser_data->docinfo); 
    16651664        SWISH_DEBUG_MSG("  word buffer length: %d bytes", 
     
    17171716    (*s3->parser->handler) (parser_data); 
    17181717 
    1719     if (SWISH_DEBUG > 1) { 
     1718    if (SWISH_DEBUG & SWISH_DEBUG_PARSER) { 
    17201719        swish_debug_docinfo(parser_data->docinfo); 
    17211720        SWISH_DEBUG_MSG("  word buffer length: %d bytes", 
     
    18791878    set_encoding(parser_data, buffer); 
    18801879 
    1881     if (SWISH_DEBUG > 3
     1880    if (SWISH_DEBUG & SWISH_DEBUG_PARSER
    18821881        SWISH_DEBUG_MSG("txt parser encoding: %s", 
    18831882                        parser_data->docinfo->encoding); 
     
    18981897        else if (xmlStrEqual 
    18991898                 (parser_data->docinfo->encoding, (xmlChar *)"unknown")) { 
    1900             if (SWISH_DEBUG > 3
     1899            if (SWISH_DEBUG & SWISH_DEBUG_PARSER
    19011900                SWISH_DEBUG_MSG("default env encoding -> %s", enc); 
    19021901 
     
    22192218    swish_Tag *thistag = swish_xmalloc(sizeof(swish_Tag)); 
    22202219 
    2221     if (SWISH_DEBUG > 3) { 
     2220    if (SWISH_DEBUG & SWISH_DEBUG_PARSER) { 
    22222221        SWISH_DEBUG_MSG(" >>>>>>> before push: tag = '%s'", tag); 
    22232222        _debug_stack(stack); 
     
    22492248    stack->flat = flatten_tag_stack(NULL, stack); 
    22502249 
    2251     if (SWISH_DEBUG > 3) { 
     2250    if (SWISH_DEBUG & SWISH_DEBUG_PARSER) { 
    22522251        SWISH_DEBUG_MSG 
    22532252            (" >>> stack size: %d  thistag count: %d  current head tag = '%s'", 
     
    22672266{ 
    22682267 
    2269     if (SWISH_DEBUG > 3) { 
     2268    if (SWISH_DEBUG & SWISH_DEBUG_PARSER) { 
    22702269        SWISH_DEBUG_MSG(" pop_tag_stack: %s from %s", stack->head->name, 
    22712270                        stack->name); 
     
    22752274 
    22762275    if (stack->count > 1) { 
    2277         if (SWISH_DEBUG > 3) { 
     2276        if (SWISH_DEBUG & SWISH_DEBUG_PARSER) { 
    22782277            SWISH_DEBUG_MSG("  >>>  %d: popping '%s' from tagstack <<<", 
    22792278                            stack->head->n, stack->head->name); 
     
    22962295    else { 
    22972296 
    2298         if (SWISH_DEBUG > 3) { 
     2297        if (SWISH_DEBUG & SWISH_DEBUG_PARSER) { 
    22992298            SWISH_DEBUG_MSG 
    23002299                ("  >>>  %d: popping '%s' from tagstack will leave stack empty (flat: %s) <<<", 
     
    23242323    stack->flat = flatten_tag_stack(NULL, stack); 
    23252324 
    2326     if (SWISH_DEBUG > 3) { 
     2325    if (SWISH_DEBUG & SWISH_DEBUG_PARSER) { 
    23272326        SWISH_DEBUG_MSG("  >> stack size = %d   head of stack = %s <<", 
    23282327                        stack->count, stack->head->name); 
     
    23492348    prev_flat = swish_xstrdup(stack->flat); 
    23502349 
    2351     if (SWISH_DEBUG > 3) { 
     2350    if (SWISH_DEBUG & SWISH_DEBUG_PARSER) { 
    23522351        SWISH_DEBUG_MSG("pop_tag_stack_on_match() for %s", stack->name); 
    23532352        SWISH_DEBUG_MSG("comparing '%s' against '%s'", tag, stack->head->name); 
     
    23572356    if (xmlStrEqual(stack->head->name, tag)) { 
    23582357 
    2359         if (SWISH_DEBUG > 3) { 
     2358        if (SWISH_DEBUG & SWISH_DEBUG_PARSER) { 
    23602359            SWISH_DEBUG_MSG 
    23612360                (" >>>>>>>>>>>>>>>>>>>  current tag = '%s' matches top of tagstack", 
     
    23692368        if (pop_tag_stack(stack)) { 
    23702369 
    2371             if (SWISH_DEBUG > 3) { 
     2370            if (SWISH_DEBUG & SWISH_DEBUG_PARSER) { 
    23722371                SWISH_DEBUG_MSG("stack popped. tag = %s   stack->head = %s", 
    23732372                                tag, stack->head->name); 
     
    23812380         */ 
    23822381        else if (stack->count) { 
    2383             if (SWISH_DEBUG > 3
     2382            if (SWISH_DEBUG & SWISH_DEBUG_PARSER
    23842383                SWISH_DEBUG_MSG("  using stack->head %s", stack->head->name); 
    23852384 
     
    23892388    } 
    23902389    else { 
    2391         if (SWISH_DEBUG > 3
     2390        if (SWISH_DEBUG & SWISH_DEBUG_PARSER
    23922391            SWISH_DEBUG_MSG("no match for '%s'", tag); 
    23932392 
  • libswish3/trunk/src/xapian/swish_xapian.cpp

    r2096 r2108  
    1818 */ 
    1919 
    20 /*  example Swish3 program using Xapian IR backend. 
     20/*   
     21    example Swish3 program using Xapian IR backend. 
    2122    many of the string conversion functions and the index_document() code 
    2223    come nearly verbatim from the xapian-omega distribution. 
    2324 
    2425*/ 
    25  
    26 //#include <config.h> 
    2726 
    2827#include <algorithm> 
     
    4544#include <getopt.h> 
    4645 
    47  
    4846#include <xapian.h> 
    4947 
     
    5452 
    5553/* prototypes */ 
    56 int             main(int argc, char **argv); 
    57 int             usage(); 
    58 void            handler(swish_ParserData * parser_data); 
    59 int             open_writeable_index(char* dbpath); 
    60 int             open_readable_index(char* dbpath); 
    61 int             do_search(char* query); 
     54int main( 
     55    int argc, 
     56    char **argv 
     57); 
     58int usage( 
     59); 
     60void handler( 
     61    swish_ParserData *parser_data 
     62); 
     63int open_writeable_index( 
     64    char *dbpath 
     65); 
     66int open_readable_index( 
     67    char *dbpath 
     68); 
     69int do_search( 
     70    char *query 
     71); 
    6272 
    6373/* global vars */ 
    64 static int      debug = 0; 
    65 static Xapian::WritableDatabase wdb; 
    66 static Xapian::Database::Database rdb; 
     74static int debug = 0; 
     75static Xapian::WritableDatabase     wdb; 
     76static Xapian::Database::Database   rdb; 
    6777static Xapian::Stem stemmer("english"); 
    68 static Xapian::TermGenerator indexer; 
    69 static int      twords = 0; 
    70 static int      skip_duplicates = 0; 
    71 static int      overwrite = 0; 
    72 static vector<bool> updated; 
    73 static swish_3* s3; 
    74  
    75  
    76 extern int SWISH_DEBUG; 
    77  
    78 static struct option longopts[] = 
    79 
    80     {"config",          required_argument,  0, 'c'}, 
    81     {"debug",           required_argument,  0, 'd'}, 
    82     {"help",            no_argument,        0, 'h'}, 
    83     {"index",           required_argument,  0, 'i'}, 
    84     {"skip-duplicates", no_argument,        0, 's'}, 
    85     {"overwrite",       no_argument,        0, 'o'}, 
    86     {"query",           required_argument,  0, 'q'}, 
     78static Xapian::TermGenerator        indexer; 
     79static int                          twords = 0; 
     80static int                          skip_duplicates = 0; 
     81static int                          overwrite = 0; 
     82static vector < bool >  updated; 
     83static swish_3                      *s3; 
     84 
     85extern int  SWISH_DEBUG; 
     86 
     87static struct option 
     88    longopts[] = { 
     89    {"config", required_argument, 0, 'c'}, 
     90    {"debug", required_argument, 0, 'd'}, 
     91    {"help", no_argument, 0, 'h'}, 
     92    {"index", required_argument, 0, 'i'}, 
     93    {"skip-duplicates", no_argument, 0, 's'}, 
     94    {"overwrite", no_argument, 0, 'o'}, 
     95    {"query", required_argument, 0, 'q'}, 
    8796    {0, 0, 0, 0} 
    8897}; 
    89  
    9098 
    9199// This ought to be enough for any of the conversions below. 
     
    108116 
    109117int 
    110 string_to_int(const string &s) 
     118string_to_int( 
     119    const string & s 
     120
    111121{ 
    112122    return atoi(s.c_str()); 
     
    114124 
    115125string 
    116 int_to_string(int val) 
     126int_to_string( 
     127    int val 
     128
    117129{ 
    118130    CONVERT_TO_STRING("%d") 
     
    120132 
    121133string 
    122 long_to_string(long val) 
     134long_to_string( 
     135    long val 
     136
    123137{ 
    124138    CONVERT_TO_STRING("%ld") 
     
    126140 
    127141string 
    128 double_to_string(double val) 
     142double_to_string( 
     143    double val 
     144
    129145{ 
    130146    CONVERT_TO_STRING("%f") 
     
    132148 
    133149string 
    134 date_to_string(int y, int m, int d) 
    135 
    136     char buf[11]; 
    137     if (y < 0) y = 0; else if (y > 9999) y = 9999; 
    138     if (m < 1) m = 1; else if (m > 12) m = 12; 
    139     if (d < 1) d = 1; else if (d > 31) d = 31; 
     150date_to_string( 
     151    int y, 
     152    int m, 
     153    int d 
     154
     155
     156    char 
     157        buf[11]; 
     158    if (y < 0) 
     159        y = 0; 
     160    else if (y > 9999) 
     161        y = 9999; 
     162    if (m < 1) 
     163        m = 1; 
     164    else if (m > 12) 
     165        m = 12; 
     166    if (d < 1) 
     167        d = 1; 
     168    else if (d > 31) 
     169        d = 31; 
    140170#ifdef SNPRINTF 
    141     int len = SNPRINTF(buf, sizeof(buf), "%04d%02d%02d", y, m, d); 
    142     if (len == -1 || len > BUFSIZE) return string(buf, BUFSIZE); 
     171    int 
     172        len = SNPRINTF(buf, sizeof(buf), "%04d%02d%02d", y, m, d); 
     173    if (len == -1 || len > BUFSIZE) 
     174        return string(buf, BUFSIZE); 
    143175    return string(buf, len); 
    144176#else 
    145177    buf[sizeof(buf) - 1] = '\0'; 
    146178    sprintf(buf, "%04d%02d%02d", y, m, d); 
    147     if (buf[sizeof(buf) - 1]) abort(); /* Uh-oh, buffer overrun */ 
     179    if (buf[sizeof(buf) - 1]) 
     180        abort();                /* Uh-oh, buffer overrun */ 
    148181    return string(buf); 
    149182#endif 
    150183} 
    151184 
    152 inline uint32_t binary_string_to_int(const std::string &s) 
    153 
    154     if (s.size() != 4) return (uint32_t)-1; 
    155     uint32_t v; 
     185inline uint32_t 
     186binary_string_to_int( 
     187    const std::string & s 
     188
     189
     190    if (s.size() != 4) 
     191        return (uint32_t) - 1; 
     192    uint32_t 
     193        v; 
    156194    memcpy(&v, s.data(), 4); 
    157195    return ntohl(v); 
    158196} 
    159197 
    160 inline std::string int_to_binary_string(uint32_t v) 
     198inline 
     199    std::string 
     200int_to_binary_string( 
     201    uint32_t v 
     202
    161203{ 
    162204    v = htonl(v); 
    163     return std::string(reinterpret_cast<const char*>(&v), 4); 
     205    return std::string(reinterpret_cast < const char *>(&v), 4); 
    164206} 
    165207 
    166208static string 
    167 get_prefix(xmlChar* metaname, swish_Config* config) 
     209get_prefix( 
     210    xmlChar *metaname, 
     211    swish_Config *config 
     212
    168213{ 
    169214    string prefix; 
    170     swish_MetaName *meta = (swish_MetaName*)swish_hash_fetch(config->metanames, metaname); 
     215    swish_MetaName *meta = 
     216        (swish_MetaName *)swish_hash_fetch(config->metanames, metaname); 
    171217    prefix = int_to_string(meta->id); 
    172218    return prefix; 
     
    174220 
    175221static unsigned int 
    176 get_weight(xmlChar* metaname, swish_Config* config) 
     222get_weight( 
     223    xmlChar *metaname, 
     224    swish_Config *config 
     225
    177226{ 
    178227    unsigned int w; 
    179     swish_MetaName *meta = (swish_MetaName*)swish_hash_fetch(config->metanames, metaname); 
    180     return meta->bias > 0 ? meta->bias : 1; // TODO need to account for negative values. 
     228    swish_MetaName *meta = 
     229        (swish_MetaName *)swish_hash_fetch(config->metanames, metaname); 
     230    return meta->bias > 0 ? meta->bias : 1;     // TODO need to account for negative values. 
    181231} 
    182232 
    183233static void 
    184 add_metanames(xmlBufferPtr buffer, void* config, xmlChar* metaname) 
     234add_metanames( 
     235    xmlBufferPtr buffer, 
     236    void *config, 
     237    xmlChar *metaname 
     238
    185239{ 
    186240    // lookup weight and prefix 
    187     string prefix       = get_prefix(metaname, (swish_Config*)config); 
    188     unsigned int weight = get_weight(metaname, (swish_Config*)config); 
    189     indexer.index_text((const char*)xmlBufferContent(buffer), weight, prefix); 
     241    string prefix = get_prefix(metaname, (swish_Config *)config); 
     242    unsigned int weight = get_weight(metaname, (swish_Config *)config); 
     243    indexer.index_text((const char *)xmlBufferContent(buffer), weight, prefix); 
    190244} 
    191245 
    192246static void 
    193 add_properties(xmlBufferPtr buffer, Xapian::Document doc, xmlChar* name) 
    194 
    195     swish_Property* prop; 
    196     prop = (swish_Property*)swish_hash_fetch(s3->config->properties, name); 
    197     SWISH_DEBUG_MSG("adding property %s [%d]: %s", name, prop->id, xmlBufferContent(buffer)); 
    198     doc.add_value(prop->id, (const char*)xmlBufferContent(buffer)); 
    199 
    200  
    201 void  
    202 handler(swish_ParserData * parser_data) 
    203 
    204     printf("nwords: %d\n", parser_data->docinfo->nwords); 
    205      
     247add_properties( 
     248    xmlBufferPtr buffer, 
     249    Xapian::Document doc, 
     250    xmlChar *name 
     251
     252
     253    swish_Property *prop; 
     254    prop = (swish_Property *)swish_hash_fetch(s3->config->properties, name); 
     255    //SWISH_DEBUG_MSG("adding property %s [%d]: %s", name, prop->id, 
     256    //                xmlBufferContent(buffer)); 
     257    doc.add_value(prop->id, (const char *)xmlBufferContent(buffer)); 
     258
     259 
     260void 
     261handler( 
     262    swish_ParserData *parser_data 
     263
     264
     265    //printf("nwords: %d\n", parser_data->docinfo->nwords); 
     266