1 /* $Id: d1_if.c,v 1.2 2002-10-22 13:19:50 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
35 * Search for a token in the supplied string up to the supplied list of stop characters or EOL
36 * At the end, return the character causing the break and fill pTokenBuffer with the token string so far
37 * After the scan, *pPosInBuffer will point to the next character after the one causing the break and
38 * pTokenBuffer will contain the actual token
40 char data1_ScanNextToken(char* pBuffer,
43 char* pWhitespaceChars,
46 char* pBuff = pTokenBuffer;
49 while ( **pPosInBuffer )
51 if ( strchr(pBreakChars,**pPosInBuffer) != NULL )
53 /* Current character is a break character */
55 return *((*pPosInBuffer)++);
59 if ( strchr(pWhitespaceChars, **pPosInBuffer) != NULL )
62 *pBuff++ = *((*pPosInBuffer)++);
66 *pBuff++ = *((*pPosInBuffer)++);
67 return(**pPosInBuffer);
71 * Attempt to find a string value given the specified tagpath
73 * Need to make this safe by passing in a buffer.....
76 char *data1_getNodeValue(data1_node* node, char* pTagPath)
80 n = data1_LookupNode(node, pTagPath );
84 /* n should be a tag node with some data under it.... */
87 if ( n->child->which == DATA1N_data )
89 return n->child->u.data.data;
93 yaz_log(LOG_WARN,"Attempting to lookup data for tagpath: Child node is not a data node");
98 yaz_log(LOG_WARN,"Found a node matching the tagpath, but it has no child data nodes");
103 yaz_log(LOG_WARN,"Unable to lookup a node on the specified tag path");
110 /* Max length of a tag */
111 #define MAX_TAG_SIZE 50
114 * data1_LookupNode : Try and find a node as specified by a tagpath
116 data1_node *data1_LookupNode(data1_node* node, char* pTagPath)
118 /* Node matching the pattern in the tagpath */
119 data1_node* matched_node = NULL;
121 /* Current Child node as we search for nodes matching the pattern in the tagpath */
122 data1_node* current_child = node->child;
124 /* Current position in string */
125 char* pCurrCharInPath = pTagPath;
128 char Buffer[MAX_TAG_SIZE];
130 /* The tag type of this node */
133 /* for non string tags, the tag value */
136 /* for string tags, the tag value */
137 char StringTagVal[MAX_TAG_SIZE];
139 /* Which occurence of that tag under this node */
142 /* Character causing a break */
145 StringTagVal[0] = '\0';
147 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ",[(."," ", Buffer);
151 /* Next component in node value is [ TagType, TagVal, TagOccurence ] */
152 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ","," ", Buffer);
153 iTagType = atoi(Buffer);
155 /* Occurence is optional... */
156 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ",]."," ", Buffer);
159 strcpy(StringTagVal,Buffer);
161 iTagValue = atoi(Buffer);
163 /* If sepchar was a ',' there should be an instance */
166 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "]."," ", Buffer);
167 iOccurences = atoi(Buffer);
172 /* See if we can scan the . for the next component or the end of the line... */
173 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "."," ", Buffer);
177 yaz_log(LOG_FATAL,"Node does not end with a ]");
184 /* We have a TagName so Read up to ( or . or EOL */
186 strcpy(StringTagVal,Buffer);
190 /* Read the occurence */
191 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ")"," ", Buffer);
192 iOccurences = atoi(Buffer);
194 /* See if we can find the . at the end of this clause */
195 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "."," ", Buffer);
200 yaz_log(LOG_DEBUG,"search node for child like [%d,%d,%s,%d]",iTagType,iTagValue,StringTagVal,iOccurences);
203 /* OK.. We have extracted tagtype, Value and Occurence, see if we can find a node */
204 /* Under the current parent matching that description */
206 while ( ( current_child ) && ( matched_node == NULL ) )
208 if ( current_child->which == DATA1N_tag )
212 if ( ( current_child->u.tag.element == NULL ) &&
213 ( strcmp(current_child->u.tag.tag, StringTagVal) == 0 ) )
217 /* Everything matched, but not yet found the
218 right occurence of the given tag */
223 /* We have matched a string tag... Is there more to
225 matched_node = current_child;
229 else /* Attempt to match real element */
231 yaz_log(LOG_WARN,"Non string tag matching not yet implemented");
234 current_child = current_child->next;
238 /* If there is more... Continue */
239 if ( ( sepchr == '.' ) && ( matched_node ) )
241 return data1_LookupNode(matched_node, pCurrCharInPath);
251 data1_CountOccurences
253 Count the number of occurences of the last instance on a tagpath.
255 @param data1_node* node : The root of the tree we wish to look for occurences in
256 @param const char* pTagPath : The tagpath we want to count the occurences of...
259 int data1_CountOccurences(data1_node* node, char* pTagPath)
262 data1_node* n = NULL;
263 data1_node* pParent = NULL;
265 n = data1_LookupNode(node, pTagPath );
269 ( n->which == DATA1N_tag ) &&
272 data1_node* current_child;
275 for ( current_child = pParent->child;
277 current_child = current_child->next )
279 if ( current_child->which == DATA1N_tag )
281 if ( current_child->u.tag.element == NULL )
283 if ( ( n->u.tag.tag ) &&
284 ( current_child->u.tag.tag ) &&
285 ( strcmp(current_child->u.tag.tag, n->u.tag.tag) == 0 ) )
290 else if ( current_child->u.tag.element == n->u.tag.element )
292 /* Hmmm... Is the above right for non string tags???? */