File: pdatst.c

package info (click to toggle)
pact 980714-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 13,096 kB
  • ctags: 26,034
  • sloc: ansic: 109,076; lisp: 9,645; csh: 7,147; fortran: 1,050; makefile: 136; lex: 95; sh: 32
file content (147 lines) | stat: -rw-r--r-- 4,078 bytes parent folder | download | duplicates (2)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * PDATST.C - Test attribute features of PDB
 *
 * Source Version: 9.0
 * Software Release #92-0043
 *
 */

#include "cpyright.h"

#include "pdb.h"

static int
 SC_DECLARE(print_info,
         (PDBfile *file, FILE *fp, HASHTAB *tab, char *s, char *var));

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

int main()
   {PDBfile *file;
    FILE *fp;
    float d[10];
    int i, err, *rank, *center, **pc;
    HASHTAB *tab;

    SC_zero_space(0);
    err = TRUE;

    file = PD_open("pdatst.db", "w");
    fp   = fopen("pdatst.rs", "w");
    if ((file == NULL) || (fp == NULL))
       {PRINT(stderr, "Error creating pdatst.db\n");
        err = FALSE;
        return(err);};
    PRINT(fp, "\nCreated pdatst.db\n\n");

/* write out one variable */
    for (i = 0; i < 10; i++)
        d[i] = (float) i;

    PD_write(file, "d(10)", "float", d);

/* define & set some attributes */
    if (PD_def_attribute(file, "rank", "integer") == FALSE)
       {PRINT(fp, "Error defining RANK\n");
        err = FALSE;};

    if (PD_def_attribute(file, "centering", "integer *") == FALSE)
       {PRINT(fp, "Error defining CENTERING\n");
        err = FALSE;};

    rank  = FMAKE(int, "PDATST.C:rank");
    *rank = 1;

    pc  = FMAKE(int *, "PDATST.C:pc");
    *pc = center = FMAKE_N (int, 2, "PDATST.C:*pc");
    center[0] = 44;
    center[1] = 55;

    if (PD_set_attribute(file, "d", "rank", (byte *) rank) == FALSE)
       {PRINT(fp, "Error setting rank attribute\n");
        err = FALSE;};

    if (PD_set_attribute(file, "d", "centering", (byte *) pc) == FALSE)
       {PRINT(fp, "Error setting center attribute\n");
        err = FALSE;};

    err &= print_info(file, fp, file->attrtab, "d", "Attribute Table");

    PD_close(file);
    SFREE(rank);
    SFREE(center);

    file = PD_open("pdatst.db", "a");
    PRINT(fp, "\nRe-opened pdatst.db\n\n");

    err &= print_info(file, fp, file->attrtab, "d", "Attribute Table");

/* try writing then reading hash table from file */
    if (!PD_write(file, "foo", "HASHTAB", file->attrtab))
       {PRINT(fp, "Error writing attribute hash table to file.\n");
        PRINT(fp, "PD_err = %s\n", PD_err);
        err = FALSE;};

    tab = FMAKE(HASHTAB, "PDATST.C:tab");

    if (PD_read(file, "foo", tab) == FALSE)
       {PRINT(fp, "Error reading attribute hash table from file.\n");
        PRINT(fp, "PD_err = %s\n", PD_err);
        err = FALSE;};

    err &= print_info(file, fp, tab, "d", "foo");

/* close file */
    PD_close(file);

    return(!err);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/

/* PRINT_INFO - print attribute table info */

static int print_info(file, fp, tab, s, var)
   PDBfile *file;
   FILE *fp;
   HASHTAB *tab;
   char *s, *var;
   {char **names;
    int i, ret, *nrank, **center;

    ret = TRUE;

    PRINT(fp, "-------------------------------------------------------\n\n");

    PRINT(fp, "Variable: %s\n", var);

/* print contents of attribute table */
    names = SC_hash_dump(tab, NULL);
    for (i = 0; i < tab->nelements; i++)
        PRINT(fp, "\t%s\n", names[i]);

    SFREE(names);
    PRINT(fp, "\n");

/* get attribute associated with var */
    nrank = (int *) PD_get_attribute(file, s, "rank");
    if (nrank == NULL)
       {PRINT(fp, "Error getting rank attribute\n");
        ret = FALSE;}
    else
       PRINT(fp, "Rank of %s : %d\n", s, *nrank);

    center = (int **) PD_get_attribute(file, s, "centering");
    if (center == NULL)
       {PRINT(fp, "Error getting centering attribute\n");
        ret = FALSE;}
    else
       PRINT(fp, "Centering of %s : %d %d\n", s, center[0][0], center[0][1]);

    PRINT(fp, "\n-------------------------------------------------------\n");

    return(ret);}

/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/