From grdetil@scrc.umanitoba.ca Tue Dec 15 09:47:09 1998 Date: Tue, 15 Dec 1998 10:07:30 -0600 (CST) From: Gilles Detillieux To: htdig@sdsu.edu Cc: htdig@as.westblaak.spirit.nl Subject: Re: htdig: Sorting results on date (2) Yesterday, regarding my sort patch, I wrote: > If you apply it to the 3.1.0b2 source, I can't promise it'll work, as > I haven't tested it, but the patch program should be able to apply it. Turns out the patch applies, but it won't compile. It relied on a small change to the score calculation in the 3.1.0b3 pre-release. Here's the sort patch for 3.1.0b2: --------------------------------------------- --- htsearch/Display.h.sort Mon Nov 2 18:21:51 1998 +++ htsearch/Display.h Mon Dec 14 18:35:40 1998 @@ -155,6 +155,8 @@ List *buildMatchList(); void sort(List *); static int compare(const void *, const void *); + static int compareTime(const void *, const void *); + static int compareTitle(const void *, const void *); int includeURL(char *); String *readFile(char *); void expandVariables(char *); --- htsearch/Display.cc.sort Mon Nov 2 18:21:51 1998 +++ htsearch/Display.cc Tue Dec 15 09:55:12 1998 @@ -169,7 +169,7 @@ displayNomatch(); return; } - maxScore = match->getScore(); + // maxScore = match->getScore(); // now done in buildMatchList() // // Display the window of matches requested. @@ -334,6 +334,7 @@ vars.Add("VERSION", new String(config["version"])); vars.Add("RESTRICT", new String(config["restrict"])); vars.Add("EXCLUDE", new String(config["exclude"])); + vars.Add("SORT", new String(input->get("sort"))); if (mystrcasecmp(config["match_method"], "and") == 0) vars.Add("MATCH_MESSAGE", new String("all")); else if (mystrcasecmp(config["match_method"], "or") == 0) @@ -471,6 +472,8 @@ s << "method=" << input->get("method") << '&'; if (input->exists("format")) s << "format=" << input->get("format") << '&'; + if (input->exists("sort")) + s << "sort=" << input->get("sort") << '&'; if (input->exists("matchesperpage")) s << "matchesperpage=" << input->get("matchesperpage") << '&'; if (input->exists("words")) @@ -784,7 +787,7 @@ // // Get the actual document record into the current ResultMatch // -// thisMatch->setRef(docDB[thisMatch->getURL()]); + thisMatch->setRef(docDB[thisMatch->getURL()]); // // Assign the incomplete score to this match. This score was @@ -919,6 +922,7 @@ { int numberOfMatches = matches->Count(); int i; + static char *sorttypes[] = { "score", "date", "title" }; ResultMatch **array = new ResultMatch*[numberOfMatches]; for (i = 0; i < numberOfMatches; i++) @@ -927,12 +931,26 @@ } matches->Release(); + if (input->exists("sort")) { + char *st = input->get("sort"); + for (i = sizeof(sorttypes)/sizeof(sorttypes[0]); --i > 0; ) + { + if (mystrcasecmp(sorttypes[i], st) == 0) + break; + } + } + else + i = 0; qsort((char *) array, numberOfMatches, sizeof(ResultMatch *), + (i == 2) ? Display::compareTitle : + (i == 1) ? Display::compareTime : Display::compare); for (i = 0; i < numberOfMatches; i++) { matches->Add(array[i]); + if (i == 0 || maxScore < array[i]->getScore()) + maxScore = array[i]->getScore(); } delete [] array; } @@ -945,6 +963,32 @@ ResultMatch *m2 = *((ResultMatch **) a2); return m2->getScore() - m1->getScore(); +} + +//***************************************************************************** +int +Display::compareTime(const void *a1, const void *a2) +{ + ResultMatch *m1 = *((ResultMatch **) a1); + ResultMatch *m2 = *((ResultMatch **) a2); + time_t t1 = (m1->getRef()) ? m1->getRef()->DocTime() : 0; + time_t t2 = (m2->getRef()) ? m2->getRef()->DocTime() : 0; + + return (int) (t2 - t1); +} + +//***************************************************************************** +int +Display::compareTitle(const void *a1, const void *a2) +{ + ResultMatch *m1 = *((ResultMatch **) a1); + ResultMatch *m2 = *((ResultMatch **) a2); + char *t1 = (m1->getRef()) ? m1->getRef()->DocTitle() : ""; + char *t2 = (m2->getRef()) ? m2->getRef()->DocTitle() : ""; + + if (!t1) t1 = ""; + if (!t2) t2 = ""; + return mystrcasecmp(t1, t2); } --------------------------------------------- If you applied my patch from yesterday to the 3.1.0b3 pre-release, you should change the compareTime and compareTitle functions to work as the ones above, as the reference value returned by getRef is still not guaranteed to be non-null. Also note that the pre-release is still in a state of flux, with other "thinkos" still being corrected. -- Gilles R. Detillieux E-mail: Spinal Cord Research Centre WWW: http://www.scrc.umanitoba.ca/~grdetil Dept. Physiology, U. of Manitoba Phone: (204)789-3766 Winnipeg, MB R3E 3J7 (Canada) Fax: (204)789-3930 ---------------------------------------------------------------------- To unsubscribe from the htdig mailing list, send a message to htdig-request@sdsu.edu containing the single word "unsubscribe" in the body of the message.