1 /* This is part of the netCDF package. Copyright 2005 University
2    Corporation for Atmospheric Research/Unidata See COPYRIGHT file for
3    conditions of use. See www.unidata.ucar.edu for more info.
4 
5    Utility to rename a group
6 */
7 
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <string.h>
11 
12 #ifndef LOGGING
13 #define LOGGING
14 #endif
15 #include "netcdf.h"
16 
17 #undef RENAME_DEBUG
18 
19 static void
usage(const char * msg)20 usage(const char* msg)
21 {
22     if(msg != NULL)
23 	fprintf(stderr,"%s\n",msg);
24     fprintf(stderr,"usage: renamegroup <filename> <old group path name> <new name>\n");
25     fflush(stderr);
26     exit(0); /* do not cause error */
27 }
28 
29 static void
check(int status)30 check(int status)
31 {
32     if(status == 0) return;
33     fprintf(stderr,"%d: %s\n",status,nc_strerror(status));
34     fflush(stderr);
35     exit(1);
36 }
37 
38 int
main(int argc,char ** argv)39 main(int argc, char **argv)
40 {
41     int stat;
42     int ncid, grpid;
43     char* filename;
44     char* oldname;
45     char* newname;
46 
47     switch (argc) {
48     case 0:
49     case 1:
50         usage("No arguments");
51 	break;
52     case 2:
53         usage("Too few arguments");
54 	break;
55     case 3:
56     default:
57 	filename = argv[1];
58 	oldname = argv[2];
59 	newname = argv[3];
60 	break;
61     }
62 
63     if(strlen(filename) == 0)
64 	usage("bad filename argument");
65     if(strlen(oldname) == 0)
66 	usage("bad old name argument");
67     if(strlen(newname) == 0)
68 	usage("bad new name argument");
69 
70     stat = nc_open(filename,NC_WRITE,&ncid);
71     check(stat);
72 
73 #ifdef RENAME_DEBUG
74     stat = nc_set_log_level(0);
75     check(stat);
76     nc_log_hdf5();
77 #endif
78 
79     stat = nc_inq_grp_full_ncid(ncid,oldname,&grpid);
80     check(stat);
81 
82     stat = nc_rename_grp(grpid,newname);
83     check(stat);
84 
85     stat = nc_close(ncid);
86     check(stat);
87 
88     exit(0);
89 }
90 
91