Changeset 2097
- Timestamp:
- 03/23/08 23:49:06 (2 months ago)
- Files:
-
- libswish3/trunk/perl/docmaker.pl (modified) (3 diffs)
- libswish3/trunk/src/example/swish.xml (modified) (2 diffs)
- libswish3/trunk/src/libswish3/config.c (modified) (3 diffs)
- libswish3/trunk/src/libswish3/hash.c (modified) (1 diff)
- libswish3/trunk/src/libswish3/header.c (modified) (25 diffs)
- libswish3/trunk/src/libswish3/libswish3.h (modified) (2 diffs)
- libswish3/trunk/src/libswish3/mime_types.c (modified) (5 diffs)
- libswish3/trunk/src/libswish3/property.c (modified) (2 diffs)
- libswish3/trunk/src/libswish3/string.c (modified) (1 diff)
- libswish3/trunk/src/swish_header.c (modified) (3 diffs)
- libswish3/trunk/src/test.pl (modified) (3 diffs, 1 prop)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
libswish3/trunk/perl/docmaker.pl
r1955 r2097 10 10 11 11 die $usage unless @ARGV; 12 13 my $docmaker = SWISH::Prog::Headers->new; 12 14 13 15 #$ENV{SWISH3} = 1; … … 83 85 ); 84 86 85 my $header = SWISH::Prog::Headers->head(87 my $header = $docmaker->head( 86 88 $xml, 87 89 { url => $i, … … 96 98 97 99 } 100 101 warn "\n"; 102 libswish3/trunk/src/example/swish.xml
r2046 r2097 6 6 <foo bias="+10" /> 7 7 <bar bias="-5" /> 8 < swishtitle bias="+50" alias="title" />8 <title bias="+50" alias_for="swishtitle" /> 9 9 <other>color size weight</other> 10 10 </MetaNames> … … 15 15 <lastmod type="date" /> 16 16 <bing ignore_case="0" /> 17 <description verbatim="1" max="10000" alias ="body" />17 <description verbatim="1" max="10000" alias_for="swishdescription" /> 18 18 <notsorted sort="0" /> 19 19 <myaliased>somealias someotheralias</myaliased> 20 <UPPERCASE alias_for="MiXeDCaSE" /> 21 <mixedcase type="text" /> 20 22 </PropertyNames> 23 24 <MIME> 25 <au>foo/bar</au> 26 </MIME> 21 27 28 <Parsers> 29 <XML>text/foo</XML> 30 <HTML>foo/bar 1234/5678</HTML> 31 <TXT>text/flop default</TXT> 32 </Parsers> 33 22 34 <Tokenize>1</Tokenize> 23 35 </swish> libswish3/trunk/src/libswish3/config.c
r2096 r2097 128 128 config->stash = NULL; 129 129 130 /* misc default flags */ 131 config->flags->tokenize = 1; 132 130 133 return config; 131 134 … … 235 238 swish_xstrdup((xmlChar *) SWISH_PROP_DESCRIPTION)); 236 239 237 /* misc default flags */238 config->flags->tokenize = 1;239 240 240 241 if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { … … 399 400 400 401 /* values in config2 override and are set in config1 */ 401 //SWISH_DEBUG_MSG("merge properties"); 402 if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 403 SWISH_DEBUG_MSG("merge properties"); 404 } 402 405 merge_properties(config1->properties, config2->properties); 403 //SWISH_DEBUG_MSG("merge metanames"); 406 407 if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 408 SWISH_DEBUG_MSG("merge metanames"); 409 } 404 410 merge_metanames(config1->metanames, config2->metanames); 405 //SWISH_DEBUG_MSG("merge parsers"); 411 412 if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 413 SWISH_DEBUG_MSG("merge parsers"); 414 } 406 415 swish_hash_merge(config1->parsers, config2->parsers); 407 //SWISH_DEBUG_MSG("merge mimes"); 416 417 if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 418 SWISH_DEBUG_MSG("merge mimes"); 419 } 408 420 swish_hash_merge(config1->mimes, config2->mimes); 409 //SWISH_DEBUG_MSG("merge index"); 421 422 if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 423 SWISH_DEBUG_MSG("merge index"); 424 } 410 425 swish_hash_merge(config1->index, config2->index); 411 //SWISH_DEBUG_MSG("merge tag_aliases"); 426 427 if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 428 SWISH_DEBUG_MSG("merge tag_aliases"); 429 } 412 430 swish_hash_merge(config1->tag_aliases, config2->tag_aliases); 413 //SWISH_DEBUG_MSG("merge misc"); 431 432 if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 433 SWISH_DEBUG_MSG("merge misc"); 434 } 414 435 swish_hash_merge(config1->misc, config2->misc); 436 437 if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 438 SWISH_DEBUG_MSG("merge complete"); 439 } 415 440 416 441 /* set flags */ 417 442 config1->flags->tokenize = config2->flags->tokenize; 418 443 419 } 420 444 if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 445 SWISH_DEBUG_MSG("flags set"); 446 } 447 448 } 449 libswish3/trunk/src/libswish3/hash.c
r2096 r2097 95 95 96 96 static void 97 merge_hashes( xml HashTablePtr hash1, xmlChar *value, xmlChar *key )97 merge_hashes( xmlChar *value, xmlHashTablePtr hash1, xmlChar *key ) 98 98 { 99 99 if (swish_hash_exists(hash1, key)) { 100 swish_hash_replace(hash1, key, value);100 swish_hash_replace(hash1, key, swish_xstrdup(value)); 101 101 } 102 102 else { 103 swish_hash_add(hash1, key, value);103 swish_hash_add(hash1, key, swish_xstrdup(value)); 104 104 } 105 105 } libswish3/trunk/src/libswish3/header.c
r2096 r2097 21 21 22 22 #include <libxml/xmlreader.h> 23 #include <libxml/xmlwriter.h> 24 #include <libxml/encoding.h> 23 25 #include "libswish3.h" 24 26 … … 40 42 } headmaker; 41 43 42 static void 43 do_index(xmlTextReaderPtr reader, headmaker *h) 44 { 45 SWISH_DEBUG_MSG("TODO index"); 46 } 47 48 static void 49 do_parser(xmlTextReaderPtr reader, headmaker *h) 50 { 51 SWISH_DEBUG_MSG("TODO parser"); 52 } 53 54 static void 55 do_alias(xmlTextReaderPtr reader, headmaker *h) 56 { 57 SWISH_DEBUG_MSG("TODO alias"); 58 } 59 60 static void 61 do_mime(xmlTextReaderPtr reader, headmaker *h) 62 { 63 SWISH_DEBUG_MSG("TODO mime"); 64 } 65 66 static void 67 do_metaname_aliases( 44 typedef struct { 45 void* thing1; 46 void* thing2; 47 void* thing3; 48 } things; 49 50 boolean 51 swish_validate_header(char *filename); 52 boolean 53 swish_merge_config_with_header(char *filename, swish_Config *c); 54 swish_Config * 55 swish_read_header(char *filename); 56 void 57 swish_write_header(char* uri, swish_Config* config); 58 static void 59 read_metaname_aliases( 60 xmlChar *str, 61 headmaker *h, 62 swish_MetaName *meta 63 ); 64 static void 65 read_metaname_attr( 66 const xmlChar *attr, 67 const xmlChar *attr_val, 68 swish_MetaName *meta, 69 headmaker *h 70 ); 71 static void 72 read_metaname(xmlTextReaderPtr reader, headmaker *h); 73 static void 74 read_property_aliases( 75 xmlChar *str, 76 headmaker *h, 77 swish_Property *prop 78 ); 79 static void 80 read_property_attr( 81 const xmlChar *attr, 82 const xmlChar *attr_val, 83 swish_Property *prop, 84 headmaker *h 85 ); 86 static void 87 read_property(xmlTextReaderPtr reader, headmaker *h); 88 static void 89 process_node(xmlTextReaderPtr reader, headmaker *h); 90 static void 91 read_header(char *filename, headmaker *h); 92 static void 93 read_key_value_pair(xmlTextReaderPtr reader, xmlHashTablePtr hash, xmlChar* name); 94 static void 95 read_key_values_pair(xmlTextReaderPtr reader, xmlHashTablePtr hash, xmlChar* name); 96 static headmaker * 97 init_headmaker(); 98 static void 99 write_open_tag(xmlTextWriterPtr writer, xmlChar* tag); 100 static void 101 write_close_tag(xmlTextWriterPtr writer); 102 static void 103 write_element_with_content(xmlTextWriterPtr writer, xmlChar* tag, xmlChar* content); 104 static void 105 write_metanames(xmlTextWriterPtr writer, xmlHashTablePtr metanames); 106 static void 107 write_hash_entry(xmlChar* value, xmlTextWriterPtr writer, xmlChar* key); 108 static void 109 write_properties(xmlTextWriterPtr writer, xmlHashTablePtr properties); 110 static void 111 write_parsers(xmlTextWriterPtr writer, xmlHashTablePtr parsers); 112 static void 113 write_mimes(xmlTextWriterPtr writer, xmlHashTablePtr mimes); 114 static void 115 write_index(xmlTextWriterPtr writer, xmlHashTablePtr index); 116 static void 117 write_tag_aliases(xmlTextWriterPtr writer, xmlHashTablePtr tag_aliases); 118 static void 119 write_misc(xmlTextWriterPtr writer, xmlHashTablePtr hash); 120 121 122 static void 123 read_metaname_aliases( 68 124 xmlChar *str, 69 125 headmaker *h, … … 76 132 strlist = swish_make_stringlist(str); 77 133 78 /* loop over each alias and create a Propertyfor each,79 setting alias_for to prop->name134 /* loop over each alias and create a MetaName for each, 135 setting alias_for to meta->name 80 136 */ 81 137 for (i=0; i < strlist->n; i++) { … … 114 170 115 171 static void 116 do_metaname_attr(172 read_metaname_attr( 117 173 const xmlChar *attr, 118 174 const xmlChar *attr_val, … … 121 177 ) 122 178 { 123 124 179 if (xmlStrEqual(attr, (xmlChar*)"bias")) { 125 180 meta->bias = (boolean)strtol((char*)attr_val, (char**)NULL, 10); … … 128 183 meta->id = (int)strtol((char*)attr_val, (char**)NULL, 10); 129 184 } 130 else if (xmlStrEqual(attr, (xmlChar*)"alias ")) {131 do_metaname_aliases( (xmlChar*)attr_val, h, meta);185 else if (xmlStrEqual(attr, (xmlChar*)"alias_for")) { 186 meta->alias_for = swish_str_tolower( BAD_CAST attr_val ); 132 187 } 133 188 else { … … 137 192 138 193 static void 139 do_metaname(xmlTextReaderPtr reader, headmaker *h)194 read_metaname(xmlTextReaderPtr reader, headmaker *h) 140 195 { 141 196 xmlChar *value; … … 149 204 && xmlTextReaderNodeType(reader) == XML_READER_TYPE_TEXT ) { 150 205 meta->name = swish_str_tolower( (xmlChar*)h->parent_name ); 151 do_metaname_aliases( xmlTextReaderValue(reader), h, meta );206 read_metaname_aliases( xmlTextReaderValue(reader), h, meta ); 152 207 return; 153 208 } … … 157 212 158 213 xmlTextReaderMoveToFirstAttribute(reader); 159 do_metaname_attr(214 read_metaname_attr( 160 215 xmlTextReaderConstName(reader), 161 216 xmlTextReaderConstValue(reader), … … 165 220 166 221 while(xmlTextReaderMoveToNextAttribute(reader) == 1) { 167 do_metaname_attr(222 read_metaname_attr( 168 223 xmlTextReaderConstName(reader), 169 224 xmlTextReaderConstValue(reader), … … 185 240 } 186 241 else { 187 SWISH_WARN("MetaName %s is already defined", meta->name); // TODO could be alias. how to check? 242 SWISH_WARN("MetaName %s is already defined", meta->name); 243 // TODO could be alias. how to check? 188 244 } 189 245 … … 201 257 202 258 static void 203 do_property_aliases(259 read_property_aliases( 204 260 xmlChar *str, 205 261 headmaker *h, … … 238 294 239 295 static void 240 do_property_attr(296 read_property_attr( 241 297 const xmlChar *attr, 242 298 const xmlChar *attr_val, … … 272 328 } 273 329 } 274 else if (xmlStrEqual(attr, (xmlChar*)"alias ")) {275 do_property_aliases( (xmlChar*)attr_val, h, prop);330 else if (xmlStrEqual(attr, (xmlChar*)"alias_for")) { 331 prop->alias_for = swish_str_tolower(BAD_CAST attr_val); 276 332 } 277 333 else { … … 282 338 283 339 static void 284 do_property(xmlTextReaderPtr reader, headmaker *h)340 read_property(xmlTextReaderPtr reader, headmaker *h) 285 341 { 286 342 xmlChar *value; … … 294 350 && xmlTextReaderNodeType(reader) == XML_READER_TYPE_TEXT ) { 295 351 prop->name = swish_str_tolower( (xmlChar*)h->parent_name ); 296 do_property_aliases( xmlTextReaderValue(reader), h, prop );352 read_property_aliases( xmlTextReaderValue(reader), h, prop ); 297 353 return; 298 354 } … … 302 358 303 359 xmlTextReaderMoveToFirstAttribute(reader); 304 do_property_attr(360 read_property_attr( 305 361 xmlTextReaderConstName(reader), 306 362 xmlTextReaderConstValue(reader), … … 310 366 311 367 while(xmlTextReaderMoveToNextAttribute(reader) == 1) { 312 do_property_attr(368 read_property_attr( 313 369 xmlTextReaderConstName(reader), 314 370 xmlTextReaderConstValue(reader), … … 359 415 return; 360 416 361 if (xmlStrEqual(name, (const xmlChar*) "swish")) {417 if (xmlStrEqual(name, (const xmlChar*)SWISH_HEADER_ROOT)) { 362 418 h->is_valid = 1; 363 419 return; … … 434 490 435 491 if (h->isprops) { 436 do_property(reader, h);492 read_property(reader, h); 437 493 return; 438 494 } 439 495 else if (h->ismetas) { 440 do_metaname(reader, h);496 read_metaname(reader, h); 441 497 return; 442 498 } 443 499 else if (h->isindex) { 444 do_index(reader, h);500 read_key_value_pair(reader, h->config->index, (xmlChar*)name); 445 501 return; 446 502 } 447 503 else if (h->isparser) { 448 do_parser(reader, h);504 read_key_values_pair(reader, h->config->parsers, (xmlChar*)name); 449 505 return; 450 506 } 451 507 else if (h->ismime) { 452 do_mime(reader, h);508 read_key_value_pair(reader, h->config->mimes, (xmlChar*)name); 453 509 return; 454 510 } 455 511 else if (h->isalias) { 456 do_alias(reader, h);512 read_key_values_pair(reader, h->config->tag_aliases, (xmlChar*)name); 457 513 return; 458 514 } 459 515 else if (type == XML_READER_TYPE_ELEMENT) { 460 461 //SWISH_DEBUG_MSG("misc header value"); 516 read_key_value_pair(reader, h->config->misc, (xmlChar*)name); 517 return; 518 } 519 520 } 521 522 523 } 524 525 static void 526 read_key_values_pair(xmlTextReaderPtr reader, xmlHashTablePtr hash, xmlChar* name) 527 { 528 swish_StringList* strlist; 529 xmlChar* str; 530 const xmlChar* value; 531 int i; 532 533 /* element. get text and add to misc */ 534 if (xmlTextReaderRead(reader) == 1) { 535 if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_TEXT) { 462 536 463 /* element. get text and add to misc */ 464 if (xmlTextReaderRead(reader) == 1) { 465 if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_TEXT) { 466 value = xmlTextReaderConstValue(reader); 467 if (swish_hash_exists(h->config->misc, (xmlChar*)name)) { 468 swish_hash_replace(h->config->misc, (xmlChar*)name, swish_xstrdup(value)); 469 } 470 else { 471 swish_hash_add(h->config->misc, (xmlChar*)name, swish_xstrdup(value)); 472 } 537 value = xmlTextReaderConstValue(reader); 538 str = swish_str_tolower( (xmlChar*)value ); 539 strlist = swish_make_stringlist( str ); 540 541 for(i=0; i<strlist->n; i++) { 542 //SWISH_DEBUG_MSG("key_values pair: %s -> %s", strlist->word[i], name); 543 if (swish_hash_exists(hash, strlist->word[i])) { 544 swish_hash_replace(hash, strlist->word[i], swish_xstrdup( name )); 473 545 } 474 546 else { 475 SWISH_CROAK("header line missing value: %s", name);547 swish_hash_add(hash, strlist->word[i], swish_xstrdup( name )); 476 548 } 477 549 } 550 551 swish_free_stringlist( strlist ); 552 swish_xfree( str ); 553 554 } 555 else { 556 SWISH_CROAK("header line missing value: %s", name); 557 } 558 } 559 else { 560 SWISH_CROAK("error reading value for header element %s", name); 561 } 562 563 } 564 565 static void 566 read_key_value_pair(xmlTextReaderPtr reader, xmlHashTablePtr hash, xmlChar* name) 567 { 568 const xmlChar* value; 569 570 /* element. get text and add to misc */ 571 if (xmlTextReaderRead(reader) == 1) { 572 if (xmlTextReaderNodeType(reader) == XML_READER_TYPE_TEXT) { 573 value = xmlTextReaderConstValue(reader); 574 //SWISH_DEBUG_MSG("read key %s for value %s", name, value); 575 if (swish_hash_exists(hash, name)) { 576 swish_hash_replace(hash, name, swish_xstrdup(value)); 577 } 478 578 else { 479 SWISH_CROAK("error reading value for header element %s", name);579 swish_hash_add(hash, name, swish_xstrdup(value)); 480 580 } 481 482 } 483 484 } 485 486 487 } 488 581 } 582 else { 583 SWISH_CROAK("header line missing value: %s", name); 584 } 585 } 586 else { 587 SWISH_CROAK("error reading value for header element %s", name); 588 } 589 590 } 489 591 490 592 static void … … 506 608 0); 507 609 508 //SWISH_DEBUG_MSG("header parsed in-memory"); 610 if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 611 SWISH_DEBUG_MSG("header parsed in-memory"); 612 } 509 613 } 510 614 else { 511 615 reader = xmlReaderForFile(filename, NULL, 0); 512 616 513 //SWISH_DEBUG_MSG("header parsed from file"); 617 if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 618 SWISH_DEBUG_MSG("header parsed from file"); 619 } 514 620 } 515 621 … … 534 640 } 535 641 642 static void 643 test_meta_alias_for(swish_MetaName* meta, swish_Config* c, xmlChar* name) 644 { 645 if ( meta->alias_for != NULL 646 && !swish_hash_exists( c->metanames, meta->alias_for ) 647 ) { 648 SWISH_CROAK("MetaName %s has alias_for value of %s but no such MetaName defined", 649 name, meta->alias_for); 650 } 651 } 652 653 static void 654 test_prop_alias_for(swish_Property* prop, swish_Config* c, xmlChar* name) 655 { 656 if ( prop->alias_for != NULL 657 && !swish_hash_exists( c->properties, prop->alias_for ) 658 ) { 659 SWISH_CROAK("Property %s has alias_for value of %s but no such Property defined", 660 name, prop->alias_for); 661 } 662 } 663 664 665 void 666 swish_config_test_alias_fors(swish_Config* c) 667 { 668 xmlHashScan(c->metanames, (xmlHashScanner)test_meta_alias_for, c); 669 xmlHashScan(c->properties, (xmlHashScanner)test_prop_alias_for, c); 670 } 671 536 672 static headmaker * 537 673 init_headmaker() … … 540 676 h = swish_xmalloc(sizeof(headmaker)); 541 677 h->config = swish_init_config(); 678 // mimes is set to NULL in default config but we need it to be a hash here. 679 h->config->mimes = swish_init_hash(8); 542 680 h->isprops = 0; 543 681 h->ismetas = 0; … … 566 704 h = init_headmaker(); 567 705 read_header( filename, h ); 706 if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 707 SWISH_DEBUG_MSG("read_header complete"); 708 } 568 709 swish_config_merge( c, h->config ); 710 if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 711 SWISH_DEBUG_MSG("config_merge complete"); 712 } 569 713 swish_free_config( h->config ); 570 714 swish_xfree( h ); 715 716 // now test that all the alias_for links resolve ok 717 swish_config_test_alias_fors(c); 718 571 719 return 1; 572 720 } … … 583 731 return c; 584 732 } 733 734 static void 735 write_open_tag(xmlTextWriterPtr writer, xmlChar* tag) 736 { 737 int rc; 738 rc = xmlTextWriterStartElement(writer, tag);\ 739 if (rc < 0) { 740 SWISH_CROAK("Error writing elemtn %s", tag); 741 } 742 } 743 744 static void 745 write_close_tag(xmlTextWriterPtr writer) 746 { 747 int rc; 748 rc = xmlTextWriterEndElement(writer); 749 if (rc < 0) { 750 SWISH_CROAK("Error at xmlTextWriterEndElement"); 751 } 752 } 753 754 static void 755 write_element_with_content(xmlTextWriterPtr writer, xmlChar* tag, xmlChar* content) 756 { 757 int rc; 758 rc = xmlTextWriterWriteElement(writer, tag, content); 759 if (rc < 0) { 760 SWISH_CROAK("Error writing element %s with content %s", tag, content); 761 } 762 } 763 764 static void 765 write_metaname(swish_MetaName* meta, xmlTextWriterPtr writer, xmlChar* name) 766 { 767 int rc; 768 boolean is_alias; 769 write_open_tag(writer, name); 770 if (meta->alias_for == NULL) { 771 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "alias_for", BAD_CAST ""); 772 is_alias = 0; 773 } 774 else { 775 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "alias_for", meta->alias_for); 776 is_alias = 1; 777 } 778 if (rc < 0) { 779 SWISH_CROAK("Error writing metaname alias_for attribute for %s", name); 780 } 781 782 if (!is_alias) { 783 rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "bias", "%d", meta->bias); 784 if (rc < 0) { 785 SWISH_CROAK("Error writing metaname bias attribute for %s", name); 786 } 787 788 } 789 790 rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "id", "%d", meta->id); 791 if (rc < 0) { 792 SWISH_CROAK("Error writing metaname id attribute for %s", name); 793 } 794 write_close_tag(writer); 795 } 796 797 static void 798 write_metanames(xmlTextWriterPtr writer, xmlHashTablePtr metanames) 799 { 800 xmlHashScan(metanames, (xmlHashScanner)write_metaname, writer); 801 } 802 803 static void 804 write_hash_entry(xmlChar* value, xmlTextWriterPtr writer, xmlChar* key) 805 { 806 write_element_with_content(writer, key, value); 807 } 808 809 static void 810 write_property(swish_Property* prop, xmlTextWriterPtr writer, xmlChar* name) 811 { 812 int rc; 813 boolean is_alias; 814 write_open_tag(writer, name); 815 if (prop->alias_for == NULL) { 816 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "alias_for", BAD_CAST ""); 817 is_alias = 0; 818 } 819 else { 820 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST "alias_for", prop->alias_for); 821 is_alias = 1; 822 } 823 if (rc < 0) { 824 SWISH_CROAK("Error writing property alias_for attribute for %s", name); 825 } 826 rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "id", "%d", prop->id); 827 if (rc < 0) { 828 SWISH_CROAK("Error writing property id attribute for %s", name); 829 } 830 831 /* all other attrs are irrelevant if this is an alias */ 832 if (!is_alias) { 833 rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "ignore_case", "%d", prop->ignore_case); 834 if (rc < 0) { 835 SWISH_CROAK("Error writing property ignore_case attribute for %s", name); 836 } 837 rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "verbatim", "%d", prop->verbatim); 838 if (rc < 0) { 839 SWISH_CROAK("Error writing property verbatim attribute for %s", name); 840 } 841 rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "type", "%d", prop->type); 842 if (rc < 0) { 843 SWISH_CROAK("Error writing property type attribute for %s", name); 844 } 845 rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "max", "%d", prop->max); 846 if (rc < 0) { 847 SWISH_CROAK("Error writing property max attribute for %s", name); 848 } 849 rc = xmlTextWriterWriteFormatAttribute(writer, BAD_CAST "sort", "%d", prop->sort); 850 if (rc < 0) { 851 SWISH_CROAK("Error writing property sort attribute for %s", name); 852 } 853 } 854 write_close_tag(writer); 855 } 856 857 static void 858 write_properties(xmlTextWriterPtr writer, xmlHashTablePtr properties) 859 { 860 xmlHashScan(properties, (xmlHashScanner)write_property, writer); 861 } 862 863 static void 864 write_parser(xmlChar* val, xmlTextWriterPtr writer, xmlChar* key) 865 { 866 write_element_with_content(writer, val, key); 867 } 868 869 static void 870 write_parsers(xmlTextWriterPtr writer, xmlHashTablePtr parsers) 871 { 872 xmlHashScan(parsers, (xmlHashScanner)write_parser, writer); 873 } 874 875 static void 876 write_mime(xmlChar* type, things* things, xmlChar* ext) 877 { 878 if ( ! swish_hash_exists((xmlHashTablePtr)things->thing1, ext) 879 || ! xmlStrEqual(swish_hash_fetch((xmlHashTablePtr)things->thing1, ext), type) 880 ) { 881 882 if (SWISH_DEBUG & SWISH_DEBUG_CONFIG) { 883 SWISH_DEBUG_MSG("writing unique MIME %s => %s", ext, type); 884 } 885 write_element_with_content( (xmlTextWriterPtr)things->thing3, ext, type ); 886 } 887 } 888 889 static void 890 write_mimes(xmlTextWriterPtr writer, xmlHashTablePtr mimes) 891 { 892 // only write what differs from the default 893 things* things; 894 things = swish_xmalloc(sizeof(things)); 895 896 things->thing1 = swish_mime_hash(); 897 things->thing2 = mimes; 898 things->thing3 = writer; 899 xmlHashScan(mimes, (xmlHashScanner)write_mime, things); 900 } 901 902 static void 903 write_index(xmlTextWriterPtr writer, xmlHashTablePtr index) 904 { 905 xmlHashScan(index, (xmlHashScanner)write_hash_entry, writer); 906 } 907 908 static void 909 write_tag_aliases(xmlTextWriterPtr writer, xmlHashTablePtr tag_aliases) 910 { 911 xmlHashScan(tag_aliases, (xmlHashScanner)write_hash_entry, writer); 912 } 913 914 static void 915 write_misc(xmlTextWriterPtr writer, xmlHashTablePtr hash) 916 { 917 xmlHashScan(hash, (xmlHashScanner)write_hash_entry, writer); 918 } 919 920 921 void 922 swish_write_header(char* uri, swish_Config* config) 923 { 924 #if !defined(LIBXML_WRITER_ENABLED) || !defined(LIBXML_OUTPUT_ENABLED) 925 SWISH_CROAK("libxml2 writer not compiled in this version of libxml2"); 926 #else 927 int rc; 928 xmlTextWriterPtr writer; 929 930 /* Create a new XmlWriter for uri, with no compression. */ 931 writer = xmlNewTextWriterFilename((const char*)uri, 0); 932 if (writer == NULL) { 933 SWISH_CROAK("Error creating the xml writer\n"); 934 } 935 936 /* set some basic formatting rules. these make it easier to debug headers */ 937 rc = xmlTextWriterSetIndent(writer, 1); 938 if (rc < 0) { 939 SWISH_CROAK("failed to set indent on XML writer"); 940 } 941 942 /* Start the document with the xml default for the version, 943 * encoding UTF-8 (default) and the default for the standalone 944 * declaration. */ 945 rc = xmlTextWriterStartDocument(writer, NULL, NULL, NULL); 946 if (rc < 0) { 947 SWISH_CROAK("Error at xmlTextWriterStartDocument\n"); 948 } 949 950 /* root element 951 NOTE the BAD_CAST macro is xml2 shortcut for (xmlChar*) 952 */ 953 write_open_tag(writer, BAD_CAST SWISH_HEADER_ROOT); 954 955 /* Write a comment indicating a computer wrote this file */ 956 rc = xmlTextWriterWriteComment(writer, 957 BAD_CAST "written by libswish3 - DO NOT EDIT"); 958 if (rc < 0) { 959 SWISH_CROAK("Error at xmlTextWriterWriteComment\n"); 960 } 961 962 write_element_with_content(writer, BAD_CAST "swish_verson", BAD_CAST SWISH_VERSION); 963 write_element_with_content(writer, BAD_CAST "swish_lib_version", BAD_CAST SWISH_LIB_VERSION); 964 965 /* write MetaNames */ 966 write_open_tag(writer, BAD_CAST SWISH_META); 967 write_metanames(writer, config->metanames); 968 write_close_tag(writer); 969 970 /* write PropertyNames */ 971 write_open_tag(writer, BAD_CAST SWISH_PROP); 972 write_properties(writer, config->properties); 973 write_close_tag(writer); 974 975 /* write Parsers */ 976 write_open_tag(writer, BAD_CAST SWISH_PARSERS); 977 write_parsers(writer, config->parsers); 978 write_close_tag(writer); 979 980 /* write MIMEs */ 981 write_open_tag(writer, BAD_CAST SWISH_MIME); 982 write_mimes(writer, config->mimes); 983 write_close_tag(writer); 984 985 /* write index */ 986 write_open_tag(writer, BAD_CAST SWISH_INDEX); 987 write_index(writer, config->index); 988 write_close_tag(writer); 989 990 write_open_tag(writer, BAD_CAST SWISH_ALIAS); 991 write_tag_aliases(writer, config->tag_aliases); 992 write_close_tag(writer); 993 994 /* misc tags have no parent */ 995 write_misc(writer, config->misc); 996 997 998 /* this function will close any open tags */ 999 rc = xmlTextWriterEndDocument(writer); 1000 if (rc < 0) { 1001 SWISH_CROAK("Error at xmlTextWriterEndDocument\n"); 1002 } 1003 1004 xmlFreeTextWriter(writer); 1005 #endif 1006 } 1007 libswish3/trunk/src/libswish3/libswish3.h
r2096 r2097 50 50 51 51 /* default config hash key names */ 52 #define SWISH_HEADER_ROOT "swish" 52 53 #define SWISH_INCLUDE_FILE "IncludeConfigFile" 53 54 #define SWISH_PROP "PropertyNames" … … 637 638 boolean swish_merge_config_with_header(char *filename, swish_Config *c); 638 639 swish_Config * swish_read_header(char *filename); 640 void swish_write_header(char* filename, swish_Config* config); 639 641 /* 640 642 =cut libswish3/trunk/src/libswish3/mime_types.c
r2041 r2097 24 24 25 25 #include <err.h> 26 #include <libxml/xmlstring.h>27 #include <libxml/hash.h>28 26 #include <stdlib.h> 29 27 #include <string.h> … … 33 31 extern int SWISH_DEBUG; 34 32 35 static char * defMimes[] = { 33 // should be total number of strings (NOT pairs!) below 34 #define SWISH_MIME_TABLE_COUNT 304 35 36 static char* 37 SWISH_MIME_TABLE[] = { 36 38 "ai", "application/postscript", 37 39 "aif", "audio/x-aiff", … … 192 194 }; 193 195 194 static int nMimes = 304;195 196 196 /* create hash of file ext => mime type */ 197 /* PUBLIC */ 198 199 xmlHashTablePtr swish_mime_hash() 197 xmlHashTablePtr 198 swish_mime_hash() 200 199 { 201 200 int i; 202 xmlHashTablePtr mimes = xmlHashCreate( 200 );203 204 205 for(i=0; i <= nMimes; i+=2)201 xmlHashTablePtr mimes; 202 mimes = xmlHashCreate( SWISH_MIME_TABLE_COUNT / 2 ); 203 204 for(i=0; i <= SWISH_MIME_TABLE_COUNT; i+=2) 206 205 { 207 206 swish_hash_add( mimes, 208 (xmlChar*)defMimes[i],209 swish_xstrdup((xmlChar*)defMimes[i+1])210 );207 (xmlChar*)SWISH_MIME_TABLE[i], 208 swish_xstrdup((xmlChar*)SWISH_MIME_TABLE[i+1]) 209 ); 211 210 } 212 211 … … 215 214 216 215 217 /* PUBLIC */218 216 /* retrieve mime type from hash */ 219 xmlChar * swish_get_mime_type( swish_Config * config, xmlChar * fileext ) 217 xmlChar* 218 swish_get_mime_type( swish_Config* config, xmlChar* fileext ) 220 219 { 221 220 xmlChar * mime; … … 224 223 { 225 224 SWISH_WARN("No MIME type known for '%s' -- using '%s'", fileext, SWISH_DEFAULT_MIME ); 226 mime = swish_xstrdup( (xmlChar *)SWISH_DEFAULT_MIME );225 mime = swish_xstrdup( (xmlChar*)SWISH_DEFAULT_MIME ); 227 226 } 228 227 return swish_xstrdup( mime ); 229 228 } 230 229 231 /* PUBLIC */232 230 /* returns parser type (TXT, HTML, XML) based on mime type */ 233 xmlChar * swish_get_parser( swish_Config * config, xmlChar *mime ) 231 xmlChar* 232 swish_get_parser( swish_Config * config, xmlChar *mime ) 234 233 { 235 234 xmlChar *parser; libswish3/trunk/src/libswish3/property.c
r2041 r2097 25 25 swish_init_property( xmlChar *name ) 26 26 { 27 swish_Property *p rop;28 p rop= swish_xmalloc(sizeof(swish_Property));29 p rop->ref_cnt = 0;30 p rop->id = 0;31 p rop->name = name;32 p rop->ignore_case = 1;33 p rop->type = SWISH_PROP_STRING;34 p rop->verbatim = 0;35 p rop->alias_for = NULL;36 p rop->max = 0;37 p rop->sort = 0;38 return p rop;27 swish_Property *p; 28 p = swish_xmalloc(sizeof(swish_Property)); 29 p->ref_cnt = 0; 30 p->id = 0; 31 p->name = name; 32 p->ignore_case = 1; 33 p->type = SWISH_PROP_STRING; 34 p->verbatim = 0; 35 p->alias_for = NULL; 36 p->max = 0; 37 p->sort = 0; 38 return p; 39 39 } 40 40 … … 43 43 { 44 44 SWISH_DEBUG_MSG("\n\ 45 p rop->ref_cnt = %d\n\46 p rop->id = %d\n\47 p rop->name = %s\n\48 p rop->ignore_case = %d\n\49 p rop->type = %d\n\50 p rop->verbatim = %d\n\51 p rop->alias_for = %s\n\52 p rop->max = %d\n\53 p rop->sort = %d\n\45 p->ref_cnt = %d\n\ 46 p->id = %d\n\ 47 p->name = %s\n\ 48 p->ignore_case = %d\n\ 49 p->type = %d\n\ 50 p->verbatim = %d\n\ 51 p->alias_for = %s\n\ 52 p->max = %d\n\ 53 p->sort = %d\n\ 54 54 ", p->ref_cnt, p->id, p->name, p->ignore_case, 55 55 p->type, p->verbatim, p->alias_for, p->max, p->sort); libswish3/trunk/src/libswish3/string.c
r2096 r2097 25 2
