Minor fix so that this source compiles using Visual C++.
[yaz-moved-to-github.git] / retrieval / d1_if.c
1 /*
2  * Copyright (c) 1995-1999, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * d1_if.c : A simple interface for extracting strings from data1_node tree structures
7  *
8  * $Log: d1_if.c,v $
9  * Revision 1.3  2000-01-06 11:27:02  adam
10  * Minor fix so that this source compiles using Visual C++.
11  *
12  * Revision 1.2  2000/01/04 17:46:17  ian
13  * Added function to count occurences of a tag spec in a data1 tree.
14  *
15  * Revision 1.1  1999/12/21 14:16:19  ian
16  * Changed retrieval module to allow data1 trees with no associated absyn.
17  * Also added a simple interface for extracting values from data1 trees using
18  * a string based tagpath.
19  *
20  *
21  */
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <yaz/data1.h>
29 #include <yaz/log.h>
30
31 #include <string.h>
32
33
34 /*
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
39  */
40 char data1_ScanNextToken(char* pBuffer,
41                          char** pPosInBuffer,
42                          char* pBreakChars,
43                          char* pWhitespaceChars,
44                          char* pTokenBuffer)
45 {
46     char* pBuff = pTokenBuffer;
47     *pBuff = '\0';
48
49     while ( **pPosInBuffer )
50     {
51         if ( strchr(pBreakChars,**pPosInBuffer) != NULL )
52         {
53             /* Current character is a break character */
54             *pBuff++ = '\0';
55             return *((*pPosInBuffer)++);
56         }
57         else
58         {
59             if ( strchr(pWhitespaceChars, **pPosInBuffer) != NULL )
60                 *pPosInBuffer++;
61             else
62                 *pBuff++ = *((*pPosInBuffer)++);
63         }
64     }
65
66     *pBuff++ = *((*pPosInBuffer)++);
67     return(**pPosInBuffer);
68 }
69
70 /* 
71  * Attempt to find a string value given the specified tagpath
72  * 
73  * Need to make this safe by passing in a buffer..... 
74  *
75  */
76 char *data1_getNodeValue(data1_node* node, char* pTagPath)
77 {
78     data1_node* n = NULL;
79
80     n = data1_LookupNode(node, pTagPath );
81
82     if ( n )
83     {
84         /* n should be a tag node with some data under it.... */
85         if ( n->child )
86         {
87             if ( n->child->which == DATA1N_data )
88             {
89                 return n->child->u.data.data;
90             }
91             else
92             {
93                 yaz_log(LOG_WARN,"Attempting to lookup data for tagpath: Child node is not a data node");
94             }
95         }
96         else
97         {
98             yaz_log(LOG_WARN,"Found a node matching the tagpath, but it has no child data nodes");
99         }
100     }
101     else
102     {
103         yaz_log(LOG_WARN,"Unable to lookup a node on the specified tag path");
104     }
105
106     return "";
107 }
108
109
110 #define MAX_TAG_SIZE 50
111 /* 
112  * data1_LookupNode : Try and find a node as specified by a tagpath
113  */
114 data1_node *data1_LookupNode(data1_node* node, char* pTagPath)
115 {
116     /* Node matching the pattern in the tagpath */
117     data1_node* matched_node = NULL;
118
119     /* Current Child node as we search for nodes matching the pattern in the tagpath */
120     data1_node* current_child = node->child;
121
122     /* Max length of a tag */
123     int iMaxTagSize=50;
124
125     /* Current position in string */
126     char* pCurrCharInPath = pTagPath;
127
128     /* Work buffer */
129     char Buffer[MAX_TAG_SIZE];
130
131     /* The tag type of this node */
132     int iTagType = 0;
133
134     /* for non string tags, the tag value */
135     int iTagValue = 0;
136
137     /* for string tags, the tag value */
138     char StringTagVal[MAX_TAG_SIZE];
139
140     /* Which occurence of that tag under this node */
141     int iOccurences=0;
142
143     /* Character causing a break */
144     char sepchr = '\0';
145     Buffer[0] = '\0';
146     StringTagVal[0] = '\0';
147
148     sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ",[(."," ", Buffer);
149
150     if ( sepchr == '[' )
151     {
152         /* Next component in node value is [ TagType, TagVal, TagOccurence ] */
153         sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ","," ", Buffer);
154         iTagType = atoi(Buffer);
155
156         /* Occurence is optional... */
157         sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ",]."," ", Buffer);
158
159         if ( iTagType == 3 )
160             strcpy(StringTagVal,Buffer);
161         else
162             iTagValue = atoi(Buffer);
163
164         /* If sepchar was a ',' there should be an instance */
165         if ( sepchr == ',' )
166         {
167             sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "]."," ", Buffer);
168             iOccurences = atoi(Buffer);
169         }
170
171         if ( sepchr == ']' )
172         {
173             /* See if we can scan the . for the next component or the end of the line... */
174             sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "."," ", Buffer);
175         }
176         else
177         {
178             yaz_log(LOG_FATAL,"Node does not end with a ]");
179             /* Fatal Error */
180             return(NULL);
181         }
182     }
183     else
184     {
185         /* We have a TagName so Read up to ( or . or EOL */
186         iTagType = 3;
187         strcpy(StringTagVal,Buffer);
188
189         if ( sepchr == '(' )
190         {
191             /* Read the occurence */
192             sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, ")"," ", Buffer);
193             iOccurences = atoi(Buffer);
194
195             /* See if we can find the . at the end of this clause */
196             sepchr = data1_ScanNextToken(pTagPath, &pCurrCharInPath, "."," ", Buffer);
197         }
198         
199     }
200
201     yaz_log(LOG_DEBUG,"search node for child like [%d,%d,%s,%d]",iTagType,iTagValue,StringTagVal,iOccurences);
202     
203
204     /* OK.. We have extracted tagtype, Value and Occurence, see if we can find a node */
205     /* Under the current parent matching that description                             */
206
207     while ( ( current_child ) && ( matched_node == NULL ) )
208     {
209         if ( current_child->which == DATA1N_tag )
210         {
211             if ( iTagType == 3 )
212             {
213                 if ( ( current_child->u.tag.element == NULL ) &&
214                      ( strcmp(current_child->u.tag.tag, StringTagVal) == 0 ) )
215                 {
216                     if ( iOccurences )
217                     {
218                         // Everything matched, but not yet found the right occurence of the given tag
219                         iOccurences--;
220                     }
221                     else
222                     {
223                         /* We have matched a string tag... Is there more to process? */
224                         matched_node = current_child;
225                     }
226                 }
227             }
228             else /* Attempt to match real element */
229             {
230                 yaz_log(LOG_WARN,"Non string tag matching not yet implemented");
231             }
232         }
233         current_child = current_child->next;
234     }
235
236
237     /* If there is more... Continue */
238     if ( ( sepchr == '.' ) && ( matched_node ) )
239     {
240         return data1_LookupNode(matched_node, pCurrCharInPath);
241     }
242     else
243     {
244         return matched_node;
245     }
246 }
247
248 /**
249
250 data1_CountOccurences
251
252 Count the number of occurences of the last instance on a tagpath.
253
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... 
256
257 */
258 int data1_CountOccurences(data1_node* node, char* pTagPath)
259 {
260     int iRetVal = 0;
261     data1_node* n = NULL;
262     data1_node* pParent = NULL;
263
264     n = data1_LookupNode(node, pTagPath );
265
266
267     if ( ( n ) &&
268          ( n->which == DATA1N_tag ) &&
269          ( n->parent ) )
270     {
271         data1_node* current_child;
272         pParent = n->parent;
273
274         for ( current_child = pParent->child;
275               current_child;
276               current_child = current_child->next )
277         {
278             if ( current_child->which == DATA1N_tag )
279             {
280                 if ( current_child->u.tag.element == NULL )
281                 {
282                     if ( ( n->u.tag.tag ) &&
283                          ( current_child->u.tag.tag ) &&
284                          ( strcmp(current_child->u.tag.tag, n->u.tag.tag) == 0 ) )
285                     {
286                         iRetVal++;
287                     }
288                 }
289                 else if ( current_child->u.tag.element == n->u.tag.element )
290                 {
291                     /* Hmmm... Is the above right for non string tags???? */
292                     iRetVal++;
293                 }
294             }
295         }
296     }
297
298     return iRetVal;
299 }