2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * d1_if.c : A simple interface for extracting strings from data1_node tree structures
9 * Revision 1.2 2000-01-04 17:46:17 ian
10 * Added function to count occurences of a tag spec in a data1 tree.
12 * Revision 1.1 1999/12/21 14:16:19 ian
13 * Changed retrieval module to allow data1 trees with no associated absyn.
14 * Also added a simple interface for extracting values from data1 trees using
15 * a string based tagpath.
25 #include <yaz/data1.h>
32 * Search for a token in the supplied string up to the supplied list of stop characters or EOL
33 * At the end, return the character causing the break and fill pTokenBuffer with the token string so far
34 * After the scan, *pPosInBuffer will point to the next character after the one causing the break and
35 * pTokenBuffer will contain the actual token
37 char data1_ScanNextToken(char* pBuffer,
40 char* pWhitespaceChars,
43 char* pBuff = pTokenBuffer;
46 while ( **pPosInBuffer )
48 if ( strchr(pBreakChars,**pPosInBuffer) != NULL )
50 /* Current character is a break character */
52 return *((*pPosInBuffer)++);
56 if ( strchr(pWhitespaceChars, **pPosInBuffer) != NULL )
59 *pBuff++ = *((*pPosInBuffer)++);
63 *pBuff++ = *((*pPosInBuffer)++);
64 return(**pPosInBuffer);
68 * Attempt to find a string value given the specified tagpath
70 * Need to make this safe by passing in a buffer.....
73 char *data1_getNodeValue(data1_node* node, char* pTagPath)
77 n = data1_LookupNode(node, pTagPath );
81 /* n should be a tag node with some data under it.... */
84 if ( n->child->which == DATA1N_data )
86 return n->child->u.data.data;
90 yaz_log(LOG_WARN,"Attempting to lookup data for tagpath: Child node is not a data node");
95 yaz_log(LOG_WARN,"Found a node matching the tagpath, but it has no child data nodes");
100 yaz_log(LOG_WARN,"Unable to lookup a node on the specified tag path");
108 * data1_LookupNode : Try and find a node as specified by a tagpath
110 data1_node *data1_LookupNode(data1_node* node, char* pTagPath)
112 /* Node matching the pattern in the tagpath */
113 data1_node* matched_node = NULL;
115 /* Current Child node as we search for nodes matching the pattern in the tagpath */
116 data1_node* current_child = node->child;
118 /* Max length of a tag */
121 /* Current position in string */
122 char* pCurrCharInPath = pTagPath;
125 char Buffer[iMaxTagSize];
127 /* The tag type of this node */
130 /* for non string tags, the tag value */
133 /* for string tags, the tag value */
134 char StringTagVal[iMaxTagSize];
136 /* Which occurence of that tag under this node */
139 /* Character causing a break */
142 StringTagVal[0] = '\0';
144 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ",[(."," ", Buffer);
148 /* Next component in node value is [ TagType, TagVal, TagOccurence ] */
149 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ","," ", Buffer);
150 iTagType = atoi(Buffer);
152 /* Occurence is optional... */
153 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ",]."," ", Buffer);
156 strcpy(StringTagVal,Buffer);
158 iTagValue = atoi(Buffer);
160 /* If sepchar was a ',' there should be an instance */
163 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "]."," ", Buffer);
164 iOccurences = atoi(Buffer);
169 /* See if we can scan the . for the next component or the end of the line... */
170 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "."," ", Buffer);
174 yaz_log(LOG_FATAL,"Node does not end with a ]");
181 /* We have a TagName so Read up to ( or . or EOL */
183 strcpy(StringTagVal,Buffer);
187 /* Read the occurence */
188 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ")"," ", Buffer);
189 iOccurences = atoi(Buffer);
191 /* See if we can find the . at the end of this clause */
192 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "."," ", Buffer);
197 yaz_log(LOG_DEBUG,"search node for child like [%d,%d,%s,%d]",iTagType,iTagValue,StringTagVal,iOccurences);
200 /* OK.. We have extracted tagtype, Value and Occurence, see if we can find a node */
201 /* Under the current parent matching that description */
203 while ( ( current_child ) && ( matched_node == NULL ) )
205 if ( current_child->which == DATA1N_tag )
209 if ( ( current_child->u.tag.element == NULL ) &&
210 ( strcmp(current_child->u.tag.tag, StringTagVal) == 0 ) )
214 // Everything matched, but not yet found the right occurence of the given tag
219 /* We have matched a string tag... Is there more to process? */
220 matched_node = current_child;
224 else /* Attempt to match real element */
226 yaz_log(LOG_WARN,"Non string tag matching not yet implemented");
229 current_child = current_child->next;
233 /* If there is more... Continue */
234 if ( ( sepchr == '.' ) && ( matched_node ) )
236 return data1_LookupNode(matched_node, pCurrCharInPath);
246 data1_CountOccurences
248 Count the number of occurences of the last instance on a tagpath.
250 @param data1_node* node : The root of the tree we wish to look for occurences in
251 @param const char* pTagPath : The tagpath we want to count the occurences of...
254 int data1_CountOccurences(data1_node* node, char* pTagPath)
257 data1_node* n = NULL;
258 data1_node* pParent = NULL;
260 n = data1_LookupNode(node, pTagPath );
264 ( n->which == DATA1N_tag ) &&
267 data1_node* current_child;
270 for ( current_child = pParent->child;
272 current_child = current_child->next )
274 if ( current_child->which == DATA1N_tag )
276 if ( current_child->u.tag.element == NULL )
278 if ( ( n->u.tag.tag ) &&
279 ( current_child->u.tag.tag ) &&
280 ( strcmp(current_child->u.tag.tag, n->u.tag.tag) == 0 ) )
285 else if ( current_child->u.tag.element == n->u.tag.element )
287 /* Hmmm... Is the above right for non string tags???? */