X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=ir-tcl.c;h=66eebf379ded6abb842aab770e0818d6896c63f1;hb=4c44d731206d69f822cbe47b0c91e45bb4ff935d;hp=fc95e30a6dbbf042b94ca54ea93741369ff74bc6;hpb=510fc2e05a3233005879413ae401865d91efe73e;p=ir-tcl-moved-to-github.git diff --git a/ir-tcl.c b/ir-tcl.c index fc95e30..66eebf3 100644 --- a/ir-tcl.c +++ b/ir-tcl.c @@ -1,11 +1,32 @@ /* * IR toolkit for tcl/tk - * (c) Index Data 1995-1998 + * (c) Index Data 1995-1999 * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * * $Log: ir-tcl.c,v $ - * Revision 1.107 1998-06-10 13:00:46 adam + * Revision 1.114 1999-05-17 20:37:41 adam + * Fixed problem with ASN code. + * + * Revision 1.113 1999/04/20 10:01:46 adam + * Modified calls to ODR encoders/decoders (name argument). + * + * Revision 1.112 1999/03/22 06:51:34 adam + * Implemented sort. + * + * Revision 1.111 1999/02/11 11:30:09 adam + * Updated for WIN32. + * + * Revision 1.110 1998/10/20 15:15:31 adam + * Changed scan response handler. + * + * Revision 1.109 1998/10/13 21:23:26 adam + * Fixed searchStatus method. + * + * Revision 1.108 1998/10/12 11:48:08 adam + * Removed printf call. + * + * Revision 1.107 1998/06/10 13:00:46 adam * Added ir-version command. * * Revision 1.106 1998/05/20 12:25:35 adam @@ -382,7 +403,7 @@ #include #include -#ifdef WINDOWS +#ifdef WIN32 #else #include @@ -479,6 +500,17 @@ static void delete_IR_record (IrTcl_RecordList *rl) xfree (rl->elements); } +static void purge_IR_records (IrTcl_SetObj *setobj) +{ + IrTcl_RecordList *rl; + while ((rl = setobj->record_list)) + { + setobj->record_list = rl->next; + delete_IR_record (rl); + xfree (rl); + } +} + static IrTcl_RecordList *new_IR_record (IrTcl_SetObj *setobj, int no, int which, const char *elements) @@ -2201,6 +2233,36 @@ static int do_presentResponse (void *o, Tcl_Interp *interp, } /* + * do_sortResponse: add sort response handler + */ +static int do_sortResponse (void *o, Tcl_Interp *interp, + int argc, char **argv) +{ + IrTcl_SetObj *obj = o; + + if (argc == 0) + { + obj->sortResponse = NULL; + return TCL_OK; + } + else if (argc == -1) + return ir_tcl_strdel (interp, &obj->sortResponse); + if (argc == 3) + { + xfree (obj->sortResponse); + if (argv[2][0]) + { + if (ir_tcl_strdup (interp, &obj->sortResponse, argv[2]) + == TCL_ERROR) + return TCL_ERROR; + } + else + obj->sortResponse = NULL; + } + return TCL_OK; +} + +/* * do_resultCount: Get number of hits */ static int do_resultCount (void *o, Tcl_Interp *interp, @@ -2243,6 +2305,35 @@ static int do_presentStatus (void *o, Tcl_Interp *interp, } /* + * do_sortStatus: Get sort status (after sort response) + */ +static int do_sortStatus (void *o, Tcl_Interp *interp, + int argc, char **argv) +{ + IrTcl_SetObj *obj = o; + char *res; + + if (argc <= 0) + { + obj->sortStatus = Z_SortStatus_failure; + return TCL_OK; + } + switch (obj->sortStatus) + { + case Z_SortStatus_success: + res = "success"; break; + case Z_SortStatus_partial_1: + res = "partial"; break; + case Z_SortStatus_failure: + res = "failure"; break; + default: + res = "unknown"; break; + } + Tcl_AppendElement (interp, res); + return TCL_OK; +} + +/* * do_nextResultSetPosition: Get next result set position * (after search/present response) */ @@ -2624,7 +2715,7 @@ static int do_getExplain (void *o, Tcl_Interp *interp, int argc, char **argv) return TCL_OK; assert (rl->u.dbrec.buf); odr_setbuf (p->odr_in, rl->u.dbrec.buf, rl->u.dbrec.size, 0); - if (!(*etype->fun)(p->odr_in, (char **) &rr, 0)) + if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0)) return TCL_OK; if (etype->what != Z_External_explainRecord) @@ -2911,6 +3002,171 @@ static int do_saveFile (void *o, Tcl_Interp *interp, } +/* ------------------------------------------------------- */ +/* + * do_sort: Do sort request + */ +static int do_sort (void *o, Tcl_Interp *interp, int argc, char **argv) +{ + Z_SortRequest *req; + Z_APDU *apdu; + IrTcl_SetObj *obj = o; + IrTcl_Obj *p; + char sort_string[64], sort_flags[64]; + char *arg; + int off; + Z_SortKeySpecList *sksl; + int oid[OID_SIZE]; + oident bib1; + + if (argc <= 0) + return TCL_OK; + + p = obj->parent; + assert (argc > 1); + if (argc != 3) + { + Tcl_AppendResult (interp, wrongArgs, *argv, " ", argv[1], "query\"", + NULL); + return TCL_ERROR; + } + logf (LOG_DEBUG, "sort %s %s", *argv, argv[2]); + if (!p->cs_link) + { + Tcl_AppendResult (interp, "not connected", NULL); + return ir_tcl_error_exec (interp, argc, argv); + } + apdu = zget_APDU (p->odr_out, Z_APDU_sortRequest); + sksl = (Z_SortKeySpecList *) odr_malloc (p->odr_out, sizeof(*sksl)); + req = apdu->u.sortRequest; + + set_referenceId (p->odr_out, &req->referenceId, + obj->set_inher.referenceId); + +#ifdef ASN_COMPILED + req->num_inputResultSetNames = 1; + req->inputResultSetNames = (Z_InternationalString **) + odr_malloc (p->odr_out, sizeof(*req->inputResultSetNames)); + req->inputResultSetNames[0] = obj->setName; +#else + req->inputResultSetNames = + (Z_StringList *)odr_malloc (p->odr_out, + sizeof(*req->inputResultSetNames)); + req->inputResultSetNames->num_strings = 1; + req->inputResultSetNames->strings = + (char **)odr_malloc (p->odr_out, + sizeof(*req->inputResultSetNames->strings)); + req->inputResultSetNames->strings[0] = obj->setName; +#endif + + req->sortedResultSetName = (char *) obj->setName; + + + req->sortSequence = sksl; + sksl->num_specs = 0; + sksl->specs = (Z_SortKeySpec **) + odr_malloc (p->odr_out, sizeof(sksl->specs) * 20); + + bib1.proto = PROTO_Z3950; + bib1.oclass = CLASS_ATTSET; + bib1.value = VAL_BIB1; + arg = argv[2]; + while ((sscanf (arg, "%63s %63s%n", sort_string, sort_flags, &off)) == 2 + && off > 1) + { + int i; + char *sort_string_sep; + Z_SortKeySpec *sks = (Z_SortKeySpec *) + odr_malloc (p->odr_out, sizeof(*sks)); + Z_SortKey *sk = (Z_SortKey *) + odr_malloc (p->odr_out, sizeof(*sk)); + + arg += off; + sksl->specs[sksl->num_specs++] = sks; + sks->sortElement = (Z_SortElement *) + odr_malloc (p->odr_out, sizeof(*sks->sortElement)); + sks->sortElement->which = Z_SortElement_generic; + sks->sortElement->u.generic = sk; + + if ((sort_string_sep = strchr (sort_string, '='))) + { + Z_AttributeElement *el = (Z_AttributeElement *) + odr_malloc (p->odr_out, sizeof(*el)); + sk->which = Z_SortKey_sortAttributes; + sk->u.sortAttributes = + (Z_SortAttributes *) + odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes)); + sk->u.sortAttributes->id = oid_ent_to_oid(&bib1, oid); + sk->u.sortAttributes->list = + (Z_AttributeList *) + odr_malloc (p->odr_out, sizeof(*sk->u.sortAttributes->list)); + sk->u.sortAttributes->list->num_attributes = 1; + sk->u.sortAttributes->list->attributes = + (Z_AttributeElement **)odr_malloc (p->odr_out, + sizeof(*sk->u.sortAttributes->list->attributes)); + sk->u.sortAttributes->list->attributes[0] = el; + el->attributeSet = 0; + el->attributeType = (int *) + odr_malloc (p->odr_out, sizeof(*el->attributeType)); + *el->attributeType = atoi (sort_string); + el->which = Z_AttributeValue_numeric; + el->value.numeric = (int *) + odr_malloc (p->odr_out, sizeof(*el->value.numeric)); + *el->value.numeric = atoi (sort_string_sep + 1); + } + else + { + sk->which = Z_SortKey_sortField; + sk->u.sortField = (char *)odr_malloc (p->odr_out, strlen(sort_string)+1); + strcpy (sk->u.sortField, sort_string); + } + sks->sortRelation = (int *) + odr_malloc (p->odr_out, sizeof(*sks->sortRelation)); + *sks->sortRelation = Z_SortRelation_ascending; + sks->caseSensitivity = (int *) + odr_malloc (p->odr_out, sizeof(*sks->caseSensitivity)); + *sks->caseSensitivity = Z_SortCase_caseSensitive; + +#ifdef ASN_COMPILED + sks->which = Z_SortKeySpec_null; + sks->u.null = odr_nullval (); +#else + sks->missingValueAction = NULL; +#endif + + for (i = 0; sort_flags[i]; i++) + { + switch (sort_flags[i]) + { + case 'a': + case 'A': + case '>': + *sks->sortRelation = Z_SortRelation_descending; + break; + case 'd': + case 'D': + case '<': + *sks->sortRelation = Z_SortRelation_ascending; + break; + case 'i': + case 'I': + *sks->caseSensitivity = Z_SortCase_caseInsensitive; + break; + case 'S': + case 's': + *sks->caseSensitivity = Z_SortCase_caseSensitive; + break; + } + } + } + if (!sksl->num_specs) + { + printf ("Missing sort specifications\n"); + return -1; + } + return ir_tcl_send_APDU (interp, p, apdu, "sort", *argv); +} + static IrTcl_Method ir_set_method_tab[] = { { "search", do_search, NULL}, { "searchResponse", do_searchResponse, NULL}, @@ -2933,6 +3189,9 @@ static IrTcl_Method ir_set_method_tab[] = { { "responseStatus", do_responseStatus, NULL}, { "loadFile", do_loadFile, NULL}, { "saveFile", do_saveFile, NULL}, + { "sort", do_sort, NULL }, + { "sortResponse", do_sortResponse, NULL}, + { "sortStatus", do_sortStatus, NULL}, { NULL, NULL} }; @@ -3338,7 +3597,6 @@ static int do_scanLine (void *obj, Tcl_Interp *interp, int argc, char **argv) " position\"", NULL); return TCL_ERROR; } - printf ("argv[2]=%s\n", argv[2]); if (Tcl_GetInt (interp, argv[2], &i) == TCL_ERROR) return TCL_ERROR; if (!p->entries_flag || !p->entries || i >= p->num_entries || i < 0) @@ -3629,7 +3887,7 @@ static void ir_handleDBRecord (IrTcl_Obj *p, IrTcl_RecordList *rl, odr_setbuf (p->odr_in, (char*) oe->u.octet_aligned->buf, oe->u.octet_aligned->len, 0); - if (!(*etype->fun)(p->odr_in, (char **) &rr, 0)) + if (!(*etype->fun)(p->odr_in, (char **) &rr, 0, 0)) { rl->u.dbrec.type = VAL_NONE; return; @@ -3769,11 +4027,13 @@ static void ir_searchResponse (void *o, Z_SearchResponse *searchrs, logf (LOG_DEBUG, "Search response, no object!"); return; } - setobj->searchStatus = searchrs->searchStatus ? 1 : 0; + setobj->searchStatus = *searchrs->searchStatus; get_referenceId (&setobj->set_inher.referenceId, searchrs->referenceId); setobj->resultCount = *searchrs->resultCount; if (searchrs->presentStatus) setobj->presentStatus = *searchrs->presentStatus; + else + setobj->presentStatus = Z_RES_NONE; if (searchrs->nextResultSetPosition) setobj->nextResultSetPosition = *searchrs->nextResultSetPosition; @@ -3862,7 +4122,6 @@ static void ir_scanResponse (void *o, Z_ScanResponse *scanrs, Z_Entry **ze; scanobj->entries_flag = 1; -#ifdef ASN_COMPILED if (scanrs->entries) { scanobj->num_entries = scanrs->entries->num_entries; @@ -3870,15 +4129,6 @@ static void ir_scanResponse (void *o, Z_ScanResponse *scanrs, sizeof(*scanobj->entries)); ze = scanrs->entries->entries; } -#else - if (scanrs->entries->which == Z_ListEntries_entries) - { - scanobj->num_entries = scanrs->entries->u.entries->num_entries; - scanobj->entries = ir_tcl_malloc (scanobj->num_entries * - sizeof(*scanobj->entries)); - ze = scanrs->entries->u.entries->entries; - } -#endif for (i=0; inum_entries; i++, ze++) { scanobj->entries[i].which = (*ze)->which; @@ -3910,22 +4160,46 @@ static void ir_scanResponse (void *o, Z_ScanResponse *scanrs, break; } } -#ifdef ASN_COMPILED if (scanrs->entries->nonsurrogateDiagnostics) ir_handleDiags (&scanobj->nonSurrogateDiagnosticList, &scanobj->nonSurrogateDiagnosticNum, scanrs->entries->nonsurrogateDiagnostics, scanrs->entries->num_nonsurrogateDiagnostics); + } +} + + +static void ir_sortResponse (void *o, Z_SortResponse *sortrs, + IrTcl_SetObj *setobj) +{ + IrTcl_Obj *p = o; + + logf (LOG_DEBUG, "Received sortResponse"); + + if (!setobj) + return; + + purge_IR_records (setobj); + + get_referenceId (&p->set_inher.referenceId, sortrs->referenceId); + + setobj->sortStatus = *sortrs->sortStatus; + + ir_deleteDiags (&setobj->nonSurrogateDiagnosticList, + &setobj->nonSurrogateDiagnosticNum); +#ifdef ASN_COMPILED + if (sortrs->diagnostics) + ir_handleDiags (&setobj->nonSurrogateDiagnosticList, + &setobj->nonSurrogateDiagnosticNum, + sortrs->diagnostics, + sortrs->num_diagnostics); #else - if (scanrs->entries->which == Z_ListEntries_nonSurrogateDiagnostics) - ir_handleDiags (&scanobj->nonSurrogateDiagnosticList, - &scanobj->nonSurrogateDiagnosticNum, - scanrs->entries->u.nonSurrogateDiagnostics-> - diagRecs, - scanrs->entries->u.nonSurrogateDiagnostics-> - num_diagRecs); + if (sortrs->diagnostics) + ir_handleDiags (&setobj->nonSurrogateDiagnosticList, + &setobj->nonSurrogateDiagnosticNum, + sortrs->diagnostics->diagRecs, + sortrs->diagnostics->num_diagRecs); #endif - } } /* @@ -4005,7 +4279,7 @@ static void ir_select_read (ClientData clientData) p->apduOffset = -1; odr_setbuf (p->odr_in, p->buf_in, r, 0); logf (LOG_DEBUG, "cs_get ok, total size %d", r); - if (!z_APDU (p->odr_in, &apdu, 0)) + if (!z_APDU (p->odr_in, &apdu, 0, 0)) { logf (LOG_DEBUG, "cs_get failed: %s", odr_errmsg (odr_geterror (p->odr_in))); @@ -4060,6 +4334,13 @@ static void ir_select_read (ClientData clientData) apdu_call = ((IrTcl_ScanObj *) cmd_info.clientData)->scanResponse; break; + case Z_APDU_sortResponse: + p->eventType = "sort"; + ir_sortResponse (p, apdu->u.sortResponse, + (IrTcl_SetObj *) cmd_info.clientData); + apdu_call = ((IrTcl_SetObj *) + cmd_info.clientData)->sortResponse; + break; default: logf (LOG_WARN, "Received unknown APDU type (%d)", apdu->which);