struct record * record_create(NMEM nmem, int num_metadata, int num_sortkeys, struct client *client, int position) { struct record * record = 0; int i = 0; // assert(nmem); record = nmem_malloc(nmem, sizeof(struct record)); record->next = 0; record->client = client; record->metadata = nmem_malloc(nmem, sizeof(struct record_metadata*) * num_metadata); for (i = 0; i < num_metadata; i++) record->metadata[i] = 0; record->sortkeys = nmem_malloc(nmem, sizeof(union data_types*) * num_sortkeys); for (i = 0; i < num_sortkeys; i++) record->sortkeys[i] = 0; record->position = position; return record; }
struct relevance *relevance_create_ccl(pp2_charset_fact_t pft, struct ccl_rpn_node *query, int rank_cluster, double follow_factor, double lead_decay, int length_divide) { NMEM nmem = nmem_create(); struct relevance *res = nmem_malloc(nmem, sizeof(*res)); int i; res->nmem = nmem; res->entries = 0; res->vec_len = 1; res->rank_cluster = rank_cluster; res->follow_factor = follow_factor; res->lead_decay = lead_decay; res->length_divide = length_divide; res->prt = pp2_charset_token_create(pft, "relevance"); pull_terms(res, query); res->doc_frequency_vec = nmem_malloc(nmem, res->vec_len * sizeof(int)); for (i = 0; i < res->vec_len; i++) res->doc_frequency_vec[i] = 0; // worker array res->term_frequency_vec_tmp = nmem_malloc(res->nmem, res->vec_len * sizeof(*res->term_frequency_vec_tmp)); res->term_pos = nmem_malloc(res->nmem, res->vec_len * sizeof(*res->term_pos)); return res; }
struct database *new_database_inherit_settings(const char *id, NMEM nmem, struct settings_array *service_settings) { struct database *db; struct setting *idset; db = nmem_malloc(nmem, sizeof(*db)); db->id = nmem_strdup(nmem, id); db->next = 0; if (service_settings && service_settings->num_settings > 0) { yaz_log(YLOG_DEBUG, "copying settings from service to database %s settings", db->id); db->num_settings = service_settings->num_settings; db->settings = nmem_malloc(nmem, sizeof(*db->settings) * db->num_settings); // Initialize database settings with service settings memcpy(db->settings, service_settings->settings, sizeof(*db->settings) * db->num_settings); } else { yaz_log(YLOG_DEBUG, "No service settings to database %s ", db->id); db->num_settings = PZ_MAX_EOF; db->settings = nmem_malloc(nmem, sizeof(*db->settings) * db->num_settings); memset(db->settings, 0, sizeof(*db->settings) * db->num_settings); } idset = nmem_malloc(nmem, sizeof(*idset)); idset->precedence = 0; idset->name = "pz:id"; idset->target = idset->value = db->id; idset->next = db->settings[PZ_ID]; db->settings[PZ_ID] = idset; return db; }
void yaz_set_esn(Z_RecordComposition **comp_p, const char *esn, NMEM nmem) { Z_RecordComposition *comp = (Z_RecordComposition *) nmem_malloc(nmem, sizeof(*comp)); comp->which = Z_RecordComp_simple; comp->u.simple = (Z_ElementSetNames *) nmem_malloc(nmem, sizeof(*comp->u.simple)); comp->u.simple->which = Z_ElementSetNames_generic; comp->u.simple->u.generic = nmem_strdup(nmem, esn); *comp_p = comp; }
sel_thread_t sel_thread_create(void (*work_handler)(void *work_data), void (*work_destroy)(void *work_data), int *read_fd, int no_of_threads) { int i; NMEM nmem = nmem_create(); sel_thread_t p = nmem_malloc(nmem, sizeof(*p)); assert(work_handler); /* work_destroy may be NULL */ assert(read_fd); assert(no_of_threads >= 1); p->nmem = nmem; #ifdef WIN32 /* use port 12119 temporarily on Windos and hope for the best */ p->spipe = yaz_spipe_create(12119, 0); #else p->spipe = yaz_spipe_create(0, 0); #endif if (!p->spipe) { nmem_destroy(nmem); return 0; } *read_fd = p->read_fd = yaz_spipe_get_read_fd(p->spipe); p->write_fd = yaz_spipe_get_write_fd(p->spipe); p->input_queue = 0; p->output_queue = 0; p->free_queue = 0; p->work_handler = work_handler; p->work_destroy = work_destroy; p->no_threads = 0; /* we if need to destroy */ p->stop_flag = 0; p->mutex = 0; yaz_mutex_create(&p->mutex); yaz_cond_create(&p->input_data); if (p->input_data == 0) /* condition variable could not be created? */ { sel_thread_destroy(p); return 0; } p->no_threads = no_of_threads; p->thread_id = nmem_malloc(nmem, sizeof(*p->thread_id) * p->no_threads); for (i = 0; i < p->no_threads; i++) p->thread_id[i] = yaz_thread_create(sel_thread_handler, p); return p; }
struct http_header * http_header_append(struct http_channel *ch, struct http_header * hp, const char *name, const char *value) { struct http_header *hpnew = 0; if (!hp | !ch) return 0; while (hp && hp->next) hp = hp->next; if(name && strlen(name)&& value && strlen(value)){ hpnew = nmem_malloc(ch->nmem, sizeof *hpnew); hpnew->name = nmem_strdup(ch->nmem, name); hpnew->value = nmem_strdup(ch->nmem, value); hpnew->next = 0; hp->next = hpnew; hp = hp->next; return hpnew; } return hp; }
normalize_record_t normalize_cache_get(normalize_cache_t nc, struct conf_service *service, const char *spec) { normalize_record_t nt; struct cached_item *ci; yaz_mutex_enter(nc->mutex); for (ci = nc->items; ci; ci = ci->next) if (!strcmp(spec, ci->spec)) break; if (ci) nt = ci->nt; else { nt = normalize_record_create(service, spec); if (nt) { ci = nmem_malloc(nc->nmem, sizeof(*ci)); ci->next = nc->items; nc->items = ci; ci->nt = nt; ci->spec = nmem_strdup(nc->nmem, spec); } } yaz_mutex_leave(nc->mutex); return nt; }
struct http_session *http_session_create(struct conf_service *service, http_sessions_t http_sessions, unsigned int sesid) { NMEM nmem = nmem_create(); struct http_session *r = nmem_malloc(nmem, sizeof(*r)); char tmp_str[50]; sprintf(tmp_str, "session#%u", sesid); r->psession = new_session(nmem, service, sesid); r->session_id = sesid; r->timestamp = 0; r->nmem = nmem; r->destroy_counter = r->activity_counter = 0; r->http_sessions = http_sessions; yaz_mutex_enter(http_sessions->mutex); r->next = http_sessions->session_list; http_sessions->session_list = r; yaz_mutex_leave(http_sessions->mutex); r->timeout_iochan = iochan_create(-1, session_timeout, 0, "http_session_timeout"); iochan_setdata(r->timeout_iochan, r); yaz_log(http_sessions->log_level, "Session %u created. timeout chan=%p timeout=%d", sesid, r->timeout_iochan, service->session_timeout); iochan_settimeout(r->timeout_iochan, service->session_timeout); iochan_add(service->server->iochan_man, r->timeout_iochan); http_session_use(1); return r; }
static void *construct_select(const xmlNode *ptr, const char *path, WRBUF wr_error) { if (strcmp((const char *) ptr->name, "select")) return 0; else { NMEM nmem = nmem_create(); struct select_info *info = nmem_malloc(nmem, sizeof(*info)); const char *attr_str; const char *xpath = 0; info->nmem = nmem; info->xpath_expr = 0; attr_str = yaz_xml_get_prop(ptr, "path%s", &xpath); if (attr_str) { wrbuf_printf(wr_error, "Bad attribute '%s'" "Expected xpath.", attr_str); nmem_destroy(nmem); return 0; } if (xpath) info->xpath_expr = nmem_strdup(nmem, xpath); return info; } }
/* Return charset, lang, selected from negotiation.. Client side */ void yaz_get_response_charneg(NMEM mem, Z_CharSetandLanguageNegotiation *p, char **charset, char **lang, int *selected) { Z_TargetResponse *res = p->u.response; if (charset && res->which == Z_TargetResponse_private && res->u.zprivate->which == Z_PrivateCharacterSet_externallySpecified) { Z_External *pext = res->u.zprivate->u.externallySpecified; if (pext->which == Z_External_octet) { *charset = (char *) nmem_malloc(mem, (1+pext->u.octet_aligned->len)*sizeof(char)); memcpy(*charset, pext->u.octet_aligned->buf, pext->u.octet_aligned->len); (*charset)[pext->u.octet_aligned->len] = 0; } } if (charset && res->which == Z_TargetResponse_iso10646) *charset = set_form(res->u.iso10646->encodingLevel); if (lang && res->selectedLanguage) *lang = nmem_strdup(mem, res->selectedLanguage); if (selected && res->recordsInSelectedCharSets) *selected = *res->recordsInSelectedCharSets; }
static int http_parse_arguments(struct http_request *r, NMEM nmem, const char *args) { const char *p2 = args; while (*p2) { struct http_argument *a; const char *equal = strchr(p2, '='); const char *eoa = strchr(p2, '&'); if (!equal) { yaz_log(YLOG_WARN, "Expected '=' in argument"); return -1; } if (!eoa) eoa = equal + strlen(equal); // last argument else if (equal > eoa) { yaz_log(YLOG_WARN, "Missing '&' in argument"); return -1; } a = nmem_malloc(nmem, sizeof(struct http_argument)); a->name = nmem_strdupn(nmem, p2, equal - p2); a->value = nmem_strdupn(nmem, equal+1, eoa - equal - 1); urldecode(a->name, a->name); urldecode(a->value, a->value); a->next = r->arguments; r->arguments = a; p2 = eoa; while (*p2 == '&') p2++; } return 0; }
void yaz_marc_add_subfield(yaz_marc_t mt, const char *code_data, size_t code_data_len) { if (mt->debug) { size_t i; char msg[80]; sprintf(msg, "subfield:"); for (i = 0; i < 16 && i < code_data_len; i++) sprintf(msg + strlen(msg), " %02X", code_data[i] & 0xff); if (i < code_data_len) sprintf(msg + strlen(msg), " .."); yaz_marc_add_comment(mt, msg); } if (mt->subfield_pp) { struct yaz_marc_subfield *n = (struct yaz_marc_subfield *) nmem_malloc(mt->nmem, sizeof(*n)); n->code_data = nmem_strdupn(mt->nmem, code_data, code_data_len); n->next = 0; /* mark subfield_pp to point to this one, so we append here next */ *mt->subfield_pp = n; mt->subfield_pp = &n->next; } }
struct record_metadata * record_metadata_create(NMEM nmem) { struct record_metadata * rec_md = nmem_malloc(nmem, sizeof(struct record_metadata)); rec_md->next = 0; rec_md->attributes = 0; return rec_md; }
void http_addheader(struct http_response *r, const char *name, const char *value) { struct http_channel *c = r->channel; struct http_header *h = nmem_malloc(c->nmem, sizeof *h); h->name = nmem_strdup(c->nmem, name); h->value = nmem_strdup(c->nmem, value); h->next = r->headers; r->headers = h; }
static struct yaz_marc_node *yaz_marc_add_node(yaz_marc_t mt) { struct yaz_marc_node *n = (struct yaz_marc_node *) nmem_malloc(mt->nmem, sizeof(*n)); n->next = 0; *mt->nodes_pp = n; mt->nodes_pp = &n->next; return n; }
normalize_cache_t normalize_cache_create(void) { NMEM nmem = nmem_create(); normalize_cache_t nc = nmem_malloc(nmem, sizeof(*nc)); nc->nmem = nmem; nc->items = 0; nc->mutex = 0; pazpar2_mutex_create(&nc->mutex, "normalize_cache"); return nc; }
struct http_response *http_create_response(struct http_channel *c) { struct http_response *r = nmem_malloc(c->nmem, sizeof(*r)); strcpy(r->code, "200"); r->msg = "OK"; r->channel = c; r->headers = 0; r->payload = 0; r->content_type = "text/xml"; return r; }
Odr_oid *odr_getoidbystr_nmem(NMEM nmem, const char *str) { Odr_oid oid[OID_SIZE]; Odr_oid *ret; if (oid_dotstring_to_oid(str, oid)) return 0; ret = (Odr_oid *)nmem_malloc(nmem, sizeof(*ret)*(oid_oidlen(oid) + 1)); oid_oidcpy(ret, oid); return ret; }
void relevance_newrec(struct relevance *r, struct record_cluster *rec) { if (!rec->term_frequency_vec) { int i; // term frequency [1,..] . [0] is total length of all fields rec->term_frequency_vec = nmem_malloc(r->nmem, r->vec_len * sizeof(*rec->term_frequency_vec)); for (i = 0; i < r->vec_len; i++) rec->term_frequency_vec[i] = 0; // term frequency divided by length of field [1,...] rec->term_frequency_vecf = nmem_malloc(r->nmem, r->vec_len * sizeof(*rec->term_frequency_vecf)); for (i = 0; i < r->vec_len; i++) rec->term_frequency_vecf[i] = 0.0; } }
Odr_oid *odr_oiddup_nmem(NMEM nmem, const Odr_oid *o) { Odr_oid *r; if (!o) return 0; if (!(r = (Odr_oid *) nmem_malloc(nmem, (oid_oidlen(o) + 1) * sizeof(Odr_oid)))) return 0; oid_oidcpy(r, o); return r; }
static struct gfs_listen * gfs_listen_new(const char *id, const char *address) { struct gfs_listen *n = (struct gfs_listen *) nmem_malloc(gfs_nmem, sizeof(*n)); if (id) n->id = nmem_strdup(gfs_nmem, id); else n->id = 0; n->next = 0; n->address = nmem_strdup(gfs_nmem, address); return n; }
struct http_response *http_parse_response_buf(struct http_channel *c, const char *buf, int len) { char tmp[MAX_HTTP_HEADER]; struct http_response *r = http_create_response(c); char *p, *p2; struct http_header **hp = &r->headers; if (len >= MAX_HTTP_HEADER) return 0; memcpy(tmp, buf, len); for (p = tmp; *p && *p != ' '; p++) // Skip HTTP version ; p++; // Response code for (p2 = p; *p2 && *p2 != ' ' && p2 - p < 3; p2++) r->code[p2 - p] = *p2; if (!(p = strstr(tmp, "\r\n"))) return 0; p += 2; while (*p) { if (!(p2 = strstr(p, "\r\n"))) return 0; if (p == p2) // End of headers break; else { struct http_header *h = *hp = nmem_malloc(c->nmem, sizeof(*h)); char *value = strchr(p, ':'); if (!value) return 0; *(value++) = '\0'; h->name = nmem_strdup(c->nmem, p); while (isspace(*(const unsigned char *) value)) value++; if (value >= p2) // Empty header; { h->value = ""; p = p2 + 2; continue; } *p2 = '\0'; h->value = nmem_strdup(c->nmem, value); h->next = 0; hp = &h->next; p = p2 + 2; } } return r; }
struct record * record_create(NMEM nmem, int num_metadata, int num_sortkeys, struct client *client, int position) { struct record * record = 0; int i = 0; const char *name = client_get_id(client); unsigned h = position; // assert(nmem); record = nmem_malloc(nmem, sizeof(struct record)); record->next = 0; record->client = client; record->metadata = nmem_malloc(nmem, sizeof(struct record_metadata*) * num_metadata); for (i = 0; i < num_metadata; i++) record->metadata[i] = 0; record->sortkeys = nmem_malloc(nmem, sizeof(union data_types*) * num_sortkeys); for (i = 0; i < num_sortkeys; i++) record->sortkeys[i] = 0; record->position = position; for (i = 0; name[i]; i++) h = h * 65509 + ((unsigned char *) name)[i]; record->checksum = h; return record; }
static void http_error(struct http_channel *hc, int no, const char *msg) { struct http_response *rs = http_create_response(hc); hc->response = rs; hc->keep_alive = 0; // not keeping this HTTP session alive sprintf(rs->code, "%d", no); rs->msg = nmem_strdup(hc->nmem, msg); rs->payload = nmem_malloc(hc->nmem, 100); yaz_snprintf(rs->payload, 99, "<error>HTTP Error %d: %s</error>\n", no, msg); http_send_response(hc); }
static struct gfs_server * gfs_server_new(void) { struct gfs_server *n = (struct gfs_server *) nmem_malloc(gfs_nmem, sizeof(*n)); memcpy(&n->cb, &control_block, sizeof(control_block)); n->next = 0; n->host = 0; n->listen_ref = 0; n->cql_transform = 0; n->ccl_transform = 0; n->server_node_ptr = 0; n->directory = 0; n->docpath = 0; n->stylesheet = 0; n->retrieval = yaz_retrieval_create(); return n; }
union data_types * data_types_assign(NMEM nmem, union data_types ** data1, union data_types data2) { // assert(nmem); if (!data1) return 0; if (!*data1){ if (!nmem) return 0; else *data1 = nmem_malloc(nmem, sizeof(union data_types)); } **data1 = data2; return *data1; }
static void pull_terms(struct relevance *res, struct ccl_rpn_node *n) { char **words; int numwords; char *ccl_field; int i; switch (n->kind) { case CCL_RPN_AND: case CCL_RPN_OR: case CCL_RPN_NOT: case CCL_RPN_PROX: pull_terms(res, n->u.p[0]); pull_terms(res, n->u.p[1]); break; case CCL_RPN_TERM: nmem_strsplit(res->nmem, " ", n->u.t.term, &words, &numwords); for (i = 0; i < numwords; i++) { const char *norm_str; ccl_field = nmem_strdup_null(res->nmem, n->u.t.qual); pp2_charset_token_first(res->prt, words[i], 0); while ((norm_str = pp2_charset_token_next(res->prt))) { struct word_entry **e = &res->entries; while (*e) e = &(*e)->next; *e = nmem_malloc(res->nmem, sizeof(**e)); (*e)->norm_str = nmem_strdup(res->nmem, norm_str); (*e)->ccl_field = ccl_field; (*e)->termno = res->vec_len++; (*e)->display_str = nmem_strdup(res->nmem, words[i]); (*e)->next = 0; } } break; default: break; } }
struct record_metadata * record_metadata_insert(NMEM nmem, struct record_metadata ** rmd, union data_types data) { struct record_metadata * tmp_rmd = 0; // assert(nmem); if(!rmd) return 0; // construct new record_metadata tmp_rmd = nmem_malloc(nmem, sizeof(struct record_metadata)); tmp_rmd->data = data; // insert in *rmd's place, moving *rmd one down the list tmp_rmd->next = *rmd; *rmd = tmp_rmd; return *rmd; }
static char *nmem_dup_xml_content(NMEM n, xmlNodePtr ptr) { unsigned char *cp; xmlNodePtr p; int len = 1; /* start with 1, because of trailing 0 */ unsigned char *str; int first = 1; /* whitespace lead flag .. */ /* determine length */ for (p = ptr; p; p = p->next) { if (p->type == XML_TEXT_NODE) len += xmlStrlen(p->content); } /* now allocate for the string */ str = (unsigned char *) nmem_malloc(n, len); *str = '\0'; /* so we can use strcat */ for (p = ptr; p; p = p->next) { if (p->type == XML_TEXT_NODE) { cp = p->content; if (first) { while(*cp && isspace(*cp)) cp++; if (*cp) first = 0; /* reset if we got non-whitespace out */ } strcat((char *)str, (const char *)cp); /* append */ } } /* remove trailing whitespace */ cp = strlen((const char *)str) + str; while (cp != str && isspace(cp[-1])) cp--; *cp = '\0'; /* return resulting string */ return (char *) str; }
static void extract_user_pass(NMEM nmem, const char *uri, char **uri_lean, char **http_user, char **http_pass) { const char *cp1 = strchr(uri, '/'); *uri_lean = 0; *http_user = 0; *http_pass = 0; if (cp1 && cp1 > uri) { cp1--; if (!strncmp(cp1, "://", 3)) { const char *cp3 = 0; const char *cp2 = cp1 + 3; while (*cp2 && *cp2 != '/' && *cp2 != '@') { if (*cp2 == ':') cp3 = cp2; cp2++; } if (*cp2 == '@' && cp3) { *uri_lean = nmem_malloc(nmem, strlen(uri) + 1); memcpy(*uri_lean, uri, cp1 + 3 - uri); strcpy(*uri_lean + (cp1 + 3 - uri), cp2 + 1); *http_user = nmem_strdupn(nmem, cp1 + 3, cp3 - (cp1 + 3)); *http_pass = nmem_strdupn(nmem, cp3 + 1, cp2 - (cp3 + 1)); } } } if (*uri_lean == 0) *uri_lean = nmem_strdup(nmem, uri); }