File: read-grs.c

package info (click to toggle)
yaz 2.1.18-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 11,988 kB
  • ctags: 11,045
  • sloc: xml: 109,719; ansic: 41,566; sh: 10,625; makefile: 1,115; tcl: 380; yacc: 288
file content (125 lines) | stat: -rw-r--r-- 3,162 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (C) 1995-2005, Index Data ApS
 * See the file LICENSE for details.
 *
 * $Id: read-grs.c,v 1.13 2005/06/25 15:46:09 adam Exp $
 */

/*
 * Little toy-thing to read a GRS-1 records from a file.
 */

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

#include <yaz/proto.h>
#include <yaz/log.h>

#define GRS_MAX_FIELDS 50

static Z_GenericRecord *read_grs1(FILE *f, ODR o)
{
    char line[512], *buf;
    int type, ivalue;
    char value[512];
    Z_GenericRecord *r = 0;

    for (;;)
    {
        Z_TaggedElement *t;
        Z_ElementData *c;

        while (fgets(buf = line, 512, f))
        {
            while (*buf && isspace(*(unsigned char *) buf))
                buf++;
            if (!*buf || *buf == '#')
                continue;
            break;
        }
        if (*buf == '}')
            return r;
        if (sscanf(buf, "(%d,%[^)])", &type, value) != 2)
        {
            yaz_log(YLOG_WARN, "Bad data in '%s'", buf);
            return 0;
        }
        if (!type && *value == '0')
            return r;
        if (!(buf = strchr(buf, ')')))
            return 0;
        buf++;
        while (*buf && isspace(*(unsigned char *) buf))
            buf++;
        if (!*buf)
            return 0;
        if (!r)
        {
            r = (Z_GenericRecord *)odr_malloc(o, sizeof(*r));
            r->elements = (Z_TaggedElement **)
                odr_malloc(o, sizeof(Z_TaggedElement*) * GRS_MAX_FIELDS);
            r->num_elements = 0;
        }
        r->elements[r->num_elements] = t = (Z_TaggedElement *)
            odr_malloc(o, sizeof(Z_TaggedElement));
        t->tagType = odr_intdup(o, type);
        t->tagValue = (Z_StringOrNumeric *)
            odr_malloc(o, sizeof(Z_StringOrNumeric));
        if ((ivalue = atoi(value)))
        {
            t->tagValue->which = Z_StringOrNumeric_numeric;
            t->tagValue->u.numeric = odr_intdup(o, ivalue);
        }
        else
        {
            t->tagValue->which = Z_StringOrNumeric_string;
            t->tagValue->u.string = (char *)odr_malloc(o, strlen(value)+1);
            strcpy(t->tagValue->u.string, value);
        }
        t->tagOccurrence = 0;
        t->metaData = 0;
        t->appliedVariant = 0;
        t->content = c = (Z_ElementData *)odr_malloc(o, sizeof(Z_ElementData));
        if (*buf == '{')
        {
            c->which = Z_ElementData_subtree;
            c->u.subtree = read_grs1(f, o);
        }
        else
        {
            c->which = Z_ElementData_string;
            buf[strlen(buf)-1] = '\0';
            c->u.string = odr_strdup(o, buf);
        }
        r->num_elements++;
    }
}

Z_GenericRecord *dummy_grs_record (int num, ODR o)
{
    FILE *f = fopen("dummy-grs", "r");
    char line[512];
    Z_GenericRecord *r = 0;
    int n;

    if (!f)
        return 0;
    while (fgets(line, 512, f))
        if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
        {
            r = read_grs1(f, o);
            break;
        }
    fclose(f);
    return r;
}

/*
 * Local variables:
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 * vim: shiftwidth=4 tabstop=8 expandtab
 */