IDZEBRA  2.1.2
d1_soif.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) Index Data
3 
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8 
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 
18 */
19 
20 
21 /*
22  * This module generates SOIF (Simple Object Interchange Format) records
23  * from d1-nodes. nested elements are flattened out, depth first, by
24  * concatenating the tag names at each level.
25  */
26 
27 #if HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include <yaz/wrbuf.h>
31 #include <idzebra/data1.h>
32 
33 static int nodetoelement(data1_node *n, int select, char *prefix, WRBUF b)
34 {
35  data1_node *c;
36  char tmp[1024];
37 
38  for (c = n->child; c; c = c->next)
39  {
40  char *tag;
41 
42  if (c->which == DATA1N_tag)
43  {
44  if (select && !c->u.tag.node_selected)
45  continue;
46  if (c->u.tag.element && c->u.tag.element->tag)
47  tag = c->u.tag.element->tag->names->name; /* first name */
48  else
49  tag = c->u.tag.tag; /* local string tag */
50 
51  if (*prefix)
52  sprintf(tmp, "%s-%s", prefix, tag);
53  else
54  strcpy(tmp, tag);
55 
56  if (nodetoelement(c, select, tmp, b) < 0)
57  return 0;
58  }
59  else if (c->which == DATA1N_data)
60  {
61  char *p = c->u.data.data;
62  int l = c->u.data.len;
63 
64  wrbuf_write(b, prefix, strlen(prefix));
65 
66  sprintf(tmp, "{%d}:\t", l);
67  wrbuf_write(b, tmp, strlen(tmp));
68  wrbuf_write(b, p, l);
69  wrbuf_putc(b, '\n');
70  }
71  }
72  return 0;
73 }
74 
75 char *data1_nodetosoif (data1_handle dh, data1_node *n, int select, int *len)
76 {
77  WRBUF b = data1_get_wrbuf (dh);
78  char buf[128];
79 
80  wrbuf_rewind(b);
81 
82  if (n->which != DATA1N_root)
83  return 0;
84  sprintf(buf, "@%s{\n", n->u.root.type);
85  wrbuf_write(b, buf, strlen(buf));
86  if (nodetoelement(n, select, "", b))
87  return 0;
88  wrbuf_write(b, "}\n", 2);
89  *len = wrbuf_len(b);
90  return wrbuf_buf(b);
91 }
92 /*
93  * Local variables:
94  * c-basic-offset: 4
95  * c-file-style: "Stroustrup"
96  * indent-tabs-mode: nil
97  * End:
98  * vim: shiftwidth=4 tabstop=8 expandtab
99  */
100 
#define DATA1N_tag
Definition: data1.h:276
#define DATA1N_root
Definition: data1.h:274
WRBUF data1_get_wrbuf(data1_handle dp)
Definition: d1_handle.c:106
char * data1_nodetosoif(data1_handle dh, data1_node *n, int select, int *len)
Definition: d1_soif.c:75
static int nodetoelement(data1_node *n, int select, char *prefix, WRBUF b)
Definition: d1_soif.c:33
char * tag
Definition: data1.h:296
union data1_node::@2 u
struct data1_node::@2::@3 root
struct data1_node * child
Definition: data1.h:341
int which
Definition: data1.h:285
struct data1_node * next
Definition: data1.h:340
char * data
Definition: data1.h:307
#define DATA1N_data
Definition: data1.h:278