IDZEBRA  2.2.7
d1_sutrs.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 /* converts data1 tree to SUTRS record */
21 
22 #if HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 #include <idzebra/data1.h>
26 
27 #define NTOBUF_INDENT 2
28 #define NTOBUF_MARGIN 75
29 
30 static int wordlen(char *b, int i)
31 {
32  int l = 0;
33 
34  while (i && !d1_isspace(*b))
35  l++, b++, i--;
36  return l;
37 }
38 
39 static int nodetobuf(data1_node *n, int select, WRBUF b, int indent, int col)
40 {
41  data1_node *c;
42 
43  for (c = n->child; c; c = c->next)
44  {
45  char *tag;
46 
47  if (c->which == DATA1N_tag)
48  {
49  if (select && !c->u.tag.node_selected)
50  continue;
51  if (c->u.tag.element && c->u.tag.element->tag)
52  tag = c->u.tag.element->tag->names->name; /* first name */
53  else
54  tag = c->u.tag.tag; /* local string tag */
55  if (data1_matchstr(tag, "wellknown")) /* skip wellknown */
56  {
57  if (col)
58  wrbuf_putc(b, '\n');
59  wrbuf_printf(b, "%*s%s:", indent * NTOBUF_INDENT, "", tag);
60  col = indent * NTOBUF_INDENT + 1 + strlen(tag);
61  }
62  if (nodetobuf(c, select, b, indent+1, col) < 0)
63  return 0;
64  }
65  else if (c->which == DATA1N_data)
66  {
67  char *p = c->u.data.data;
68  int l = c->u.data.len;
69  int first = 0;
70 
71  if ((c->u.data.what == DATA1I_text ||
72  c->u.data.what == DATA1I_xmltext) && c->u.data.formatted_text)
73  {
74  wrbuf_putc(b, '\n');
75  wrbuf_write(b, c->u.data.data, c->u.data.len);
76  wrbuf_printf(b, "%*s", indent * NTOBUF_INDENT, "");
77  col = indent * NTOBUF_INDENT;
78  }
79  else if (c->u.data.what == DATA1I_text ||
80  c->u.data.what == DATA1I_xmltext)
81  {
82  while (l)
83  {
84  int wlen;
85 
86  while (l && d1_isspace(*p))
87  p++, l--;
88  if (!l)
89  break;
90  /* break if we'll cross margin and word is not too long */
91  if (col + (wlen = wordlen(p, l)) > NTOBUF_MARGIN && wlen <
93  {
94  wrbuf_printf(b, "\n%*s", indent * NTOBUF_INDENT, "");
95  col = indent * NTOBUF_INDENT;
96  first = 1;
97  }
98  if (!first)
99  {
100  wrbuf_putc(b, ' ');
101  col++;
102  }
103  while (l && !d1_isspace(*p))
104  {
105  if (col > NTOBUF_MARGIN)
106  {
107  wrbuf_putc(b, '=');
108  wrbuf_putc(b, '\n');
109  wrbuf_printf(b, "%*s", indent * NTOBUF_INDENT, "");
110  col = indent * NTOBUF_INDENT;
111  }
112  wrbuf_putc(b, *p);
113  p++;
114  l--;
115  col++;
116  }
117  first = 0;
118  }
119  }
120  else if (c->u.data.what == DATA1I_num)
121  {
122  wrbuf_putc(b, ' ');
123  wrbuf_write(b, c->u.data.data, c->u.data.len);
124  }
125  }
126  }
127  return 0;
128 }
129 
130 /*
131  * Return area containing SUTRS-formatted data. Ownership of this data
132  * remains in this module, and the buffer is reused on next call. This may
133  * need changing.
134  */
135 
136 char *data1_nodetobuf(data1_handle dh, data1_node *n, int select, int *len)
137 {
138  WRBUF b = data1_get_wrbuf(dh);
139 
140  wrbuf_rewind(b);
141  if (nodetobuf(n, select, b, 0, 0))
142  return 0;
143  wrbuf_putc(b, '\n');
144  *len = wrbuf_len(b);
145  return wrbuf_buf(b);
146 }
147 /*
148  * Local variables:
149  * c-basic-offset: 4
150  * c-file-style: "Stroustrup"
151  * indent-tabs-mode: nil
152  * End:
153  * vim: shiftwidth=4 tabstop=8 expandtab
154  */
155 
#define NTOBUF_INDENT
Definition: d1_sutrs.c:27
#define NTOBUF_MARGIN
Definition: d1_sutrs.c:28
char * data1_nodetobuf(data1_handle dh, data1_node *n, int select, int *len)
Definition: d1_sutrs.c:136
static int wordlen(char *b, int i)
Definition: d1_sutrs.c:30
static int nodetobuf(data1_node *n, int select, WRBUF b, int indent, int col)
Definition: d1_sutrs.c:39
static void indent(WRBUF b, int col)
Definition: d1_write.c:43
WRBUF data1_get_wrbuf(data1_handle dp)
Definition: d1_handle.c:102
#define data1_matchstr(s1, s2)
Definition: data1.h:36
#define DATA1N_tag
Definition: data1.h:276
#define DATA1N_data
Definition: data1.h:278
#define d1_isspace(c)
Definition: data1.h:31
#define DATA1I_num
Definition: data1.h:316
#define DATA1I_text
Definition: data1.h:314
#define DATA1I_xmltext
Definition: data1.h:320
struct data1_node * child
Definition: data1.h:341
char * tag
Definition: data1.h:296
char * data
Definition: data1.h:307
struct data1_node * next
Definition: data1.h:340
union data1_node::@2 u
int which
Definition: data1.h:285