void printTrieSymbol(FILE *fp, Cell symbol) { if ( symbol == ESCAPE_NODE_SYMBOL ) fprintf(fp, "%lu [ESCAPE_NODE_SYMBOL]", ESCAPE_NODE_SYMBOL); else { switch(TrieSymbolType(symbol)) { case XSB_INT: fprintf(fp, IntegerFormatString, int_val(symbol)); break; case XSB_FLOAT: fprintf(fp, "%f", float_val(symbol)); break; case XSB_STRING: fprintf(fp, "%s", string_val(symbol)); break; case XSB_TrieVar: fprintf(fp, "V" IntegerFormatString, DecodeTrieVar(symbol)); break; case XSB_STRUCT: { Psc psc = DecodeTrieFunctor(symbol); fprintf(fp, "%s/%d", get_name(psc), get_arity(psc)); } break; case XSB_LIST: fprintf(fp, "LIST"); break; default: fprintf(fp, "Unknown symbol (tag = %ld)", cell_tag(symbol)); break; } } }
static const cst_val *segment_duration(const cst_item *seg) { const cst_item *s = item_as(seg,"Segment"); if (!s) return VAL_STRING_0; else if (item_prev(s) == NULL) return item_feat(s,"end"); else /* It should be okay to construct this as it will get dereferenced when the CART interpreter frees its feature cache. */ return float_val(item_feat_float(s,"end") - item_feat_float(item_prev(s),"end")); }
int sprintTrieSymbol(char * buffer, Cell symbol) { int ctr; if ( symbol == ESCAPE_NODE_SYMBOL ) return sprintf(buffer, "%lu [ESCAPE_NODE_SYMBOL]", ESCAPE_NODE_SYMBOL); else { switch(TrieSymbolType(symbol)) { case XSB_INT: return sprintf(buffer, IntegerFormatString, int_val(symbol)); break; case XSB_FLOAT: return sprintf(buffer, "%f", float_val(symbol)); break; case XSB_STRING: return sprintf(buffer, "%s", string_val(symbol)); break; case XSB_TrieVar: return sprintf(buffer, "_V" IntegerFormatString, DecodeTrieVar(symbol)); break; case XSB_STRUCT: { Psc psc; if (isboxedfloat(symbol)) { return sprintf(buffer, "%lf", boxedfloat_val(symbol)); break; } psc = DecodeTrieFunctor(symbol); ctr = sprint_quotedname(buffer, 0, get_name(psc)); return sprintf(buffer+ctr, "/%d", get_arity(psc)); } break; case XSB_LIST: return sprintf(buffer, "LIST"); break; default: return sprintf(buffer, "Unknown symbol (tag = %" Intfmt")", cell_tag(symbol)); break; } } }
void feat_set_float(cst_features *f, const char *name, float v) { feat_set(f,name,float_val(v)); }
int compare(CTXTdeclc const void * v1, const void * v2) { int comp; CPtr cptr1, cptr2; Cell val1 = (Cell) v1 ; Cell val2 = (Cell) v2 ; XSB_Deref(val2); /* val2 is not in register! */ XSB_Deref(val1); /* val1 is not in register! */ if (val1 == val2) return 0; switch(cell_tag(val1)) { case XSB_FREE: case XSB_REF1: if (isattv(val2)) return vptr(val1) - (CPtr)dec_addr(val2); else if (isnonvar(val2)) return -1; else { /* in case there exist local stack variables in the */ /* comparison, globalize them to guarantee that their */ /* order is retained as long as nobody "touches" them */ /* in the future -- without copying garbage collection */ if ((top_of_localstk <= vptr(val1)) && (vptr(val1) <= (CPtr)glstack.high-1)) { bld_free(hreg); bind_ref(vptr(val1), hreg); hreg++; val1 = follow(val1); /* deref again */ } if ((top_of_localstk <= vptr(val2)) && (vptr(val2) <= (CPtr)glstack.high-1)) { bld_free(hreg); bind_ref(vptr(val2), hreg); hreg++; val2 = follow(val2); /* deref again */ } return vptr(val1) - vptr(val2); } case XSB_FLOAT: if (isref(val2) || isattv(val2)) return 1; else if (isofloat(val2)) return sign(float_val(val1) - ofloat_val(val2)); else return -1; case XSB_INT: if (isref(val2) || isofloat(val2) || isattv(val2)) return 1; else if (isinteger(val2)) return int_val(val1) - int_val(val2); else if (isboxedinteger(val2)) return int_val(val1) - boxedint_val(val2); else return -1; case XSB_STRING: if (isref(val2) || isofloat(val2) || isinteger(val2) || isattv(val2)) return 1; else if (isstring(val2)) { return strcmp(string_val(val1), string_val(val2)); } else return -1; case XSB_STRUCT: // below, first 2 if-checks test to see if this struct is actually a number representation, // (boxed float or boxed int) and if so, does what the number case would do, only with boxed_val // macros. if (isboxedinteger(val1)) { if (isref(val2) || isofloat(val2) || isattv(val2)) return 1; else if (isinteger(val2)) return boxedint_val(val1) - int_val(val2); else if (isboxedinteger(val2)) return boxedint_val(val1) - boxedint_val(val2); else return -1; } else if (isboxedfloat(val1)) { if (isref(val2) || isattv(val2)) return 1; else if (isofloat(val2)) return sign(boxedfloat_val(val1) - ofloat_val(val2)); else return -1; } else if (cell_tag(val2) != XSB_STRUCT && cell_tag(val2) != XSB_LIST) return 1; else { int arity1, arity2; Psc ptr1 = get_str_psc(val1); Psc ptr2 = get_str_psc(val2); arity1 = get_arity(ptr1); if (islist(val2)) arity2 = 2; else arity2 = get_arity(ptr2); if (arity1 != arity2) return arity1-arity2; if (islist(val2)) comp = strcmp(get_name(ptr1), "."); else comp = strcmp(get_name(ptr1), get_name(ptr2)); if (comp || (arity1 == 0)) return comp; cptr1 = clref_val(val1); cptr2 = clref_val(val2); for (arity2 = 1; arity2 <= arity1; arity2++) { if (islist(val2)) comp = compare(CTXTc (void*)cell(cptr1+arity2), (void*)cell(cptr2+arity2-1)); else comp = compare(CTXTc (void*)cell(cptr1+arity2), (void*)cell(cptr2+arity2)); if (comp) break; } return comp; } break; case XSB_LIST: if (cell_tag(val2) != XSB_STRUCT && cell_tag(val2) != XSB_LIST) return 1; else if (isconstr(val2)) return -(compare(CTXTc (void*)val2, (void*)val1)); else { /* Here we are comparing two list structures. */ cptr1 = clref_val(val1); cptr2 = clref_val(val2); comp = compare(CTXTc (void*)cell(cptr1), (void*)cell(cptr2)); if (comp) return comp; return compare(CTXTc (void*)cell(cptr1+1), (void*)cell(cptr2+1)); } break; case XSB_ATTV: if (isattv(val2)) return (CPtr)dec_addr(val1) - (CPtr)dec_addr(val2); else if (isref(val2)) return (CPtr)dec_addr(val1) - vptr(val2); else return -1; default: xsb_abort("Compare (unknown tag %ld); returning 0", cell_tag(val1)); return 0; } }
static void symstkPrintNextTerm(FILE *fp, xsbBool list_recursion) { Cell symbol; if ( SymbolStack_IsEmpty ) { fprintf(fp, "<no subterm>"); return; } SymbolStack_Pop(symbol); switch ( TrieSymbolType(symbol) ) { case XSB_INT: if ( list_recursion ) fprintf(fp, "|" IntegerFormatString "]", int_val(symbol)); else fprintf(fp, IntegerFormatString, int_val(symbol)); break; case XSB_FLOAT: if ( list_recursion ) fprintf(fp, "|%f]", float_val(symbol)); else fprintf(fp, "%f", float_val(symbol)); break; case XSB_STRING: { char *string = string_val(symbol); if ( list_recursion ) { if ( string == nil_sym ) fprintf(fp, "]"); else fprintf(fp, "|%s]", string); } else fprintf(fp, "%s", string); } break; case XSB_TrieVar: if ( list_recursion ) fprintf(fp, "|V" IntegerFormatString "]", DecodeTrieVar(symbol)); else fprintf(fp, "V" IntegerFormatString, DecodeTrieVar(symbol)); break; case XSB_STRUCT: { Psc psc; int i; if ( list_recursion ) fprintf(fp, "|"); psc = DecodeTrieFunctor(symbol); fprintf(fp, "%s(", get_name(psc)); for (i = 1; i < (int)get_arity(psc); i++) { symstkPrintNextTerm(fp,FALSE); fprintf(fp, ","); } symstkPrintNextTerm(fp,FALSE); fprintf(fp, ")"); if ( list_recursion ) fprintf(fp, "]"); } break; case XSB_LIST: if ( list_recursion ) fprintf(fp, ","); else fprintf(fp, "["); symstkPrintNextTerm(fp,FALSE); symstkPrintNextTerm(fp,TRUE); break; default: fprintf(fp, "<unknown symbol>"); break; } }
static int symstkSPrintNextTerm(CTXTdeclc char * buffer, xsbBool list_recursion) { int ctr = 0; Cell symbol; if ( SymbolStack_IsEmpty ) { // fprintf(fp, "<no subterm>"); return 0; } SymbolStack_Pop(symbol); switch ( TrieSymbolType(symbol) ) { case XSB_INT: if ( list_recursion ) ctr = ctr + sprintf(buffer+ctr, "|" IntegerFormatString "]", int_val(symbol)); else ctr = ctr + sprintf(buffer+ctr, IntegerFormatString, int_val(symbol)); break; case XSB_FLOAT: if ( list_recursion ) ctr = ctr + sprintf(buffer+ctr, "|%f]", float_val(symbol)); else ctr = ctr + sprintf(buffer+ctr, "%f", float_val(symbol)); break; case XSB_STRING: { char *string = string_val(symbol); if ( list_recursion ) { if ( string == nil_string ) ctr = ctr + sprintf(buffer+ctr, "]"); else { // ctr = ctr + sprintf(buffer+ctr, "|%s]", string); ctr = ctr + sprintf(buffer+ctr, "|"); ctr = sprint_quotedname(buffer, ctr, string); ctr = ctr + sprintf(buffer+ctr, "]"); } } else // ctr = ctr + sprintf(buffer+ctr, "%s", string); ctr = sprint_quotedname(buffer, ctr, string); } break; case XSB_TrieVar: if ( list_recursion ) ctr = ctr + sprintf(buffer+ctr, "|V" IntegerFormatString "]", DecodeTrieVar(symbol)); else ctr = ctr + sprintf(buffer+ctr, "_V" IntegerFormatString, DecodeTrieVar(symbol)); break; case XSB_STRUCT: { Psc psc; int i; if (isboxedfloat(symbol)) { if ( list_recursion ) ctr = ctr + sprintf(buffer+ctr, "|%lf]", boxedfloat_val(symbol)); else ctr = ctr + sprintf(buffer+ctr, "%lf", boxedfloat_val(symbol)); break; } if ( list_recursion ) ctr = ctr + sprintf(buffer+ctr, "|"); psc = DecodeTrieFunctor(symbol); // ctr = ctr + sprintf(buffer+ctr, "%s(", get_name(psc)); ctr = sprint_quotedname(buffer, ctr, get_name(psc)); ctr = ctr + sprintf(buffer+ctr, "("); for (i = 1; i < (int)get_arity(psc); i++) { ctr = ctr + symstkSPrintNextTerm(CTXTc buffer+ctr,FALSE); ctr = ctr + sprintf(buffer+ctr, ","); } ctr = ctr + symstkSPrintNextTerm(CTXTc buffer+ctr,FALSE); ctr = ctr + sprintf(buffer+ctr, ")"); if ( list_recursion ) ctr = ctr + sprintf(buffer+ctr, "]"); } break; case XSB_LIST: if ( list_recursion ) ctr = ctr + sprintf(buffer+ctr, ","); else ctr = ctr + sprintf(buffer+ctr, "["); ctr = ctr + symstkSPrintNextTerm(CTXTc buffer+ctr,FALSE); ctr = ctr + symstkSPrintNextTerm(CTXTc buffer+ctr,TRUE); break; default: ctr = ctr + sprintf(buffer+ctr, "<unknown symbol>"); break; } return ctr; }
/*-----------------------------------------------------------------------------*/ void SetBindVal() { RETCODE rc; struct Cursor *cur = (struct Cursor *)ptoc_int(2); int j = ptoc_int(3); Cell BindVal = ptoc_tag(4); if (!((j >= 0) && (j < cur->NumBindVars))) xsb_exit("Abnormal argument in SetBindVal!"); /* if we're reusing an opened cursor w/ the statement number*/ /* reallocate BindVar if type has changed (May not be such a good idea?)*/ if (cur->Status == 2) { if (isinteger(BindVal)) { if (cur->BindTypes[j] != 0) { if (cur->BindTypes[j] != 2) free((void *)cur->BindList[j]); cur->BindList[j] = (UCHAR *)malloc(sizeof(int)); cur->BindTypes[j] = 0; rc = SQLBindParameter(cur->hstmt, (short)(j+1), SQL_PARAM_INPUT, SQL_C_SLONG, SQL_INTEGER, 0, 0, (int *)(cur->BindList[j]), 0, NULL); if (rc != SQL_SUCCESS) { ctop_int(5,PrintErrorMsg(cur)); SetCursorClose(cur); return; } } *((int *)cur->BindList[j]) = oint_val(BindVal); } else if (isfloat(BindVal)) { if (cur->BindTypes[j] != 1) { /*printf("ODBC: Changing Type: flt to %d\n",cur->BindTypes[j]);*/ if (cur->BindTypes[j] != 2) free((void *)cur->BindList[j]); cur->BindList[j] = (UCHAR *)malloc(sizeof(float)); cur->BindTypes[j] = 1; rc = SQLBindParameter(cur->hstmt, (short)(j+1), SQL_PARAM_INPUT, SQL_C_FLOAT, SQL_FLOAT, 0, 0, (float *)(cur->BindList[j]), 0, NULL); if (rc != SQL_SUCCESS) { ctop_int(5,PrintErrorMsg(cur)); SetCursorClose(cur); return; } } *((float *)cur->BindList[j]) = (float)float_val(BindVal); } else if (isstring(BindVal)) { if (cur->BindTypes[j] != 2) { /*printf("ODBC: Changing Type: str to %d\n",cur->BindTypes[j]);*/ free((void *)cur->BindList[j]); cur->BindTypes[j] = 2; /* SQLBindParameter will be done anyway*/ } cur->BindList[j] = string_val(BindVal); } else if (isconstr(BindVal) && get_arity(get_str_psc(BindVal))==1) { letter_flag = 1; wcan_disp = 0; write_canonical_term(p2p_arg(BindVal,1)); if (term_string[j]) free(term_string[j]); term_string[j] = malloc(wcan_disp+1); strncpy(term_string[j],wcan_string,wcan_disp); term_string[j][wcan_disp] = '\0'; cur->BindTypes[j] = 2; cur->BindList[j] = term_string[j]; } else { xsb_exit("Unknown bind variable type, %d", cur->BindTypes[j]); } ctop_int(5,0); return; } /* otherwise, memory needs to be allocated in this case*/ if (isinteger(BindVal)) { cur->BindTypes[j] = 0; cur->BindList[j] = (UCHAR *)malloc(sizeof(int)); if (!cur->BindList[j]) xsb_exit("Not enough memory for an int in SetBindVal!"); *((int *)cur->BindList[j]) = oint_val(BindVal); } else if (isfloat(BindVal)) { cur->BindTypes[j] = 1; cur->BindList[j] = (UCHAR *)malloc(sizeof(float)); if (!cur->BindList[j]) xsb_exit("Not enough memory for a float in SetBindVal!"); *((float *)cur->BindList[j]) = (float)float_val(BindVal); } else if (isstring(BindVal)) { cur->BindTypes[j] = 2; cur->BindList[j] = string_val(BindVal); } else if (isconstr(BindVal) && get_arity(get_str_psc(BindVal))==1) { letter_flag = 1; wcan_disp = 0; write_canonical_term(p2p_arg(BindVal,1)); if (term_string[j]) free(term_string[j]); term_string[j] = malloc(wcan_disp+1); strncpy(term_string[j],wcan_string,wcan_disp); term_string[j][wcan_disp] = '\0'; cur->BindTypes[j] = 2; cur->BindList[j] = term_string[j]; } else { xsb_exit("Unknown bind variable type, %d", cur->BindTypes[j]); } ctop_int(5,0); return; }