00001
00002
00003
00004
00005
00011 #include <stdlib.h>
00012 #include <string.h>
00013
00014 #include <yaz/cql.h>
00015
00016 void cql_fputs(const char *buf, void *client_data)
00017 {
00018 FILE *f = (FILE *) client_data;
00019 fputs(buf, f);
00020 }
00021
00022 struct cql_node *cql_node_dup (NMEM nmem, struct cql_node *cp)
00023 {
00024 struct cql_node *cn = 0;
00025
00026 if (!cp)
00027 return 0;
00028 switch (cp->which)
00029 {
00030 case CQL_NODE_ST:
00031 cn = cql_node_mk_sc(nmem, cp->u.st.index,
00032 cp->u.st.relation,
00033 cp->u.st.term);
00034 cn->u.st.modifiers = cql_node_dup(nmem, cp->u.st.modifiers);
00035 cn->u.st.index_uri = cp->u.st.index_uri ?
00036 nmem_strdup(nmem, cp->u.st.index_uri) : 0;
00037 cn->u.st.relation_uri = cp->u.st.relation_uri ?
00038 nmem_strdup(nmem, cp->u.st.relation_uri) : 0;
00039 break;
00040 case CQL_NODE_BOOL:
00041 cn = cql_node_mk_boolean(nmem, cp->u.boolean.value);
00042 cn->u.boolean.left = cql_node_dup(nmem, cp->u.boolean.left);
00043 cn->u.boolean.right = cql_node_dup(nmem, cp->u.boolean.right);
00044 }
00045 return cn;
00046 }
00047
00048 struct cql_node *cql_node_mk_sc(NMEM nmem,
00049 const char *index,
00050 const char *relation,
00051 const char *term)
00052 {
00053 struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
00054 p->which = CQL_NODE_ST;
00055 p->u.st.index = 0;
00056 if (index)
00057 p->u.st.index = nmem_strdup(nmem, index);
00058 p->u.st.index_uri = 0;
00059 p->u.st.term = 0;
00060 if (term)
00061 p->u.st.term = nmem_strdup(nmem, term);
00062 p->u.st.relation = 0;
00063 if (relation)
00064 p->u.st.relation = nmem_strdup(nmem, relation);
00065 p->u.st.relation_uri = 0;
00066 p->u.st.modifiers = 0;
00067 p->u.st.extra_terms = 0;
00068 return p;
00069 }
00070
00071 struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op)
00072 {
00073 struct cql_node *p = (struct cql_node *) nmem_malloc(nmem, sizeof(*p));
00074 p->which = CQL_NODE_BOOL;
00075 p->u.boolean.value = 0;
00076 if (op)
00077 p->u.boolean.value = nmem_strdup(nmem, op);
00078 p->u.boolean.left = 0;
00079 p->u.boolean.right = 0;
00080 p->u.boolean.modifiers = 0;
00081 return p;
00082 }
00083
00084 const char *cql_uri(void)
00085 {
00086 return "info:srw/cql-context-set/1/cql-v1.2";
00087 }
00088
00089 struct cql_node *cql_apply_prefix(NMEM nmem,
00090 struct cql_node *n, const char *prefix,
00091 const char *uri)
00092 {
00093 if (n->which == CQL_NODE_ST)
00094 {
00095 if (!n->u.st.index_uri && n->u.st.index)
00096 {
00097 const char *cp = strchr(n->u.st.index, '.');
00098 if (prefix && cp &&
00099 strlen(prefix) == (size_t) (cp - n->u.st.index) &&
00100 !cql_strncmp(n->u.st.index, prefix, strlen(prefix)))
00101 {
00102 char *nval = nmem_strdup(nmem, cp+1);
00103 n->u.st.index_uri = nmem_strdup(nmem, uri);
00104 n->u.st.index = nval;
00105 }
00106 else if (!prefix && !cp)
00107 {
00108 n->u.st.index_uri = nmem_strdup(nmem, uri);
00109 }
00110 }
00111 if (!n->u.st.relation_uri && n->u.st.relation)
00112 {
00113 const char *cp = strchr(n->u.st.relation, '.');
00114 if (prefix && cp &&
00115 strlen(prefix) == (size_t)(cp - n->u.st.relation) &&
00116 !cql_strncmp(n->u.st.relation, prefix, strlen(prefix)))
00117 {
00118 char *nval = nmem_strdup(nmem, cp+1);
00119 n->u.st.relation_uri = nmem_strdup(nmem, uri);
00120 n->u.st.relation = nval;
00121 }
00122 }
00123 }
00124 else if (n->which == CQL_NODE_BOOL)
00125 {
00126 cql_apply_prefix(nmem, n->u.boolean.left, prefix, uri);
00127 cql_apply_prefix(nmem, n->u.boolean.right, prefix, uri);
00128 }
00129 return n;
00130 }
00131
00132 void cql_node_destroy(struct cql_node *cn)
00133 {
00134 if (!cn)
00135 return;
00136 switch (cn->which)
00137 {
00138 case CQL_NODE_ST:
00139 cql_node_destroy(cn->u.st.modifiers);
00140 break;
00141 case CQL_NODE_BOOL:
00142 cql_node_destroy(cn->u.boolean.left);
00143 cql_node_destroy(cn->u.boolean.right);
00144 cql_node_destroy(cn->u.boolean.modifiers);
00145 }
00146 }
00147
00148 int cql_strcmp(const char *s1, const char *s2)
00149 {
00150 while (*s1 && *s2)
00151 {
00152 int c1 = *s1++;
00153 int c2 = *s2++;
00154 if (c1 >= 'A' && c1 <= 'Z')
00155 c1 = c1 + ('a' - 'A');
00156 if (c2 >= 'A' && c2 <= 'Z')
00157 c2 = c2 + ('a' - 'A');
00158 if (c1 != c2)
00159 return c1 - c2;
00160 }
00161 return *s1 - *s2;
00162 }
00163
00164 int cql_strncmp(const char *s1, const char *s2, size_t n)
00165 {
00166 while (*s1 && *s2 && n)
00167 {
00168 int c1 = *s1++;
00169 int c2 = *s2++;
00170 if (c1 >= 'A' && c1 <= 'Z')
00171 c1 = c1 + ('a' - 'A');
00172 if (c2 >= 'A' && c2 <= 'Z')
00173 c2 = c2 + ('a' - 'A');
00174 if (c1 != c2)
00175 return c1 - c2;
00176 --n;
00177 }
00178 if (!n)
00179 return 0;
00180 return *s1 - *s2;
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191