1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
|
/*
** $Id: headers.c 1736 2005-05-12 15:41:22Z karman $
**
This file is part of Swish-e.
Swish-e is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Swish-e is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Swish-e; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
See the COPYING file that accompanies the Swish-e distribution for details
of the GNU GPL and the special exception available for linking against
the Swish-e library.
** Mon May 9 15:51:39 CDT 2005
** added GPL
*/
#include "swish.h"
#include "swstring.h"
#include "mem.h"
#include "error.h"
#include "list.h"
#include "search.h"
#include "headers.h"
#include "stemmer.h"
#include <stddef.h> /* for offsetof macro */
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
/***************************************************************************
* Routines for accessing index header data by name and header name
*
****************************************************************************/
typedef struct
{
const char *description;
SWISH_HEADER_TYPE data_type;
int min_verbose_level;
size_t offset;
} HEADER_MAP;
static HEADER_MAP header_map[] = {
{ "Name", SWISH_STRING, 2, offsetof( INDEXDATAHEADER, indexn ) },
{ "Saved as", SWISH_STRING, 2, offsetof( INDEXDATAHEADER, savedasheader ) },
/* $$$ Total Words is unique words, and is Not corrected for removed words */
{ "Total Words", SWISH_NUMBER, 2, offsetof( INDEXDATAHEADER, totalwords ) },
{ "Total Files", SWISH_NUMBER, 2, offsetof( INDEXDATAHEADER, totalfiles ) },
{ "Removed Files", SWISH_NUMBER, 2, offsetof( INDEXDATAHEADER, removedfiles ) },
{ "Total Word Pos", SWISH_NUMBER, 2, offsetof( INDEXDATAHEADER, total_word_positions ) },
{ "Removed Word Pos", SWISH_NUMBER, 2, offsetof( INDEXDATAHEADER, removed_word_positions ) },
{ "Indexed on", SWISH_STRING, 2, offsetof( INDEXDATAHEADER, indexedon ) },
{ "Description", SWISH_STRING, 2, offsetof( INDEXDATAHEADER, indexd ) },
{ "Pointer", SWISH_STRING, 2, offsetof( INDEXDATAHEADER, indexp ) },
{ "Maintained by", SWISH_STRING, 2, offsetof( INDEXDATAHEADER, indexa ) },
{ "MinWordLimit", SWISH_NUMBER, 2, offsetof( INDEXDATAHEADER, minwordlimit ) },
{ "MaxWordLimit", SWISH_NUMBER, 2, offsetof( INDEXDATAHEADER, maxwordlimit ) },
{ "WordCharacters", SWISH_STRING, 2, offsetof( INDEXDATAHEADER, wordchars ) },
{ "BeginCharacters", SWISH_STRING, 2, offsetof( INDEXDATAHEADER, beginchars ) },
{ "EndCharacters", SWISH_STRING, 2, offsetof( INDEXDATAHEADER, endchars ) },
{ "IgnoreFirstChar", SWISH_STRING, 2, offsetof( INDEXDATAHEADER, ignorefirstchar ) },
{ "IgnoreLastChar", SWISH_STRING, 2, offsetof( INDEXDATAHEADER, ignorelastchar ) },
{ "StopWords", SWISH_WORD_HASH, 3, offsetof( INDEXDATAHEADER, hashstoplist ) },
{ "BuzzWords", SWISH_WORD_HASH, 3, offsetof( INDEXDATAHEADER, hashbuzzwordlist ) },
{ "Stemming Applied", SWISH_OTHER_DATA, 2, offsetof( INDEXDATAHEADER, fuzzy_data ) },
{ "Soundex Applied", SWISH_OTHER_DATA, 2, offsetof( INDEXDATAHEADER, fuzzy_data ) },
{ "Fuzzy Mode", SWISH_OTHER_DATA, 2, offsetof( INDEXDATAHEADER, fuzzy_data ) },
{ "IgnoreTotalWordCountWhenRanking", SWISH_BOOL, 2, offsetof( INDEXDATAHEADER, ignoreTotalWordCountWhenRanking ) }
};
static SWISH_HEADER_VALUE fetch_header( IndexFILE *indexf, const char *name, SWISH_HEADER_TYPE *data_type );
static const char **create_string_list( SWISH *sw, struct swline *swline );
static const char **string_list_from_hash( SWISH *sw, WORD_HASH_TABLE table );
static DB_RESULTS *db_results_by_name( RESULTS_OBJECT *results, const char *index_name );
static SWISH_HEADER_VALUE fetch_single_header( IndexFILE *indexf, HEADER_MAP *header_map, SWISH_HEADER_TYPE *data_type );
static void print_header_value( SWISH *sw, const char *name, SWISH_HEADER_VALUE head_value, SWISH_HEADER_TYPE head_type );
/*********** PUBLIC *************************/
/***********************************************************************
* print_index_headers - prints all the headers for a given indexf
*
* Called by swish-e binary (swish.c)
*
************************************************************************/
void print_index_headers( IndexFILE *indexf )
{
int i;
int array_size = sizeof(header_map) / sizeof(header_map[0]);
SWISH_HEADER_VALUE value;
SWISH_HEADER_TYPE data_type;
int verbose_level = indexf->sw->headerOutVerbose;
for (i = 0; i < array_size; i++)
{
if ( header_map[i].min_verbose_level > verbose_level )
continue;
value = fetch_single_header( indexf, &header_map[i], &data_type );
print_header_value( indexf->sw, header_map[i].description, value, data_type );
}
}
/*********************************************************************
* SwishHeaderNames -- return a list of possible header names
*
**********************************************************************/
const char **SwishHeaderNames(SWISH *sw)
{
int array_size = sizeof(header_map) / sizeof(header_map[0]);
int i;
if ( !sw )
progerr("SwishHeaderNames requires a valid swish handle");
if ( sw->header_names )
return sw->header_names;
sw->header_names = (const char **)emalloc( sizeof(char *) * ( 1 + array_size ) );
for (i = 0; i < array_size; i++)
sw->header_names[i] = (const char *)header_map[i].description;
sw->header_names[i] = NULL;
return sw->header_names;
}
/*********************************************************************
* SwishIndexNames -- return a list of associated index file names
*
**********************************************************************/
const char **SwishIndexNames(SWISH *sw)
{
IndexFILE *indexf;
int index_count;
if ( !sw )
progerr("SwishIndexNames requires a valid swish handle");
if ( sw->index_names )
return sw->index_names;
for ( index_count = 0, indexf = sw->indexlist; indexf; indexf = indexf->next )
index_count++;
if ( !index_count ) /* should not happen */
progerr("Swish Handle does not have any associated index files!?!?");
sw->index_names = (const char **)emalloc( sizeof(char *) * (1+index_count) );
for ( index_count = 0, indexf = sw->indexlist; indexf; indexf = indexf->next )
sw->index_names[index_count++] = (const char *)indexf->line;
sw->index_names[index_count] = NULL;
return sw->index_names;
}
/*********************************************************************
* SwishResultIndexValue -- lookup a header via a result structure
*
**********************************************************************/
SWISH_HEADER_VALUE SwishResultIndexValue( RESULT *result, const char *name, SWISH_HEADER_TYPE *data_type )
{
return fetch_header( result->db_results->indexf, name, data_type );
}
/********************************************************************************
* SwishHeaderValue -- lookup a header via a index file name, and a header name
*
*********************************************************************************/
SWISH_HEADER_VALUE SwishHeaderValue( SWISH *sw, const char *index_name, const char *cur_header, SWISH_HEADER_TYPE *data_type )
{
IndexFILE *indexf;
SWISH_HEADER_VALUE value;
value.string = NULL;
if ( !sw )
progerr("SwishHeaderValue requires a valid swish handle");
indexf = indexf_by_name( sw, index_name );
if ( indexf )
return fetch_header( indexf, cur_header, data_type );
*data_type = SWISH_HEADER_ERROR;
set_progerr( HEADER_READ_ERROR, sw, "Index file '%s' is not an active index file", index_name );
return value;
}
/************* Local support function **********************************/
/****************************************************************************
* fetch_single_header -- returns a SWISH_HEADER_VALUE
*
* Pass:
* indexf, HEADER_MAP element, and a *data_type
*
* Return:
* SWISH_HEADER_VALUE
*
*
*****************************************************************************/
static SWISH_HEADER_VALUE fetch_single_header( IndexFILE *indexf, HEADER_MAP *header_map, SWISH_HEADER_TYPE *data_type )
{
SWISH_HEADER_VALUE value;
INDEXDATAHEADER *header = &indexf->header;
char *data_pointer = (char *)header + header_map->offset; /* should that be void* as a generic address? */
value.string = NULL;
*data_type = header_map->data_type;
switch ( header_map->data_type )
{
case SWISH_STRING:
value.string = *(const char **) data_pointer;
return value;
case SWISH_NUMBER:
case SWISH_BOOL:
value.number = *(unsigned long *) data_pointer;
/* $$$ Ugly hack alert! */
/* correct for removed files */
if ( (void *)data_pointer == &header->totalfiles )
value.number -= header->removedfiles;
if ( (void *)data_pointer == &header->total_word_positions )
value.number -= header->removed_word_positions;
return value;
case SWISH_LIST:
{
struct swline *first_item = *(struct swline **) data_pointer;
value.string_list = create_string_list( indexf->sw, first_item );
return value;
}
case SWISH_WORD_HASH:
{
WORD_HASH_TABLE table = *(WORD_HASH_TABLE *) data_pointer;
*data_type = SWISH_LIST;
value.string_list = string_list_from_hash( indexf->sw, table );
return value;
}
case SWISH_OTHER_DATA:
if ( strcasecmp( "Fuzzy Mode", header_map->description ) == 0 )
{
value.string = fuzzy_string( header->fuzzy_data );
*data_type = SWISH_STRING;
return value;
}
else if ( strcasecmp( "Stemming Applied", header_map->description ) == 0 )
{
value.number = stemmer_applied( header->fuzzy_data );
*data_type = SWISH_BOOL;
return value;
}
else if ( strcasecmp( "Soundex Applied", header_map->description ) == 0 )
{
value.number = FUZZY_SOUNDEX == fuzzy_mode_value( header->fuzzy_data ) ? 1 : 0;
*data_type = SWISH_BOOL;
return value;
}
else
progerr("Invalid OTHER header '%s'", header_map->description );
default:
progerr("Invalid HEADER type '%d'", header_map->data_type );
}
return value; /* make MS compiler happy */
}
/************************************************************
* fetch_header - fetches a header by name
*
*************************************************************/
static SWISH_HEADER_VALUE fetch_header( IndexFILE *indexf, const char *name, SWISH_HEADER_TYPE *data_type )
{
int i;
int array_size = sizeof(header_map) / sizeof(header_map[0]);
SWISH_HEADER_VALUE value;
value.string = NULL;
for (i = 0; i < array_size; i++)
{
if ( strcasecmp(header_map[i].description, name) != 0 )
continue; /* nope */
return fetch_single_header( indexf, &header_map[i], data_type );
}
*data_type = SWISH_HEADER_ERROR;
set_progerr( HEADER_READ_ERROR, indexf->sw, "Index file '%s' does not have header '%s'", indexf->line, name );
return value;
}
SWISH_HEADER_VALUE SwishParsedWords( RESULTS_OBJECT *results, const char *index_name )
{
SWISH_HEADER_VALUE value;
DB_RESULTS *db_results;
if ( !results )
progerr("Must pass a results object to SwishParsedWords");
value.string_list = NULL;
db_results = db_results_by_name( results, index_name );
if ( db_results )
value.string_list = create_string_list( results->sw, db_results->parsed_words );
return value;
}
SWISH_HEADER_VALUE SwishRemovedStopwords( RESULTS_OBJECT *results, const char *index_name )
{
SWISH_HEADER_VALUE value;
DB_RESULTS *db_results;
if ( !results )
progerr("Must pass a results object to SwishRemovedStopwords");
value.string_list = NULL;
db_results = db_results_by_name( results, index_name );
if ( db_results )
value.string_list = create_string_list( results->sw, db_results->removed_stopwords );
return value;
}
/**********************************************************************
* create_string_list - creates a list of strings from a swline
* $$$ should this return NULL if there's none to return, or an empty list?
*
***********************************************************************/
static const char **create_string_list( SWISH *sw, struct swline *swline )
{
int i;
struct swline *cur_item;
/* first count up how many items there are */
i = 1; /* always need one */
for ( cur_item = swline; cur_item; cur_item = cur_item->next )
i++;
if ( i > sw->temp_string_buffer_len )
{
sw->temp_string_buffer_len = i;
sw->temp_string_buffer = (const char **)erealloc( sw->temp_string_buffer, sizeof(char *) * i );
}
i = 0;
for ( cur_item = swline; cur_item; cur_item = cur_item->next )
sw->temp_string_buffer[i++] = (const char*)cur_item->line;
sw->temp_string_buffer[i] = NULL; /* end of list */
return sw->temp_string_buffer;
}
/**********************************************************************
* string_list_from_hash - creates a list of strings from a swline
* $$$ should this return NULL if there's none to return, or an empty list?
*
***********************************************************************/
static const char **string_list_from_hash( SWISH *sw, WORD_HASH_TABLE table )
{
int i;
struct swline *sp, *next;
int count;
i = table.count + 1; /* always return one */
if ( i > sw->temp_string_buffer_len )
{
sw->temp_string_buffer_len = i;
sw->temp_string_buffer = (const char **)erealloc( sw->temp_string_buffer, sizeof(char *) * i );
}
/* first count them up */
count = 0;
if ( table.count )
{
for (i = 0; i < HASHSIZE; i++)
{
if ( !table.hash_array[i])
continue;
sp = table.hash_array[i];
while (sp)
{
next = sp->next;
sw->temp_string_buffer[count++] = sp->line;
sp = next;
}
}
}
sw->temp_string_buffer[count] = NULL; /* end of list */
return sw->temp_string_buffer;
}
/* no longer static since we want to use this in metanames.c and stemmer.c
ther'es probably a better way to organize this... karman Mon Nov 8 21:37:44 CST 2004
*/
IndexFILE *indexf_by_name( SWISH *sw, const char *index_name )
{
IndexFILE *indexf = sw->indexlist;
while ( indexf )
{
if (strcmp( index_name, indexf->line ) == 0 )
return indexf;
indexf = indexf->next;
}
return NULL;
}
static DB_RESULTS *db_results_by_name( RESULTS_OBJECT *results, const char *index_name )
{
DB_RESULTS *db_results = results->db_results;
while ( db_results )
{
if (strcmp( index_name, db_results->indexf->line ) == 0)
return db_results;
db_results = db_results->next;
}
return NULL;
}
/**************************************************************************
* print_index_headers - prints list of headers for the given index
*
* Note:
* This is not used in the library code. Perhaps move elsewhere
*
***************************************************************************/
static void print_header_value( SWISH *sw, const char *name, SWISH_HEADER_VALUE head_value, SWISH_HEADER_TYPE head_type )
{
const char **string_list;
printf("# %s:", name );
switch ( head_type )
{
case SWISH_STRING:
printf(" %s\n", head_value.string ? head_value.string : "" );
return;
case SWISH_NUMBER:
printf(" %lu\n", head_value.number );
return;
case SWISH_BOOL:
printf(" %s\n", head_value.boolean ? "1" : "0" );
// printf(" %s\n", head_value.boolean ? "Yes" : "No" );
return;
case SWISH_LIST:
string_list = head_value.string_list;
while ( *string_list )
{
printf(" %s", *string_list );
string_list++;
}
printf("\n");
return;
case SWISH_HEADER_ERROR:
SwishAbortLastError( sw );
default:
printf(" Unknown header type '%d'\n", (int)head_type );
return;
}
}
|