YAZ  5.23.1
facet.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 
11 #if HAVE_CONFIG_H
12 #include <config.h>
13 #endif
14 
15 #include <yaz/facet.h>
16 #include <yaz/diagbib1.h>
17 #include <yaz/oid_db.h>
18 #include <yaz/oid_std.h>
19 #include <yaz/otherinfo.h>
20 #include <yaz/pquery.h>
21 #include <assert.h>
22 
24  Z_OtherInformation **otherInformation, ODR odr,
25  Z_FacetList *facet_list)
26 {
27  int categoryValue = 1;
28  Z_External *z_external = 0;
30  yaz_oi_update(otherInformation, odr, yaz_oid_userinfo_facet_1,
31  categoryValue, 0);
32  if (!oi)
33  return;
35  z_external = odr_malloc(odr, sizeof(*z_external));
36  z_external->which = Z_External_userFacets;
38  z_external->indirect_reference = 0;
39  z_external->descriptor = 0;
40  z_external->u.facetList = facet_list;
41  oi->information.externallyDefinedInfo = z_external;
42 }
43 
45 {
46  Z_OtherInformation *oi = *otherInformation;
47  if (oi)
48  {
49  int i;
50  for (i = 0; i < oi->num_elements; i++)
51  {
52  Z_OtherInformationUnit *oiu = oi->list[i];
56  {
58  }
59  }
60  }
61  return 0;
62 }
63 
64 /* Little helper to extract a string attribute */
65 /* Gets the first string, there is usually only one */
66 /* in case of errors, returns null */
67 
68 void yaz_facet_attr_init(struct yaz_facet_attr *attr_values)
69 {
70  attr_values->errcode = 0;
71  attr_values->errstring = 0;
72  attr_values->sortorder = 0;
73  attr_values->useattr = 0;
74  attr_values->useattrbuff[0] = 0;
75  attr_values->limit = 0;
76  attr_values->start = 0;
77 }
78 
79 static const char *stringattr(Z_ComplexAttribute *c)
80 {
81  int i;
82  Z_StringOrNumeric *son;
83  for (i = 0; i < c->num_list; i++)
84  {
85  son = c->list[i];
86  if ( son->which == Z_StringOrNumeric_string)
87  return son->u.string;
88  }
89  return 0;
90 }
91 
92 /* Use attribute, @attr1, can be numeric or string */
93 static void useattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
94 {
95  const char *s;
97  {
98  s = stringattr(ae->value.complex);
99  if (s)
100  {
101  if (!av->useattr)
102  av->useattr = s;
103  else
104  { /* already seen one, can't have duplicates */
106  av->errstring = "multiple use attributes";
107  }
108  }
109  else
110  { /* complex that did not return a string */
112  av->errstring = "non-string complex attribute";
113  }
114  }
115  else
116  { /* numeric - could translate 4 to 'title' etc */
117  sprintf(av->useattrbuff, ODR_INT_PRINTF, *ae->value.numeric);
118  av->useattr = av->useattrbuff;
119  }
120 }
121 
122 
123 static void numattr(Z_AttributeElement *ae, struct yaz_facet_attr *av,
124  int *v)
125 {
126  if (ae->which == Z_AttributeValue_numeric)
127  {
128  *v = *ae->value.numeric;
129  }
130  else
131  {
133  av->errstring = "non-numeric limit/sort/start attribute";
134  }
135 }
136 
137 /* Get the index to be searched from the attributes.
138  @attr 1
139  can be either "string", or some well-known value like
140  4 for title
141  Returns a null and sets errors in rr,
142  emtpy string if no attr found,
143  or the string itself - always a pointer to the Z-structs,
144  so no need to free that string!
145 */
146 
148  struct yaz_facet_attr *av)
149 {
150  int i;
151  Z_AttributeElement *ae;
152  for (i = 0; i < attributes->num_attributes; i++)
153  {
154  ae = attributes->attributes[i];
155  /* ignoring the attributeSet here */
156  if (*ae->attributeType == 1)
157  { /* use attribute */
158  useattr(ae, av);
159  }
160  else if (*ae->attributeType == 2)
161  {
162  numattr(ae, av, &av->sortorder);
163  }
164  else if (*ae->attributeType == 3)
165  {
166  numattr(ae, av, &av->limit);
167  }
168  else if (*ae->attributeType == 4)
169  {
170  numattr(ae, av, &av->start);
171  }
172  else
173  { /* unknown attribute */
175  sprintf(av->useattrbuff, ODR_INT_PRINTF,
176  *ae-> attributeType);
177  av->errstring = av->useattrbuff;
178  yaz_log(YLOG_WARN, "Unsupported attribute type %s",
179  av->useattrbuff);
180  /* would like to give a better message, but the standard */
181  /* tells me to return the attribute type */
182  }
183  if (av->errcode)
184  return; /* no need to dig deeper, return on first error */
185  }
186  return;
187 }
188 
190 {
191  Z_FacetTerm *facet_term = odr_malloc(odr, sizeof(*facet_term));
192  Z_Term *term = z_Term_create(odr, Z_Term_general, cstr, strlen(cstr));
193  facet_term->term = term;
194  facet_term->count = odr_intdup(odr, freq);
195  return facet_term;
196 }
197 
199  int num_terms)
200 {
201  Z_FacetField *facet_field = odr_malloc(odr, sizeof(*facet_field));
202  facet_field->attributes = attributes;
203  facet_field->num_terms = num_terms;
204  facet_field->terms = odr_malloc(odr, num_terms * sizeof(*facet_field->terms));
205  return facet_field;
206 }
207 
209  Z_FacetTerm *facet_term, int index)
210 {
211  assert(0 <= index && index < field->num_terms);
212  field->terms[index] = facet_term;
213 }
214 
216 {
217  Z_FacetList *facet_list = odr_malloc(odr, sizeof(*facet_list));
218  facet_list->num = num_facets;
219  facet_list->elements =
220  odr_malloc(odr, facet_list->num * sizeof(*facet_list->elements));
221  return facet_list;
222 }
223 
225  int index)
226 {
227  assert(0 <= index && index < list->num);
228  list->elements[index] = field;
229 }
230 
231 /*
232  * Local variables:
233  * c-basic-offset: 4
234  * c-file-style: "Stroustrup"
235  * indent-tabs-mode: nil
236  * End:
237  * vim: shiftwidth=4 tabstop=8 expandtab
238  */
239 
#define Z_AttributeValue_numeric
Definition: z-core.h:586
Z_AttributeList * attributes
Definition: z-facet-1.h:36
void yaz_facet_attr_get_z_attributes(const Z_AttributeList *attributes, struct yaz_facet_attr *av)
Definition: facet.c:147
#define Z_Term_general
Definition: z-core.h:547
#define Z_AttributeValue_complex
Definition: z-core.h:587
int errcode
Definition: facet.h:54
int start
Definition: facet.h:62
static void useattr(Z_AttributeElement *ae, struct yaz_facet_attr *av)
Definition: facet.c:93
Odr_int * numeric
Definition: z-core.h:584
Odr_oid * direct_reference
Definition: prt-ext.h:60
Standard Object Identifiers: Generated from ./oid.csv.
Header for PQF parsing.
nmem_int_t Odr_int
Definition: odr.h:47
Z_FacetList * facet_list_create(ODR odr, int num_facets)
Definition: facet.c:215
Z_AttributeElement ** attributes
Definition: z-core.h:533
#define Z_External_userFacets
Definition: prt-ext.h:100
void * odr_malloc(ODR o, size_t size)
Definition: odr_mem.c:31
int num_terms
Definition: z-facet-1.h:37
void yaz_oi_set_facetlist(Z_OtherInformation **otherInformation, ODR odr, Z_FacetList *facet_list)
Definition: facet.c:23
Header for OID database.
union Z_External::@30 u
int which
Definition: prt-ext.h:63
void facet_field_term_set(ODR odr, Z_FacetField *field, Z_FacetTerm *facet_term, int index)
Definition: facet.c:208
Header for Z39.50 OtherInfo utilities.
#define YAZ_BIB1_UNSUPP_ATTRIBUTE_COMBI
Definition: diagbib1.h:67
Definition: z-core.h:536
static void numattr(Z_AttributeElement *ae, struct yaz_facet_attr *av, int *v)
Definition: facet.c:123
union Z_AttributeElement::@53 value
static const char * stringattr(Z_ComplexAttribute *c)
Definition: facet.c:79
char * errstring
Definition: facet.h:55
#define Z_StringOrNumeric_string
Definition: z-core.h:1323
Z_FacetField * facet_field_create(ODR odr, Z_AttributeList *attributes, int num_terms)
Definition: facet.c:198
Z_External * externallyDefinedInfo
Definition: z-core.h:1285
char * descriptor
Definition: prt-ext.h:62
Odr_int * attributeType
Definition: z-core.h:581
Header for the facet utilities.
Z_OtherInformationUnit * yaz_oi_update(Z_OtherInformation **otherInformationP, ODR odr, const Odr_oid *oid, int categoryValue, int delete_flag)
Definition: otherinfo.c:76
Odr_int * indirect_reference
Definition: prt-ext.h:61
#define YAZ_BIB1_UNSUPP_ATTRIBUTE
Definition: diagbib1.h:146
#define ODR_INT_PRINTF
Definition: odr.h:49
Odr_int * odr_intdup(ODR o, Odr_int v)
Definition: odr_mem.c:51
Definition: odr.h:124
Z_FacetList * facetList
Definition: prt-ext.h:146
const char * useattr
Definition: facet.h:56
void yaz_facet_attr_init(struct yaz_facet_attr *attr_values)
Definition: facet.c:68
Z_InternationalString * string
Definition: z-core.h:1321
Odr_oid * odr_oiddup(ODR odr, const Odr_oid *o)
Definition: odr_util.c:60
Z_FacetField ** elements
Definition: z-facet-1.h:32
Z_Term * z_Term_create(ODR o, int term_type, const char *buf, size_t len)
Definition: pquery.c:323
union Z_StringOrNumeric::@75 u
Z_StringOrNumeric ** list
Definition: z-core.h:574
char useattrbuff[30]
Definition: facet.h:59
union Z_OtherInformationUnit::@74 information
#define YAZ_BIB1_UNSUPP_ATTRIBUTE_TYPE
Definition: diagbib1.h:57
int num_attributes
Definition: z-core.h:532
#define YLOG_WARN
log level: warning
Definition: log.h:46
structure for all known EXTERNALs
Definition: prt-ext.h:58
Odr_int * count
Definition: z-facet-1.h:43
#define Z_OtherInfo_externallyDefinedInfo
Definition: z-core.h:1289
Z_ComplexAttribute * complex
Definition: z-core.h:585
void yaz_log(int level, const char *fmt,...)
Writes log message.
Definition: log.c:485
Z_FacetTerm * facet_term_create_cstr(ODR odr, const char *cstr, Odr_int freq)
Definition: facet.c:189
Z_FacetList * yaz_oi_get_facetlist(Z_OtherInformation **otherInformation)
Definition: facet.c:44
const Odr_oid yaz_oid_userinfo_facet_1[]
Definition: oid_std.c:118
int sortorder
Definition: facet.h:60
Z_OtherInformationUnit ** list
Definition: z-core.h:1296
int limit
Definition: facet.h:61
void facet_list_field_set(ODR odr, Z_FacetList *list, Z_FacetField *field, int index)
Definition: facet.c:224
Diagnostics: Generated by csvtodiag.tcl from ./bib1.csv.
Z_Term * term
Definition: z-facet-1.h:42
Z_FacetTerm ** terms
Definition: z-facet-1.h:38