YAZ  5.34.0
read-grs.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 
15 #include <yaz/proto.h>
16 #include <yaz/log.h>
17 
18 #include "ztest.h"
19 
20 #define GRS_MAX_FIELDS 50
21 
22 static Z_GenericRecord *read_grs1(FILE *f, ODR o)
23 {
24  char line[512], *buf;
25  int type, ivalue;
26  char value[512];
27  Z_GenericRecord *r = 0;
28 
29  for (;;)
30  {
31  Z_TaggedElement *t;
32  Z_ElementData *c;
33 
34  while (fgets(buf = line, 512, f))
35  {
36  while (*buf && yaz_isspace(*buf))
37  buf++;
38  if (!*buf || *buf == '#')
39  continue;
40  break;
41  }
42  if (*buf == '}')
43  return r;
44  if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
45  {
46  yaz_log(YLOG_WARN, "Bad data in '%s'", buf);
47  return 0;
48  }
49  if (!type && *value == '0')
50  return r;
51  if (!(buf = strchr(buf, ')')))
52  return 0;
53  buf++;
54  while (*buf && yaz_isspace(*buf))
55  buf++;
56  if (!*buf)
57  return 0;
58  if (!r)
59  {
60  r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
61  r->elements = (Z_TaggedElement **)
63  r->num_elements = 0;
64  }
65  r->elements[r->num_elements] = t = (Z_TaggedElement *)
66  odr_malloc(o, sizeof(Z_TaggedElement));
67  t->tagType = odr_intdup(o, type);
69  odr_malloc(o, sizeof(Z_StringOrNumeric));
70  if ((ivalue = atoi(value)))
71  {
73  t->tagValue->u.numeric = odr_intdup(o, ivalue);
74  }
75  else
76  {
78  t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
79  strcpy(t->tagValue->u.string, value);
80  }
81  t->tagOccurrence = 0;
82  t->metaData = 0;
83  t->appliedVariant = 0;
84  t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
85  if (*buf == '{')
86  {
88  c->u.subtree = read_grs1(f, o);
89  }
90  else
91  {
93  buf[strlen(buf)-1] = '\0';
94  c->u.string = odr_strdup(o, buf);
95  }
96  r->num_elements++;
97  }
98 }
99 
101 {
102  FILE *f = fopen("dummy-grs", "r");
103  char line[512];
104  Z_GenericRecord *r = 0;
105  int n;
106 
107  if (!f)
108  return 0;
109  while (fgets(line, 512, f))
110  if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
111  {
112  r = read_grs1(f, o);
113  break;
114  }
115  fclose(f);
116  return r;
117 }
118 
119 /*
120  * Local variables:
121  * c-basic-offset: 4
122  * c-file-style: "Stroustrup"
123  * indent-tabs-mode: nil
124  * End:
125  * vim: shiftwidth=4 tabstop=8 expandtab
126  */
127 
enum l_file_type type
Definition: log.c:47
void yaz_log(int level, const char *fmt,...)
Writes log message.
Definition: log.c:487
Logging utility.
#define YLOG_WARN
log level: warning
Definition: log.h:46
char * odr_strdup(ODR o, const char *str)
Definition: odr_mem.c:36
Odr_int * odr_intdup(ODR o, Odr_int v)
Definition: odr_mem.c:51
void * odr_malloc(ODR o, size_t size)
Definition: odr_mem.c:31
Header for Z39.50 Protocol.
static Z_GenericRecord * read_grs1(FILE *f, ODR o)
Definition: read-grs.c:22
#define GRS_MAX_FIELDS
Definition: read-grs.c:20
Z_GenericRecord * dummy_grs_record(int num, ODR o)
Definition: read-grs.c:100
union Z_ElementData::@101 u
int which
Definition: z-grs.h:69
Z_InternationalString * string
Definition: z-grs.h:75
Z_GenericRecord * subtree
Definition: z-grs.h:83
int num_elements
Definition: z-grs.h:55
Z_TaggedElement ** elements
Definition: z-grs.h:56
union Z_StringOrNumeric::@72 u
Odr_int * numeric
Definition: z-core.h:1322
Z_InternationalString * string
Definition: z-core.h:1321
Z_ElementData * content
Definition: z-grs.h:63
Z_Variant * appliedVariant
Definition: z-grs.h:65
Z_ElementMetaData * metaData
Definition: z-grs.h:64
Z_StringOrNumeric * tagValue
Definition: z-grs.h:61
Odr_int * tagType
Definition: z-grs.h:60
Odr_int * tagOccurrence
Definition: z-grs.h:62
Definition: odr.h:125
#define yaz_isspace(x)
Definition: yaz-iconv.h:87
#define Z_StringOrNumeric_numeric
Definition: z-core.h:1324
#define Z_StringOrNumeric_string
Definition: z-core.h:1323
#define Z_ElementData_subtree
Definition: z-grs.h:96
#define Z_ElementData_string
Definition: z-grs.h:88
Internal header for yaz-ztest.