File: dumplib.c

package info (click to toggle)
libhdf4 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 29,892 kB
  • sloc: ansic: 128,688; sh: 14,969; fortran: 12,444; java: 5,864; xml: 1,305; makefile: 900; yacc: 678; pascal: 418; perl: 360; javascript: 203; lex: 163; csh: 41
file content (193 lines) | stat: -rw-r--r-- 4,090 bytes parent folder | download | duplicates (3)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*********************************************************************
 *   Copyright 1993, University Corporation for Atmospheric Research
 *   See netcdf/README file for copying and redistribution conditions.
 *********************************************************************/

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "hdf.h"

#ifdef H4_HAVE_NETCDF
#include "netcdf.h"
#else
#include "hdf4_netcdf.h"
#endif

#include "dumplib.h"

/*
 * Print error message to stderr, don't exit
 */
void
error(const char *fmt, ...)
{
    va_list args;

    fprintf(stderr, "*** %s: ", progname);

    va_start(args, fmt);
    (void)vfprintf(stderr, fmt, args);
    va_end(args);

    fprintf(stderr, "\n");
    (void)fflush(stderr); /* to ensure log files are current */
}

#define LINEPIND "    " /* indent of continued lines */

static int linep;
static int max_line_len;

void
set_indent(int in)
{
    linep = in;
}

void
set_max_len(int len)
{
    max_line_len = len - 2;
}

void
lput(const char *cp)
{
    int nn = strlen(cp);

    if (nn + linep > max_line_len && nn > 2) {
        (void)fputs("\n", stdout);
        (void)fputs(LINEPIND, stdout);
        linep = strlen(LINEPIND);
    }
    (void)fputs(cp, stdout);
    linep += nn;
}

static char *formats[] = {
    "%d",    /* bytes, shorts */
    "%s",    /* char arrays as strings */
    "%ld",   /* longs */
    "%.7g ", /* floats */
    "%.15g"  /* doubles */
};

/* In case different formats specified with -d option, set them here. */
void
set_formats(char *flt, char *dbl)
{
    strcpy(formats[3], flt);
    strcpy(formats[4], dbl);
}

/* ncid - netcdf id */
/* varid - variable id */
static char *
has_c_format_att(int ncid, int varid)
{
    nc_type cfmt_type;
    int     cfmt_len;
    int     savopts;
#define C_FMT_NAME   "C_format" /* name of C format attribute */
#define MAX_CFMT_LEN 100        /* max length of C format attribute */
    static char cfmt[MAX_CFMT_LEN];

    /*
     * turn off error handling, we expect ncattinq to fail if there is no
     * C_format attribute
     */
    savopts = ncopts;
    ncopts  = 0;
    if (ncattinq(ncid, varid, "C_format", &cfmt_type, &cfmt_len) != -1) {
        ncopts = savopts; /* restore error handling */
        if (cfmt_type == NC_CHAR && cfmt_len > 0 && cfmt_len < MAX_CFMT_LEN) {
            if (ncattget(ncid, varid, "C_format", (void *)cfmt) != -1)
                return &cfmt[0];
        }
    }
    ncopts = savopts; /* restore error handling */
    return 0;
}

/*
 * Determine print format to use for each value for this variable.  Use value
 * of attribute C_format if it exists, otherwise a sensible default.
 */
/* ncid  - netcdf id */
/* varid - variable id */
/* type  - netCDF data type */
const char *
get_fmt(int ncid, int varid, nc_type type)
{
    char *c_format_att = has_c_format_att(ncid, varid);

    /* If C_format attribute exists, return it */
    if (c_format_att)
        return c_format_att;

    /* Otherwise return sensible default. */
    switch (type) {
        case NC_BYTE:
            return formats[0];
        case NC_CHAR:
            return formats[1];
        case NC_SHORT:
            return formats[0];
        case NC_LONG:
            return formats[2];
        case NC_FLOAT:
            return formats[3];
        case NC_DOUBLE:
            return formats[4];
        default:
            error("pr_vals: bad type");
            return NULL;
    }
}

static vnode *
newvnode()
{
    vnode *newvp = (vnode *)malloc(sizeof(vnode));

    if (!newvp) {
        error("out of memory!");
        exit(EXIT_FAILURE);
    }
    return newvp;
}

vnode *
newvlist()
{
    vnode *vp = newvnode();

    vp->next = 0;
    vp->id   = -1; /* bad id */

    return vp;
}

void
varadd(vnode *vlist, int varid)
{
    vnode *newvp = newvnode();

    newvp->next = vlist->next;
    newvp->id   = varid;
    vlist->next = newvp;
}

int
varmember(vnode *vlist, int varid)
{
    vnode *vp = vlist->next;

    for (; vp; vp = vp->next)
        if (vp->id == varid)
            return 1;
    return 0;
}