2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
5 * d1_if.c : A simple interface for extracting strings from data1_node tree structures
8 * Revision 1.4 2000-01-06 14:30:56 adam
9 * Minor change - to prevent warnings.
11 * Revision 1.3 2000/01/06 11:27:02 adam
12 * Minor fix so that this source compiles using Visual C++.
14 * Revision 1.2 2000/01/04 17:46:17 ian
15 * Added function to count occurences of a tag spec in a data1 tree.
17 * Revision 1.1 1999/12/21 14:16:19 ian
18 * Changed retrieval module to allow data1 trees with no associated absyn.
19 * Also added a simple interface for extracting values from data1 trees using
20 * a string based tagpath.
29 #include <yaz/data1.h>
36 * Search for a token in the supplied string up to the supplied list of stop characters or EOL
37 * At the end, return the character causing the break and fill pTokenBuffer with the token string so far
38 * After the scan, *pPosInBuffer will point to the next character after the one causing the break and
39 * pTokenBuffer will contain the actual token
41 char data1_ScanNextToken(char* pBuffer,
44 char* pWhitespaceChars,
47 char* pBuff = pTokenBuffer;
50 while ( **pPosInBuffer )
52 if ( strchr(pBreakChars,**pPosInBuffer) != NULL )
54 /* Current character is a break character */
56 return *((*pPosInBuffer)++);
60 if ( strchr(pWhitespaceChars, **pPosInBuffer) != NULL )
63 *pBuff++ = *((*pPosInBuffer)++);
67 *pBuff++ = *((*pPosInBuffer)++);
68 return(**pPosInBuffer);
72 * Attempt to find a string value given the specified tagpath
74 * Need to make this safe by passing in a buffer.....
77 char *data1_getNodeValue(data1_node* node, char* pTagPath)
81 n = data1_LookupNode(node, pTagPath );
85 /* n should be a tag node with some data under it.... */
88 if ( n->child->which == DATA1N_data )
90 return n->child->u.data.data;
94 yaz_log(LOG_WARN,"Attempting to lookup data for tagpath: Child node is not a data node");
99 yaz_log(LOG_WARN,"Found a node matching the tagpath, but it has no child data nodes");
104 yaz_log(LOG_WARN,"Unable to lookup a node on the specified tag path");
111 /* Max length of a tag */
112 #define MAX_TAG_SIZE 50
115 * data1_LookupNode : Try and find a node as specified by a tagpath
117 data1_node *data1_LookupNode(data1_node* node, char* pTagPath)
119 /* Node matching the pattern in the tagpath */
120 data1_node* matched_node = NULL;
122 /* Current Child node as we search for nodes matching the pattern in the tagpath */
123 data1_node* current_child = node->child;
125 /* Current position in string */
126 char* pCurrCharInPath = pTagPath;
129 char Buffer[MAX_TAG_SIZE];
131 /* The tag type of this node */
134 /* for non string tags, the tag value */
137 /* for string tags, the tag value */
138 char StringTagVal[MAX_TAG_SIZE];
140 /* Which occurence of that tag under this node */
143 /* Character causing a break */
146 StringTagVal[0] = '\0';
148 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ",[(."," ", Buffer);
152 /* Next component in node value is [ TagType, TagVal, TagOccurence ] */
153 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ","," ", Buffer);
154 iTagType = atoi(Buffer);
156 /* Occurence is optional... */
157 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ",]."," ", Buffer);
160 strcpy(StringTagVal,Buffer);
162 iTagValue = atoi(Buffer);
164 /* If sepchar was a ',' there should be an instance */
167 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "]."," ", Buffer);
168 iOccurences = atoi(Buffer);
173 /* See if we can scan the . for the next component or the end of the line... */
174 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "."," ", Buffer);
178 yaz_log(LOG_FATAL,"Node does not end with a ]");
185 /* We have a TagName so Read up to ( or . or EOL */
187 strcpy(StringTagVal,Buffer);
191 /* Read the occurence */
192 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ")"," ", Buffer);
193 iOccurences = atoi(Buffer);
195 /* See if we can find the . at the end of this clause */
196 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "."," ", Buffer);
201 yaz_log(LOG_DEBUG,"search node for child like [%d,%d,%s,%d]",iTagType,iTagValue,StringTagVal,iOccurences);
204 /* OK.. We have extracted tagtype, Value and Occurence, see if we can find a node */
205 /* Under the current parent matching that description */
207 while ( ( current_child ) && ( matched_node == NULL ) )
209 if ( current_child->which == DATA1N_tag )
213 if ( ( current_child->u.tag.element == NULL ) &&
214 ( strcmp(current_child->u.tag.tag, StringTagVal) == 0 ) )
218 // Everything matched, but not yet found the right occurence of the given tag
223 /* We have matched a string tag... Is there more to process? */
224 matched_node = current_child;
228 else /* Attempt to match real element */
230 yaz_log(LOG_WARN,"Non string tag matching not yet implemented");
233 current_child = current_child->next;
237 /* If there is more... Continue */
238 if ( ( sepchr == '.' ) && ( matched_node ) )
240 return data1_LookupNode(matched_node, pCurrCharInPath);
250 data1_CountOccurences
252 Count the number of occurences of the last instance on a tagpath.
254 @param data1_node* node : The root of the tree we wish to look for occurences in
255 @param const char* pTagPath : The tagpath we want to count the occurences of...
258 int data1_CountOccurences(data1_node* node, char* pTagPath)
261 data1_node* n = NULL;
262 data1_node* pParent = NULL;
264 n = data1_LookupNode(node, pTagPath );
268 ( n->which == DATA1N_tag ) &&
271 data1_node* current_child;
274 for ( current_child = pParent->child;
276 current_child = current_child->next )
278 if ( current_child->which == DATA1N_tag )
280 if ( current_child->u.tag.element == NULL )
282 if ( ( n->u.tag.tag ) &&
283 ( current_child->u.tag.tag ) &&
284 ( strcmp(current_child->u.tag.tag, n->u.tag.tag) == 0 ) )
289 else if ( current_child->u.tag.element == n->u.tag.element )
291 /* Hmmm... Is the above right for non string tags???? */