1 /* $Id: d1_if.c,v 1.6 2005-01-15 19:38:18 adam Exp $
2 Copyright (C) 1995-2005
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
28 #include <idzebra/data1.h>
34 * Search for a token in the supplied string up to the supplied list of stop characters or EOL
35 * At the end, return the character causing the break and fill pTokenBuffer with the token string so far
36 * After the scan, *pPosInBuffer will point to the next character after the one causing the break and
37 * pTokenBuffer will contain the actual token
39 char data1_ScanNextToken(char* pBuffer,
42 char* pWhitespaceChars,
45 char* pBuff = pTokenBuffer;
48 while ( **pPosInBuffer )
50 if ( strchr(pBreakChars,**pPosInBuffer) != NULL )
52 /* Current character is a break character */
54 return *((*pPosInBuffer)++);
58 if ( strchr(pWhitespaceChars, **pPosInBuffer) != NULL )
61 *pBuff++ = *((*pPosInBuffer)++);
65 *pBuff++ = *((*pPosInBuffer)++);
66 return(**pPosInBuffer);
70 * Attempt to find a string value given the specified tagpath
72 * Need to make this safe by passing in a buffer.....
75 char *data1_getNodeValue(data1_node* node, char* pTagPath)
79 n = data1_LookupNode(node, pTagPath );
83 /* n should be a tag node with some data under it.... */
86 if ( n->child->which == DATA1N_data )
88 return n->child->u.data.data;
92 yaz_log(YLOG_WARN,"Attempting to lookup data for tagpath: Child node is not a data node");
97 yaz_log(YLOG_WARN,"Found a node matching the tagpath, but it has no child data nodes");
102 yaz_log(YLOG_WARN,"Unable to lookup a node on the specified tag path");
109 /* Max length of a tag */
110 #define MAX_TAG_SIZE 50
113 * data1_LookupNode : Try and find a node as specified by a tagpath
115 data1_node *data1_LookupNode(data1_node* node, char* pTagPath)
117 /* Node matching the pattern in the tagpath */
118 data1_node* matched_node = NULL;
120 /* Current Child node as we search for nodes matching the pattern in the tagpath */
121 data1_node* current_child = node->child;
123 /* Current position in string */
124 char* pCurrCharInPath = pTagPath;
127 char Buffer[MAX_TAG_SIZE];
129 /* The tag type of this node */
132 /* for non string tags, the tag value */
135 /* for string tags, the tag value */
136 char StringTagVal[MAX_TAG_SIZE];
138 /* Which occurence of that tag under this node */
141 /* Character causing a break */
144 StringTagVal[0] = '\0';
146 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ",[(."," ", Buffer);
150 /* Next component in node value is [ TagType, TagVal, TagOccurence ] */
151 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ","," ", Buffer);
152 iTagType = atoi(Buffer);
154 /* Occurence is optional... */
155 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ",]."," ", Buffer);
158 strcpy(StringTagVal,Buffer);
160 iTagValue = atoi(Buffer);
162 /* If sepchar was a ',' there should be an instance */
165 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "]."," ", Buffer);
166 iOccurences = atoi(Buffer);
171 /* See if we can scan the . for the next component or the end of the line... */
172 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "."," ", Buffer);
176 yaz_log(YLOG_FATAL,"Node does not end with a ]");
183 /* We have a TagName so Read up to ( or . or EOL */
185 strcpy(StringTagVal,Buffer);
189 /* Read the occurence */
190 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ")"," ", Buffer);
191 iOccurences = atoi(Buffer);
193 /* See if we can find the . at the end of this clause */
194 sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "."," ", Buffer);
199 yaz_log(YLOG_DEBUG,"search node for child like [%d,%d,%s,%d]",iTagType,iTagValue,StringTagVal,iOccurences);
202 /* OK.. We have extracted tagtype, Value and Occurence, see if we can find a node */
203 /* Under the current parent matching that description */
205 while ( ( current_child ) && ( matched_node == NULL ) )
207 if ( current_child->which == DATA1N_tag )
211 if ( ( current_child->u.tag.element == NULL ) &&
212 ( strcmp(current_child->u.tag.tag, StringTagVal) == 0 ) )
216 /* Everything matched, but not yet found the
217 right occurence of the given tag */
222 /* We have matched a string tag... Is there more to
224 matched_node = current_child;
228 else /* Attempt to match real element */
230 yaz_log(YLOG_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???? */