File: hdiff_misc.c

package info (click to toggle)
libhdf4 4.2.10-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 25,488 kB
  • ctags: 12,954
  • sloc: ansic: 146,962; sh: 14,905; fortran: 12,480; makefile: 922; yacc: 680; pascal: 418; lex: 170; csh: 41
file content (277 lines) | stat: -rw-r--r-- 6,102 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF.  The full HDF copyright notice, including       *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at      *
 * http://hdfgroup.org/products/hdf4/doc/Copyright.html.  If you do not have *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


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



#include "hdf.h"
#include "mfhdf.h"

#include "hdiff.h"

/* 
* convert pathname of netcdf file into name for cdl unit, by taking 
* last component of path and stripping off any extension.
*/
char *
name_path(char *path)
{
 char *cp, *newc;
 
#ifdef MSDOS
#define FILE_DELIMITER '\\'
#endif    
#ifndef FILE_DELIMITER /* default to unix */
#define FILE_DELIMITER '/'
#endif
 cp = strrchr(path, FILE_DELIMITER);
 if (cp == 0)  /* no delimiter */
  cp = path;
 else   /* skip delimeter */
  cp++;
 newc = (char *) malloc((unsigned) (strlen(cp)+1));
 if (newc == 0) {
  fprintf(stderr,"Out of memory!\n");
  exit(EXIT_FAILURE);
 }
 (void) strcpy(newc, cp); /* copy last component of path */
 if ((cp = strrchr(newc, '.')) != NULL)
  *cp = '\0';  /* strip off any extension */
 return newc;
}


const char *
type_name(nc_type type)
{
 switch (type) {
 case DFNT_INT8:
  return "byte";
 case DFNT_CHAR:
  return "char";
 case DFNT_INT16:
  return "short";
 case DFNT_INT32:
  return "long";
 case DFNT_FLOAT:
  return "float";
 case DFNT_DOUBLE:
  return "double";
 default:
  fprintf(stderr,"type_name: bad type %d", type);
  return "bogus";
 }
}

/*
* Remove trailing zeros (after decimal point) but not trailing decimal
* point from ss, a string representation of a floating-point number that
* might include an exponent part.
*/
void
tztrim(char *ss)
  /* returned string representing dd */
{
 char *cp, *ep;
 
 cp = ss;
 if (*cp == '-')
  cp++;
 while(isdigit((int)*cp) || *cp == '.')
  cp++;
 if (*--cp == '.')
  return;
 ep = cp+1;
 while (*cp == '0')
  cp--;
 cp++;
 if (cp == ep)
  return;
 while (*ep)
  *cp++ = *ep++;
 *cp = '\0';
 return;
}


/*
* Print list of attribute values.  Attribute values must be printed with
* explicit type tags, because their types are not declared.
*/
void
pr_att_vals(nc_type type, int len, void *vals)
{
 int iel;
 union {
  char *cp;
  int16 *sp;
  int32 *lp;
  float32 *fp;
  float64 *dp;
 } gp;
 char *sp;
 unsigned char uc;
 char gps[30];  /* for ascii of a float or double precision */
 char *f_fmt = "%#.8g";
 char *d_fmt = "%#.16g";
 
 switch (type) {
 case DFNT_INT8:
  gp.cp = (char *) vals;
  for (iel = 0; iel < len; iel++)
   if (isprint(uc = *gp.cp++ & 0377))
    Printf ("'%c'%s", uc, iel<len-1 ? ", " : "");
   else
    Printf ("'\\%o'%s", uc, iel<len-1 ? ", " : "");
   break;
 case DFNT_CHAR:
  gp.cp = (char *) vals;
  Printf ("\"");
  /* adjust len so trailing nulls don't get printed */
  sp = gp.cp + len - 1;
  while (*sp-- == '\0' && len > 0)
   len--;
  for (iel = 0; iel < len; iel++)
   switch (uc = *gp.cp++ & 0377) {
 case '\b':
  Printf ("\\b");
  break;
 case '\f':
  Printf ("\\f");
  break;
 case '\n':  /* generate linebreaks after new-lines */
  Printf ("\\n\",\n    \"");
  break;
 case '\r':
  Printf ("\\r");
  break;
 case '\t':
  Printf ("\\t");
  break;
 case '\v':
  Printf ("\\v");
  break;
 case '\\':
  Printf ("\\\\");
  break;
 case '\'':
  Printf ("\\'");
  break;
 case '\"':
  Printf ("\\\"");
  break;
 default:
  Printf ("%c",uc);
  break;
  }
  Printf ("\"");
  break;
 case DFNT_INT16:
  gp.sp = (int16 *) vals;
  for (iel = 0; iel < len; iel++)
   Printf ("%ds%s",*gp.sp++,iel<len-1 ? ", " : "");
  break;
 case DFNT_INT32:
  gp.lp = (int32 *) vals;
  for (iel = 0; iel < len; iel++)
   Printf ("%ld%s",*gp.lp++,iel<len-1 ? ", " : "");
  break;
 case DFNT_FLOAT:
  gp.fp = (float32 *) vals;
  for (iel = 0; iel < len; iel++) {
   int ll;
   (void) sprintf(gps, f_fmt, * gp.fp++);
   /* append a trailing "f" for floating-point attributes */
   ll = strlen(gps);
   gps[ll + 1] = '\0';
   gps[ll] = 'f';
   tztrim(gps); /* trim trailing 0's after '.' */
   Printf ("%s%s", gps, iel<len-1 ? ", " : "");
  }
  break;
 case DFNT_DOUBLE:
  gp.dp = (float64 *) vals;
  for (iel = 0; iel < len; iel++) {
   (void) sprintf(gps, d_fmt, *gp.dp++);
   tztrim(gps); /* trim trailing 0's after '.' */
   Printf ("%s%s", gps, iel<len-1 ? ", " : "");
  }
  break;
 default:
  fprintf(stderr,"pr_att_vals: bad type - %d", type);
 }
}



void
make_vars(char *optarg, diff_opt_t *opt, int option)
{
 char *cp = optarg;
 int nvars = 1;
 char ** cpp;
 
 /* compute number of variable names in comma-delimited list */
 if (option == 1)
  opt->nlvars = 1;
 else
  opt->nuvars = 1;
 
 while (*cp++)
  if (*cp == ',')
   nvars++;
  
  if (option == 1)
  {
   opt->lvars = (char **) malloc(nvars * sizeof(char*));
   if (!opt->lvars) {
    fprintf(stderr,"Out of memory!\n");
    exit(EXIT_FAILURE);
   }
   cpp = opt->lvars;
  }
  else
  {
   opt->uvars = (char **) malloc(nvars * sizeof(char*));
   if (!opt->uvars) {
    fprintf(stderr,"Out of memory!\n");
    exit(EXIT_FAILURE);
   }
   cpp = opt->uvars;
  }
  
  /* copy variable names into list */
  for (cp = strtok(optarg, ",");
  cp != NULL;
  cp = strtok((char *) NULL, ",")) {
   
   *cpp = (char *) malloc(strlen(cp) + 1);
   if (!*cpp) {
    fprintf(stderr,"Out of memory!\n");
    exit(EXIT_FAILURE);
   }
   strcpy(*cpp, cp);
   cpp++;
  }
  if (option == 1)
   opt->nlvars = nvars;
  else
   opt->nuvars = nvars;
}