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.1 1999-12-21 14:16:19 ian
10 * Changed retrieval module to allow data1 trees with no associated absyn.
11 * Also added a simple interface for extracting values from data1 trees using
12 * a string based tagpath.
22 #include <yaz/data1.h>
29 * Search for a token in the supplied string up to the supplied list of stop characters or EOL
30 * At the end, return the character causing the break and fill pTokenBuffer with the token string so far
31 * After the scan, *pPosInBuffer will point to the next character after the one causing the break and
32 * pTokenBuffer will contain the actual token
34 char data1_ScanNextToken(char* pBuffer,
37 char* pWhitespaceChars,
40 char* pBuff = pTokenBuffer;
43 while ( **pPosInBuffer )
45 if ( strchr(pBreakChars,**pPosInBuffer) != NULL )
47 /* Current character is a break character */
49 return *((*pPosInBuffer)++);
53 if ( strchr(pWhitespaceChars, **pPosInBuffer) != NULL )
56 *pBuff++ = *((*pPosInBuffer)++);
60 *pBuff++ = *((*pPosInBuffer)++);
61 return(**pPosInBuffer);
65 * Attempt to find a string value given the specified tagpath
67 * Need to make this safe by passing in a buffer.....
70 char *data1_getNodeValue(data1_node* node, char* pTagPath)
74 n = data1_LookupNode(node, pTagPath );
78 /* n should be a tag node with some data under it.... */
81 if ( n->child->which == DATA1N_data )
83 return n->child->u.data.data;
87 yaz_log(LOG_WARN,"Attempting to lookup data for tagpath: Child node is not a data node");
92 yaz_log(LOG_WARN,"Found a node matching the tagpath, but it has no child data nodes");
97 yaz_log(LOG_WARN,"Unable to lookup a node on the specified tag path");
105 * data1_LookupNode : Try and find a node as specified by a tagpath
107 data1_node *data1_LookupNode(data1_node* node, char* pTagPath)
109 /* Node matching the pattern in the tagpath */
110 data1_node* matched_node = NULL;
112 /* Current Child node as we search for nodes matching the pattern in the tagpath */
113 data1_node* current_child = node->child;
115 /* Max length of a tag */
118 /* Current position in string */
119 char* pCurrCharInPath = pTagPath;
122 char Buffer[iMaxTagSize];
124 /* The tag type of this node */
127 /* for non string tags, the tag value */
130 /* for string tags, the tag value */
131 char StringTagVal[iMaxTagSize];
133 /* Which occurence of that tag under this node */
136 /* Character causing a break */
139 StringTagVal[0] = '\0';
141 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ",[(."," ", Buffer);
145 /* Next component in node value is [ TagType, TagVal, TagOccurence ] */
146 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ","," ", Buffer);
147 iTagType = atoi(Buffer);
149 /* Occurence is optional... */
150 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ",]."," ", Buffer);
153 strcpy(StringTagVal,Buffer);
155 iTagValue = atoi(Buffer);
157 /* If sepchar was a ',' there should be an instance */
160 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "]."," ", Buffer);
161 iOccurences = atoi(Buffer);
166 /* See if we can scan the . for the next component or the end of the line... */
167 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "."," ", Buffer);
171 yaz_log(LOG_FATAL,"Node does not end with a ]");
178 /* We have a TagName so Read up to ( or . or EOL */
180 strcpy(StringTagVal,Buffer);
184 /* Read the occurence */
185 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ")"," ", Buffer);
186 iOccurences = atoi(Buffer);
188 /* See if we can find the . at the end of this clause */
189 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "."," ", Buffer);
194 yaz_log(LOG_DEBUG,"search node for child like [%d,%d,%s,%d]",iTagType,iTagValue,StringTagVal,iOccurences);
197 /* OK.. We have extracted tagtype, Value and Occurence, see if we can find a node */
198 /* Under the current parent matching that description */
200 while ( ( current_child ) && ( matched_node == NULL ) )
202 if ( current_child->which == DATA1N_tag )
206 if ( ( current_child->u.tag.element == NULL ) &&
207 ( strcmp(current_child->u.tag.tag, StringTagVal) == 0 ) )
211 // Everything matched, but not yet found the right occurence of the given tag
216 /* We have matched a string tag... Is there more to process? */
217 matched_node = current_child;
221 else /* Attempt to match real element */
223 yaz_log(LOG_WARN,"Non string tag matching not yet implemented");
226 current_child = current_child->next;
230 /* If there is more... Continue */
231 if ( ( sepchr == '.' ) && ( matched_node ) )
233 return data1_LookupNode(matched_node, pCurrCharInPath);