2 * Copyright (c) 1995-2002, Index Data.
3 * See the file LICENSE for details.
5 * d1_if.c : A simple interface for extracting strings from data1_node
8 * $Id: d1_if.c,v 1.1 2002-10-22 12:53:33 adam Exp $
23 * Search for a token in the supplied string up to the supplied list of stop characters or EOL
24 * At the end, return the character causing the break and fill pTokenBuffer with the token string so far
25 * After the scan, *pPosInBuffer will point to the next character after the one causing the break and
26 * pTokenBuffer will contain the actual token
28 char data1_ScanNextToken(char* pBuffer,
31 char* pWhitespaceChars,
34 char* pBuff = pTokenBuffer;
37 while ( **pPosInBuffer )
39 if ( strchr(pBreakChars,**pPosInBuffer) != NULL )
41 /* Current character is a break character */
43 return *((*pPosInBuffer)++);
47 if ( strchr(pWhitespaceChars, **pPosInBuffer) != NULL )
50 *pBuff++ = *((*pPosInBuffer)++);
54 *pBuff++ = *((*pPosInBuffer)++);
55 return(**pPosInBuffer);
59 * Attempt to find a string value given the specified tagpath
61 * Need to make this safe by passing in a buffer.....
64 char *data1_getNodeValue(data1_node* node, char* pTagPath)
68 n = data1_LookupNode(node, pTagPath );
72 /* n should be a tag node with some data under it.... */
75 if ( n->child->which == DATA1N_data )
77 return n->child->u.data.data;
81 yaz_log(LOG_WARN,"Attempting to lookup data for tagpath: Child node is not a data node");
86 yaz_log(LOG_WARN,"Found a node matching the tagpath, but it has no child data nodes");
91 yaz_log(LOG_WARN,"Unable to lookup a node on the specified tag path");
98 /* Max length of a tag */
99 #define MAX_TAG_SIZE 50
102 * data1_LookupNode : Try and find a node as specified by a tagpath
104 data1_node *data1_LookupNode(data1_node* node, char* pTagPath)
106 /* Node matching the pattern in the tagpath */
107 data1_node* matched_node = NULL;
109 /* Current Child node as we search for nodes matching the pattern in the tagpath */
110 data1_node* current_child = node->child;
112 /* Current position in string */
113 char* pCurrCharInPath = pTagPath;
116 char Buffer[MAX_TAG_SIZE];
118 /* The tag type of this node */
121 /* for non string tags, the tag value */
124 /* for string tags, the tag value */
125 char StringTagVal[MAX_TAG_SIZE];
127 /* Which occurence of that tag under this node */
130 /* Character causing a break */
133 StringTagVal[0] = '\0';
135 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ",[(."," ", Buffer);
139 /* Next component in node value is [ TagType, TagVal, TagOccurence ] */
140 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ","," ", Buffer);
141 iTagType = atoi(Buffer);
143 /* Occurence is optional... */
144 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ",]."," ", Buffer);
147 strcpy(StringTagVal,Buffer);
149 iTagValue = atoi(Buffer);
151 /* If sepchar was a ',' there should be an instance */
154 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "]."," ", Buffer);
155 iOccurences = atoi(Buffer);
160 /* See if we can scan the . for the next component or the end of the line... */
161 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "."," ", Buffer);
165 yaz_log(LOG_FATAL,"Node does not end with a ]");
172 /* We have a TagName so Read up to ( or . or EOL */
174 strcpy(StringTagVal,Buffer);
178 /* Read the occurence */
179 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ")"," ", Buffer);
180 iOccurences = atoi(Buffer);
182 /* See if we can find the . at the end of this clause */
183 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "."," ", Buffer);
188 yaz_log(LOG_DEBUG,"search node for child like [%d,%d,%s,%d]",iTagType,iTagValue,StringTagVal,iOccurences);
191 /* OK.. We have extracted tagtype, Value and Occurence, see if we can find a node */
192 /* Under the current parent matching that description */
194 while ( ( current_child ) && ( matched_node == NULL ) )
196 if ( current_child->which == DATA1N_tag )
200 if ( ( current_child->u.tag.element == NULL ) &&
201 ( strcmp(current_child->u.tag.tag, StringTagVal) == 0 ) )
205 /* Everything matched, but not yet found the
206 right occurence of the given tag */
211 /* We have matched a string tag... Is there more to
213 matched_node = current_child;
217 else /* Attempt to match real element */
219 yaz_log(LOG_WARN,"Non string tag matching not yet implemented");
222 current_child = current_child->next;
226 /* If there is more... Continue */
227 if ( ( sepchr == '.' ) && ( matched_node ) )
229 return data1_LookupNode(matched_node, pCurrCharInPath);
239 data1_CountOccurences
241 Count the number of occurences of the last instance on a tagpath.
243 @param data1_node* node : The root of the tree we wish to look for occurences in
244 @param const char* pTagPath : The tagpath we want to count the occurences of...
247 int data1_CountOccurences(data1_node* node, char* pTagPath)
250 data1_node* n = NULL;
251 data1_node* pParent = NULL;
253 n = data1_LookupNode(node, pTagPath );
257 ( n->which == DATA1N_tag ) &&
260 data1_node* current_child;
263 for ( current_child = pParent->child;
265 current_child = current_child->next )
267 if ( current_child->which == DATA1N_tag )
269 if ( current_child->u.tag.element == NULL )
271 if ( ( n->u.tag.tag ) &&
272 ( current_child->u.tag.tag ) &&
273 ( strcmp(current_child->u.tag.tag, n->u.tag.tag) == 0 ) )
278 else if ( current_child->u.tag.element == n->u.tag.element )
280 /* Hmmm... Is the above right for non string tags???? */