1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #ifdef _WIN32
8 # include <io.h>
9 # define unlink _unlink
10 #else
11 # include <unistd.h>
12 #endif
13 
14 #include "cgns_io.h"
15 #include "getargs.h"
16 
17 #if CG_HAVE_STAT64_STRUCT
18 #ifdef _WIN32
19 #define stat _stat64
20 #else
21 #define stat stat64
22 #endif
23 #endif
24 
25 static char options[] = "ahfl";
26 static char *usgmsg[] = {
27     "usage  : cgnsconvert [options] InputFile [OutputFile]",
28     "options:",
29     "   -a : write ADF file",
30     "   -h : write HDF5 file",
31     "   -f : force output if input format is same as output",
32     "   -l : expand links in output file",
33     NULL
34 };
35 
main(int argc,char ** argv)36 int main (int argc, char **argv)
37 {
38     char *inpfile, *outfile;
39     char tempfile[1024];
40     int n, inptype, outtype = CGIO_FILE_NONE;
41     int inpcg, outcg, links = 0;
42     int force = 0;
43     struct stat inpst, outst;
44     time_t ts, te;
45     static char *FileType[] = {"NONE", "ADF", "HDF5", "ADF2"};
46 
47     if (argc < 2)
48         print_usage (usgmsg, NULL);
49     while ((n = getargs (argc, argv, options)) > 0) {
50         switch (n) {
51             case 'a':
52                 outtype = CGIO_FILE_ADF;
53                 break;
54             case 'h':
55                 outtype = CGIO_FILE_HDF5;
56                 break;
57             case 'f':
58                 force = 1;
59                 break;
60             case 'l':
61                 links = 1;
62                 break;
63         }
64     }
65 
66     if (argind == argc)
67         print_usage (usgmsg, "InputFile not given");
68     if (outtype != CGIO_FILE_NONE && cgio_is_supported(outtype)) {
69         fprintf(stderr, "output type %s not supported\n",
70             FileType[outtype]);
71         exit(1);
72     }
73 
74     inpfile = argv[argind++];
75     if (argind < argc)
76         outfile = argv[argind];
77     else
78         outfile = inpfile;
79     if (strlen(outfile) > 1018) {
80         fprintf(stderr, "output file name is too long\n");
81         exit(1);
82     }
83     sprintf(tempfile, "%s.temp", outfile);
84     unlink(tempfile);
85 
86     if (stat (inpfile, &inpst)) {
87         fprintf (stderr, "can't stat %s\n", inpfile);
88         exit (1);
89     }
90 
91     if (cgio_open_file (inpfile, 'r', CGIO_FILE_NONE, &inpcg))
92         cgio_error_exit("cgio_open_file");
93     if (cgio_get_file_type (inpcg, &inptype))
94         cgio_error_exit("cgio_get_file_type");
95 
96     if (outtype == CGIO_FILE_NONE) {
97         if (inptype == CGIO_FILE_ADF)
98             outtype = CGIO_FILE_HDF5;
99         else
100             outtype = CGIO_FILE_ADF;
101     }
102     if (!force) {
103         if (((inptype == CGIO_FILE_ADF  || inptype == CGIO_FILE_ADF2)  && (outtype == CGIO_FILE_ADF  || outtype == CGIO_FILE_ADF2)) ||
104             ((inptype == CGIO_FILE_HDF5) && (outtype == CGIO_FILE_HDF5))) {
105             cgio_close_file(inpcg);
106             fputs("input and output formats the same: use -f to force write\n", stderr);
107             return 1;
108         }
109     }
110 
111     printf("converting %s file %s to %s file %s\n",
112         FileType[inptype], inpfile, FileType[outtype], outfile);
113     if (links)
114         printf ("links will be included in output file\n");
115     fflush(stdout);
116 
117     ts = time (NULL);
118     if (cgio_open_file (tempfile, 'w', outtype, &outcg))
119         cgio_error_exit("cgio_open_file");
120     if (cgio_copy_file (inpcg, outcg, links))
121         cgio_error_exit("cgio_copy_file");
122     if (cgio_close_file (inpcg) || cgio_close_file (outcg))
123         cgio_error_exit("cgio_close_file");
124     te = time (NULL);
125 
126     unlink (outfile);
127     if (rename (tempfile, outfile)) {
128         fprintf (stderr, "rename %s -> %s failed", tempfile, outfile);
129         exit (1);
130     }
131 
132     if (stat (outfile, &outst)) {
133         fprintf (stderr, "can't stat %s\n", outfile);
134         exit (1);
135     }
136 
137     printf ("%-4s input  file size  = %ld bytes\n",
138         FileType[inptype], (long)inpst.st_size);
139     printf ("%-4s output file size  = %ld bytes\n",
140         FileType[outtype], (long)outst.st_size);
141     printf ("conversion time = %d secs\n", (int)(te - ts));
142     return 0;
143 }
144 
145