Added grs.c. new version.
[ir-tcl-moved-to-github.git] / grs.c
1 /*
2  * IR toolkit for tcl/tk
3  * (c) Index Data 1995
4  * See the file LICENSE for details.
5  * Sebastian Hammer, Adam Dickmeiss
6  *
7  * $Log: grs.c,v $
8  * Revision 1.1  1995-08-29 15:38:34  adam
9  * Added grs.c. new version.
10  *
11  */
12
13 #include <stdlib.h>
14 #include <stdio.h>
15 #include <ctype.h>
16 #include <assert.h>
17
18 #include "ir-tclp.h"
19
20 void ir_tcl_read_grs (Z_GenericRecord *r, IrTcl_GRS_Record **grs_record)
21 {
22     int i;
23     struct GRS_Record_entry *e;
24
25     *grs_record = NULL;
26     if (!r)
27         return;
28     *grs_record = ir_tcl_malloc (sizeof(**grs_record));
29     if (!((*grs_record)->noTags = r->num_elements))
30     {
31         (*grs_record)->entries = NULL;
32         return;
33     }
34     e = (*grs_record)->entries = ir_tcl_malloc (r->num_elements *
35                                                 sizeof(*e));
36     for (i = 0; i < r->num_elements; i++, e++)
37     {
38         Z_TaggedElement *t;
39
40         t = r->elements[i];
41         if (t->tagType)
42             e->tagType = *t->tagType;
43         else
44             e->tagType = 0;
45         e->tagWhich = t->tagValue->which;
46         if (t->tagValue->which == Z_StringOrNumeric_numeric)
47             e->tagVal.num = *t->tagValue->u.numeric;
48         else
49             ir_tcl_strdup (NULL, &e->tagVal.str, t->tagValue->u.string);
50         e->dataWhich = t->content->which;
51
52         if (t->content->which == Z_ElementData_subtree)
53             ir_tcl_read_grs (t->content->u.subtree, &e->tagData.sub);
54         else if (t->content->which == Z_ElementData_string)
55             ir_tcl_strdup (NULL, &e->tagData.str, t->content->u.string);
56     }
57 }
58
59 static int ir_tcl_get_grs_r (Tcl_Interp *interp, IrTcl_GRS_Record *grs_record,
60                              int argc, char **argv, int argno)
61 {
62     static char tmpbuf[32];
63     int i;
64     struct GRS_Record_entry *e = grs_record->entries;
65
66     if (argno >= argc)
67     {
68         Tcl_AppendResult (interp, "{ ", NULL);
69
70         for (i = 0; i<grs_record->noTags; i++, e++)
71         {
72
73             Tcl_AppendResult (interp, "{ ", NULL);
74             sprintf (tmpbuf, "%d", e->tagType);
75             Tcl_AppendElement (interp, tmpbuf);
76
77             if (e->tagWhich == Z_StringOrNumeric_numeric)
78             {
79                 Tcl_AppendElement (interp, "N");
80                 sprintf (tmpbuf, "%d", e->tagVal.num);
81                 Tcl_AppendElement (interp, tmpbuf);
82             }
83             else
84             {
85                 Tcl_AppendResult (interp, " S ", NULL);
86                 Tcl_AppendElement (interp, e->tagVal.str);
87             }
88             if (e->dataWhich == Z_ElementData_subtree)
89             {
90                 Tcl_AppendResult (interp, " R ", NULL);
91                 ir_tcl_get_grs_r (interp, e->tagData.sub, argc, argv, argno+1);
92             }
93             else
94             {
95                 Tcl_AppendElement (interp, "S");
96                 if (e->tagData.str)
97                     Tcl_AppendElement (interp, e->tagData.str );
98                 else
99                     Tcl_AppendResult (interp, " {} ", NULL);
100             }
101             Tcl_AppendResult (interp, " }", NULL);
102         }
103     }
104     return TCL_OK;
105 }
106
107 int ir_tcl_get_grs (Tcl_Interp *interp, IrTcl_GRS_Record *grs_record, 
108                      int argc, char **argv)
109 {
110     return ir_tcl_get_grs_r (interp, grs_record, argc, argv, 4);
111 }
112